Skip to content

Commit

Permalink
Merge branch 'master' into refactor_codegen_unit_test
Browse files Browse the repository at this point in the history
  • Loading branch information
wodiwudi authored Sep 4, 2024
2 parents e9b3615 + 82c2441 commit e9757e2
Show file tree
Hide file tree
Showing 35 changed files with 1,289 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
@Builder
public class RequestPayload {

@SerializedName("system")
private String system;

@SerializedName("anthropic_version")
private String anthropicVersion;
Expand Down
54 changes: 54 additions & 0 deletions jcommon/ai/gpt/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?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>gpt</artifactId>
<version>1.5-jdk8-SNAPSHOT</version>

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

<dependencies>

<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.10.0</version>
</dependency>

</dependencies>

<build>

<plugins>

<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>8</source>
<target>8</target>
<verbose>true</verbose>
<encoding>UTF-8</encoding>
<compilerArguments>
<sourcepath>${project.basedir}/src/main/java</sourcepath>
</compilerArguments>
</configuration>
</plugin>


</plugins>

</build>

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

import com.google.gson.Gson;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import run.mone.ai.gpt.bo.ResponsePayload;
import run.mone.ai.gpt.bo.multiModal.GptVisionRequest;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@Data
@Slf4j
public class GptClient {

private static Gson gson = new Gson();

public ResponsePayload visionCall(String url, String token, GptVisionRequest gptVisionRequest) {
return baseCall(url, token, gson.toJson(gptVisionRequest));
}

private ResponsePayload baseCall(String url, String token, String bodyStr) {
OkHttpClient client = new OkHttpClient.Builder().readTimeout(5, TimeUnit.MINUTES).build();
MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
RequestBody body = RequestBody.create(mediaType, bodyStr);
Request request = new Request.Builder()
.url(url)
.post(body)
.addHeader("api-key", token)
.addHeader("Content-Type", "application/json; charset=utf-8")
.build();

try (Response response = client.newCall(request).execute()) {
if (response.code() == 429) {
ResponsePayload res = new ResponsePayload();
ResponsePayload.Error error = new ResponsePayload.Error();
error.setCode("429");
error.setMessage("被gpt限流了");
res.setError(error);
return res;
}
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
// Handle the response
String res = response.body().string();
log.info("claude3 res:{}", res);
return new Gson().fromJson(res, ResponsePayload.class);
} catch (Throwable e) {
log.error(e.getMessage(), e);
}
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package run.mone.ai.gpt.bo;

import lombok.Data;

import java.util.List;

@Data
public class ResponsePayload {
private List<Choice> choices;
private long created;
private String id;
private String model;
private String object;
private List<PromptFilterResult> prompt_filter_results;
private String system_fingerprint;
private Usage usage;
private Error error;

@Data
public static class Error {
private String code;
private String message;
}

@Data
public static class Choice {
private ContentFilterResults content_filter_results;
private String finish_reason;
private int index;
private Message message;


@Data
public static class ContentFilterResults {
private FilterResult hate;
private FilterResult self_harm;
private FilterResult sexual;
private FilterResult violence;

}

@Data
public static class FilterResult {
private boolean filtered;
private String severity;

}

@Data
public static class Message {
private String content;
private String role;

}
}

@Data
public static class PromptFilterResult {
private int prompt_index;
private ContentFilterResult content_filter_result;


@Data
public static class ContentFilterResult {
private JailbreakResult jailbreak;
private Choice.FilterResult sexual;
private Choice.FilterResult violence;
private Choice.FilterResult hate;
private Choice.FilterResult self_harm;


@Data
public static class JailbreakResult {
private boolean filtered;
private boolean detected;

}
}
}

@Data
public static class Usage {
private int completion_tokens;
private int prompt_tokens;
private int total_tokens;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package run.mone.ai.gpt.bo.multiModal;

import com.google.gson.annotations.SerializedName;
import lombok.Builder;
import lombok.Data;

import java.util.Map;

@Data
@Builder
public class GptVisionContent {

@SerializedName("type")
private String type;

@SerializedName("text")
private String text;

@SerializedName("image_url")
private Map<String, String> imageUrl;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package run.mone.ai.gpt.bo.multiModal;

import com.google.gson.annotations.SerializedName;
import lombok.Builder;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
* @author [email protected]
* @date 2023/5/25 14:16
*/
@Data
@Builder
public class GptVisionMsg implements Serializable {

@SerializedName("role")
private String role;

@SerializedName("content")
private List<GptVisionContent> content;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package run.mone.ai.gpt.bo.multiModal;

import com.google.gson.annotations.SerializedName;
import lombok.Builder;
import lombok.Data;

import java.util.List;

/**
* @author [email protected]
* @date 2024/4/9 16:36
*/
@Data
@Builder
public class GptVisionRequest {

@SerializedName("model")
private String model;

@SerializedName("temperature")
private double temperature;

@SerializedName("n")
@Builder.Default
private int n = 1;

@SerializedName("stream")
private boolean stream;

@SerializedName("top_p")
private double topP;

@SerializedName("max_tokens")
private int maxTokens;

@SerializedName("presence_penalty")
private double presencePenalty;

@SerializedName("frequency_penalty")
private double frequencyPenalty;

@SerializedName("messages")
private List<GptVisionMsg> messages;


}
64 changes: 64 additions & 0 deletions jcommon/ai/neo4j/src/main/java/run/mone/neo4j/BotCall.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package run.mone.neo4j;

import com.google.gson.JsonObject;
import lombok.Setter;
import okhttp3.*;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
* @author [email protected]
* @date 2024/8/21 13:50
*/
public class BotCall {

//调用bot的地址
@Setter
private static String url = "";


public static String call(String desc, String input) {
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.build();

// 使用 Gson 的 JsonObject 构建请求体
JsonObject mainObject = new JsonObject();
mainObject.addProperty("userName", "");
mainObject.addProperty("botId", "");
mainObject.addProperty("input", "");

JsonObject paramsObject = new JsonObject();
paramsObject.addProperty("desc", desc);
paramsObject.addProperty("input", input);
mainObject.add("params", paramsObject);

// 将 JsonObject 转换为字符串
String jsonBody = mainObject.toString();

RequestBody body = RequestBody.create(MediaType.parse("application/json"), jsonBody);

Request request = new Request.Builder()
.url(url)
.post(body)
.addHeader("Accept", "application/json, text/plain, */*")
.addHeader("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8")
.addHeader("Cache-Control", "no-cache")
.addHeader("Connection", "keep-alive")
.addHeader("Content-Type", "application/json")
.addHeader("Authorization", "")
.build();

try {
Response response = client.newCall(request).execute();
return response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}

}
Loading

0 comments on commit e9757e2

Please sign in to comment.