网站首页 > 技术教程 正文
作为一名 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函数即工具 - 汇智网
猜你喜欢
- 2024-12-13 徐曦|他们都是艺术家:在牛津看卡夫卡、钱德勒和勒卡雷
- 2024-12-13 推荐收藏:20个ChatGPT使用场景和提问案例
- 2024-12-13 AI大模型实战篇:AI Agent设计模式-REWOO
- 2024-12-13 AI又出圈:ChatGPT火爆的背后
- 2024-12-13 微信发句号就是“我生气了”?语言学家这么说……
- 2024-12-13 肝了一周,我终于有自己的ChatGPT站点了!
- 2024-12-13 “亲密无间?揭露闺蜜间的聊天记录真相”
- 2024-12-13 绝对纯净的 WebSocket 聊天插件,你值得拥有
- 2024-12-13 一句深情走心的情话,情不知所起,一往而深
- 2024-12-13 2024年11月20日,AI狂飙:模型升级,功能革新
欢迎 你 发表评论:
- 10-23Excel计算工龄和年份之差_excel算工龄的公式year
- 10-23Excel YEARFRAC函数:时间的"年份比例尺"详解
- 10-23最常用的10个Excel函数,中文解读,动图演示,易学易用
- 10-23EXCEL中如何计算截止到今日(两个时间中)的时间
- 10-2390%人不知道的Excel神技:DATEDIF 精准计算年龄,告别手动算错!
- 10-23计算工龄及工龄工资(90%的人搞错了):DATE、DATEDIF组合应用
- 10-23Excel中如何计算工作日天数?用这两个函数轻松计算,附新年日历
- 10-23怎样快速提取单元格中的出生日期?用「Ctrl+E」批量搞定
- 最近发表
-
- Excel计算工龄和年份之差_excel算工龄的公式year
- Excel YEARFRAC函数:时间的"年份比例尺"详解
- 最常用的10个Excel函数,中文解读,动图演示,易学易用
- EXCEL中如何计算截止到今日(两个时间中)的时间
- 90%人不知道的Excel神技:DATEDIF 精准计算年龄,告别手动算错!
- 计算工龄及工龄工资(90%的人搞错了):DATE、DATEDIF组合应用
- Excel中如何计算工作日天数?用这两个函数轻松计算,附新年日历
- 怎样快速提取单元格中的出生日期?用「Ctrl+E」批量搞定
- Excel日期函数之DATEDIF函数_excel函数datedif在哪里
- Excel函数-DATEDIF求司龄_exceldatedif函数计算年龄
- 标签列表
-
- 下划线是什么 (87)
- 精美网站 (58)
- qq登录界面 (90)
- nginx 命令 (82)
- nginx .http (73)
- nginx lua (70)
- nginx 重定向 (68)
- Nginx超时 (65)
- nginx 监控 (57)
- odbc (59)
- rar密码破解工具 (62)
- annotation (71)
- 红黑树 (57)
- 智力题 (62)
- php空间申请 (61)
- 按键精灵 注册码 (69)
- 软件测试报告 (59)
- ntcreatefile (64)
- 闪动文字 (56)
- guid (66)
- abap (63)
- mpeg 2 (65)
- column (63)
- dreamweaver教程 (57)
- excel行列转换 (56)

本文暂时没有评论,来添加一个吧(●'◡'●)