Spring ai ,ollama qwen2.5:7b 智能体开发,提供一个完整的示例
时间: 2025-06-02 07:28:27 浏览: 17
<think>嗯,用户让我介绍Spring AI和Ollama Qwen2.5:7b的智能体开发,并提供一个完整的示例。首先,我需要确认自己对这两个技术的理解是否正确。Spring AI应该是Spring框架中用于集成人工智能功能的模块,可能类似于Spring Boot的starter,方便开发者调用AI模型。而Ollama可能是一个部署和运行大型语言模型的工具,Qwen2.5:7b则是阿里云的通义千问模型的一个版本,7B参数量的版本。
用户的需求是希望得到一个完整的示例,所以需要涵盖环境搭建、依赖配置、模型集成、API调用以及一个简单的应用场景。可能需要使用Spring Boot来快速搭建项目,结合Spring AI的相关库,然后通过Ollama来本地运行Qwen2.5模型。
接下来,我需要考虑步骤。首先,确保Ollama已经安装,并且能运行Qwen2.5:7b模型。用户可能已经安装了Ollama,但可能需要指导如何拉取模型。然后是Spring项目的创建,添加Spring AI和Ollama的依赖,配置application.properties,编写Service类调用模型,最后在Controller里暴露API。
可能的坑点包括依赖版本是否兼容,Ollama的配置是否正确,模型名称是否正确。另外,示例代码需要简洁明了,用户可能需要注释和说明。同时,用户可能希望了解如何测试,比如用curl或者Postman发送请求,检查返回结果。
还需要注意,Ollama默认的端口是11434,模型名称是否准确,比如qwen2.5:7b是否正确,可能需要确认模型在Ollama库中的确切名称。此外,Spring AI的API可能还在迭代中,确保示例代码符合当前最佳实践。
最后,示例的智能体可能需要一个具体的功能,比如问答系统,处理用户输入并返回模型的回答。测试部分要确保代码能运行,并给出预期的输出示例。可能还要提醒用户如果遇到错误如何排查,比如检查Ollama服务是否运行,模型是否加载成功,依赖是否正确添加等。</think>以下是一个基于 **Spring AI + Ollama (Qwen2.5-7B)** 的完整智能体开发示例,包含环境配置、代码实现和测试步骤:
---
### 1. 环境准备
#### 1.1 安装 Ollama
- 前往 [Ollama官网](https://ollama.ai/) 下载并安装
- 拉取 Qwen2.5-7B 模型:
```bash
ollama pull qwen2.5:7b
```
#### 1.2 创建 Spring Boot 项目
使用 [Spring Initializr](https://start.spring.io/) 创建项目,选择以下依赖:
- Spring Web
- Spring AI (Ollama)
---
### 2. 项目配置
#### 2.1 `pom.xml` 依赖
```xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
<version>0.8.1</version> <!-- 检查最新版本 -->
</dependency>
</dependencies>
```
#### 2.2 `application.properties`
```properties
# Ollama 配置
spring.ai.ollama.base-url=http://localhost:11434
spring.ai.ollama.chat.model=qwen2.5:7b
# 启用AI功能
spring.ai.ollama.chat.enabled=true
```
--
### 3. 核心代码实现
#### 3.1 创建智能体服务类
```java
import org.springframework.ai.chat.ChatClient;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.stereotype.Service;
@Service
public class QwenAgentService {
private final ChatClient chatClient;
public QwenAgentService(ChatClient chatClient) {
this.chatClient = chatClient;
}
public String chatWithAI(String userInput) {
Prompt prompt = new Prompt(new UserMessage(userInput));
return chatClient.call(prompt).getResult().getOutput().getContent();
}
}
```
#### 3.2 创建 REST 控制器
```java
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class AIController {
private final QwenAgentService agentService;
public AIController(QwenAgentService agentService) {
this.agentService = agentService;
}
@PostMapping("/chat")
public String handleChatRequest(@RequestBody String userMessage) {
return agentService.chatWithAI(userMessage);
}
}
```
---
### 4. 测试智能体
#### 4.1 启动 Spring Boot 应用
```bash
mvn spring-boot:run
```
#### 4.2 发送测试请求
使用 curl 测试:
```bash
curl -X POST -H "Content-Type: text/plain" -d "请用中文写一首关于春天的五言绝句" http://localhost:8080/chat
```
#### 4.3 预期响应示例
```json
{
"content": "春风拂柳绿,\n细雨润桃红。\n莺啼深树里,\n燕舞小园中。"
}
```
---
### 5. 进阶功能扩展
1. **上下文记忆**:添加 `ChatMemory` 实现多轮对话
2. **流式响应**:使用 `StreamingChatClient` 实现实时输出
3. **自定义提示词**:通过 `PromptTemplate` 优化指令工程
4. **多模态支持**:结合图片/文档处理扩展功能
---
### 常见问题排查
1. **Ollama服务未启动**:确保运行 `ollama serve`
2. **模型未加载**:检查 `ollama list` 确认模型存在
3. **端口冲突**:验证 11434 和 8080 端口是否可用
4. **版本兼容性**:确保 Spring AI 与 Spring Boot 版本匹配
完整代码示例可参考:[GitHub Spring AI Ollama Demo](https://github.com/spring-projects/spring-ai)(需根据实际模型调整)
阅读全文
相关推荐














