Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update multiple files in gpt module without content changes #887

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;


}
1 change: 1 addition & 0 deletions jcommon/ai/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<module>aws</module>
<module>minimax</module>
<module>bytedance</module>
<module>gpt</module>
</modules>

<properties>
Expand Down
Loading