-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: update multiple files in gpt module without content changes (#887
- Loading branch information
Showing
7 changed files
with
287 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
54
jcommon/ai/gpt/src/main/java/run/mone/ai/gpt/GptClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
87 changes: 87 additions & 0 deletions
87
jcommon/ai/gpt/src/main/java/run/mone/ai/gpt/bo/ResponsePayload.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
jcommon/ai/gpt/src/main/java/run/mone/ai/gpt/bo/multiModal/GptVisionContent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
jcommon/ai/gpt/src/main/java/run/mone/ai/gpt/bo/multiModal/GptVisionMsg.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
46 changes: 46 additions & 0 deletions
46
jcommon/ai/gpt/src/main/java/run/mone/ai/gpt/bo/multiModal/GptVisionRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters