Skip to content

Commit

Permalink
refactor: code merge
Browse files Browse the repository at this point in the history
  • Loading branch information
wtt40122 committed Aug 29, 2024
2 parents ab9f2ac + 766ce45 commit 07d8a2b
Show file tree
Hide file tree
Showing 44 changed files with 2,333 additions and 118 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;


}
51 changes: 51 additions & 0 deletions jcommon/ai/neo4j/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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.6.0-jdk21-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

<artifactId>neo4j</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>com.github.javaparser</groupId>
<artifactId>javaparser-core</artifactId>
<version>3.26.1</version>
</dependency>

<dependency>
<groupId>org.neo4j.driver</groupId>
<artifactId>neo4j-java-driver</artifactId>
<version>5.23.0</version>
</dependency>

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

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


</dependencies>

</project>
Loading

0 comments on commit 07d8a2b

Please sign in to comment.