diff --git a/jcommon/ai/aws/pom.xml b/jcommon/ai/aws/pom.xml new file mode 100644 index 000000000..55c70c2de --- /dev/null +++ b/jcommon/ai/aws/pom.xml @@ -0,0 +1,38 @@ + + + 4.0.0 + + run.mone + ai + 1.4-jdk20-SNAPSHOT + + + aws + + + 21 + 21 + UTF-8 + + + + + + software.amazon.awssdk + bedrockruntime + 2.25.29 + + + + org.json + json + 20240303 + + + + + + + \ No newline at end of file diff --git a/jcommon/ai/aws/src/main/java/run/mone/Content.java b/jcommon/ai/aws/src/main/java/run/mone/Content.java new file mode 100644 index 000000000..0788f5447 --- /dev/null +++ b/jcommon/ai/aws/src/main/java/run/mone/Content.java @@ -0,0 +1,19 @@ +package run.mone; + +import com.google.gson.annotations.SerializedName; +import lombok.Data; + +/** + * @author goodjava@qq.com + * @date 2024/4/9 16:43 + */ +@Data +public class Content { + + @SerializedName("type") + private String type; + + @SerializedName("text") + private String text; + +} diff --git a/jcommon/ai/aws/src/main/java/run/mone/Key.java b/jcommon/ai/aws/src/main/java/run/mone/Key.java new file mode 100644 index 000000000..c909c5bd8 --- /dev/null +++ b/jcommon/ai/aws/src/main/java/run/mone/Key.java @@ -0,0 +1,19 @@ +package run.mone; + +import lombok.Builder; +import lombok.Data; + +/** + * @author goodjava@qq.com + * @date 2024/4/12 15:04 + */ +@Data +@Builder +public class Key { + + private String keyId; + + private String key; + + +} diff --git a/jcommon/ai/aws/src/main/java/run/mone/ModelEnum.java b/jcommon/ai/aws/src/main/java/run/mone/ModelEnum.java new file mode 100644 index 000000000..056788248 --- /dev/null +++ b/jcommon/ai/aws/src/main/java/run/mone/ModelEnum.java @@ -0,0 +1,20 @@ +package run.mone; + +/** + * @author goodjava@qq.com + * @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; + } + +} diff --git a/jcommon/ai/aws/src/main/java/run/mone/ResponsePayload.java b/jcommon/ai/aws/src/main/java/run/mone/ResponsePayload.java new file mode 100644 index 000000000..85b2c1a2e --- /dev/null +++ b/jcommon/ai/aws/src/main/java/run/mone/ResponsePayload.java @@ -0,0 +1,39 @@ +package run.mone; + +import com.google.gson.annotations.SerializedName; +import lombok.Data; + +import java.util.List; + +/** + * @author goodjava@qq.com + * @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; + + @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; + +} diff --git a/jcommon/ai/aws/src/main/java/run/mone/Usage.java b/jcommon/ai/aws/src/main/java/run/mone/Usage.java new file mode 100644 index 000000000..c597b6385 --- /dev/null +++ b/jcommon/ai/aws/src/main/java/run/mone/Usage.java @@ -0,0 +1,19 @@ +package run.mone; + +import com.google.gson.annotations.SerializedName; + +/** + * @author goodjava@qq.com + * @date 2024/4/9 16:42 + */ +public class Usage { + + + @SerializedName("input_tokens") + private int inputTokens; + + @SerializedName("output_tokens") + private int outputTokens; + + +} diff --git a/jcommon/ai/aws/src/test/java/run/mone/aws/client/test/AwsClientTest.java b/jcommon/ai/aws/src/test/java/run/mone/aws/client/test/AwsClientTest.java new file mode 100644 index 000000000..574b14faf --- /dev/null +++ b/jcommon/ai/aws/src/test/java/run/mone/aws/client/test/AwsClientTest.java @@ -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 goodjava@qq.com + * @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()); + } + +} diff --git a/jcommon/ai/google/pom.xml b/jcommon/ai/google/pom.xml index af5c82b69..6213e1dbc 100644 --- a/jcommon/ai/google/pom.xml +++ b/jcommon/ai/google/pom.xml @@ -10,10 +10,11 @@ google + 1.4-jdk8-SNAPSHOT - 21 - 21 + 8 + 8 UTF-8 @@ -41,4 +42,27 @@ + + + + + + maven-compiler-plugin + 3.11.0 + + 8 + 8 + true + UTF-8 + + ${project.basedir}/src/main/java + + + + + + + + + \ No newline at end of file diff --git a/jcommon/ai/google/src/main/java/run/mone/ai/google/CloudeClient.java b/jcommon/ai/google/src/main/java/run/mone/ai/google/CloudeClient.java index 871f05495..63f71c457 100644 --- a/jcommon/ai/google/src/main/java/run/mone/ai/google/CloudeClient.java +++ b/jcommon/ai/google/src/main/java/run/mone/ai/google/CloudeClient.java @@ -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 goodjava@qq.com * @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; } diff --git a/jcommon/ai/pom.xml b/jcommon/ai/pom.xml index 97a16315e..0c71cad63 100644 --- a/jcommon/ai/pom.xml +++ b/jcommon/ai/pom.xml @@ -15,6 +15,7 @@ zhipu moonshot google + aws @@ -23,4 +24,29 @@ UTF-8 + + + + org.projectlombok + lombok + 1.18.30 + provided + + + + + + + + central + maven-release-virtual + https://pkgs.d.xiaomi.net/artifactory/maven-release-virtual + + + snapshots + maven-snapshot-virtual + https://pkgs.d.xiaomi.net/artifactory/maven-snapshot-virtual + + + \ No newline at end of file diff --git a/jcommon/ai/zhipu/pom.xml b/jcommon/ai/zhipu/pom.xml index 5bebe7c32..ce2c33045 100644 --- a/jcommon/ai/zhipu/pom.xml +++ b/jcommon/ai/zhipu/pom.xml @@ -19,6 +19,8 @@ + + cn.bigmodel.openapi oapi-java-sdk diff --git a/jcommon/pom.xml b/jcommon/pom.xml index f95e07618..bd8a0c84c 100644 --- a/jcommon/pom.xml +++ b/jcommon/pom.xml @@ -1,271 +1,272 @@ - - 4.0.0 - run.mone - jcommon - 1.6.0-jdk21-SNAPSHOT - pom - ${project.artifactId} - The parent project of jcommon - https://github.com/XiaoMi/mone - 2020 - - - Apache License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0 - repo - - - - aliyun - log - common - annotation - antlr - rpc - redis - http - monitor - struct - gitlab - api - nacos - mongodb - coloregg - ks3 - test - docker - codegen - jmonitor - codecheck - cron - nginx - dbLocking - aop - crypto - catPlugin - feishu - sre - mistarter - health - docean - rcurve - micloud - docean-plugin - ssh - es - file - k8s - grpc - easy - df - monespring - graalvm - mquic - prometheus - buddy - hera - mv-plugin - teambition - spider - disruptor - pool - raft - event - geth - docean-plugin/docean-plugin-sidecar - sidecar-api - schedule - infra-result - rpc-codes - mybatis-plugins - processor - openai - excel - match - infra-common - docean-spring-starter - ai - + + 4.0.0 + run.mone + jcommon + 1.6.0-jdk21-SNAPSHOT + pom + ${project.artifactId} + The parent project of jcommon + https://github.com/XiaoMi/mone + 2020 + + + Apache License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0 + repo + + + + aliyun + log + common + annotation + antlr + rpc + redis + http + monitor + struct + gitlab + api + nacos + mongodb + coloregg + ks3 + test + docker + codegen + jmonitor + codecheck + cron + nginx + dbLocking + aop + crypto + catPlugin + feishu + sre + mistarter + health + docean + rcurve + micloud + docean-plugin + ssh + es + file + k8s + grpc + easy + df + monespring + graalvm + mquic + prometheus + buddy + hera + mv-plugin + teambition + spider + disruptor + pool + raft + event + geth + docean-plugin/docean-plugin-sidecar + sidecar-api + schedule + infra-result + rpc-codes + mybatis-plugins + processor + openai + excel + match + infra-common + docean-spring-starter + ai + - - - - - - - - - - + + + + + + + + + + - - - central - maven-release-virtual - https://pkgs.d.xiaomi.net/artifactory/maven-release-virtual - - - snapshots - maven-snapshot-virtual - https://pkgs.d.xiaomi.net/artifactory/maven-snapshot-virtual - - + + + central + maven-release-virtual + https://pkgs.d.xiaomi.net/artifactory/maven-release-virtual + + + snapshots + maven-snapshot-virtual + https://pkgs.d.xiaomi.net/artifactory/maven-snapshot-virtual + + - - ~/ - 8 - 8 - 1.6.0-jdk21-SNAPSHOT - - - - junit - junit - 4.12 - test - - - com.google.code.gson - gson - 2.8.5 - - - org.projectlombok - lombok - 1.18.30 - - - ch.qos.logback - logback-classic - 1.1.2 - provided - - - ch.qos.logback - logback-core - 1.1.2 - provided - - - com.google.guava - guava - 25.1-jre - - - org.springframework - spring-core - 5.0.6.RELEASE - provided - - - org.springframework - spring-context - 5.0.6.RELEASE - provided - - - - - - maven-resources-plugin - 3.2.0 - - - copy-license - prepare-package - - copy-resources - - - target/classes/META-INF - - - ${maven.jcommonDirectory} - - LICENSE.txt - - - - - - - + + ~/ + 8 + 8 + 1.6.0-jdk21-SNAPSHOT + + + + junit + junit + 4.12 + test + + + com.google.code.gson + gson + 2.8.5 + + + org.projectlombok + lombok + 1.18.30 + + + ch.qos.logback + logback-classic + 1.1.2 + provided + + + ch.qos.logback + logback-core + 1.1.2 + provided + + + com.google.guava + guava + 25.1-jre + + + org.springframework + spring-core + 5.0.6.RELEASE + provided + + + org.springframework + spring-context + 5.0.6.RELEASE + provided + + + + + + maven-resources-plugin + 3.2.0 + + + copy-license + prepare-package + + copy-resources + + + target/classes/META-INF + + + ${maven.jcommonDirectory} + + LICENSE.txt + + + + + + + - - org.codehaus.mojo - build-helper-maven-plugin - 3.4.0 - + + org.codehaus.mojo + build-helper-maven-plugin + 3.4.0 + - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + - - maven-compiler-plugin - 3.11.0 - - 20 - 20 - true - UTF-8 - - ${project.basedir}/src/main/java - - - + + maven-compiler-plugin + 3.11.0 + + 20 + 20 + true + UTF-8 + + ${project.basedir}/src/main/java + + + - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + - - + + diff --git a/jcommon/struct/src/main/java/com/xiaomi/data/push/graph/Graph2.java b/jcommon/struct/src/main/java/com/xiaomi/data/push/graph/Graph2.java index c601477f2..b92aab971 100644 --- a/jcommon/struct/src/main/java/com/xiaomi/data/push/graph/Graph2.java +++ b/jcommon/struct/src/main/java/com/xiaomi/data/push/graph/Graph2.java @@ -58,6 +58,22 @@ public D getVertexData(int id) { return vertexMap.get(id); } + //获取当前节点(vertex)的前置节点列表(class) + public List getPredecessors(int vertex) { + // Check if the vertex exists in the graph + if (!vertexMap.containsKey(vertex)) { + throw new IllegalArgumentException("Vertex does not exist."); + } + List predecessors = new ArrayList<>(); + // Iterate over the adjacency map to find all vertices that point to the current vertex + for (Map.Entry> entry : adjMap.entrySet()) { + if (entry.getValue().contains(vertex)) { + predecessors.add(entry.getKey()); + } + } + return predecessors; + } + //帮我实现下拓扑排序(class) public List topologicalSort() { diff --git a/jcommon/struct/src/test/java/run/mone/struct/test/GraphTest.java b/jcommon/struct/src/test/java/run/mone/struct/test/GraphTest.java index f523339a5..803077f90 100644 --- a/jcommon/struct/src/test/java/run/mone/struct/test/GraphTest.java +++ b/jcommon/struct/src/test/java/run/mone/struct/test/GraphTest.java @@ -26,12 +26,17 @@ public void test1() { graph.addEdge(33,44); graph.addEdge(44,22); graph.addEdge(22,88); + graph.addEdge(44,88); // graph.removeVertex(44); List list = graph.topologicalSort(); System.out.println(list); + + + System.out.println(graph.getPredecessors(88)); + } @Test