Ollama函数即工具

作为一名 AI 工程师,我要向 Ollama 表示感谢。我学到了很多东西,并且在我的 AI 旅程中积极使用 Ollama。这个里程碑版本 0.45 带来了令人兴奋的新功能和改进,增强了库的功能和可用性。

函数即工具:版本 0.4.5 带来了令人兴奋的新功能和改进,增强了库的功能和可用性。你现在可以将 Python 函数直接作为 Ollama 库中的工具传递。此功能允许无缝集成 Python 库、SDK 等中的现有函数,从而使你的开发过程更加高效。

要开始使用最新版本的 Python,请运行:

pip3 install -U ollama

检查你的本地模型:

 ollama list
# Loading models: ollama pull {model_name}
# ollama pull llama3.2
列出本地模型

基于 Ollama 函数调用示例,以下脚本演示了工具仍然可以手动定义并传递到聊天中。

import ollama
import pprint
from typing import Dict, Any
# multi functions
import asyncio
from ollama import ChatResponse


def decodefunction_response(response,available_functions):
    if response.message.tool_calls:
        for tool in response.message.tool_calls:
            if function_to_call := available_functions.get(tool.function.name):
                pprint.pprint(f"Calling function: {tool.function.name}")
                pprint.pprint(f"Arguments: {tool.function.arguments}")
                pprint.pprint(f"Function out: {function_to_call(**tool.function.arguments)}")
            else:
                pprint.pprint(f"Function {tool.function.name}, NOT FOUND ERROR.")

# here must use int(a) operator with int(b), like int(a)_int(b)
def add_two_numbers(a:int, b:int)->int:
    return int(a)+int(b)
def subtract_two_numbers(a:int, b:int)->int:
    return int(a)-int(b)


add_two_numbers_tool = {
  'type': 'function',
  'function': {
    'name': 'add_two_numbers',
    'description': 'Add two numbers and return sum of these two number',
    'parameters': {
      'type': 'object',
      'required': ['a', 'b'],
      'properties': {
        'a': {'type': 'integer', 'description': 'The first number'},
        'b': {'type': 'integer', 'description': 'The second number'},
      },
    },
  },
}

subtract_two_numbers_tool = {
  'type': 'function',
  'function': {
    'name': 'subtract_two_numbers',
    'description': 'Subtract two numbers',
    'parameters': {
      'type': 'object',
      'required': ['a', 'b'],
      'properties': {
        'a': {'type': 'integer', 'description': 'The first number'},
        'b': {'type': 'integer', 'description': 'The second number'},
      },
    },
  },
}

async def testmultiplefunction():
    client= ollama.AsyncClient()
    prompt = "what is three plus one?"
    available_functions = {
        "add_two_numbers":add_two_numbers,
        "subtract_two_numbers":subtract_two_numbers
    }
    response: ChatResponse = await client.chat(
        'llama3.2',
        messages=[
            {
                'role': 'user',
                'content':prompt
            }
        ],
        tools=[add_two_numbers, subtract_two_numbers]
    )
    decodefunction_response(response,available_functions)
    return response


async def testmultiplefunction_2():
    client= ollama.AsyncClient()
    prompt = "what is three subtract one?"
    available_functions = {
        "add_two_numbers":add_two_numbers,
        "subtract_two_numbers":subtract_two_numbers
    }
    response: ChatResponse = await client.chat(
        'llama3.2',
        messages=[
            {
                'role': 'user',
                'content':prompt
            }
        ],
        tools=[add_two_numbers_tool, subtract_two_numbers_tool]
    )
    decodefunction_response(response,available_functions)
    return response


if __name__ == "__main__":
    try:
        response = asyncio.run(testmultiplefunction())
        pprint.pprint(response)
    except:
        pprint.pprint("Error")
    pprint.pprint("***************************************")
    try:
        response = asyncio.run(testmultiplefunction_2())
        pprint.pprint(response)
    except:
        pprint.pprint("Error")
函数输出和模型响应。

注意:从响应 json 中,我们注意到内容为空 😓 字符串,而函数调用的结果被解码以显示正确的结果。猜测这个问题将在未来的版本中添加。

定义函数时,使用显式类型语句🤖 很重要。例如,使用 int(a) + int(b) 而不是 a + b 来确保正确的整数加法。这可以避免来自 add_two_numbers(3, 1) 的“31”等意外结果,它会连接字符串而不是添加整数。

输出错误
...

def add_two_numbers(a:int, b:int)->int:
    return int(a)+int(b)

# NOT 
def add_two_numbers(a:int, b:int)->int:
    return a+b

...

接下来,我们将测试一个函数调用的多个参数:

import ollama
import pprint
import json
import base64
import requests
import yfinance as yfintech
from typing import Dict, Any, Callable

def decodefunction_response(response,available_functions):
    if response.message.tool_calls:
        for tool in response.message.tool_calls:
            if function_to_call := available_functions.get(tool.function.name):
                pprint.pprint(f"Calling function: {tool.function.name}")
                pprint.pprint(f"Arguments: {tool.function.arguments}")
                pprint.pprint(f"Function out: {function_to_call(**tool.function.arguments)}")
            else:
                pprint.pprint(f"Function {tool.function.name}, NOT FOUND ERROR.")

def get_stock_price(symbol:str) -> float:
    ticker = yfintech.Ticker(symbol)
    pprint.pprint(f"symbol===={symbol}")
    return ticker.info.get('regularMarketPrice') or ticker.fast_info.last_price


def testonefunctioncall_getstockprice(prompt):
    pprint.pprint(f"*************prompt: {prompt}**************************")
    available_functions: Dict[str,Callable]={
        'get_stock_price':get_stock_price
    }
    try:
        response = ollama.chat(
            'llama3.2',
            messages=[
                {
                    'role': 'user',
                    'content':prompt
                }
            ],
            tools=[get_stock_price]
        )
        decodefunction_response(response,available_functions)
        pprint.pprint(response)
        return response
    except:
        pprint.pprint("Error")



if __name__ == "__main__":
    prompt="What are the current stock price of Microsoft, APPLE and Nvidia?"
    testonefunctioncall_getstockprice(prompt)

这里我们有一个函数 get_stock_price,但使用 prompt 时只允许一个参数:

prompt="What are the current stock price of Microsoft, APPLE and Nvidia?"

从上面的示例中,新功能和改进增强了库的功能和可用性。但是,仍有潜在的改进,例如 json 响应有结果、处理函数类型。prompt 也很重要。


原文链接:Ollama’s 100th release: functions can now be provided as tools

汇智网翻译整理,转载请标明出处