Skip to content

Commit

Permalink
The vertices of the graph support the map data structure clsoe(XiaoMi…
Browse files Browse the repository at this point in the history
  • Loading branch information
goodjava committed Mar 13, 2024
1 parent 72614e3 commit 45628ab
Show file tree
Hide file tree
Showing 15 changed files with 491 additions and 1 deletion.
24 changes: 24 additions & 0 deletions jcommon/ai/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>run.mone</groupId>
<artifactId>jcommon</artifactId>
<version>1.4-jdk20-SNAPSHOT</version>
</parent>

<artifactId>ai</artifactId>
<packaging>pom</packaging>
<modules>
<module>zhipu</module>
</modules>

<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

</project>
80 changes: 80 additions & 0 deletions jcommon/ai/src/main/resources/prompt.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
你是一名高级groovy工程师.
你要完成的好,我会给你100$小费.
我会给你提供一些代码只是和业务需求,请帮我生成相应的$code(groovy代码)代码和$params和$out.
你只需要返回一个方法和参数列表,方法的名字统一叫execute,参数: 第一个是
JsonObject input, 第二个是Object context
JsonObject必须用Gson中的库
不要生成任何测试代码
不要生成任何说明,只需要返回一个json结构的内容即可
你要生成一些必要的import
你给我返回的是一个JsonObject(Gson中的类)
你的返回结果里决不能用任何markdown格式包裹(比如:```json ``` ```groovy ```)
不要输出换行符 比如 \n \r等
$code就是你要生成的代码内容
$params就是参数名列表
$outs就是返回结果的列表

最终你产生的结果
{"code":$code,"params":$params,"outs":$outs}

$params的格式举例:[{"name":"a","tpye":"int"}]
$outs的格式举例:[{"name":"sum","type":"int"}]

一些工具库使用,你可以借鉴:


DbUtils里边有些工具方法可以操作数据库

/**
* 将提供的键值对数据插入到指定的数据库表中。
*/
public Long insert(String tableName, Map<String, Object> data)

/**
* 更新指定表的指定ID的记录,通过传入的键值对映射来设置新的列值
*/
public void update(String tableName, String primaryKeyName, Map<String, Object> data)

// 根据id列表批量删除
public void deleteByIds(String tableName, String idName, List<Long> ids)


你尽量使用我给你提供的工具类,DbUtils 直接可以从context中获取 context.getDbUtils


我给你一个例子:

需求:
计算两数和


返回:
{
"code": "def execute(JsonObject input, Object context) {\n if (!input.has('a') || !input.has('b')) {\n throw new IllegalArgumentException(\"JSON对象必须包含键'a'和'b'。\");\n }\n int a = input.get('a').getAsInt();\n int b = input.get('b').getAsInt();\n int sum = a + b;\n JsonObject result = new JsonObject();\n result.addProperty(\"sum\", sum);\n return result;\n}",
"params": [
{
"name": "a",
"type": "int"
},
{
"name": "b",
"type": "int"
}
],
"outs": [
{
"name": "sum",
"type": "int"
}
]
}

例子结束



需求:
给定一个List<Integer>,返回这个list中的最大值和最小值

返回:

46 changes: 46 additions & 0 deletions jcommon/ai/src/main/resources/prompt2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
你是一名高级groovy工程师.
你要完成的好,我会给你100$小费.
我会给你提供一个groovy方法,你帮我生成这个方法的描述(必须少于15个字).
你的返回结果永远是一个json格式的数据.
你的返回结果里决不能用任何markdown格式包裹(比如:```groovy ```)
不要生成任何说明,只需要返回一个json结构的内容即可

$comment就是你生成的注释

{"comment":"$comment"}




我给你一个例子:

code:
def execute(JsonObject input, Object context) {
if (!input.has('a') || !input.has('b')) {
throw new IllegalArgumentException("JSON对象必须包含键'a'和'b'。");
}
int a = input.get('a').getAsInt();
int b = input.get('b').getAsInt();
int sum = a + b;
JsonObject result = new JsonObject();
result.addProperty("result", sum);
return result;
}


你返回的:
{"comment":"计算两数和"}



例子结束






code:
int a(int a, int b) { return a * b; }

你的返回:
1 change: 1 addition & 0 deletions jcommon/ai/src/main/resources/prompt3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
李清照最好的6首词,并且给我解读
38 changes: 38 additions & 0 deletions jcommon/ai/zhipu/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>run.mone</groupId>
<artifactId>ai</artifactId>
<version>1.4-jdk20-SNAPSHOT</version>
</parent>

<artifactId>zhipu</artifactId>

<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>

<dependency>
<groupId>cn.bigmodel.openapi</groupId>
<artifactId>oapi-java-sdk</artifactId>
<version>release-V4-2.0.0</version>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>


</dependencies>


</project>
57 changes: 57 additions & 0 deletions jcommon/ai/zhipu/src/main/java/run/mone/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package run.mone;

import com.zhipu.oapi.ClientV4;
import com.zhipu.oapi.Constants;
import com.zhipu.oapi.service.v4.model.ChatCompletionRequest;
import com.zhipu.oapi.service.v4.model.ChatMessage;
import com.zhipu.oapi.service.v4.model.ChatMessageRole;
import com.zhipu.oapi.service.v4.model.ModelApiResponse;
import lombok.SneakyThrows;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
* @author [email protected]
* @date 2024/3/10 07:55
*/
public class Main {

private static final String requestIdTemplate = "myoz-%d";


private static final String KEY = System.getenv("zhipu");

private static final String API_KEY = KEY.split("\\.")[0];

private static final String API_SECRET = KEY.split("\\.")[1];

private static final ClientV4 client = new ClientV4.Builder(API_KEY, API_SECRET).build();


@SneakyThrows
public static void main(String[] args) {
List<ChatMessage> messages = new ArrayList<>();

String promptName = "prompt.txt";

String content = Files.readString(Paths.get("/Users/zhangzhiyong/IdeaProjects/goodjava/mone/jcommon/ai/zhipu/src/main/resources/" + promptName));

ChatMessage chatMessage = new ChatMessage(ChatMessageRole.USER.value(), content);
messages.add(chatMessage);
String requestId = String.format(requestIdTemplate, System.currentTimeMillis());
ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
.model(Constants.ModelChatGLM4)
.stream(Boolean.FALSE)
.invokeMethod(Constants.invokeMethod)
.messages(messages)
.requestId(requestId)
.build();
ModelApiResponse invokeModelApiResp = client.invokeModelApi(chatCompletionRequest);
String resContent = invokeModelApiResp.getData().getChoices().get(0).getMessage().getContent().toString();
System.out.println(resContent);
// System.out.println("model output:"+ new Gson().toJson(invokeModelApiResp));
}
}
23 changes: 23 additions & 0 deletions jcommon/docean/src/main/java/com/xiaomi/youpin/docean/Ioc.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.gson.reflect.TypeToken;
import com.xiaomi.youpin.docean.anno.Component;
import com.xiaomi.youpin.docean.anno.Controller;
import com.xiaomi.youpin.docean.anno.IocConfiguration;
import com.xiaomi.youpin.docean.anno.Service;
import com.xiaomi.youpin.docean.bo.Bean;
import com.xiaomi.youpin.docean.common.*;
Expand Down Expand Up @@ -69,6 +70,9 @@ public class Ioc {
@Getter
private String[] scanPackages;

@Getter
private Class<?> primarySource;

/**
* It needs to be used when interacting with containers like spring
*/
Expand Down Expand Up @@ -357,6 +361,25 @@ public Ioc classLoader(ClassLoader classLoader) {
return this;
}

public static Ioc run(Class<?> primarySource, String... args) {
IocConfiguration configuration = primarySource.getAnnotation(IocConfiguration.class);
Ioc ioc = Ioc.ins();
ioc.primarySource = primarySource;
parseArgumentsAndPopulateIoc(args, ioc);
return ioc.init(configuration.basePackage());
}

private static void parseArgumentsAndPopulateIoc(String[] args, Ioc ioc) {
//Determine if args is an even number; if so, place it into a map.
Map<String, String> argsMap = new HashMap<>();
if (args.length % 2 == 0) {
for (int i = 0; i < args.length; i += 2) {
argsMap.put(args[i], args[i + 1]);
}
}
argsMap.entrySet().forEach(entry -> ioc.putBean("$" + entry.getKey(), entry.getValue()));
}

public Ioc init(String... scanPackages) {
this.scanPackages = scanPackages;
this.publishEvent(new Event(EventType.initBegin));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.xiaomi.youpin.docean.anno;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* @author [email protected]
* @date 2024/3/3 09:19
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE) // 应用于类
public @interface IocConfiguration {
String[] basePackage();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.xiaomi.youpin.docean.test;

import com.xiaomi.youpin.docean.Ioc;
import com.xiaomi.youpin.docean.anno.IocConfiguration;
import com.xiaomi.youpin.docean.test.demo.DemoDao;

/**
* @author [email protected]
* @date 2024/3/5 14:47
* <p>
* Evaluating the efficacy of @IocConfiguration.
*/
@IocConfiguration(basePackage = {"com.xiaomi.youpin.docean.test.demo"})
public class TestRun {

public static void main(String[] args) {
DemoDao demoA = Ioc.run(TestRun.class, args).getBean(DemoDao.class);
System.out.println(demoA.get());
}

}
6 changes: 5 additions & 1 deletion jcommon/openai/src/main/java/run/mone/openai/OpenaiCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,9 @@ public void onFailure(EventSource eventSource, @Nullable Throwable t, @Nullable
});
}

public static void callStream2(String req, StreamListener sl, ReqConfig config) {
callStream2(req, sl, config, null);
}

/**
* 原生的调用,底层只依赖okhttp
Expand All @@ -278,10 +281,11 @@ public void onFailure(EventSource eventSource, @Nullable Throwable t, @Nullable
* @param sl
* @param config
*/
public static void callStream2(String req, StreamListener sl, ReqConfig config) {
public static void callStream2(String req, StreamListener sl, ReqConfig config, Headers headers) {
MediaType mediaType = MediaType.parse("application/json; charset=utf-8");

Request request = new Request.Builder()
.headers(headers)
.url(config.getAskUrl())
.post(RequestBody.create(mediaType, req.getBytes(Charset.forName("utf8"))))
.build();
Expand Down
Loading

0 comments on commit 45628ab

Please sign in to comment.