掘金 人工智能 10小时前
一文理解 Function Calling
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

Function Calling技术是增强大型语言模型(LLM)能力的关键。它允许LLM通过微调,识别用户意图并以结构化的JSON格式调用外部工具(Functions)。这些工具可以用于获取LLM无法直接访问的实时信息,如天气预报。实现过程涉及系统提示、用户输入和工具描述,LLM据此判断是否调用工具,并返回函数名和参数。程序接收到JSON后执行相应函数,并将结果反馈给LLM,最终由LLM整合信息并回复用户。文章通过一个获取天气的示例,详细阐述了Function Calling的完整流程,从LLM初始化到工具函数定义、参数校验,再到多轮交互与结果汇总。

✨ **Function Calling赋能LLM调用外部工具**:Function Calling技术的核心在于使大型语言模型(LLM)能够访问和利用外部工具,从而扩展其能力边界。这通常通过对基础LLM进行微调实现,使其能够理解用户指令,识别并调用预设的工具函数,并将所需的参数以JSON格式结构化输出。

⚙️ **实现LLM与工具的交互流程**:Function Calling的实现并非LLM直接执行代码,而是通过一个包含系统提示、用户输入和工具描述的完整提示集。LLM根据这些信息判断用户意图是否需要调用工具。若需调用,LLM会返回一个包含目标函数名及其参数的JSON对象。程序随后解析此JSON,执行相应的函数,并将执行结果连同原始提示一起发送回LLM,LLM最终整合信息并向用户提供反馈。

☀️ **通过天气示例解析Function Calling**:文章以一个获取当前天气的实例,详细展示了Function Calling的实际应用。示例中,首先初始化了一个Azure OpenAI客户端,接着定义了一个`get_current_weather`函数,该函数能根据地点返回天气信息。然后,通过构造包含系统指令、用户问题以及`get_current_weather`函数详细描述(包括参数类型、描述及可选值)的messages和tools,调用LLM生成包含函数调用信息的JSON响应。

📞 **多轮交互与结果汇总**:在LLM返回函数调用信息后,程序会解析JSON,调用对应的`get_current_weather`函数,并将获取到的天气数据以工具响应的形式再次发送给LLM。LLM接收到这一信息后,能够结合用户原始问题和工具返回的结果,生成一个整合了实时信息的最终回答,例如告知用户旧金山和东京的天气情况。

🛠️ **工具描述的重要性**:在Function Calling的实现中,清晰、准确的工具描述至关重要。这包括函数的名称、功能描述以及参数的类型、描述和约束(如枚举值)。例如,`get_current_weather`函数的描述中明确了地点参数的格式,以及单位参数可以为“celsius”或“fahrenheit”,这有助于LLM准确理解和调用函数。

Funcation Calling 就是能够让 LLM 具备访问外部工具,增强 LLM 的能力。要使用 Function Calling 功能,就需要一个具备 Function Calling 能力的 LLM

LLM With Function Calling

具备 Funcation Calling 的 LLM,一般都需要对基础 LLM 进行微调,也就是通过少量的数据进行训练,让 LLM 能够根据 Prompt 识别该使用哪个 Function,将要调用的 Function 和其需要的参数通过 JSON 的方式结构化输出。

Funcation 其实就是类似一个Tool,使用这种 Tool 就可以增强 LLM 的功能,比如:获取上海明天的天气,这种实时数据无法通过 LLM 获取,所以就可以使用 LLM + Tool 来解决这个问题

Understand Function Calling

对于 Function Calling的误解就是会觉得是 LLM 直接调用 Function,其实并不是这样的。Function Calling 的实现其实是 LLM 根据提示词判断是不是需要调用 Funcation,这个提示词分为三部分:

    System Prompt:这部分就是告诉 LLM 这个 Agent 的功能是什么User Prompt:这部分是用户输入 问题Tool Description:这部分主要是用于描述一个 Function,以结构化的方式,告诉 LLM 这个 Function 是什么,有哪些参数

将这些 Prompt 传递给 LLM,LLM会根据用户的意图来判断是不是需要调用 Tool,如果需要,那么 LLM 就会返回一个 Json,其中包含要调用的函数的名字和参数。

程序收到返回后,就可以调用 LLM 返回的 Function,并且传入 LLM 返回的函数参数。调用完成之后,再将调用的结果和之前的 Prompt 一起发送给 LLM,LLM就会将结构进行汇总返回给用户

