argsMap = new HashMap<>();
+ if (args.length % 2 == 0) {
+ for (int i = 0; i < args.length; i += 2) {
+ argsMap.put(args[i], args[i + 1]);
+ }
+ }
+ argsMap.entrySet().forEach(entry -> ioc.putBean("$" + entry.getKey(), entry.getValue()));
+ }
+
public Ioc init(String... scanPackages) {
this.scanPackages = scanPackages;
this.publishEvent(new Event(EventType.initBegin));
diff --git a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/anno/IocConfiguration.java b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/anno/IocConfiguration.java
new file mode 100644
index 000000000..e2c51adaf
--- /dev/null
+++ b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/anno/IocConfiguration.java
@@ -0,0 +1,16 @@
+package com.xiaomi.youpin.docean.anno;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * @author goodjava@qq.com
+ * @date 2024/3/3 09:19
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.TYPE) // 应用于类
+public @interface IocConfiguration {
+ String[] basePackage();
+}
diff --git a/jcommon/docean/src/test/java/com/xiaomi/youpin/docean/test/TestRun.java b/jcommon/docean/src/test/java/com/xiaomi/youpin/docean/test/TestRun.java
new file mode 100644
index 000000000..1d168dc94
--- /dev/null
+++ b/jcommon/docean/src/test/java/com/xiaomi/youpin/docean/test/TestRun.java
@@ -0,0 +1,21 @@
+package com.xiaomi.youpin.docean.test;
+
+import com.xiaomi.youpin.docean.Ioc;
+import com.xiaomi.youpin.docean.anno.IocConfiguration;
+import com.xiaomi.youpin.docean.test.demo.DemoDao;
+
+/**
+ * @author goodjava@qq.com
+ * @date 2024/3/5 14:47
+ *
+ * Evaluating the efficacy of @IocConfiguration.
+ */
+@IocConfiguration(basePackage = {"com.xiaomi.youpin.docean.test.demo"})
+public class TestRun {
+
+ public static void main(String[] args) {
+ DemoDao demoA = Ioc.run(TestRun.class, args).getBean(DemoDao.class);
+ System.out.println(demoA.get());
+ }
+
+}
diff --git a/jcommon/openai/src/main/java/run/mone/openai/OpenaiCall.java b/jcommon/openai/src/main/java/run/mone/openai/OpenaiCall.java
index 8f4fafa1d..8644f7415 100644
--- a/jcommon/openai/src/main/java/run/mone/openai/OpenaiCall.java
+++ b/jcommon/openai/src/main/java/run/mone/openai/OpenaiCall.java
@@ -270,6 +270,9 @@ public void onFailure(EventSource eventSource, @Nullable Throwable t, @Nullable
});
}
+ public static void callStream2(String req, StreamListener sl, ReqConfig config) {
+ callStream2(req, sl, config, null);
+ }
/**
* 原生的调用,底层只依赖okhttp
@@ -278,10 +281,11 @@ public void onFailure(EventSource eventSource, @Nullable Throwable t, @Nullable
* @param sl
* @param config
*/
- public static void callStream2(String req, StreamListener sl, ReqConfig config) {
+ public static void callStream2(String req, StreamListener sl, ReqConfig config, Headers headers) {
MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
Request request = new Request.Builder()
+ .headers(headers)
.url(config.getAskUrl())
.post(RequestBody.create(mediaType, req.getBytes(Charset.forName("utf8"))))
.build();
diff --git a/jcommon/openai/src/test/java/run/mone/openapi/OpenApiTest.java b/jcommon/openai/src/test/java/run/mone/openapi/OpenApiTest.java
index 1c25e6a22..dcb862c5b 100644
--- a/jcommon/openai/src/test/java/run/mone/openapi/OpenApiTest.java
+++ b/jcommon/openai/src/test/java/run/mone/openapi/OpenApiTest.java
@@ -26,6 +26,7 @@
import io.reactivex.disposables.Disposable;
import lombok.Data;
import lombok.SneakyThrows;
+import okhttp3.Headers;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import okhttp3.sse.EventSource;
@@ -298,6 +299,36 @@ public void test4() {
System.out.println(res.getChoices().get(0).getMessage().getContent());
}
+ @SneakyThrows
+ @Test
+ public void testMoonshot() {
+ Stopwatch stopwatch = Stopwatch.createStarted();
+ String question = Files.readString(Paths.get("/Users/zhangzhiyong/IdeaProjects/goodjava/mone/jcommon/ai/src/main/resources/prompt.txt"));
+ OpenAiClient client = OpenaiCall.client(System.getenv("moonshot_token"), "https://api.moonshot.cn/");
+ ChatCompletionResponse res = client.chatCompletion(ChatCompletion.builder().model("moonshot-v1-8k").messages(Lists.newArrayList(Message.builder()
+ .role(Message.Role.USER).content(question)
+ .build())).build());
+ System.out.println(res.getChoices().get(0).getMessage().getContent());
+ System.out.println("use time:" + stopwatch.elapsed(TimeUnit.MILLISECONDS));
+ }
+
+ @SneakyThrows
+ @Test
+ public void testMoonshot2() {
+ String question = Files.readString(Paths.get("/Users/zhangzhiyong/IdeaProjects/goodjava/mone/jcommon/ai/src/main/resources/prompt3.txt"));
+ ChatCompletion completion = ChatCompletion.builder().stream(true).model("moonshot-v1-8k").messages(Lists.newArrayList(Message.builder()
+ .role(Message.Role.USER).content(question)
+ .build())).build();
+
+ OpenaiCall.callStream2(new Gson().toJson(completion), new StreamListener() {
+ @Override
+ public void onEvent(String str) {
+ System.out.println(str);
+ }
+ }, ReqConfig.builder().model("moonshot-v1-8k").askUrl("https://api.moonshot.cn/v1/chat/completions").build(), Headers.of("Authorization", "Bearer " + System.getenv("moonshot_token")));
+ System.in.read();
+ }
+
/**
* 测试使用Azure的openai
* POST https://b2c-mione-gpt35.openai.azure.com/openai/deployments/gpt-35-turbo/completions?api-version=2023-05-15
diff --git a/jcommon/pom.xml b/jcommon/pom.xml
index 5cdfcac09..e96e2bf0c 100644
--- a/jcommon/pom.xml
+++ b/jcommon/pom.xml
@@ -86,6 +86,7 @@
match
infra-common
docean-spring-starter
+ ai
diff --git a/jcommon/struct/src/main/java/com/xiaomi/data/push/graph/Graph.java b/jcommon/struct/src/main/java/com/xiaomi/data/push/graph/Graph.java
index 548604199..b14bb9cec 100644
--- a/jcommon/struct/src/main/java/com/xiaomi/data/push/graph/Graph.java
+++ b/jcommon/struct/src/main/java/com/xiaomi/data/push/graph/Graph.java
@@ -62,6 +62,18 @@ public void addVertex(Vertex vertex) {
this.vertexMap.put(vertex.getV(), vertex.getData());
}
+ //删除这个顶点,且删除和它相关的所有边(class)
+ public void removeVertex(int v) {
+ // 删除顶点数据
+ vertexMap.remove(v);
+ // 删除所有出边
+ adj[v].clear();
+ // 删除所有入边
+ for (List edges : adj) {
+ edges.removeIf(edge -> edge == v);
+ }
+ }
+
public List dependList(int v) {
List result = Lists.newArrayList();
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
new file mode 100644
index 000000000..c601477f2
--- /dev/null
+++ b/jcommon/struct/src/main/java/com/xiaomi/data/push/graph/Graph2.java
@@ -0,0 +1,114 @@
+package com.xiaomi.data.push.graph;
+
+import com.google.common.collect.Maps;
+
+import java.util.*;
+
+/**
+ * @author goodjava@qq.com
+ * @date 2024/3/12 14:49
+ */
+public class Graph2 {
+
+
+ //存储顶点的map
+ private Map vertexMap = Maps.newHashMap();
+
+ //存储边
+ private Map> adjMap = Maps.newHashMap();
+
+
+ /**
+ * 添加顶点
+ *
+ * @param vertex
+ */
+ public void addVertex(Vertex vertex) {
+ this.vertexMap.put(vertex.getV(), vertex.getData());
+ }
+
+ public void addEdge(int u, int v) {
+ //添加边
+ if (!adjMap.containsKey(u)) {
+ adjMap.put(u, new ArrayList<>());
+ }
+ adjMap.get(u).add(v);
+ }
+
+ //删除这个顶点,且删除和它相关的所有边(class)
+ public void removeVertex(int vertex) {
+ // Remove the vertex from vertexMap
+ if (!vertexMap.containsKey(vertex)) {
+ throw new IllegalArgumentException("Vertex does not exist.");
+ }
+ vertexMap.remove(vertex);
+
+ // Remove all edges associated with this vertex from adjMap
+ if (adjMap.containsKey(vertex)) {
+ adjMap.remove(vertex);
+ }
+
+ // Remove the vertex from all adjacency lists
+ for (List edges : adjMap.values()) {
+ edges.removeIf(edge -> edge.equals(vertex));
+ }
+ }
+
+ public D getVertexData(int id) {
+ return vertexMap.get(id);
+ }
+
+
+ //帮我实现下拓扑排序(class)
+ public List topologicalSort() {
+ List result = new ArrayList<>();
+ Map inDegree = new HashMap<>();
+ Queue queue = new LinkedList<>();
+
+ // Initialize in-degree of all vertices
+ for (Integer v : vertexMap.keySet()) {
+ inDegree.put(v, 0);
+ }
+
+ // Calculate in-degree of each vertex
+ for (List edges : adjMap.values()) {
+ for (Integer edge : edges) {
+ inDegree.put(edge, inDegree.get(edge) + 1);
+ }
+ }
+
+ // Find all vertices with in-degree 0
+ for (Map.Entry entry : inDegree.entrySet()) {
+ if (entry.getValue() == 0) {
+ queue.add(entry.getKey());
+ }
+ }
+
+ // Process vertices with in-degree 0
+ while (!queue.isEmpty()) {
+ Integer vertex = queue.poll();
+ result.add(vertex);
+
+ // Decrease in-degree by 1 for all adjacent vertices
+ if (adjMap.containsKey(vertex)) {
+ for (Integer adjVertex : adjMap.get(vertex)) {
+ inDegree.put(adjVertex, inDegree.get(adjVertex) - 1);
+
+ // If in-degree becomes 0, add it to the queue
+ if (inDegree.get(adjVertex) == 0) {
+ queue.add(adjVertex);
+ }
+ }
+ }
+ }
+
+ // Check if there was a cycle
+ if (result.size() != vertexMap.size()) {
+ throw new IllegalStateException("Graph has a cycle, topological sort not possible");
+ }
+
+ return result;
+ }
+
+
+}
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 92fd594ca..f523339a5 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
@@ -1,6 +1,7 @@
package run.mone.struct.test;
import com.xiaomi.data.push.graph.Graph;
+import com.xiaomi.data.push.graph.Graph2;
import com.xiaomi.data.push.graph.Vertex;
import org.junit.Test;
@@ -12,6 +13,27 @@
*/
public class GraphTest {
+
+ @Test
+ public void test1() {
+ Graph2 graph = new Graph2<>();
+
+ graph.addVertex(new Vertex<>(44, VertexData.builder().data("执行").id(0).build()));
+ graph.addVertex(new Vertex<>(88, VertexData.builder().data("aaa").id(0).build()));
+ graph.addVertex(new Vertex<>(33, VertexData.builder().data("开始").id(0).build()));
+ graph.addVertex(new Vertex<>(22, VertexData.builder().data("结束").id(0).build()));
+
+ graph.addEdge(33,44);
+ graph.addEdge(44,22);
+ graph.addEdge(22,88);
+
+
+// graph.removeVertex(44);
+
+ List list = graph.topologicalSort();
+ System.out.println(list);
+ }
+
@Test
public void initializeAndTopologicallySortGraph() {
Graph graph = new Graph<>(5);