diff --git a/pom.xml b/pom.xml index d47f76d..6dfb09d 100644 --- a/pom.xml +++ b/pom.xml @@ -41,6 +41,11 @@ langchain4j-easy-rag 1.0.0-beta1 + + dev.langchain4j + langchain4j-community-zhipu-ai + 1.0.0-beta1 + dev.langchain4j diff --git a/src/main/java/com/ai/config/ZhipuImgModelConfig.java b/src/main/java/com/ai/config/ZhipuImgModelConfig.java new file mode 100644 index 0000000..40f450b --- /dev/null +++ b/src/main/java/com/ai/config/ZhipuImgModelConfig.java @@ -0,0 +1,26 @@ +package com.ai.config; + +import dev.langchain4j.community.model.zhipu.ZhipuAiImageModel; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import java.time.Duration; + +@Configuration +public class ZhipuImgModelConfig { + + private static final String apiKey = "a467e654abf94681a4c358a4b9c1b4c8.2lHG0ovQQoYQ7Qdf"; + + @Bean + public ZhipuAiImageModel zhipuAiImageModel() { + return ZhipuAiImageModel.builder() + .apiKey(apiKey) + .model("CogView-3-Flash") + .logRequests(true) + .logResponses(true) + .callTimeout(Duration.ofSeconds(60)) + .connectTimeout(Duration.ofSeconds(60)) + .writeTimeout(Duration.ofSeconds(60)) + .readTimeout(Duration.ofSeconds(60)) + .build(); + } +} diff --git a/src/main/java/com/ai/controller/LangChainController.java b/src/main/java/com/ai/controller/LangChainController.java index be07137..9e7000e 100644 --- a/src/main/java/com/ai/controller/LangChainController.java +++ b/src/main/java/com/ai/controller/LangChainController.java @@ -3,9 +3,12 @@ package com.ai.controller; import com.ai.service.Assist; import com.ai.service.LangChainService; import dev.langchain4j.community.model.dashscope.QwenChatModel; +import dev.langchain4j.community.model.zhipu.ZhipuAiImageModel; +import dev.langchain4j.data.image.Image; import dev.langchain4j.memory.ChatMemory; import dev.langchain4j.memory.chat.MessageWindowChatMemory; import dev.langchain4j.model.chat.ChatLanguageModel; +import dev.langchain4j.model.output.Response; import dev.langchain4j.service.AiServices; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; @@ -13,6 +16,8 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import java.net.URI; + @RestController @RequestMapping("/langchain") public class LangChainController { @@ -23,6 +28,9 @@ public class LangChainController { @Autowired private Assist assist; + @Autowired + private ZhipuAiImageModel zhipuAiImageModel; + /** * 处理用户输入并调用 LangChainService 获取响应 * @param input 用户输入的内容 @@ -40,23 +48,17 @@ public class LangChainController { return assist.chat(input); } -// ChatLanguageModel qwenModel = QwenChatModel.builder() -// .apiKey("sk-2f703a41fff0488e9b6888013d2ee58a") -// .modelName("deepseek-v3") -// .build(); -// -// ChatMemory chatMemory = MessageWindowChatMemory.builder() -// .maxMessages(10) -// .build(); -// -// Assist assist = AiServices.builder(Assist.class) -// .chatLanguageModel(qwenModel) // the model -// .chatMemoryProvider(memoryId -> MessageWindowChatMemory.withMaxMessages(10)) // memory -// .build(); - @GetMapping("/high/memory-chat") public String memoryChat(@RequestParam("memoryId") String memoryId, @RequestParam("input") String input) { System.out.println("start highlevel memory chat..."); return assist.memoryChat(memoryId, input); } + + @GetMapping("/zhipu/img") + public String memoryChat( @RequestParam("input") String input) { + System.out.println("start highlevel memory chat..."); + Response generate = zhipuAiImageModel.generate(input); + URI url = generate.content().url(); + return "Your remote image is here: " + url; + } } diff --git a/src/main/java/com/ai/http/chat.http b/src/main/java/com/ai/http/chat.http index b2e8618..87ee8de 100644 --- a/src/main/java/com/ai/http/chat.http +++ b/src/main/java/com/ai/http/chat.http @@ -10,3 +10,6 @@ GET http://localhost:8080/langchain/high/memory-chat?memoryId=1&input=你好,我 GET http://localhost:8080/langchain/high/memory-chat?memoryId=1&input=我想要性价比高的 ### 测试 LangChainController 的 highlevel memory chat 接口 GET http://localhost:8080/langchain/high/memory-chat?memoryId=1&input=详细介绍一下 + +### 测试 LangChainController 的 highlevel chat 接口 +GET http://localhost:8080/langchain/zhipu/img?input=请画一张魔兽世界里的兽人高举锤子的图