【AI-Ollama进阶】函数调用(Function Calling)



写在前面 / 链接存档

**函数调用(Function Calling)**是一种强大的特性

它允许大型语言模型(LLMs)通过调用外部函数来扩展其功能和能力

这项技术可以使得语言模型能够访问和处理实时数据,或者执行一些需要特定工具才能完成的任务

函数调用的原理

  • 检测需求

    LLMs 被训练来识别何时需要进行函数调用,

    通常是基于用户的输入提示

  • 结构化请求

    当检测到需要函数调用时,

    模型会生成一个结构化的调用请求,

    而不是传统的文本回复

  • 交互外部系统

    通过函数调用,

    LLMs 能够与外部系统和APIs进行交互,

    获取或发送数据

Function Calling 原理

什么是函数调用

  • 函数调用 是 OpenAI 的 GPT-4-0613GPT-3.5 Turbo-0613 模型中的一项新功能

  • 这些 AI 模型经过训练,可以根据用户的提示检测函数调用的需求,并以结构化的调用请求(而不是常规文本)进行响应

  • 函数调用允许大语言模型与其他系统交互,从而使 LLMs 能够回答它们原本无法回答的问题

    例如需要实时信息或训练集中未包含的数据的问题

  • 换句话说:函数调用提供了另一种方法来教 AI 模型如何与外部世界交互

LLMs函数调用流程

LLMs函数调用流程

Function Calling 模型

  • Ollama 现在支持与 Llama 3.1 等热门模型进行工具调用

  • 使模型能够使用它所了解的工具来回答给定的提示,从而使模型能够执行更复杂的任务或与外界互动

  • 示例工具包括:

    • 函数和 API
    • 网页浏览
    • 代码解释器
    • 多得多!

支持的型号

  • 可以在模型页面的工具类别下找到受支持的模型列表

注意

请运行命令检查您是否拥有最新型号ollama pull <model>

支持的型号

Qwen2

Qwen2 是阿里巴巴集团推出的全新系列大型语言模型
Qwen2基于29种语言的数据进行训练,包括英语和中文

  • 文档地址:https://ollama.com/library/qwen2
  • 它有 4 种参数大小:0.5B、1.5B、7B、72B
  • 在 7B 和 72B 模型中,上下文长度已扩展至128k 个 token

Qwen2不同参数模型的属性

  • 注意: 安装指令ollama run qwen2
    • 将下载一个 4.4GB 的 docker 镜像

GLM-4

GLM-4-9B 是智谱 AI 推出的最新一代预训练模型 GLM-4 系列中的开源版本

在语义、数学、推理、代码和知识等多方面的数据集测评中,GLM-4-9B 及其人类偏好对齐的版本 GLM-4-9B-Chat 均表现出超越 Llama-3-8B 的卓越性能

除了能进行多轮对话,GLM-4-9B-Chat 还具备网页浏览、代码执行、自定义工具调用(Function Call)和长文本推理(支持最大 128K 上下文)等高级功能

本代模型增加了多语言支持,支持包括日语,韩语,德语在内的 26 种语言

我们还推出了支持 1M 上下文长度(约 200 万中文字符)的 GLM-4-9B-Chat-1M 模型和基于 GLM-4-9B 的多模态模型 GLM-4V-9B

GLM-4V-9B 具备 1120 * 1120 高分辨率下的中英双语多轮对话能力,在中英文综合能力、感知推理、文字识别、图表理解等多方面多模态评测中,GLM-4V-9B 表现出超越 GPT-4-turbo-2024-04-09、Gemini1.0 Pro、Qwen-VL-Max 和 Claude 3 Opus 的卓越性能。

1
ollama run glm4

Mistral

Mistral 是一个 7B 参数模型,使用 Apache 许可证发布

它可用于指令(指令跟踪)和文本完成

  • Mistral AI 团队指出,Mistral 7B

    • 在所有基准测试中均优于 Llama 2 13B
    • 在许多基准测试中均优于 Llama 1 34B
    • 代码性能接近 CodeLlama 7B,同时仍能很好地完成英语任务
  • 文档地址:https://ollama.com/library/mistral

1
ollama run mistral

版本

Mistral版本

Function Calling

Mistral 0.3 支持使用 Ollama 的原始模式进行函数调用

  • 示例Raw Prompt
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
[AVAILABLE_TOOLS] [{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
},
"format": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "The temperature unit to use. Infer this from the users location."
}
}, # end of properties
"required": ["location", "format"]
} # end of parameters
} # end of function
}]
[/AVAILABLE_TOOLS]

[INST]
What is the weather like today in San Francisco
[/INST]
  • 示例Response
1
2
3
4
5
6
7
8
9
10
11
[TOOL_CALLS] 
[
{
"name": "get_current_weather",
"arguments": {
"location": "San Francisco, CA",
"format": "celsius"
}
}
]

Mixtral

Mixtral 大型语言模型 (LLM) 是一组预先训练的生成稀疏混合专家

  • Mixtral模型有两种参数大小:

    • mixtral:8x22b
    • mixtral:8x7b
  • 混合式8x22b

1
ollama run mixtral:8x22b

Mixtral 8x22B 为 AI 社区树立了新的性能和效率标准

它是一种稀疏混合专家 (SMoE) 模型,仅使用 141B 中的 39B 个活动参数,

就其规模而言,具有无与伦比的成本效益

  • Mixtral 8x22B 具有以下优势:
    • 精通英语、法语、意大利语、德语和西班牙语
    • 它具有强大的数学和编码能力
    • 它本身具有功能函数调用
    • 64K 标记上下文窗口允许从大型文档中精确地调用信息

工具调用

tools要启用工具调用,

请通过Ollama API 中的字段提供可用工具列表

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
29
30
31
import ollama

response = ollama.chat(
model='llama3.1',
messages=[{
'role': 'user',
'content': 'What is the weather in Toronto?'
}],

# provide a weather checking tool to the model
tools=[{
'type': 'function',
'function': {
'name': 'get_current_weather',
'description': 'Get the current weather for a city',
'parameters': {
'type': 'object',
'properties': {
'city': {
'type': 'string',
'description': 'The name of the city',
},
},
'required': ['city'],
},
},
},
],
)

print(response['message']['tool_calls'])
  • 支持的模型现在将回复tool_calls
  • 工具响应可以通过角色消息提供tool
  • 有关更多信息,请参阅API 文档
  • Copyrights © 2024-2025 brocademaple
  • 访问人数: | 浏览次数:

      请我喝杯咖啡吧~

      支付宝
      微信