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

【python openai function2json小工具】

两种方法 一种openai-swarm中提供的、一种langchain实现的

一、openai工具函数调用

import inspectdef merge_chunk(final_response: dict, delta: dict) -> None:delta.pop("role", None)merge_fields(final_response, delta)tool_calls = delta.get("tool_calls")if tool_calls and len(tool_calls) > 0:index = tool_calls[0].pop("index")merge_fields(final_response["tool_calls"][index], tool_calls[0])def function_to_json(func) -> dict:"""Converts a Python function into a JSON-serializable dictionarythat describes the function's signature, including its name,description, and parameters.Args:func: The function to be converted.Returns:A dictionary representing the function's signature in JSON format."""type_map = {str: "string",int: "integer",float: "number",bool: "boolean",list: "array",dict: "object",type(None): "null",}try:signature = inspect.signature(func)except ValueError as e:raise ValueError(f"Failed to get signature for function {func.__name__}: {str(e)}")parameters = {}for param in signature.parameters.values():try:param_type = type_map.get(param.annotation, "string")except KeyError as e:raise KeyError(f"Unknown type annotation {param.annotation} for parameter {param.name}: {str(e)}")parameters[param.name] = {"type": param_type}required = [param.namefor param in signature.parameters.values()if param.default == inspect._empty]return {"type": "function","function": {"name": func.__name__,"description": func.__doc__ or "","parameters": {"type": "object","properties": parameters,"required": required,},},}if __name__=='__main__':print(function_to_json(merge_chunk))

二、使用langchain

from langchain_core.utils.function_calling import (convert_to_openai_function,convert_to_openai_tool,
)
from langchain_core.tools.convert import tool
from typing import Annotated@tool
def slide_ops(idx: Annotated[int, "元素index"],left: Annotated[int, "元素的左上横坐标"] = None,top: Annotated[int, "元素的左上纵坐标"] = None,width: Annotated[int, "元素的宽度"] = None,height: Annotated[int, "元素的高度"] = None,fontcolor: Annotated[str, "元素的前景颜色"] = None,backcolor: Annotated[int, "元素的背景颜色"] = None,delete: Annotated[bool, "删除此元素"] = None,
):"""修改幻灯片中元素的信息"""...convert_to_openai_function(slide_ops)
convert_to_openai_tool(slide_ops)


http://www.mrgr.cn/news/55890.html

相关文章:

  • MySQL8.0.28解压版安装windows
  • 深度优先搜索(DFS)和广度优先搜索(BFS)
  • Rust小练习,编写井字棋
  • 【详解】下载MySql安装教程(帮助数据库下载)
  • 深度学习 简易环境安装(不含Anaconda)
  • 浏览器实时更新esp32-c3 Supermini http server 数据
  • Vuejs设计与实现 —— 实现响应式系统
  • 【MATLAB源码-第186期】matlab基于MLE算法的8天线阵列DOA估计仿真,对比粗估计、精确估计输出RMSE对比图。
  • DeepSpeed:所有人都能用的超大规模模型训练工具
  • 开源神器!CodeFormer:一键去除马赛克,高清修复照片视频
  • 将VSCode界面的显示语言改为简体中文,切换VScode界面的显示语言
  • 智能码二维码ZHINENGMA.CN在设备巡检中的使用效果如何?
  • 前端性能优化之卡顿篇
  • 数据的全量加载和增量加载
  • pdman在关系图中展示表的后排字段
  • WORFBENCH:一个创新的评估基准,目的是全面测试大型语言模型在生成复杂工作流 方面的性能。
  • Centos7.6版本安装mysql详细步骤
  • Vue3 学习笔记(一)Vue3 介绍及环境部署
  • Docker 教程十一(Docker Machine)
  • 如何借助通达信API构建自动化交易系统?
  • # 更正 Excel 表格中 #VALUE! 错误
  • 信号源(函数发生器)的输出幅度是什么
  • 目前我国网络安全人才市场状况
  • MyBatis入门之一对一关联关系(示例)
  • 北斗小型化NTP时间服务器|北斗智能终端设备
  • 如何处理让人眼花缭乱的销售数据?使用报表制作工具轻松解决