链载Ai

标题: 让 LLM 学会使用工具的正确方式无疑就是 function calling,今天我们就来聊一聊如何将其实现(1) [打印本页]

作者: 链载Ai    时间: 4 小时前
标题: 让 LLM 学会使用工具的正确方式无疑就是 function calling,今天我们就来聊一聊如何将其实现(1)

今天会从 openAI 的 function calling 聊起,首先聊一聊什么是 function calling,还会聊一聊设计 function calling 的基本思路。介绍如何基于本地模型,自己去实现一个 function calling 模块。


这一系列将覆盖了 langchain 这样 agent 框架核心模块背后实现原理,相应的实现代码也会上传到我的 github 上

https://github.com/zideajang/llm_function_calling

概述

我们先来看 openAI 提供了 Function calling 功能,这个 Function calling 位于 CAPABILITIES 单元里,说明这是 LLM 的一种能力,那么是什么能力呢? 就是让 LLM 学会使用工具,从而扩展 LLM 能力。

考虑 azent 框架设计初衷是面向企业,模型优先考虑从本地大语言模型。不过一些想法还是参考了 openAI Function calling 设计,在 openAI API call 中,通过结构化的数据提供一系列 Function 供大语言模型使用,然后让 LLM 根据用户问题,来适当地选择一个或者多个 Function 来执行,并不是在 LLM 端执行,而是 LLM 返回一个 json 结构化数据结构来告诉 agent 要执行哪些 Funciton 以及传入参数的方式是什么。

基本方式


关于 Function calling 使用场景

Function calling 使用场景很多,几乎现在稍微复杂一些任务都需要 agent 通过使用工具来完成。例如利用搜索工具来获取当前关于某一个 query 最近动态的信息,从而确保信息实时性,例如当天的股价信息。


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"],                },            },        }    ]


那么 LLM 应该如何了解

You have access to the following tools:{function_to_json(get_weather)}{function_to_json(calculate_mortgage_payment)}{function_to_json(get_directions)}{function_to_json(get_article_details)}
You must follow these instructions:Always select one or more of the above tools based on the user queryIf a tool is found, you must respond in the JSON format matching the following schema:{{ "tools": {{ "tool": "<name of the selected tool>", "tool_input": <parameters for the selected tool, matching the tool's JSON schema }}}}If there are multiple tools required, make sure a list of tools are returned in a JSON array.If there is no tool that match the user request, you will respond with empty json.Do not add any additional Notes or Explanations
User Query


当我们在 User Query 位置输入下面的话,就会返回一个 json 数据,tools 数组,数组包含一个或者多个 tool 。


User Query:What's the weather in London, UK?


{ "tools": [ { "tool": "get_weather", "tool_input": { "location": "London, UK" } } ] }

有了这样的 LLM 返回的 json 结构化的数据,接下来要做的工作就是根据 LLM 的选择,来调用执行选择 function 来完成任务,或者是将 function 返回数据再次结合 prompt 返回给 LLM。


接下来我们就来看一看如何从 function 提取一个函数的信息,这些函数信息作为函数的描述,LLM 会根据用户 Query 信息首先选择一个或者多个函数来解决或者回答用户的 Query。

def get_type_name(t):    name = str(t)    if "list" in name or "dict" in name:        return name    else:        return t.__name__
def function_to_json(func): signature = inspect.signature(func) type_hints = get_type_hints(func)
console.log(func.__doc__) console.print(type_hints)
function_info = { "name": func.__name__, "description": func.__doc__, "parameters": {"type": "object", "properties": {}}, "returns": type_hints.get("return", "void").__name__, }
for name, _ in signature.parameters.items(): param_type = get_type_name(type_hints.get(name, type(None))) function_info["parameters"]["properties"][name] = {"type": param_type}
return json.dumps(function_info, indent=2)

这里定义 function_to_json 的函数,用于解析 function 来获取 function 的信息,也就是 function 到底长什么样子,也就是需要知道函数名字,调用 function 的参数,以及 function 的返回值。然后接下来就是要考虑如何执行返回的 function。

这份内容比较多,而且也比较重要,准备分几期给大家说清楚,还希望大家多多支持。

接下来我们介绍如何注册一个 function 以及如何执行 function,并且会进步一介绍如何并行去执行 function calling。


大模型 aZent 框架,让大模型真正成为你的同事(1)

大模型 Agent 框架 aZent ,让大模型真正成为你的同事(2)—培养编写代码能力

大模型 Agent 框架 aZent ,让大模型真正成为你的同事(3)—让他成为一个过目不忘的项目经理

大模型 aZent 框架,让大模型真正成为你的同事(4)—aZent 的外貌渐渐浮现

大模型 aZent 框架,让大模型真正成为你的同事(5)—aZent 的 prompt 自我提升方向,以及如何简单有效实现多模态






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