# 查看可用模型列表 $ ollama ls NAME ID SIZE MODIFIED qwq:latest cc1091b0e276 19 GB 17 minutes ago # 启动模型交互 $ ollama run qwq:latest >>> 请用java实现快速排序算法
<dependency> <groupId>io.github.pig-mesh.ai</groupId> <artifactId>deepseek-spring-boot-starter</artifactId> <version>1.4.5</version> </dependency>
deepseek: base-url: http://127.0.0.1:11434/v1 model: qwq:latest api-key: local-key
@Autowired private DeepSeekClient deepSeekClient; @GetMapping(value = "/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE) public Flux<ChatCompletionResponse> chat(String prompt) { return deepSeekClient.chatFluxCompletion(prompt); }
@GetMapping(value = "/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE) public Flux<ChatCompletionResponse> chat(String prompt) { // 定义天气查询函数 Function WEATHER_FUNCTION = Function.builder() .name("get_current_weather") .description("Get the current weather in a given location") .parameters(JsonObjectSchema.builder() .properties(new LinkedHashMap<String, JsonSchemaElement>() {{ put("location", JsonStringSchema.builder() .description("The city name") .build()); }}) .required(asList("location", "unit")) .build()) .build(); // 将 Function 转换为 Tool Tool WEATHER_TOOL = Tool.from(WEATHER_FUNCTION); // 创建请求 ChatCompletionRequest request = ChatCompletionRequest.builder() .model("Qwen/QwQ-32B") // 指定模型 .addUserMessage(prompt) .tools(WEATHER_TOOL) // 添加工具函数 .build(); // 发送请求并获取响应 ChatCompletionResponse response = deepSeekClient.chatCompletion(request).execute(); // 获取工具调用信息 AssistantMessage assistantMessage = response.choices().get(0).message(); ToolCall toolCall = assistantMessage.toolCalls().get(0); // 解析函数调用参数 FunctionCall functionCall = toolCall.function(); String arguments = functionCall.arguments(); // 例如: {"location": "北京"} // 执行函数获取结果 Map map = Json.fromJson(arguments, Map.class); String weatherResult = map.get("location") + "气温 20°"; // 创建工具消息 ToolMessage toolMessage = ToolMessage.from(toolCall.id(), weatherResult); // 继续对话 ChatCompletionRequest followUpRequest = ChatCompletionRequest.builder() .model("Qwen/QwQ-32B") .messages( UserMessage.from(prompt), assistantMessage, toolMessage // 添加工具消息 ) .build(); return deepSeekClient.chatFluxCompletion(followUpRequest); }