创建AI-LangChain4j项目 一、新建项目1、配置文件改成yml格式 properties 不利于写很多配置内容也改为yml格式2.为了不让提交到git 一些敏感信息新建一个application-local.yml并且在.gitignore 中配置### CUSTOM ### application-local.yml在提交git时不会被提交效果如下时黄色的文件3.配置加载环境为新建的application-local.yml4.配置阿里大模型jar包加入langchain4jdashscopespoort的整合包!-- https://mvnrepository.com/artifact/dev.langchain4j/langchain4j-community-dashscope-spring-boot-starter -- dependency groupIddev.langchain4j/groupId artifactIdlangchain4j-community-dashscope-spring-boot-starter/artifactId version1.1.0-beta7/version /dependency5.获取大模型的密钥去阿里云百炼中获取大模型密钥大模型服务平台百炼 - 大模型应用构建 - 阿里云yaml中配置密钥如下图spring: application: name: api-code-helper profiles: active: local langchain4j: community: dashscope: chat-model: model-name: qwen-max api-key: 你的keyapplication.yml中也要配置如果不配置测试时会报错如下spring: application: name: api-code-helper langchain4j: community: dashscope: chat-model: model-name: qwen-max api-key: 你的key5.新建AICodeHelper类package org.huhuan.apicodehelper.ai; import dev.langchain4j.data.message.AiMessage; import dev.langchain4j.data.message.UserMessage; import dev.langchain4j.model.chat.ChatModel; import dev.langchain4j.model.chat.response.ChatResponse; import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; Service Slf4j public class AICodeHelper { Resource private ChatModel qwenChatModel; public String chat(String message){ //获取用户消息 UserMessage userMessage UserMessage.from(message); //将消息传给千问 ChatResponse chatResponse qwenChatModel.chat(userMessage); //从response中获取ai回复 AiMessage aiMessage chatResponse.aiMessage(); //打印输出 log.info(AI 输出aiMessage.toString()); //返回ai的实际文本 return aiMessage.text(); } }创建chat方法的单元测试在方法上按住alt加回车变更为package org.huhuan.apicodehelper; import jakarta.annotation.Resource; import org.huhuan.apicodehelper.ai.AICodeHelper; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; SpringBootTest class ApiCodeHelperApplicationTests { Resource private AICodeHelper aiCodeHelper; Test void chat() { aiCodeHelper.chat(你好我是谁); } }执行结果ps如果报Slf4j 的错误更改如下以上是一个调用千问大模型的demo,感谢阅读点赞加关注。谢谢大家的支持