当前位置: 首页 > article >正文

硅基流动前端如何设置tool工具

虽然python后台可以设置agent并调用工具,但是后台和前端交互速度不如直接在前端JavaScript调用快,在内网调用时确实可以改善使用体验。
下面以硅基流动的API为例子,让AI调用本地tools工具。

  const options = {
    method: 'POST',
    headers: {
      Authorization: 'Bearer sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      tools: [
        {
          type: "function",
          function: {
            name: "multiplication",
            description: "Calculate the multiplication of two numbers",
            parameters: {
              number1:"数字1",
              number2:"数字2"
            }
          },
          strict: false
        },
        {
          type: "function",
          function: {
            name: "weather",
            description: "获取天气预报数据",
          },
          strict: false
        }
      ],
      model: "deepseek-ai/DeepSeek-V2.5",
      stream: false,
      messages: [
        {
          role: "user",
          content: "今天天气怎么样"
        }
      ]
    })
  };
  
  fetch('https://api.siliconflow.cn/v1/chat/completions', options)
    .then(response => response.json())
    .then(response => console.log(response))
    .catch(err => console.error(err));
  

创建了一个数字相乘和获取天气的接口
当用户问道数字相乘时会返回

[
    {
        "index": 0,
        "message": {
            "role": "assistant",
            "content": "",
            "tool_calls": [
                {
                    "id": "01955b43e609018a7ff2b475594c4fe5",
                    "type": "function",
                    "function": {
                        "name": "multiplication",
                        "arguments": "{\"number1\":256,\"number2\":789}"
                    }
                }
            ]
        },
        "finish_reason": "tool_calls"
    }
]

根据arguments参数和function_name:multiplication可以调用计算相关的function
当用户问道天气相关的数据时会返回

[
    {
        "index": 0,
        "message": {
            "role": "assistant",
            "content": "",
            "tool_calls": [
                {
                    "id": "01955b44a36f329401e0578374922cb1",
                    "type": "function",
                    "function": {
                        "name": "weather",
                        "arguments": "{}"
                    }
                }
            ]
        },
        "finish_reason": "tool_calls"
    }
]

此时调用weather方法,在方法内调用天气查询的api即可。


http://www.kler.cn/a/570478.html

相关文章:

  • LINUX网络基础 - 网络编程套接字,UDP与TCP
  • 27、Java 反射机制
  • 数据结构(初阶)(五)----栈
  • 如何使用Python和SQLAlchemy结合外键映射来获取其他表中的数据
  • 迷你世界脚本组队接口:Team
  • C语言:51单片机 程序设计基础
  • 大模型学习笔记------LLM模型开发流程
  • Django与数据库
  • Docker 模拟 kubernetes 的 pod
  • STM32引脚类型
  • Linux IO编程核心要点解析:文件、流与缓冲区实战指南
  • Python配置文件的全面解析与使用
  • WEB10(CTFshow)
  • 算法题笔记(自用)——Python
  • 编程题 - 汽水瓶【JavaScript/Node.js解法】
  • 【含文档+PPT+源码】基于SpringBoot和Vue的编程学习系统
  • CentOS 7 IP 地址设置保姆级教程
  • 动态扩缩容引发的JVM堆内存震荡:从原理到实践的GC调优指南
  • Spark核心之02:RDD、算子分类、常用算子
  • cursor for linux