llm-axe ollama 函数调用试用
以前简单介绍了llm-axe是支持函数调用的,以下是对于ollama 函数调用的简单试用
参考使用
- me.py
from llm_axe.agents import FunctionCaller
from llm_axe.models import OllamaChat
import time
def get_time():
return time.strftime("%I:%M %p")
def get_date():
return time.strftime("%Y-%m-%d")
def get_location():
return "Beijing, China"
def add(num1:int, num2:int):
return num1 + num2
def multiply(num1:int, num2:int):
return num1 * num2
def get_distance(lat1:int, lon1:int, lat2:int, lon2:int):
"""
Calculates the distance between two points on the Earth's surface using the Haversine formula.
:param lat1: latitude of point 1
:param lon1: longitude of point 1
:param lat2: latitude of point 2
:param lon2: longitude of point 2
"""
return(lat1, lon1, lat2, lon2)
# 会调用get_date 函数
prompt = "今天是几号"
llm = OllamaChat(model="qwen2:7b",host="http://localhost:11434")
fc = FunctionCaller(llm, [get_time, get_date, get_location,get_distance, add, multiply])
result = fc.get_function(prompt)
# If no function was found, exit
if(result is None):
print("No function found")
exit()
func = result['function']
params = result['parameters']
print(func(**params))
print(result['parameters'])
print(result['prompts'])
print(result['raw_response'])
- 效果
内部处理
实际上目前对于开源ollama 的函数调用还是提示词以及json 结构化输出,然后再进行函数的执行,还有一种基于instructor 的结构化输出,然后在进行函数执行的方法
参考资料
https://github.com/emirsahin1/llm-axe
https://python.useinstructor.com/
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 上周热点回顾(2.17-2.23)
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· spring官宣接入deepseek,真的太香了~
2023-08-21 spark on k8s 开发部署简单实践
2022-08-21 nginx ngx_http_degradation_module 模块
2022-08-21 基于iap 的安全控制
2022-08-21 openlitespeed LiteSpeed 的开源版
2022-08-21 一些不错的web 性能测试工具
2021-08-21 easy-rules-centraldogma-spring-boot-starter 使用说明
2021-08-21 wagon maven 通用的artifact 以及repository 模块