spring-ai 使用版本与坐标
<ai.version>1.0.0-M6</ai.version>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-core</artifactId>
<version>${ai.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
<version>${ai.version}</version>
</dependency>
Controller 分别使用Ollama中chatModel 与Core中chatModel+Tools
package com.vecter.mini.program.web.controller;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@Slf4j
@RestController
@RequestMapping("/api/mcp")
public class McpServerController {
@Resource
private OllamaChatModel chatModel;
@Resource
private ChatClient chatModelClient;
@PostMapping("/ollam")
public String ollam(@RequestParam String message){
message = "请使用中文简体回答:" + message;
Prompt prompt = new Prompt(new UserMessage(message));
ChatResponse call = chatModel.call(prompt);
String callstr = call.toString();
log.info(callstr);
return message;
}
@GetMapping("/mcpServer")
public String mcpServer(@RequestParam String message){
ChatClient.CallResponseSpec call = chatModelClient.prompt(message).call();
log.info(call.content());
return call.content();
}
}
Tools服务
package com.vecter.mini.program.business.service;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Service;
import java.util.Random;
@Service
public class AiWebfluxService {
@Tool(description = "通给出的物料编码获取物料库存信息")
public String getWeather(@ToolParam(description = "物料编码") String productName) {
Random random = new Random();
int randomInt = random.nextInt();
int randomIntInRange = random.nextInt(100);
return "物料编码" +productName + "库存是" + randomIntInRange+"PCS";
}
}
装载Tools入Chat
package com.vecter.mini.program.business.config;
import com.vecter.mini.program.business.service.AiWebfluxService;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.tool.method.MethodToolCallbackProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class McpToolsConfig {
@Resource
private ChatModel chatModel;
@Resource
private AiWebfluxService aiWebfluxService;
@Bean
public ChatClient chatModelClient() {
return ChatClient
.builder(chatModel)
.defaultTools(MethodToolCallbackProvider.builder().toolObjects(aiWebfluxService).build())
.build();
}
}
