Ollama在自然语言处理(NLP)领域展现了强大的能力,能够理解和生成语言,极大丰富了人机交互的可能性。以下是如何将Ollama与Spring Boot项目集成的详细步骤。
1. Ollama设置
- 下载与安装: 从官方网站下载 Ollama。
- 启动Ollama服务: 安装完成后,打开浏览器访问 http://localhost:11434/ 启动服务。
2. Spring Boot项目初始化
- 项目初始化: 使用Spring Initializer进行初始化,选择Web和Ollama AI作为项目依赖。
- 实验性提示: 本项目目前处于实验阶段,仅提供快照版本,可能存在不稳定因素。
3. 项目结构与配置
- 配置文件: 创建
application.properties
文件以配置项目属性。
spring.application.name=spring-AI
spring.ai.ollama.base-url=http://localhost:11434/api
spring.ai.ollama.model=llama3
server.port=8686
spring.main.allow-bean-definition-overriding=true
- 响应模型: 创建
LlamaResponse
模型以表示服务响应。
package com.ai.spring.AI.response;
import lombok.Data;
import lombok.experimental.Accessors;
@Data
@Accessors(chain = true)
public class LlamaResponse {
private String message;
}
- 服务接口: 创建
LlamaAiService
接口。
package com.ai.spring.AI.service;
import com.ai.spring.AI.response.LlamaResponse;
public interface LlamaAiService {
LlamaResponse generateMessage(String prompt);
LlamaResponse generateJoke(String topic);
}
- 服务实现: 实现
LlamaAiServiceImpl
。
package com.ai.spring.AI.service.impl;
import com.ai.spring.AI.response.LlamaResponse;
import com.ai.spring.AI.service.LlamaAiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class LlamaAiServiceImpl implements LlamaAiService {
@Autowired
private OllamaChatClient chatClient;
@Override
public LlamaResponse generateMessage(String promptMessage) {
final String llamaMessage = chatClient.generate(promptMessage);
return new LlamaResponse().setMessage(llamaMessage);
}
@Override
public LlamaResponse generateJoke(String topic) {
final String llamaMessage = chatClient.generate(String.format("Tell me a joke about %s", topic));
return new LlamaResponse().setMessage(llamaMessage);
}
}
- 客户端接口: 创建
ChatClient
接口。
package com.ai.spring.AI.service;
public interface ChatClient {
String generate(String prompt);
}
- Ollama客户端实现: 实现
OllamaChatClient
。
package com.ai.spring.AI.service.impl;
import com.ai.spring.AI.service.ChatClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
@Component("customOllamaChatClient")
public class OllamaChatClient implements ChatClient {
private final RestTemplate restTemplate;
private final String baseUrl;
private final String model;
public OllamaChatClient(RestTemplate restTemplate,
@Value("${spring.ai.ollama.base-url}") String baseUrl,
@Value("${spring.ai.ollama.model}") String model) {
this.restTemplate = restTemplate;
this.baseUrl = baseUrl;
this.model = model;
}
@Override
public String generate(String prompt) {
String url = String.format("%s/generate", baseUrl);
Map<String, Object> request = new HashMap<>();
request.put("model", model);
request.put("prompt", prompt);
request.put("stream", false);
try {
// 打印URL和请求负载以进行调试
System.out.println("URL: " + url);
System.out.println("Request: " + request);
return restTemplate.postForObject(url, request, String.class);
} catch (HttpClientErrorException e) {
// 记录错误详情
System.err.println("Error: " + e.getStatusCode() + " - " + e.getResponseBodyAsString());
throw e;
}
}
}
- 配置类: 创建
AppConfig
以配置RestTemplate
。
package com.ai.spring.AI.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class AppConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
- 控制器: 创建
LlamaRestController
。
package com.ai.spring.AI.controller;
import com.ai.spring.AI.response.LlamaResponse;
import com.ai.spring.AI.service.impl.LlamaAiServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
public class LlamaRestController {
@Autowired
private LlamaAiServiceImpl llamaAiService;
@GetMapping("api/v1/ai/generate")
public ResponseEntity<LlamaResponse> generate(
@RequestParam(value = "promptMessage", defaultValue = "Why is the sky blue?") String promptMessage) {
final LlamaResponse aiResponse = llamaAiService.generateMessage(promptMessage);
return ResponseEntity.status(HttpStatus.OK).body(aiResponse);
}
@PostMapping("api/v1/ai/generate/joke/{topic}")
public ResponseEntity<LlamaResponse> generateJoke(@PathVariable String topic) {
final LlamaResponse aiResponse = llamaAiService.generateJoke(topic);
return ResponseEntity.status(HttpStatus.OK).body(aiResponse);
}
}
4. 启动与测试
启动Spring Boot应用程序后,可以通过以下URL进行测试:
您也可以在Ollama命令行界面中进行测试。