工具支援

2024年7月25日

ollama with a box of tools, ready to serve you

Ollama 現在支援工具呼叫,適用於 Llama 3.1 等熱門模型。這使模型能夠使用它所知的工具來回答給定的提示,從而使模型能夠執行更複雜的任務或與外部世界互動。

範例工具包括

  • 函式與 API
  • 網頁瀏覽
  • 程式碼直譯器
  • 還有更多!

工具呼叫

若要啟用工具呼叫,請透過 Ollama API 中的 tools 欄位提供可用工具的清單。

import ollama

response = ollama.chat(
    model='llama3.1',
    messages=[{'role': 'user', 'content':
        'What is the weather in Toronto?'}],

		# provide a weather checking tool to the model
    tools=[{
      'type': 'function',
      'function': {
        'name': 'get_current_weather',
        'description': 'Get the current weather for a city',
        'parameters': {
          'type': 'object',
          'properties': {
            'city': {
              'type': 'string',
              'description': 'The name of the city',
            },
          },
          'required': ['city'],
        },
      },
    },
  ],
)

print(response['message']['tool_calls'])

支援的模型現在將以 tool_calls 回應。工具回應可以透過具有 tool 角色的訊息提供。請參閱 API 文件以取得更多資訊。

支援的模型

支援模型的列表可以在模型頁面的「工具」類別下找到

注意:請執行 ollama pull <model> 以檢查您是否擁有最新模型

ollama.com tool models

OpenAI 相容性

Ollama 的 OpenAI 相容端點現在也支援工具,使得切換到使用 Llama 3.1 和其他模型成為可能。

import openai

openai.base_url = "https://127.0.0.1:11434/v1"
openai.api_key = 'ollama'

response = openai.chat.completions.create(
	model="llama3.1",
	messages=messages,
	tools=tools,
)

完整範例

未來改進

  • 串流工具呼叫:串流回傳工具呼叫,以便在返回多個工具時更快開始採取行動
  • 工具選擇:強制模型使用工具

讓我們一起建構

我們非常興奮為您帶來工具支援,並期待看到您用它建構什麼!

如果您有任何意見回饋,請隨時透過我們的 Discordhello@ollama.com 告訴我們。