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/

posted on   荣锋亮  阅读(56)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 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 模块

导航

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示