-
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.
Merge branch 'XiaoMi:master' into master
- Loading branch information
Showing
31 changed files
with
927 additions
and
29 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,44 @@ | ||
<?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>google</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.google.auth</groupId> | ||
<artifactId>google-auth-library-oauth2-http</artifactId> | ||
<version>1.23.0</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.squareup.okhttp3</groupId> | ||
<artifactId>okhttp</artifactId> | ||
<version>4.10.0</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter</artifactId> | ||
<version>RELEASE</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
|
||
</dependencies> | ||
|
||
</project> |
69 changes: 69 additions & 0 deletions
69
jcommon/ai/google/src/main/java/run/mone/ai/google/CloudeClient.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,69 @@ | ||
package run.mone.ai.google; | ||
|
||
import com.google.auth.oauth2.AccessToken; | ||
import com.google.auth.oauth2.GoogleCredentials; | ||
import com.google.gson.Gson; | ||
import lombok.Data; | ||
import lombok.SneakyThrows; | ||
import okhttp3.*; | ||
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; | ||
|
||
/** | ||
* @author [email protected] | ||
* @date 2024/4/9 15:59 | ||
*/ | ||
@Data | ||
public class CloudeClient { | ||
|
||
private String url = "https://us-central1-aiplatform.googleapis.com/v1/projects/"; | ||
|
||
private String googleUrl = "www.googleapis.com"; | ||
|
||
private String projectId = ""; | ||
|
||
private String model = "claude-3-haiku@20240307"; | ||
|
||
private String token; | ||
|
||
|
||
@SneakyThrows | ||
public String token() { | ||
GoogleCredentials credentials = GoogleCredentials.fromStream( | ||
new FileInputStream("/tmp/key.json")) | ||
.createScoped(Collections.singleton("https://" + googleUrl + "/auth/cloud-platform")); | ||
// Use the credentials to authenticate and generate an access token | ||
credentials.refreshIfExpired(); | ||
AccessToken token = credentials.getAccessToken(); | ||
// Now you can use the access token | ||
this.token = token.getTokenValue(); | ||
return this.token; | ||
} | ||
|
||
|
||
public ResponsePayload call(String token, RequestPayload requestPayload) { | ||
OkHttpClient client = new OkHttpClient(); | ||
MediaType mediaType = MediaType.parse("application/json; charset=utf-8"); | ||
RequestBody body = RequestBody.create(mediaType, new Gson().toJson(requestPayload)); | ||
Request request = new Request.Builder() | ||
.url(url + projectId + "/locations/us-central1/publishers/anthropic/models/" + model + ":streamRawPredict") | ||
.post(body) | ||
.addHeader("Authorization", "Bearer " + token) | ||
.addHeader("Content-Type", "application/json; charset=utf-8") | ||
.build(); | ||
|
||
try (Response response = client.newCall(request).execute()) { | ||
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(); | ||
} | ||
return null; | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
jcommon/ai/google/src/main/java/run/mone/ai/google/bo/Content.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,20 @@ | ||
package run.mone.ai.google.bo; | ||
|
||
import lombok.Data; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
|
||
/** | ||
* @author [email protected] | ||
* @date 2024/4/9 16:43 | ||
*/ | ||
@Data | ||
public class Content { | ||
|
||
@SerializedName("type") | ||
private String type; | ||
|
||
@SerializedName("text") | ||
private String text; | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
jcommon/ai/google/src/main/java/run/mone/ai/google/bo/Message.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,21 @@ | ||
package run.mone.ai.google.bo; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
|
||
/** | ||
* @author [email protected] | ||
* @date 2024/4/9 16:36 | ||
*/ | ||
@Data | ||
@Builder | ||
public class Message { | ||
|
||
@SerializedName("role") | ||
private String role; | ||
|
||
@SerializedName("content") | ||
private String content; | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
jcommon/ai/google/src/main/java/run/mone/ai/google/bo/RequestPayload.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,32 @@ | ||
package run.mone.ai.google.bo; | ||
|
||
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 RequestPayload { | ||
|
||
|
||
@SerializedName("anthropic_version") | ||
private String anthropicVersion; | ||
|
||
@SerializedName("messages") | ||
private List<Message> messages; | ||
|
||
@SerializedName("max_tokens") | ||
private int maxTokens; | ||
|
||
@SerializedName("stream") | ||
private boolean stream; | ||
|
||
|
||
|
||
} |
39 changes: 39 additions & 0 deletions
39
jcommon/ai/google/src/main/java/run/mone/ai/google/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,39 @@ | ||
package run.mone.ai.google.bo; | ||
|
||
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
19
jcommon/ai/google/src/main/java/run/mone/ai/google/bo/Usage.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,19 @@ | ||
package run.mone.ai.google.bo; | ||
|
||
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; | ||
|
||
|
||
} |
28 changes: 28 additions & 0 deletions
28
jcommon/ai/google/src/test/java/run/mone/ai/google/test/ClientTest.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,28 @@ | ||
package run.mone.ai.google.test; | ||
|
||
import com.google.common.collect.Lists; | ||
import org.junit.Test; | ||
import run.mone.ai.google.CloudeClient; | ||
import run.mone.ai.google.bo.Message; | ||
import run.mone.ai.google.bo.RequestPayload; | ||
import run.mone.ai.google.bo.ResponsePayload; | ||
|
||
/** | ||
* @author [email protected] | ||
* @date 2024/4/9 16:24 | ||
*/ | ||
public class ClientTest { | ||
|
||
@Test | ||
public void test1() { | ||
String content = "天空为什么是蓝色的?"; | ||
// String content = "树上有10只鸟,我开了一枪还有几只鸟?"; | ||
CloudeClient c = new CloudeClient(); | ||
c.setProjectId(System.getenv("google_project_id")); | ||
RequestPayload payload = RequestPayload.builder().maxTokens(4000).anthropicVersion("vertex-2023-10-16").messages(Lists.newArrayList(Message.builder().role("user") | ||
.content(content) | ||
.build())).build(); | ||
ResponsePayload r = c.call(c.token(), payload); | ||
System.out.println(r.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?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>moonshot</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.squareup.okhttp3</groupId> | ||
<artifactId>okhttp</artifactId> | ||
<version>4.10.0</version> | ||
</dependency> | ||
|
||
|
||
</dependencies> | ||
|
||
</project> |
Oops, something went wrong.