1.springboot3整合langchain4j
springboot3整合langchain4j
在查阅官方文档(https://docs.langchain4j.dev/tutorials/spring-boot-integration)之后,官方提供了starter
Spring Boot Starters
Spring Boot starters help with creating and configuring language models, embedding models, embedding stores, and other core LangChain4j components through properties.
To use one of the Spring Boot starters, import the corresponding dependency.
The naming convention for the Spring Boot starter dependency is: langchain4j-{integration-name}-spring-boot-starter.
For example, for OpenAI (langchain4j-open-ai), the dependency name would be langchain4j-open-ai-spring-boot-starter:
▼xml复制代码<dependency> <groupId>dev.langchain4j</groupId> <artifactId>langchain4j-open-ai-spring-boot-starter</artifactId> <version>1.1.0-beta7</version> </dependency>
Then, you can configure model parameters in the application.properties file as follows:
▼propertoes复制代码langchain4j.open-ai.chat-model.api-key=${OPENAI_API_KEY} langchain4j.open-ai.chat-model.model-name=gpt-4o langchain4j.open-ai.chat-model.log-requests=true langchain4j.open-ai.chat-model.log-responses=true ...
In this case, an instance of OpenAiChatModel (an implementation of a ChatModel) will be automatically created, and you can autowire it where needed:
▼java复制代码@RestController public class ChatController { ChatModel chatModel; public ChatController(ChatModel chatModel) { this.chatModel = chatModel; } @GetMapping("/chat") public String model(@RequestParam(value = "message", defaultValue = "Hello") String message) { return chatModel.chat(message); } }
1.跟着官方文档整合
- 复制依赖到pom中
- 修改配置参数
▼yml复制代码langchain4j: open-ai: chat-model: api-key: ${OPENAI_API_KEY:your_key} base-url: ${OPENAI_API_BASE_URL:https://dashscope.aliyuncs.com/compatible-mode/v1} model-name: ${OPENAI_API_MODEL_NAME:qwen-max} log-requests: true log-responses: true
- 复制controller并启动项目测试接口是否正常:
▼text复制代码2025-06-25T11:37:30.190+08:00 INFO 29908 --- [langchain4j-study] [nio-8889-exec-1] d.l.http.client.log.LoggingHttpClient : HTTP request: - method: POST - url: https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions - headers: [Authorization: Beare...7d], [User-Agent: langchain4j-openai], [Content-Type: application/json] - body: { "model" : "qwen-max", "messages" : [ { "role" : "user", "content" : "你是谁?" } ], "stream" : false } 2025-06-25T11:37:33.834+08:00 INFO 29908 --- [langchain4j-study] [nio-8889-exec-1] d.l.http.client.log.LoggingHttpClient : HTTP response: - status code: 200 - headers: [vary: Origin,Access-Control-Request-Method,Access-Control-Request-Headers, Accept-Encoding], [x-request-id: eff665b0-5365-973e-a51b-7bbd102775b4], [x-dashscope-call-gateway: true], [content-type: application/json], [content-length: 660], [req-cost-time: 3296], [req-arrive-time: 1750822651340], [resp-start-time: 1750822654637], [x-envoy-upstream-service-time: 3294], [set-cookie: acw_tc=eff665b0-5365-973e-a51b-7bbd102775b43e7b1377cea701bda69bbda9e698e3d4;path=/;HttpOnly;Max-Age=1800], [date: Wed, 25 Jun 2025 03:37:34 GMT], [server: istio-envoy] - body: {"choices":[{"message":{"role":"assistant","content":"我是Qwen,由阿里云开发的大型语言模型。我的目的是帮助用户获得准确、有用的信息,并以自然的方式与用户进行交流。无论是回答问题、提供信息还是进行对话,我都会尽力提供最好的帮助。有什么我可以帮到你的吗?"},"finish_reason":"stop","index":0,"logprobs":null}],"object":"chat.completion","usage":{"prompt_tokens":11,"completion_tokens":55,"total_tokens":66,"prompt_tokens_details":{"cached_tokens":0}},"created":1750822655,"system_fingerprint":null,"model":"qwen-max","id":"chatcmpl-eff665b0-5365-973e-a51b-7bbd102775b4"}
大功告成!!!!
2.langchain4j也提供了阿里百炼大模型的依赖的starter
文档:https://docs.langchain4j.dev/integrations/language-models/dashscope/
DashScope (Qwen)
DashScope is a platform developed by Alibaba Cloud. It provides an interface for model visualization, monitoring, and debugging, particularly when working with AI/ML models in production environments. The platform allows users to visualize performance metrics, track model behavior, and identify potential issues early on in the deployment cycle.
Qwen models are a series of generative AI models developed by Alibaba Cloud. The Qwen family of models are specifically designed for tasks like text generation, summarization, question answering, and various NLP tasks.
You can refer to DashScope Document for more details. LangChain4j integrates with DashScope by Using DashScope Java SDK
Spring Boot
注意:
Since 1.0.0-alpha1, langchain4j-dashscope-spring-boot-starter has migrated to langchain4j-community and is renamed to langchain4j-community-dashscope-spring-boot-starter.
- Before
1.0.0-alpha1:
▼xml复制代码<dependency> <groupId>dev.langchain4j</groupId> <artifactId>langchain4j-dashscope-spring-boot-starter</artifactId> <version>${previous version here}</version> </dependency>
1.0.0-alpha1and later:
▼xml复制代码<dependency> <groupId>dev.langchain4j</groupId> <artifactId>langchain4j-community-dashscope-spring-boot-starter</artifactId> <version>${latest version here}</version> </dependency>
- 对应的依赖版本号可以去maven中央仓库搜(https://central.sonatype.com/)
- After introduce
langchain4j-community-dashscope-spring-boot-starterdependency, you can simply registerQwenChatModelbean by using below configuration:
▼yml复制代码langchain4j.community.dashscope.chat-model.api-key=<You API Key here> langchain4j.community.dashscope.chat-model.model-name=qwen-max # The properties are the same as `QwenChatModel` # e.g. # langchain4j.community.dashscope.chat-model.temperature=0.7 # langchain4j.community.dashscope.chat-model.max-tokens=4096
3.跟着官方文档操作
- 添加上面的依赖(1.0.0-alpha1之后的):langchain4j-community-dashscope-spring-boot-starter
- 修改配置文件
▼yml复制代码langchain4j: community: dashscope: chat-model: api-key: ${OPENAI_API_KEY:your_key} # 注意,在使用了dashscope的starter之后,不需要配置base-url了,不然会报错 # base-url: ${OPENAI_API_BASE_URL:https://dashscope.aliyuncs.com/compatible-mode/v1} model-name: ${OPENAI_API_MODEL_NAME:qwen-max}
- 修改controller的构造方法(将模型改为qwenModel)
▼java复制代码public ChatController(ChatModel qwenModel ) { this.chatModel = qwenModel ; }
- 启动测试(因为qwenModel没有提供控制台日志打印所以就看不到日志信息,下面是接口返回的信息):
▼text复制代码我是Qwen,由阿里云开发的超大规模语言模型。我被设计用来回答问题、创作文字,比如写故事、写公文、写邮件、写剧本等等,还能表达观点,玩游戏等。我的目标是帮助用户高效地获得信息、提高创造力和生产力。有什么我可以帮到你的吗?
