Function Call实践(二)——本地开源模型部署部署使用 #127

Open
opened 2024-09-26 12:59:15 +08:00 by 12390900721cs · 0 comments

使用fastchat来启动模型服务

# 按顺序依次执行
# 新建终端,输入如下命令
python -m fastchat.serve.controller --host 0.0.0.0
# 新建终端,输入如下命令
python -m fastchat.serve.model_worker --model-path /dataset/Llama-3-8B-Instruct/ --host 0.0.0.0 --num-gpus 4 --max-gpu-memory 15GiB
# 新建终端,输入如下命令
python -m fastchat.serve.openai_api_server --host 0.0.0.0

这里与之前的实验类似,使用fastchat框架,完成controllor、worker和api_server的部署,此时模型就可以通过openai的api格式调用。、

安装openai并调用测试

# 这一步是在安装openai python SDK 以及 pyautogen
# 环境需要连接互联网才可用如下命令进行安装
pip install openai -i https://mirrors.ustc.edu.cn/pypi/web/simple
pip install pyautogen -i https://mirrors.ustc.edu.cn/pypi/web/simple

这里用国内的镜像安装。

函数调用测试

完成网络配置后,首先用本地llama模型调用测试。

python /code/autogen_calculations_function_call.py

运行结果:

再尝试用GLM-4进行测试:

运行结果:

可以看到都取得了正确的结果,代码能正常的运行。

代码理解

from typing import Annotated, Literal

Operator = Literal["+", "-", "*", "/"]

def calculator(a: int, b: int, operator: Annotated[Operator, "operator"]) -> int:
    if operator == "+":
        return a + b
    elif operator == "-":
        return a - b
    elif operator == "*":
        return a * b
    elif operator == "/":
        return int(a / b)
    else:
        raise ValueError("Invalid operator")

import os

from autogen import ConversableAgent


config_list=[
    {
        "model": "GLM-4-Plus",
        "base_url": "https://open.bigmodel.cn/api/paas/v4/",
        "api_type": "openai",
        "api_key": "cc28e0d694973a7276d02d6822e5958c.5h0H2TcNId2qBGBi", 
    },
]

# Let's first define the assistant agent that suggests tool calls.
assistant = ConversableAgent(
    name="Assistant",
    system_message="You are a helpful AI assistant. "
    "You can help with simple calculations. "
    "Return 'TERMINATE' when the task is done.",
    llm_config={"config_list": config_list},
)

# The user proxy agent is used for interacting with the assistant agent
# and executes tool calls.
user_proxy = ConversableAgent(
    name="User",
    llm_config=False,
    is_termination_msg=lambda msg: msg.get("content") is not None and "TERMINATE" in msg["content"],
    human_input_mode="NEVER",
)

# Register the tool signature with the assistant agent.
assistant.register_for_llm(name="calculator", description="A simple calculator")(calculator)

# Register the tool function with the user proxy agent.
user_proxy.register_for_execution(name="calculator")(calculator)

chat_result = user_proxy.initiate_chat(assistant, message="What is (44232 + 13312 / (232 - 32)) * 5?", clear_history=True)

这段代码定义了一个简单的计算器工具,并通过两个代理(assistant 和 user_proxy)实现交互。

  1. 计算器函数calculator 接收两个整数和一个操作符,支持加、减、乘、除运算。
  2. 配置列表config_list 定义了一个包含模型信息的字典,包括模型名称、API 地址和 API 密钥。
  3. 助手代理assistant 是一个可交互的代理,配置了系统消息以帮助用户进行简单计算,并注册了 calculator 工具。
  4. 用户代理user_proxy 用于与助手代理交互并执行工具调用,定义了终止消息的判定条件。
  5. 聊天发起user_proxy.initiate_chat 发起对助手的询问,计算表达式 (44232 + 13312 / (232 - 32)) * 5 的结果,并清除历史记录。

function call的使用:

  • 函数注册:在 assistant 中,使用 assistant.register_for_llmcalculator 函数注册为一个工具。这意味着助手可以调用这个函数来处理用户的请求。
  • 函数调用:在 user_proxy 中,使用 user_proxy.register_for_execution 注册同样的 calculator 函数。这使得用户代理可以在交互过程中调用计算器功能。
  • 实际调用:当 user_proxy.initiate_chat 被调用时,内部逻辑会解析用户输入的表达式, 在用户输入的表达式被解析为可计算的形式时,会调用已注册的 calculator 函数来执行实际计算(这通常发生在解析器识别到用户的请求需要特定的计算操作,且相应的计算函数已经注册并能够处理该类型的请求时。)。这种机制允许助手代理和用户代理之间进行动态函数调用。

这里明显没有之前调用api-key实现大模型时候的function call注册详细,我感觉应该是做了什么封装,省去了一大堆的内容吧。不然确实次次都要自己写tools,再自己写response搞格式让模型看看要不要调用函数,再写代码传参数改格式,再把结果转回去自然语言,确实好麻烦。之前那个应该是详细解释。

