@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);
}