-
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.
ai support Use local LLM(claude3) close #827
- Loading branch information
Showing
14 changed files
with
545 additions
and
265 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,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> |
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,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; | ||
|
||
} |
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,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; | ||
|
||
|
||
} |
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,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
39
jcommon/ai/aws/src/main/java/run/mone/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,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; | ||
|
||
} |
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,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; | ||
|
||
|
||
} |
34 changes: 34 additions & 0 deletions
34
jcommon/ai/aws/src/test/java/run/mone/aws/client/test/AwsClientTest.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,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()); | ||
} | ||
|
||
} |
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
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 |
---|---|---|
|
@@ -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/"; | ||
|
@@ -30,6 +36,8 @@ public class CloudeClient { | |
|
||
private String token; | ||
|
||
private static Gson gson = new Gson(); | ||
|
||
|
||
@SneakyThrows | ||
public String token() { | ||
|
@@ -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() | ||
|
@@ -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; | ||
} | ||
|
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
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
Oops, something went wrong.