←新闻详情

Ollama Blog更新于 10月14日 12:43

Ollama Python 库 0.4 发布:支持将函数作为工具调用

Ollama Python library 0.4 with function calling improvements · Ollama Blog

AI 导读

Ollama Python 库 0.4 版本支持将 Python 函数直接作为工具传给模型,库内借助 Pydantic 和 docstring 解析自动生成 JSON Schema,此前需手动提供。该版本还加入完整类型支持,并更新了 GitHub 上的示例。

正文

当前显示原文,中文全文译文尚未就绪。

November 25, 2024

In the latest version of the Ollama Python library, functions can now be provided as tools. The library now also has full typing support and new examples have been added.

Start by installing or upgrading the Ollama Python library:

Passing Python functions as tools

Define a Python function

Start by defining a regular Python function. For better results, annotate parameter and return values types and optionally add a Google-style docstring:

Pass the function as a tool to Ollama

Next, use the tools field to pass the function as a tool to Ollama:

Call the function from the model response

Use the returned tool call and arguments provided by the model to call the respective function:

Pass existing functions as tools

Functions from existing Python libraries, SDKs, and elsewhere can now also be provided as tools. For example, the following code passes the request function from the requests library as a tool to fetch the contents of the Ollama website:

How it works: generating JSON Schema from functions

The Ollama Python library uses Pydantic and docstring parsing to generate the JSON schema. As an example, for the add_two_nubmers function declared at the start of this post, the following JSON schema is generated (and was previously required to be provided manually as a tool):

Additional improvements to the Ollama Python library

The 0.4 release of the Ollama Python library includes additional improvements:

Examples have been updated on the Ollama Python GitHub; Full typing support throughout the library to support direct object access while maintaining existing functionality

来源

Ollama Blog