小编分享DeepSeek。

访客 470 0

本文主要探讨了如何在ollama + deepseek-r1的环境中尝试function call功能。

步骤

首先,我们自定义了一个JavaScript函数,代码如下:

@Service
@Slf4j
@Description("根据用户的查询生成天气相关信息")
public class DemoFunction implements Function<DemoFunction.Request, DemoFunction.Response> {
    @JsonClassDescription("用户的查询")
    public record Request(
            @JsonProperty(required = true, value = "query") 
            @JsonPropertyDescription("用户的查询") String query) {
    }
    @JsonClassDescription("天气信息")
    public record Response(String result) {
    }
    @Override
    public Response apply(Request s) {
        log.info("call demoFunction query:{}", s.query);
        return new Response("今天深圳天气晴朗");
    }
}
登录后复制

接着,我们编写了API调用代码:

@GetMapping("/function-all")
public String functionCall(HttpServletResponse response, @RequestParam("query") String query) {
    response.setCharacterEncoding("UTF-8");
    OllamaOptions customOptions = OllamaOptions.builder()
            .topP(0.7)
            .temperature(0.8)
            .function("demoFunction")
            .build();
    return ollamaChatModel.call(new Prompt(Arrays.asList(new SystemMessage("请基于用户的查询调用function来回答"), new UserMessage(query)), customOptions)).getResult().getOutput().getContent();
}
登录后复制

然而,在实际运行中,我们遇到了以下错误:

2025-02-21T17:45:28.633+08:00 ERROR 26728 --- [spring-ai-alibaba-ollama-chat-model-example] [io-10005-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: java.lang.RuntimeException: [400] Bad Request - {"error":"registry.ollama.ai/library/deepseek-r1:8b does not support tools"}] with root cause
java.lang.RuntimeException: [400] Bad Request - {"error":"registry.ollama.ai/library/deepseek-r1:8b does not support tools"}
登录后复制

通过查阅官方文档,我们发现当前版本的deepseek-chat模型在Function Calling方面的功能不稳定,可能会导致循环调用或空响应。官方正在积极修复,预计在下一个版本中解决。

此外,DeepSeek R1模型本身并不原生支持function calling或结构化输出,主要优化用于推理任务(如数学、代码和STEM),并遵循对话格式。

为了解决这个问题,我们尝试切换模型到MFDoom/deepseek-r1-tool-calling,并将spring.ai.ollama.chat.model设置为MFDoom/deepseek-r1-tool-calling:1.5b。经过重新运行,发现该模型支持function calling。

以下是步骤的详细解释:

确定查询:用户的查询是“今天天气怎么样”。确定所需参数:工具期望一个名为query的字符串参数。根据查询生成输出:使用提供的demoFunction函数,我们可以根据查询生成天气信息。构建输出字符串:将占位符替换为用户的具体查询,并按要求格式化输出。

最终输出如下:

{"result":"今天深圳天气晴朗"}
登录后复制

尽管如此,实际操作中prompt的书写仍然是一个挑战,书写不当可能会导致多次function调用。

总结

目前,DeepSeek的r1模型不支持function call,尽管有第三方改造支持,但效果还不太理想。建议等到官方支持后再尝试。

参考文档

    使用spring-ai-alibaba本地集成ollama+deepseekFunction calling #9 Function Calling MFDoom/deepseek-r1-tool-calling 终于有一家支持 Function Call 了!!!

DeepSeek funcation call尝试

以上就是DeepSeek funcation call尝试的详细内容,更多请关注楠楠科技社其它相关文章!

标签: #DeepSeek #funcation #call