首页 > 其他分享 >ollama 0.2.7 支持函数调用了

ollama 0.2.7 支持函数调用了

时间:2024-07-19 18:07:31浏览次数:9  
标签:info function end get 0.2 函数调用 time ollama

就在最新的ollama 发布版本中对于类似openai 的函数调用支持了,但是目前有一些问题,就是相关历史的model 都需要修改下(添加TEMPLATE 对于tools的支持),一些是一个简单的测试

参考示例

使用了phidata 这个工具包

  • 一个参考qwen2:7b 模型的修改
    参考了llama3-groq-tool-use 这个模型的
    Moddelfile
FROM qwen2:7b
 
TEMPLATE """{{- if .Messages }}
{{- if or .System .Tools }}<|start_header_id|>system<|end_header_id|>
 
{{ if .System }}{{ .System }}
{{- end }}
In addition to plain text responses, you can chose to call one or more of the provided functions.
 
Use the following rule to decide when to call a function:
  * if the response can be generated from your internal knowledge (e.g., as in the case of queries like "What is the capital of Poland?"), do so
  * if you need external information that can be obtained by calling one or more of the provided functions, generate a function calls
 
If you decide to call functions:
  * prefix function calls with functools marker (no closing marker required)
  * all function calls should be generated in a single JSON list formatted as functools[{"name": [function name], "arguments": [function arguments as JSON]}, ...]
  * follow the provided JSON schema. Do not hallucinate arguments or values. Do to blindly copy values from the provided samples
  * respect the argument type formatting. E.g., if the type if number and format is float, write value 7 as 7.0
  * make sure you pick the right functions that match the user intent
 
Available functions as JSON spec:
{{- if .Tools }}
{{ .Tools }}
{{- end }}<|eot_id|>
{{- end }}
{{- range .Messages }}
{{- if ne .Role "system" }}<|start_header_id|>{{ .Role }}<|end_header_id|>
{{- if and .Content (eq .Role "tools") }}
 
{"result": {{ .Content }}}
{{- else if .Content }}
 
{{ .Content }}
{{- else if .ToolCalls }}
 
functools[
{{- range .ToolCalls }}{{ "{" }}"name": "{{ .Function.Name }}", "arguments": {{ .Function.Arguments }}{{ "}" }}
{{- end }}]
{{- end }}<|eot_id|>
{{- end }}
{{- end }}<|start_header_id|>assistant<|end_header_id|>
 
{{ else }}
{{- if .System }}<|start_header_id|>system<|end_header_id|>
 
{{ .System }}<|eot_id|>{{ end }}{{ if .Prompt }}<|start_header_id|>user<|end_header_id|>
 
{{ .Prompt }}<|eot_id|>{{ end }}<|start_header_id|>assistant<|end_header_id|>
 
{{ end }}{{ .Response }}{{ if .Response }}<|eot_id|>{{ end }}
"""

构建命令

ollama create myqwen2:7b -f Modelfile
  • 参考代码
from phi.llm.openai.like import OpenAILike
from phi.assistant import Assistant
import datetime
import json
import time
my_ollama = OpenAILike(
        model="myqwen2:7b",
        api_key="demo",
        base_url="http://172.16.1.205:11434/v1"
    )
 
def get_time():
    info = {
        "time": time.strftime("%I:%M %p")
    }
    return json.dumps(info)
 
def get_username():
    info = {
        "username": "John Doe"
    }
    return json.dumps(info)
def get_date():
    info = {
        "date": time.strftime("%Y-%m-%d")
    }
    return json.dumps(info)
 
def get_datatime():
    now = datetime.datetime.now()
    info = {
        "time": now.strftime("%Y-%m-%d %H:%M:%S")
    }
    return json.dumps(info)
 
ollama_assistant = Assistant(
    llm=my_ollama,
    tools=[get_time,get_date], show_tool_calls=True, markdown=False
)
ollama_assistant.print_response("现在的时间", stream=False)
  • 效果

说明

ollama 不少模型还是需要修改才能支持函数调用的,不然会有一些问题,对于支持函数调用还是一个很不错的功能的,如果不使用内置的,基于其他工具的也是一些不错的选择(instructor 值得使用)

参考资料

