20250329 智普AI文生图功能

This commit is contained in:
liangjinglin 2025-03-29 23:21:32 +08:00
parent 7e1e31e0bd
commit 3ee1c62f70
4 changed files with 50 additions and 14 deletions

View File

@ -41,6 +41,11 @@
<artifactId>langchain4j-easy-rag</artifactId> <artifactId>langchain4j-easy-rag</artifactId>
<version>1.0.0-beta1</version> <version>1.0.0-beta1</version>
</dependency> </dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-community-zhipu-ai</artifactId>
<version>1.0.0-beta1</version>
</dependency>
<dependency> <dependency>
<groupId>dev.langchain4j</groupId> <groupId>dev.langchain4j</groupId>

View File

@ -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();
}
}

View File

@ -3,9 +3,12 @@ package com.ai.controller;
import com.ai.service.Assist; import com.ai.service.Assist;
import com.ai.service.LangChainService; import com.ai.service.LangChainService;
import dev.langchain4j.community.model.dashscope.QwenChatModel; 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.ChatMemory;
import dev.langchain4j.memory.chat.MessageWindowChatMemory; import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatLanguageModel; import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.output.Response;
import dev.langchain4j.service.AiServices; import dev.langchain4j.service.AiServices;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; 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.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.net.URI;
@RestController @RestController
@RequestMapping("/langchain") @RequestMapping("/langchain")
public class LangChainController { public class LangChainController {
@ -23,6 +28,9 @@ public class LangChainController {
@Autowired @Autowired
private Assist assist; private Assist assist;
@Autowired
private ZhipuAiImageModel zhipuAiImageModel;
/** /**
* 处理用户输入并调用 LangChainService 获取响应 * 处理用户输入并调用 LangChainService 获取响应
* @param input 用户输入的内容 * @param input 用户输入的内容
@ -40,23 +48,17 @@ public class LangChainController {
return assist.chat(input); 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") @GetMapping("/high/memory-chat")
public String memoryChat(@RequestParam("memoryId") String memoryId, @RequestParam("input") String input) { public String memoryChat(@RequestParam("memoryId") String memoryId, @RequestParam("input") String input) {
System.out.println("start highlevel memory chat..."); System.out.println("start highlevel memory chat...");
return assist.memoryChat(memoryId, input); return assist.memoryChat(memoryId, input);
} }
@GetMapping("/zhipu/img")
public String memoryChat( @RequestParam("input") String input) {
System.out.println("start highlevel memory chat...");
Response<Image> generate = zhipuAiImageModel.generate(input);
URI url = generate.content().url();
return "Your remote image is here: " + url;
}
} }

View File

@ -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=我想要性价比高的 GET http://localhost:8080/langchain/high/memory-chat?memoryId=1&input=我想要性价比高的
### 测试 LangChainController 的 highlevel memory chat 接口 ### 测试 LangChainController 的 highlevel memory chat 接口
GET http://localhost:8080/langchain/high/memory-chat?memoryId=1&input=详细介绍一下 GET http://localhost:8080/langchain/high/memory-chat?memoryId=1&input=详细介绍一下
### 测试 LangChainController 的 highlevel chat 接口
GET http://localhost:8080/langchain/zhipu/img?input=请画一张魔兽世界里的兽人高举锤子的图