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

ai support Use local LLM(claude3) close #827 #828

Merged
merged 11 commits into from
Apr 12, 2024
38 changes: 38 additions & 0 deletions jcommon/ai/aws/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>aws</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>software.amazon.awssdk</groupId>
<artifactId>bedrockruntime</artifactId>
<version>2.25.29</version>
</dependency>

<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20240303</version>
</dependency>



</dependencies>

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

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

/**
* @author [email protected]
* @date 2024/4/9 16:43
*/
@Data
public class Content {

@SerializedName("type")
private String type;

@SerializedName("text")
private String text;

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

import lombok.Builder;
import lombok.Data;

/**
* @author [email protected]
* @date 2024/4/12 15:04
*/
@Data
@Builder
public class Key {

private String keyId;

private String key;


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

/**
* @author [email protected]
* @date 2024/4/12 14:59
*/
public enum ModelEnum {


Sonnet("anthropic.claude-3-sonnet-20240229-v1:0"),
Haiku("anthropic.claude-3-haiku-20240307-v1:0");


public String modelName;

ModelEnum(String name) {
this.modelName = name;
}

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

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

import java.util.List;

/**
* @author [email protected]
* @date 2024/4/9 16:41
*/
@Data
public class ResponsePayload {

@SerializedName("id")
private String id;

@SerializedName("type")
private String type;

@SerializedName("role")
private String role;

@SerializedName("content")
private List<Content> content;

@SerializedName("model")
private String model;

@SerializedName("stop_reason")
private String stopReason;

@SerializedName("stop_sequence")
private Object stopSequence; // Use Object if the value can be null or of different types

@SerializedName("usage")
private Usage usage;

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

import com.google.gson.annotations.SerializedName;

/**
* @author [email protected]
* @date 2024/4/9 16:42
*/
public class Usage {


@SerializedName("input_tokens")
private int inputTokens;

@SerializedName("output_tokens")
private int outputTokens;


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package run.mone.aws.client.test;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Test;
import run.mone.AwsClient;
import run.mone.Key;
import run.mone.ModelEnum;
import run.mone.ResponsePayload;
import software.amazon.awssdk.regions.Region;

/**
* @author [email protected]
* @date 2024/4/12 14:57
*/
public class AwsClientTest {


@Test
public void test1() {
JSONObject payload = new JSONObject()
.put("anthropic_version", "bedrock-2023-05-31")
.put("max_tokens", 1000)
.put("messages", new JSONArray()
.put(new JSONObject().put("role", "user")
.put("content", "天空为什么是蓝色的?"
)
)
);
ResponsePayload res = AwsClient.call(payload, Region.EU_WEST_3, ModelEnum.Haiku.modelName, Key.builder().keyId("").key("").build());
System.out.println(res.getContent().get(0).getText());
}

}
28 changes: 26 additions & 2 deletions jcommon/ai/google/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
</parent>

<artifactId>google</artifactId>
<version>1.4-jdk8-SNAPSHOT</version>

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

Expand Down Expand Up @@ -41,4 +42,27 @@

</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>
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,28 @@

import com.google.auth.oauth2.AccessToken;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import lombok.Data;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import run.mone.ai.google.bo.Content;
import run.mone.ai.google.bo.RequestPayload;
import run.mone.ai.google.bo.ResponsePayload;

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

/**
* @author [email protected]
* @date 2024/4/9 15:59
*/
@Data
@Slf4j
public class CloudeClient {

private String url = "https://us-central1-aiplatform.googleapis.com/v1/projects/";
Expand All @@ -30,6 +36,8 @@ public class CloudeClient {

private String token;

private static Gson gson = new Gson();


@SneakyThrows
public String token() {
Expand All @@ -46,7 +54,7 @@ public String token() {


public ResponsePayload call(String token, RequestPayload requestPayload) {
OkHttpClient client = new OkHttpClient();
OkHttpClient client = new OkHttpClient.Builder().readTimeout(5, TimeUnit.MINUTES).build();
MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
RequestBody body = RequestBody.create(mediaType, new Gson().toJson(requestPayload));
Request request = new Request.Builder()
Expand All @@ -57,11 +65,21 @@ public ResponsePayload call(String token, RequestPayload requestPayload) {
.build();

try (Response response = client.newCall(request).execute()) {
if (response.code() == 429) {
ResponsePayload res = new ResponsePayload();
Content content = new Content();
content.setText(gson.toJson(ImmutableMap.of("message", "被claude3限流了", "code", "429")));
log.info("claude res:{}", content.getText());
res.setContent(Lists.newArrayList(content));
return res;
}
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
// Handle the response
return new Gson().fromJson(response.body().string(), ResponsePayload.class);
} catch (Exception e) {
e.printStackTrace();
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;
}
Expand Down
26 changes: 26 additions & 0 deletions jcommon/ai/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<module>zhipu</module>
<module>moonshot</module>
<module>google</module>
<module>aws</module>
</modules>

<properties>
Expand All @@ -23,4 +24,29 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>


</dependencies>

<distributionManagement>
<repository>
<id>central</id>
<name>maven-release-virtual</name>
<url>https://pkgs.d.xiaomi.net/artifactory/maven-release-virtual</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>maven-snapshot-virtual</name>
<url>https://pkgs.d.xiaomi.net/artifactory/maven-snapshot-virtual</url>
</snapshotRepository>
</distributionManagement>

</project>
2 changes: 2 additions & 0 deletions jcommon/ai/zhipu/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

<dependencies>



<dependency>
<groupId>cn.bigmodel.openapi</groupId>
<artifactId>oapi-java-sdk</artifactId>
Expand Down
Loading
Loading