链载Ai

标题: 使用Ollama和Phi4mini在边缘设备上构建AI Agent [打印本页]

作者: 链载Ai    时间: 3 小时前
标题: 使用Ollama和Phi4mini在边缘设备上构建AI Agent

最新发布的Phi-4-mini和Phi-4-multimodal现已支持函数调用功能。该功能使模型能够连接外部工具和API。通过在边缘设备上部署具有函数调用能力的Phi-4-mini和Phi-4-multimodal,我们可以实现本地知识能力的扩展,并提高其任务执行效率。本文将重点介绍如何利用Phi-4-mini的函数调用能力在边缘设备上构建高效的AI代理。




什么是函数调用

工作原理

首先我们需要了解函数调用的工作原理:


应用场景


Phi-4-mini / Phi-4-multimodal的函数调用

Phi-4-mini / Phi-4-multimodal支持单函数和并行函数调用。调用时需要注意:


单函数调用

tools=[{"name":"get_match_result","description":"获取比赛结果","parameters":{"match":{"description":"比赛名称","type":"str","default":"阿森纳vs曼城"}}},]messages=[{"role":"system","content":"你是一个有用的助手","tools":json.dumps(tools),#使用tools参数将工具传递到系统消息中},{"role":"user","content":"今天阿森纳对曼城的比赛结果是什么?"}]


并行函数调用

AGENT_TOOLS = {"booking_fight": { "name":"booking_fight", "description":"预订航班", "parameters": {  "departure": {   "description":"出发机场代码",   "type":"str",   },  "destination": {   "description":"目的地机场代码",   "type":"str",   },  "outbound_date": {   "description":"出发日期",   "type":"str",   },  "return_date": {   "description":"返回日期",   "type":"str",   }  } },"booking_hotel": { "name":"booking_hotel", "description":"预订酒店", "parameters": {  "query": {   "description":"城市名称",   "type":"str",   },  "check_in_date": {   "description":"入住日期",   "type":"str",   },  "check_out_date": {   "description":"退房日期",   "type":"str",   }  } },}
SYSTEM_PROMPT ="""你是我的旅行助手,可以使用以下工具。"""
messages = [ { "role":"system", "content": SYSTEM_PROMPT, "tools": json.dumps(AGENT_TOOLS),# 使用tools参数将工具传递到系统消息中 }, { "role":"user", "content":"""我将于2025年3月21日至2025年3月27日从伦敦到纽约出差,你能帮我预订酒店和机票吗""" }]


使用Ollama和Phi-4-mini函数调用在边缘设备上创建AI代理

Ollama是一个流行的免费工具,用于本地部署LLM/SLM,可与VS Code的AI工具包结合使用。除了可以部署在PC/笔记本电脑上,还可以部署在IoT、手机、容器等设备上。要在Ollama上使用Phi-4-mini,需要使用Ollama 0.5.13+。Ollama支持不同的量化版本,如下图所示:


使用Ollama,我们可以在边缘部署Phi-4-mini,并在有限的计算能力下实现具有函数调用的AI代理,使生成式AI在边缘设备上得到更有效的应用。


当前问题

一个令人遗憾的体验 - 如果直接使用上述方式尝试调用Ollama,你会发现函数调用不会被触发。在Ollama的GitHub Issue上有相关讨论。你可以进入Issue https://github.com/ollama/ollama/issues/9437。通过修改ModelFile中的Phi-4-mini模板来实现单函数调用,但并行函数调用的调用仍然失败。


解决方案

我们通过对模板进行调整实现了修复。我们根据Phi-4-mini的Chat Template进行了改进,并重新修改了Modelfile。当然,量化模型对结果有很大影响。调整如下:

TEMPLATE"""{{- if .Messages }}{{- if or .System .Tools }}<|system|>
{{ 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 }}<|end|>{{- end }}{{- range .Messages }}{{- if ne .Role "system" }}<|{{ .Role }}|>{{- 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 }}<|end|>{{- end }}{{- end }}<|assistant|>
{{ else }}{{- if .System }}<|system|>
{{ .System }}<|end|>{{ end }}{{ if .Prompt }}<|user|>
{{ .Prompt }}<|end|>{{ end }}<|assistant|>
{{ end }}{{ .Response }}{{ if .Response }}<|user|>{{ end }}"""


我们使用不同的量化模型测试了该解决方案。在笔记本电脑环境中,我们建议使用以下模型来启用单/并行函数调用:phi4-mini:3.8b-fp16。注意:你需要将定义的Modelfile与phi4-mini:3.8b-fp16绑定在一起才能使其工作。

请在命令行中执行以下命令:

# 如果还没有下载,请先执行此命令ollamarun phi4-mini:3.8b-fp16
# 与调整后的Modelfile绑定ollamacreate phi4-mini:3.8b-fp16 -f {你的Modelfile路径}


测试Phi-4-mini的单函数调用和并行函数调用。

单函数调用


并行函数调用


以上示例只是一个简单的介绍。随着开发的推进,我们希望找到更简单的方法在边缘设备上应用它,使用函数调用扩展Phi-4-mini / Phi-4-multimodal的应用场景,并在垂直行业中开发更多用例。







欢迎光临 链载Ai (https://www.lianzai.com/) Powered by Discuz! X3.5