本文主要探讨了如何在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。
以下是步骤的详细解释:
最终输出如下:
{"result":"今天深圳天气晴朗"}
尽管如此,实际操作中prompt的书写仍然是一个挑战,书写不当可能会导致多次function调用。
总结
目前,DeepSeek的r1模型不支持function call,尽管有第三方改造支持,但效果还不太理想。建议等到官方支持后再尝试。
参考文档
以上就是DeepSeek funcation call尝试的详细内容,更多请关注php中文网其它相关文章!
DeepSeek (深度求索)杭州深度求索(DeepSeek)官方推出的AI助手,免费体验与全球领先AI模型的互动交流。它通过学习海量的数据和知识,能够像人类一样理解和处理信息。多项性能指标对齐海外顶尖模型,用更快的速度、更加全面强大的功能答疑解惑,助力高效美好的生活。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号