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

[Java Client] Request for CLAUDE 3 Support #823

Merged
merged 10 commits into from
Apr 9, 2024
44 changes: 44 additions & 0 deletions jcommon/ai/google/pom.xml
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>
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 jcommon/ai/google/src/main/java/run/mone/ai/google/bo/Content.java
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 jcommon/ai/google/src/main/java/run/mone/ai/google/bo/Message.java
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;

}
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;



}
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 jcommon/ai/google/src/main/java/run/mone/ai/google/bo/Usage.java
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;


}
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());
}
}
31 changes: 31 additions & 0 deletions jcommon/ai/moonshot/pom.xml
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>
Loading
Loading