### 使用fastchat来启动模型服务 ```plain # 按顺序依次执行 # 新建终端,输入如下命令 python -m fastchat.serve.controller --host 0.0.0.0 # 新建终端,输入如下命令 python -m fastchat.serve.model_worker --model-path /dataset/Llama-3-8B-Instruct/ --host 0.0.0.0 --num-gpus 4 --max-gpu-memory 15GiB # 新建终端,输入如下命令 python -m fastchat.serve.openai_api_server --host 0.0.0.0 ``` 这里与之前的实验类似,使用fastchat框架,完成controllor、worker和api_server的部署,此时模型就可以通过openai的api格式调用。、 ### 安装openai并调用测试 ```plain # 这一步是在安装openai python SDK 以及 pyautogen # 环境需要连接互联网才可用如下命令进行安装 pip install openai -i https://mirrors.ustc.edu.cn/pypi/web/simple pip install pyautogen -i https://mirrors.ustc.edu.cn/pypi/web/simple ``` 这里用国内的镜像安装。 ### 函数调用测试 完成网络配置后,首先用本地llama模型调用测试。 ```plain python /code/autogen_calculations_function_call.py ``` ![](https://cdn.nlark.com/yuque/0/2024/png/48516026/1727251109558-63ad8d6c-37fd-4d03-8ed1-edde970b9ce8.png) 运行结果: ![](https://cdn.nlark.com/yuque/0/2024/png/48516026/1727251174988-6aeb2daf-ac88-4476-834b-317bd84ba2c2.png) 再尝试用GLM-4进行测试: ![](https://cdn.nlark.com/yuque/0/2024/png/48516026/1727251199775-45ec1450-3071-4c6e-b64a-5ff7b0ac0f3c.png) 运行结果: ![](https://cdn.nlark.com/yuque/0/2024/png/48516026/1727251212695-170da224-414b-4b69-a0da-2281c2498a8e.png) 可以看到都取得了正确的结果,代码能正常的运行。 ### 代码理解 ```plain from typing import Annotated, Literal Operator = Literal["+", "-", "*", "/"] def calculator(a: int, b: int, operator: Annotated[Operator, "operator"]) -> int: if operator == "+": return a + b elif operator == "-": return a - b elif operator == "*": return a * b elif operator == "/": return int(a / b) else: raise ValueError("Invalid operator") import os from autogen import ConversableAgent config_list=[ { "model": "GLM-4-Plus", "base_url": "https://open.bigmodel.cn/api/paas/v4/", "api_type": "openai", "api_key": "cc28e0d694973a7276d02d6822e5958c.5h0H2TcNId2qBGBi", }, ] # Let's first define the assistant agent that suggests tool calls. assistant = ConversableAgent( name="Assistant", system_message="You are a helpful AI assistant. " "You can help with simple calculations. " "Return 'TERMINATE' when the task is done.", llm_config={"config_list": config_list}, ) # The user proxy agent is used for interacting with the assistant agent # and executes tool calls. user_proxy = ConversableAgent( name="User", llm_config=False, is_termination_msg=lambda msg: msg.get("content") is not None and "TERMINATE" in msg["content"], human_input_mode="NEVER", ) # Register the tool signature with the assistant agent. assistant.register_for_llm(name="calculator", description="A simple calculator")(calculator) # Register the tool function with the user proxy agent. user_proxy.register_for_execution(name="calculator")(calculator) chat_result = user_proxy.initiate_chat(assistant, message="What is (44232 + 13312 / (232 - 32)) * 5?", clear_history=True) ``` 这段代码定义了一个简单的计算器工具,并通过两个代理(assistant 和 user_proxy)实现交互。 1. **计算器函数**:`calculator` 接收两个整数和一个操作符,支持加、减、乘、除运算。 2. **配置列表**:`config_list` 定义了一个包含模型信息的字典,包括模型名称、API 地址和 API 密钥。 3. **助手代理**:`assistant` 是一个可交互的代理,配置了系统消息以帮助用户进行简单计算,并注册了 `calculator` 工具。 4. **用户代理**:`user_proxy` 用于与助手代理交互并执行工具调用,定义了终止消息的判定条件。 5. **聊天发起**:`user_proxy.initiate_chat` 发起对助手的询问,计算表达式 `(44232 + 13312 / (232 - 32)) * 5` 的结果,并清除历史记录。 function call的使用: + **函数注册**:在 `assistant` 中,使用 `assistant.register_for_llm` 将 `calculator` 函数注册为一个工具。这意味着助手可以调用这个函数来处理用户的请求。 + **函数调用**:在 `user_proxy` 中,使用 `user_proxy.register_for_execution` 注册同样的 `calculator` 函数。这使得用户代理可以在交互过程中调用计算器功能。 + **实际调用**:当 `user_proxy.initiate_chat` 被调用时,内部逻辑会解析用户输入的表达式, 在用户输入的表达式被解析为可计算的形式时,会调用已注册的 `calculator` 函数来执行实际计算(这通常发生在解析器识别到用户的请求需要特定的计算操作,且相应的计算函数已经注册并能够处理该类型的请求时。)。这种机制允许助手代理和用户代理之间进行动态函数调用。 这里明显没有之前调用api-key实现大模型时候的function call注册详细,我感觉应该是做了什么封装,省去了一大堆的内容吧。不然确实次次都要自己写tools,再自己写response搞格式让模型看看要不要调用函数,再写代码传参数改格式,再把结果转回去自然语言,确实好麻烦。之前那个应该是详细解释。
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: HswOAuth/llm_course#127
No description provided.