Ollama下的各种function calling实现

2024 年 6 月 26 日 星期三(已编辑)
/ , ,
476
摘要
这篇文章介绍了Ollama尚未完成的function calling功能在不同框架下的实现方式。作者列举了几个支持Ollama function calling的SDK,包括langchain、ollama-rs、Hermes-Function-Calling、Qwen-Agent和liteLLM,并对这些框架的实现方式进行了对比分析。尽管Ollama官方尚未支持这一功能,但通过精心设计的prompt,这些框架仍能实现类似的功能。文章还讨论了开源模型在未微调情况下,通过长提示词实现function calling的可行性及其效果。
这篇文章上次修改于 2024 年 6 月 26 日 星期三,可能部分内容已经不适用,如有疑问可询问作者。

阅读此文章之前,你可能需要首先阅读以下的文章才能更好的理解上下文。

Ollama下的各种function calling实现

Ollama实际上是支持function calling这个功能的,但是在

其OpenAI兼容介绍中可以看到OpenAI接口实际并未完成。

所以要使用Http Api实现function calling的话可以参考我之前的文章,根据system prompt来控制LLM大模型的回复格式,并自己实现动态函数注册和内容解析就可以。

Ollama各路LLM 框架下的function calling

虽然Ollama官方目前还是没完成这部分的代码,但是各路语言模型框架已经自己实现了这种功能。

我自己搜到的几个SDK

框架语言支持后端代码状态模型限制链接
langchain- python
- JS/TS
- OpenAI
- Anthropic
- Google
- Cohere
- FireworksAI
- MistralAI
- TogetherAI
正式版官方模型link
langchain-python- python- ollama实验性任意模型link
ollama-rs- rust- ollama正式版任意模型link
Hermes-Function-Calling- python- Hermes正式版Hermeslink
Qwen-Agent- python- OpenAI api兼容正式版理论上可以任意模型link
liteLLM- python超多后端正式版任意模型link

详细研究实现方式

在后端Ollama和模型并不明确支持function calling的情况下这些框架是如何做到的呢?

其实还是万能的prompt,除了商业模型,大部分的开源模型都没有针对function calling进行微调。像官方api中就有此功能的必然是会调用微调过的模型,其他的开源模型没有经过微调效果可能会差一些。

所以我们现在就看看针对开源模型的prompt是怎么写的。

langchain的function calling prompt

DEFAULT_SYSTEM_TEMPLATE = """You have access to the following tools:

{tools}

You must always select one of the above tools and respond with only a JSON object matching the following schema:

{{
  "tool": <name of the selected tool>,
  "tool_input": <parameters for the selected tool, matching the tool's JSON schema>
}}
"""  # noqa: E501

Ollama-rs的function calling prompt

Hermes的function calling prompt

Qwen的function calling prompt

结论

实际上可以看到,各个库为了有更好的效果提示词都超级长,像我之前测试的模型,使用的是最短的langchain的提示词,效果应该就是会比其他的描述那么详细的提示词效果差。

其中像Hermes的提示词都已经有500多token了还没算上tools的描述。

所以未来超长上下文的模型一定是发展趋势,但是单纯的长文本可能又会带来“幻觉”,现在是各种大模型的爆发期,只能看未来还会有怎么样的模型出现吧。

  • Loading...
  • Loading...
  • Loading...
  • Loading...
  • Loading...