Dict.CN 在线词典, 英语学习, 在线翻译 ------------- MyGitee My腾云code My51cto

Happy_EveryDay

可以平凡 不可以平庸 无爱则无忧,无欲则无求,无怒而无敌,无怨才是佛。所有烦恼,都是放不下的执著 开源技群 328035181 MyGitee

博客园 首页 管理

 

image

 

 

 

1、pom

<properties>
<java.version>17</java.version>
<spring-ai.version>2.0.0-M7</spring-ai.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webmvc-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>${spring-ai.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

 

 

2、application.yml

${OPENAI_API_KEY}

image

 

 

 

spring:
application:
name: dashscope18088
ai:
openai:
api-key: ${OPENAI_API_KEY}
base-url: https://dashscope.aliyuncs.com/compatible-mode/v1
chat:
model: qwen3.6-plus
# chat:
# model: qwen3.6-plus
# 可选:超时/重试配置
# options:
# temperature: 0.7
# max-tokens: 2000
server:
port: 18088



3、config

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AiConfiguration {

@Bean
public ChatClient chatClient(OpenAiChatModel model){
return ChatClient
.builder(model) // 绑定OpenAiChatModel模型
.build(); // 构建ChatClient实例
}
}

 

 

4、controller

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyAiChatController {

@Autowired
private ChatClient chatClient;

@RequestMapping("/ai")
public String ai(String question){
return chatClient.prompt() // 创建 Prompt 对象,用于构建聊天请求
.user(question) // 设置用户输入
.call() // 调用模型,返回结果
.content(); // 获取结果内容
}
}

 

 

 

5、访问

http://localhost:18088/ai?question=你是谁?

image

 

 
posted on 2026-05-27 21:34  cn2025  阅读(3)  评论(0)    收藏  举报