server/model_test.go
https://github.com/ollama/ollama/blob/main/docs/modelfile.md#template
https://ollama.com/library/qwen2/blobs/62fbfd9ed093
https://ollama.com/library/llama3-groq-tool-use
https://python.useinstructor.com/

标签:info,function,end,get,0.2,函数调用,time,ollama
From: https://www.cnblogs.com/rongfengliang/p/18312069

相关文章

  • Spring AI(使用Ollama大模型进行AI验证)
    接上篇:SpringAI(大模型工具Ollama介绍)1、Ollama大模型代码验证pom.xml引入jar<!--spring-ai-ollama的starter依赖,启动依赖--><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-ollama-......
  • Spring AI(大模型工具Ollama介绍)
    接上篇:SpringAI(多模态) 1、大模型工具Ollama介绍官网:https://ollama.com/2、大模型工具Ollama下载官网下载地址:https://ollama.com/download3、大模型工具Ollama安装4、大模型工具Ollama运行5、大模型工具Ollama默认端口netstat-ano|findstr11434 ......
  • Beyond Compare v5.0.0.29773 破解版
    BeyondCompare是一款文件及文件夹(目录)的对比工具。BeyondCompare不仅可以快速比较出两个目录的不同,还可以比较每个文件的内容,而且可以任意显示比较结果。BeyondCompare程序内建了文件浏览器,方便您对文件、文件夹、压缩包、FTP网站之间的差异比对以及资料同步。软件截图:使用......
  • Ubuntu+Docker+Ollama+WebUI
    Ubuntu备份源文件sudocp/etc/apt/sources.list/etc/apt/sources.list.backup编辑/etc/apt/sources.list#默认注释了源码镜像以提高aptupdate速度,如有需要可自行取消注释debhttps://mirrors.tuna.tsinghua.edu.cn/ubuntu/focalmainrestricteduniversemultivers......
  • 华为高斯数据库openGauss_5.0.2 企业版部署学习
    系统环境欧拉系统官方下载链接openEuler-22.03-LTS-SP4-x86_64-dvd.iso https://mirrors.tuna.tsinghua.edu.cn/openeuler/openEuler-22.03-LTS-SP4/ISO/x86_64/openEuler-22.03-LTS-SP4-x86_64-dvd.iso openEuler下载|openEulerISO镜像|openEuler社区官网 数据库安......
  • ollama 模型国内加速下载,制作自定义Modelfile模型文件
    参考:https://www.zhihu.com/question/640579563/answer/3562899008https://github.com/ollama/ollama/blob/main/docs/modelfile.mdgguf格式介绍:https://www.datalearner.com/blog/10517057188355861、ollama模型国内加速下载ollama主要的模型文件格式是gguf,可以在mo......
  • [Windows] 无需PS基础也香 Inpaint v10.2高级便携版
    描述对于经常在互联网上进行操作的学生,白领等!一款好用的软件总是能得心应手,事半功倍。今天给大家带了一款高科技软件Inpaintv10.2高级便携版无需额外付费,永久免费!亲测可运行!!内容目前主要的内容以资源破解,对于学习破解资源有比较大的帮助!但是网络上面错综复杂,很多......
  • MoneyPrinterPlus全面支持本地Ollama大模型
    MoneyPrinterPlus现在支持批量混剪,一键AI生成视频,一键批量发布短视频这些功能了。之前支持的大模型是常用的云厂商,比如OpenAI,Azure,Kimi,Qianfan,Baichuan,TongyiQwen,DeepSeek这些。支持云厂商的原因是现在大模型使用基本都很便宜,并且大厂的稳定性,性能都比本地搭建要好很多......
  • 函数调用栈
    1、程序源代码:[xuanmiao@localhostPractice]$catmain.c#include<stdio.h>intplus(inta,intb){intc=a+b;returnc;}intmain(){inta=1,b=2;intc=0;c=plus(a,b);return0;}2、编译[xuanmiao@localhostPractice......
  • 【LangChain v0.2:向稳定性迈进的一大步】
    文末有福利!LangChainv0.2:向稳定性迈进的一大步❝本文经翻译并二次整理自LangChainv0.2:ALeapTowardsStability一文。LangChain官方正式对外宣布了LangChainv0.2的预发布版本,这个版本在提升LangChain的稳定性和安全性方面迈出了重要一步。langchainv0.2的预发......