Example

下面就通过获取天气的例子来看下 Funcation Calling 具体是如何实现的。首先,需要初始化一个 LLM,此处以 Azure Openai 为例:

client = openai.AzureOpenAI(    azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),    api_key=os.getenv("AZURE_OPENAI_API_KEY"),    api_version=os.getenv("AZURE_OPENAI_API_VERSION"),)DEPLOYMENT_NAME = os.getenv("AZURE_OPENAI_DEPLOYMENT_NAME")

接下来写一个获取添加的 Tool :

def get_current_weather(location, unit="fahrenheit"):    """Get the current weather in a given location"""    if "tokyo" in location.lower():        return json.dumps({"location": "Tokyo", "temperature": "10", "unit": unit})    elif "san francisco" in location.lower():        return json.dumps({"location": "San Francisco", "temperature": "72", "unit": unit})    elif "paris" in location.lower():        return json.dumps({"location": "Paris", "temperature": "22", "unit": unit})    else:        return json.dumps({"location": location, "temperature": "unknown"})

定义 Tool的描述,使用 llm调用:

def get_function_and_args(tool_call, available_functions):    """    Retrieves the function and its arguments based on the tool call.    Verifies if the function exists and has the correct number of arguments.    Args:        tool_call (ToolCall): The tool call object containing the function name and arguments.        available_functions (dict): A dictionary of available functions.    Returns:        tuple: A tuple containing the function to call and its arguments.            If the function or arguments are invalid, returns an error message and None.    """    # verify function exists    if tool_call.function.name not in available_functions:        return "Function " + tool_call.function.name + " does not exist", None    function_to_call = available_functions[tool_call.function.name]    # verify function has correct number of arguments    function_args = json.loads(tool_call.function.arguments)    if check_args(function_to_call, function_args) is False:        return "Invalid number of arguments for function: " + tool_call.function.name, None    return function_to_call, function_argsdef run_conversation():    messages = [        {            "role": "system",            "content": """                You are a helpful assistant.                You have access to a function that can get the current weather in a given location.                Determine a reasonable Unit of Measurement (Celsius or Fahrenheit) for the temperature based on the location.            """,        },        {            "role": "user",            "content": "What's the weather like in San Francisco, Tokyo?",        },    ]    tools = [        {            "type": "function",            "function": {                "name": "get_current_weather",                "description": """                    Get the current weather in a given location.                     Note: any US cities have temperatures in Fahrenheit                """,                "parameters": {                    "type": "object",                    "properties": {                        "location": {                            "type": "string",                            "description": "The city and state, e.g. San Francisco, CA",                        },                        "unit": {                            "type": "string",                            "description": "Unit of Measurement (Celsius or Fahrenheit) for the temperature based on the location",                            "enum": ["celsius", "fahrenheit"],                        },                    },                    "required": ["location"],                },            },        }    ]    response = client.chat.completions.create(        model=DEPLOYMENT_NAME,        messages=messages,        tools=tools,        tool_choice="auto",  # auto is default, but we'll be explicit        temperature=0,  # Adjust the variance by changing the temperature value (default is 0.8)    )    response_message = response.choices[0].message    tool_calls = response_message.tool_calls    if tool_calls:        messages.append(response_message)  # extend conversation with assistant's reply        available_functions = {            "get_current_weather": get_current_weather,        }  # only one function in this example, but you can have multiple        for tool_call in tool_calls:            # Step 3: call the function            # Note: the JSON response may not always be valid; be sure to handle errors            function_name = tool_call.function.name            # get the function and arguments            function_to_call, function_args = get_function_and_args(tool_call, available_functions)            # call the function            function_response = function_to_call(**function_args)            # Step 4: send the info for each function call and function response to the model            messages.append(                {                    "tool_call_id": tool_call.id,                    "role": "tool",                    "name": function_name,                    "content": function_response,                }            )  # extend conversation with function response        second_response = client.chat.completions.create(            model=DEPLOYMENT_NAME,            messages=messages,            temperature=0,  # Adjust the variance by changing the temperature value (default is 0.8)        )  # get a new response from the model where it can see the function response        return second_response

最后写一个:

if __name__ == '__main__':     result = run_conversation()     message_content = result.choices[0].message.content     print(message_content)

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

Function Calling LLM AI工具
相关文章