大模型的FunctionCalling到MCP

文章目录[x]
  1. 1:OpenAI的对话接口
  2. 1.1: 1. 系统角色(system)
  3. 1.2:2. 用户角色(user)
  4. 1.3:3. 助手角色(assistant)
  5. 1.4:4.工具(tool)
  6. 2: 消息记录的形式
  7. 3:使用FunctionCalling的步骤
  8. 4:MCP
  9. 4.1:简介
  10. 4.2:架构
  11. 4.3:流程

OpenAI的对话接口

 

role的种类:system(o1之后的模型使用developer)、user、assistant、tool

 1. 系统角色(system)

定位:控制对话的全局方向和模型的行为模式。
作用:
设定上下文:定义对话的背景、目标或规则。例如,可以设定“你是一个专业翻译,只回答与翻译相关的问题”。

 

2. 用户角色(user)

定位:代表真实用户输入,是驱动对话的核心。
作用:
- 提出问题或请求,如“帮我写一首关于夏天的诗”。
- 提供补充信息,如“上一句翻译成法语”。
- 修正模型行为,如“用更简单的语言解释”。

 

技术细节:
在API调用中,user消息是必选的上下文组成部分。
模型会优先关注最近的user消息内容。

 

3. 助手角色(assistant)

定位:模型生成的回复内容。
作用:
回答问题:基于上下文生成符合用户需求的回复。

 

4.工具(tool)

定位:代表工具执行结果。
作用:把工具执行结果返回大模型。
技术细节:不能单独出现,必须跟随在带有tool_calls的assistant消息后,并且数量需要跟tool_calls的数量一致。

 

 消息记录的形式

 

1. 常规对话
"messages": [
      {
        "role": "system",
        "content": "You are a helpful assistant."
      },
      {
        "role": "user",
        "content": "Hello!"
      },
      {
        "role": "assistant",
        "content": "Hello! How can I assist you today?"
      }
    ]
2. 带有Function calling
{
  "messages": [
    {
      "role": "system",
      "content": "You are a helpful assistant."
    },
    {
      "role": "user",
      "content": "What is the weather like in Boston today?",
      "tools": [
        {
          "type": "function",
          "function": {
            "name": "get_current_weather",
            "description": "Get the current weather in a given location",
            "parameters": {
              "type": "object",
              "properties": {
                "location": {
                  "type": "string",
                  "description": "The city and state, e.g. San Francisco, CA"
                },
                "unit": {
                  "type": "string",
                  "enum": [
                    "celsius",
                    "fahrenheit"
                  ]
                }
              },
              "required": [
                "location"
              ]
            }
          }
        }
      ],
      "tool_choice": "auto"
    },
    {
      "role": "assistant",
      "content": null,
      "tool_calls": [
        {
          "id": "call_abc123",
          "type": "function",
          "function": {
            "name": "get_current_weather",
            "arguments": "{\n\"location\": \"Boston, MA\"\n}"
          }
        }
      ]
    },
    {
      "role": "tool",
      "tool_call_id": "call_abc123",
      "content": "18°C"
    },
    {
      "role": "assistant",
      "content": "It's about 18°C in Boston."
    }
  ]
}

使用FunctionCalling的步骤

1. 定义函数,并告诉模型有哪些可用的函数
2. 模型决定调用哪个函数,以及函数的参数
3. 执行函数,并把结果返回给模型
4. 模型给出最终的回答

MCP

简介

Model Context Protocol (MCP) 是一个开放协议,它使 LLM 应用与外部数据源和工具之间的无缝集成成为可能。
无论你是构建 AI 驱动的 IDE、改善 chat 交互,还是构建自定义的 AI 工作流,MCP 提供了一种标准化的方式,将 LLM 与它们所需的上下文连接起来。
MCP 是一个协议,它可以在 host 应用(如 Claude Desktop)和本地服务之间建立安全连接。

架构

- MCP Hosts: 像 Claude Desktop、IDEs 或 AI 工具这样的程序,它们希望通过 MCP 访问资源
- MCP Servers: 轻量级程序,通过标准化的 Model Context Protocol 暴露特定功能
- Local Resources: 你的计算机资源(数据库、文件、服务),MCP 服务器可以安全地访问这些资源
- Remote Resources: 通过互联网可用的资源(例如,通过 APIs),MCP 服务器可以连接到这些资源

流程

1. 服务器发现:Claude Desktop 在启动时连接到你配置的 MCP 服务器
2. 协议握手:当你询问数据时,Claude Desktop:
  - 确定哪个 MCP 服务器可以提供帮助(在本例中是 sqlite)
  - 通过协议协商能力
  - 从 MCP 服务器请求数据或操作
3. 流程图(以Claude Desktop作为客户端为例)
点赞

发表回复

昵称和uid可以选填一个,填邮箱必填(留言回复后将会发邮件给你)
tips:输入uid可以快速获得你的昵称和头像(已失效)