Funtion call 让GPT学会使用工具
- 在 Prompt 中加入方法描述
- functions方法用途描述
- function_call 控制模型如何响应函数调用
- none 表示模型不调用函数
- auto 表示模型可以自动调用函数
- 默认值为 auto
response = openai.ChatCompletion.create(
engine=deployment,
model=model,
messages=msg,
functions=[
{
"name": "get_current_cluster_state",
"description": "Get the current state in a given cluster",
"parameters": {
"type": "object",
"properties": {
"cluster_name": {
"type": "string",
"description": "the name of the cluster",
},
},
"required": ["cluster_name"],
},
}
],
function_call="auto",
)
Function calling 实现基本套路
-
- 构建一个 dict 对象存储 {"方法1":方法1,...}
-
- 在 Prompt 中加入方法定义
-
- 根据 LLM 的返回,决定是否调用函数(返回信息中含有"function_call"),还是直接返回信息给用户
-
- 如果需要调用,则调用 LLM 指定函数,并将结果及调用的函数一起放在 Prompt 中再次调用 LLM
实现
- 环境变量
deployment = "gpt4"
model = "gpt-4"
- 定一个需要调用的函数 Function
# 定一个需要调用的函数
# 模拟返回给 gpt 的结果
def get_current_cluster_state(cluster_name):
print(f"cluster:{cluster_name}")
return """ERROR: Failed to pull image "chaocai/docker/dsp:latest"""
- 实现
import openai
import json
funcs = {"get_current_cluster_state": get_current_cluster_state}
def run(input):
msg=[{"role":"user","content":input}]
ret = run_conversation(msg)
return ret["content"]
def run_conversation(msg):
response = openai.ChatCompletion.create(
engine=deployment,
model=model,
messages=msg,
functions=[
{
"name": "get_current_cluster_state",
"description": "Get the current state in a given cluster",
"parameters": {
"type": "object",
"properties": {
"cluster_name": {
"type": "string",
"description": "the name of the cluster",
},
},
"required": ["cluster_name"],
},
}
],
function_call="auto",
)
message = response["choices"][0]["message"]
print("----- message ----")
print(message)
# {
# "role": "assistant",
# "function_call": {
# "name": "get_current_cluster_state",
# "arguments": "{\n \"cluster_name\": \"DSP\"\n}"
# }
# }
print("----- message ----")
# 如果不需要调用function,则直接返回结果
if not message.get("function_call"):
return message
# 获取调用的方法
function_name = message["function_call"]["name"] # get_current_cluster_state
function_args = json.loads(message["function_call"]["arguments"]) # {"cluster_name":"DSP"}
print(function_args)
res = funcs[function_name](function_args["cluster_name"])
message["content"]=None
msg.append(message)
msg.append({
"role": "function",
"name": function_name,
"content": res,
}) # 返回给gpt
return run_conversation(msg)
- 调用
print(run("What's wrong with the cluster 'DSP'? And if there's an error, give me some suggestion. "))
LangChain Agent
- Agent 的核心思想是使用语言模型来选择要采取的一系列操作。在前面的学校过的 Chain 中,一系列操作是硬编码的,而 Agent 使用语言模型作为推理引擎来确定采取哪些操作及采取的顺序
- 定义好工具集后,LangChain 自动调用相关的方法,自动传入参数!
from langchain.agents import initialize_agent, Tool
from langchain.chat_models import AzureChatOpenAI
from langchain.chains import LLMRequestsChain
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
import time
# from langchain.chat_models import ChatOpenAI #直接访问OpenAI的GPT服务
#llm = ChatOpenAI(model_name="gpt-4", temperature=0) #直接访问OpenAI的GPT服务
llm = AzureChatOpenAI(deployment_name = deployment, model_name=model, temperature=0, max_tokens=200) #通过Azure的OpenAI服务
- 定义工具集
- 方法
- 描述
tools = [
Tool(
name = "Get current info",func=query_web,
description="""invoke it for when you need to answer questions about current info.
And the input should be a search query."""),
Tool(
name="Get current cluster state", func=get_current_cluster_state,
description="""invoke it for when you need to get the current state in a given cluster,
And the input should be service name.""")
]
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", max_iterations=3, verbose=True)
- 执行
agent.run("北京今天穿什么合适?")
Entering new AgentExecutor chain... 我需要获取当前的天气信息来回答这个问题。 Action: Get current info Action Input: 北京今天的天气 Observation: 72°F Thought:根据观察到的天气信息,我可以推断出适合穿什么。 Final Answer: 今天北京的天气是72°F,所以穿一件长袖T恤和一条牛仔裤应该很合适。
Finished chain. '今天北京的天气是72°F,所以穿一件长袖T恤和一条牛仔裤应该很合适。'
- 执行2
agent.run("如何修复的ADX集群?")
Entering new AgentExecutor chain... 首先,我需要知道ADX集群的当前状态。 Action: Get current cluster state Action Input: ADX Observation: ERROR: Failed to pull image "chaocai/docker/dsp:latest Thought:看来ADX集群的问题在于无法拉取最新的镜像。我需要查找如何解决这个问题。 Action: Get current info Action Input: 如何修复无法拉取docker镜像的问题 Observation: 原因: 系统的时间和docker hub时间不一致,需要做系统时间与网络时间同步 · 安装时间同步 sudo yum -y install ntp ntpdate. 1 · 同步时间 sudo ntpdate cn. 更新:重启后/etc/resolv.conf的修改确实失效恢复到默认。解决办法:. 在/etc/sysconfig/network-scripts/ifcfg-eth 文件中加入PEERDNS 选项。可以 ... 您好,这个镜像确实是存在,我用您的命令到ssh中拉取镜像是可以拉取的,您拉取不成功,可能是因为您的网络环境和Docker Hub之间的连接不稳定或者速度较慢,或者他们在拉取时的时间Docker Hub的镜像仓库处于不可用状态,导致拉取镜像失败。 1、检查您的网络连接是否正常,并尝试使用 ... 在节点性能较差或镜像较大时,可能出现镜像无法成功下载,负载启动失败的现象。 解决方案:. (推荐)方法一:登录节点使用docker pull命令手动下拉镜像, ... 网络问题:Docker 拉取镜像需要访问Docker Hub 或其他镜像仓库,如果网络不稳定或连接不畅,可能会导致卡住。可以尝试检查网络连接,并尝试使用VPN 或更改网络环境等 ... 反馈bug/问题模板,提建议请删除1.关于你要提交的问题Q:是否搜索了issue (使用"x" 选择) [x ] 没有类似的issue 2. 详细叙述(1) 具体问题A: ... 我正在使用最简单的docker文件从另一个镜像创建docker镜像: FROM 成功构建和标记映像。但是,当我将其推送到Azure容器注册表并拉取镜像时, ... 如果我们无法拉取一个特定的镜像,可能是因为该镜像不存在于Docker镜像仓库中 ... 我们还可以尝试重新配置Docker Credential Helper来修复权限问题。 然而,在某些情况下,我们可能会遇到服务器内网无法拉取Docker 镜像的问题。本文将介绍可能导致该问题的原因,并提供相应的解决方案。 授权问题:请确认您是否已经登录Docker Hub 或Alibaba Cloud Registry,并且具备拉取镜像的权限。如果需要进行身份验证,请使用 docker login 命令进行 ... Thought:根据我找到的信息,无法拉取docker镜像的问题可能是由于网络问题、服务器时间不一致、镜像不存在或者权限问题等原因导致的。解决这个问题的方法可能包括检查网络连接、同步服务器时间、手动拉取镜像、重新配置Docker Credential Helper或者使用docker login命令进行身份验证等。 Final Answer: 修复ADX集群的方法可能包括:1. 检查网络连接,确保网络稳定;2. 同步服务器时间,确保与docker hub时间一致;3. 手动使用docker pull命令拉取镜像;4. 重新配置Docker Credential Helper来修复权限问题;5. 使用docker login命令进行身份验证,确保具备拉
Finished chain. '修复ADX集群的方法可能包括:1. 检查网络连接,确保网络稳定;2. 同步服务器时间,确保与docker hub时间一致;3. 手动使用docker pull命令拉取镜像;4. 重新配置Docker Credential Helper来修复权限问题;5. 使用docker login命令进行身份验证,确保具备拉'
利用LangChain中的工具
- wolframalpha 数学计算
tools = [
Tool(
name = "Get current info",func=query_web,
description="""invoke it for when you need to answer questions about current info.
And the input should be a search query."""),
Tool(
name="Get current cluster state", func=get_current_cluster_state,
description="""invoke it for when you need to get the current state in a given cluster,
And the input should be service name."""),
Tool(
name="Invoke WolframAlpha API", func=wolfram_api,
description="""invoke it for when you need to anwser or solve a mathematical question,
And the input is the mathematical equations""")
]
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", max_iterations=10, verbose=True)
agent.run("在一个外星球上,这里的鸡都长着3只脚1个头,免子有4只脚1个头,一个装着鸡和免子的笼子里一共有20个头,71只脚,请问笼子里各有多少只鸡和免子?")
LangChain中的PlanAndExecute
- 模拟定一个外部的方法
def get_current_cluster_state(cluster_name):
return """ERROR: Failed to pull image "chaocai/docker/dsp:latest"""
- 主体
from langchain.agents import initialize_agent, Tool
from langchain_experimental.plan_and_execute import PlanAndExecute, load_agent_executor, load_chat_planner
from langchain.chat_models import AzureChatOpenAI
from langchain.chains import LLMRequestsChain
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
import time
llm = AzureChatOpenAI(deployment_name = deployment, model_name=model, temperature=0, max_tokens=200)
tools = [
Tool(
name="Get current cluster state", func=get_current_cluster_state,
description="""invoke it for when you need to get the current state in a given service cluster,
And the input of the function is only one parameter, the service name, for example, to get the state of ADX service,
the input for invoking the function is "ADX"
""")
]
planner = load_chat_planner(llm)
executor = load_agent_executor(llm, tools, verbose=True)
agent = PlanAndExecute(planner=planner, executor=executor, verbose=True)
- 执行
agent.run("Check the service named 'DSP', if there's something wrong with the service, give me some suggestions to fix it.")
Entering new PlanAndExecute chain... steps=[Step(value="Identify the service named 'DSP'."), Step(value="Check the status of the 'DSP' service."), Step(value='If the service is running correctly, inform the user that there is no issue with the service.'), Step(value='If the service is not running correctly, diagnose the problem.'), Step(value='Based on the diagnosis, suggest possible solutions to fix the issue.'), Step(value="Given the above steps taken, respond to the user's original request by providing the status of the 'DSP' service and any necessary solutions to fix it, if applicable.\n")]
Entering new AgentExecutor chain... Thought: The user has not provided the name of the DSP service yet. I need to ask for it again to proceed with the task. Action:
{
"action": "Final Answer",
"action_input": "Could you please specify the name of the DSP service that you want to check?"
}
Finished chain.
Step: Identify the service named 'DSP'.
Response: Could you please specify the name of the DSP service that you want to check?
Entering new AgentExecutor chain... Thought: The user wants to check the status of the 'DSP' service. I will use the 'Get current cluster state' tool to get the current state of the 'DSP' service.
Action:
{
"action": "Get current cluster state",
"action_input": "DSP"
}
Observation: ERROR: Failed to pull image "chaocai/docker/dsp:latest Thought:The 'Get current cluster state' tool returned an error indicating that it failed to pull the latest image for the 'DSP' service. This could be due to a variety of reasons such as network issues, incorrect image name, or the image not being available. The user should verify the image name and availability. If the problem persists, they may need to check their network connection or contact their system administrator.
Action:
{
"action": "Final Answer",
"action_input": "The tool returned an error indicating that it failed to pull the latest image for the 'DSP' service. This could be due to a variety of reasons such as network issues, incorrect image name, or the image not being available. Please verify the image name and availability. If the problem persists, you may need to check your network connection or contact your system administrator."
}
Finished chain.
Step: Check the status of the 'DSP' service.
Response: The tool returned an error indicating that it failed to pull the latest image for the 'DSP' service. This could be due to a variety of reasons such as network issues, incorrect image name, or the image not being available. Please verify the image name and availability. If the problem persists, you may need to check your network connection or contact your system administrator.
Entering new AgentExecutor chain... Thought: The user has not specified the name of the DSP service that needs to be checked. However, the previous steps indicate that there was an error pulling the latest image for the 'DSP' service. I need to check the current state of the DSP service to provide an accurate response.
Action:
{
"action": "Get current cluster state",
"action_input": "DSP"
}
Observation: ERROR: Failed to pull image "chaocai/docker/dsp:latest Thought:The observation indicates that there was an error pulling the latest image for the DSP service. This could be due to a variety of reasons such as network issues, incorrect image name, or the image not being available. I will inform the user about this issue.
Action:
{
"action": "Final Answer",
"action_input": "There seems to be an issue with the DSP service. The system failed to pull the latest image for the DSP service. This could be due to network issues, incorrect image name, or the image not being available. Please verify the image name and availability. If the problem persists, you may need to check your network connection or contact your system administrator."
}
Finished chain.
Step: If the service is running correctly, inform the user that there is no issue with the service.
Response: There seems to be an issue with the DSP service. The system failed to pull the latest image for the DSP service. This could be due to network issues, incorrect image name, or the image not being available. Please verify the image name and availability. If the problem persists, you may need to check your network connection or contact your system administrator.
Entering new AgentExecutor chain... Thought: The previous steps indicate that there is an issue with the DSP service as the system failed to pull the latest image for it. This could be due to network issues, incorrect image name, or the image not being available. To diagnose the problem, we need to check the current state of the DSP service. Action:
{
"action": "Get current cluster state",
"action_input": "DSP"
}
Observation: ERROR: Failed to pull image "chaocai/docker/dsp:latest Thought:The error message indicates that the system failed to pull the latest image for the DSP service from the specified location. This could be due to a variety of reasons such as network issues, incorrect image name, or the image not being available. The user should verify the image name and its availability. If the problem persists, they may need to check their network connection or contact their system administrator. Action:
{
"action": "Final Answer",
"action_input": "The system failed to pull the latest image for the DSP service. This could be due to network issues, incorrect image name, or the image not being available. Please verify the image name and availability. If the problem persists, you may need to check your network connection or contact your system administrator."
}
Finished chain.
Step: If the service is not running correctly, diagnose the problem.
Response: The system failed to pull the latest image for the DSP service. This could be due to network issues, incorrect image name, or the image not being available. Please verify the image name and availability. If the problem persists, you may need to check your network connection or contact your system administrator.
Entering new AgentExecutor chain... Thought: The user has not provided the name of the DSP service that needs to be checked. However, based on the previous responses, it seems there is an issue with pulling the latest image for the DSP service. This could be due to network issues, incorrect image name, or the image not being available. The suggested solutions would be to verify the image name and availability, check the network connection, or contact the system administrator.
Action:
{
"action": "Final Answer",
"action_input": "There seems to be an issue with pulling the latest image for the DSP service. This could be due to network issues, incorrect image name, or the image not being available. I suggest you to verify the image name and availability. If the problem persists, you may need to check your network connection or contact your system administrator."
}
Finished chain.
Step: Based on the diagnosis, suggest possible solutions to fix the issue.
Response: There seems to be an issue with pulling the latest image for the DSP service. This could be due to network issues, incorrect image name, or the image not being available. I suggest you to verify the image name and availability. If the problem persists, you may need to check your network connection or contact your system administrator.
Entering new AgentExecutor chain... Thought: The user has not provided a specific DSP service to check. However, based on the previous steps, it seems there is an issue with pulling the latest image for a DSP service. The suggested solution was to verify the image name and availability, check the network connection, or contact the system administrator. I will reiterate this information in my response.
Action:
{
"action": "Final Answer",
"action_input": "There seems to be an issue with pulling the latest image for the DSP service. This could be due to network issues, incorrect image name, or the image not being available. I suggest you to verify the image name and availability. If the problem persists, you may need to check your network connection or contact your system administrator."
}
Finished chain.
Step: Given the above steps taken, respond to the user's original request by providing the status of the 'DSP' service and any necessary solutions to fix it, if applicable.
Response: There seems to be an issue with pulling the latest image for the DSP service. This could be due to network issues, incorrect image name, or the image not being available. I suggest you to verify the image name and availability. If the problem persists, you may need to check your network connection or contact your system administrator.
Finished chain. 'There seems to be an issue with pulling the latest image for the DSP service. This could be due to network issues, incorrect image name, or the image not being available. I suggest you to verify the image name and availability. If the problem persists, you may need to check your network connection or contact your system administrator.'