让GPT学会使用工具,拓展技能

1 篇文章 0 订阅
1 篇文章 0 订阅

引言

ChatGPT 无疑是现在最热门的话题,但我们都清楚 GPT 的训练数据基于过去的信息,且或许并未涵盖您所需场景的所有相关数据。另外,某些数据信息需要在特定环境中实时获取,例如:掌握集群当前的工作状态等。这些都大大限制了 GPT 的应用场景,甚至你无法让 GPT 告诉你根据今天的天气穿什么衣服更合适,更无法解决你的环境中跟实时事件和数据有关的问题。因此,赋予 GPT 使用工具、执行任务以及获取外部数据的功能将极大地拓展其应用范围。值得注意的是,当前备受关注的 AutoGPT 项目也是利用这一相关技术。

让 GPT 选择正确的工具(方法)

首先,我们都知道 GPT 是 LLM(Large Language Model)具有很强大的语言理解能力,所以我们只要简单告诉 GPT 哪个工具是干什么的,他就可以在不同情况下选择正确的工具。

下面的示例使用了 LangChain,所以我们要先安装:

pip install openaipip install langchain


from langchain.agents import initialize_agent, Tool
from langchain.llms import AzureOpenAI

llm = AzureOpenAI( model_name="gpt-4",temperature=0, engine="gpt4")

def get_node_info(input: str) -> str:
    return "The disk is full."

def get_pod_info(input: str) -> str:
    return "The pod's is running well."

tools = [
    Tool(
        name = "Get Node Info",func=get_node_info, 
        description="By invoking it, you can get the node info, also the node here is the (VM) virtual machine"
    ),
    Tool(name="Get Pod Info", func=get_pod_info, 
         description="By invoking it, you can get the pod info. Your application is always running in the pods."
    )
]
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)
好了运行完以上代码后我们来看看效果,当然不要忘记了给程序提供你的 OPEN_AI_KEY, 上面的程序中我是通过 Azure 提供的 openai 服务来完成的。
接下来我们来验证一下:
print(agent.run("What's wrong with the machine (hostname:chao)?"))

运行上面程序后,返回如下结果:

> Entering new AgentExecutor chain...
I need to check the node info to see if there are any issues with the virtual machine.
Action: Get Node Info
Action Input: chao
Observation: The disk is full.
Thought:I now know the final answer.
Final Answer: The issue with the machine (hostname: chao) is that its disk is full.

> Finished chain.
The issue with the machine (hostname: chao) is that its disk is full.

可以看到 GPT 准确的调用了 get_node_info 这个方法。上面的程序中我们利用了 LangChain 中的 Tool 和 Agent,可以看到在 Tool 的定义中我们为每个 Tool(方法)都加入一个描述(description),这样 GPT 通过对于 description 的理解就可以根据上下文正确的调用工具。

我们再来测试一个:

print(agent.run("How's my application(DSP)?"))

这次返回的结果是

Entering new AgentExecutor chain...
I need to check the pod information to see the status of the application.
Action: Get Pod Info
Action Input: DSP

Observation: The pod's is running well.
Thought:I now know the final answer.
Final Answer: Your application (DSP) is running well.

> Finished chain.
Your application (DSP) is running well.

到这里你一定在想 LangChain 的 Tool 是怎么实现的这个魔术的呢?还有似乎看着这里有一个逐步推理的到得出结论的过程(Thought-Action-Observation-Thougt -),这个过程实际被称为 LLM 的 ReAct (Reasoning+Acting),关于 ReAct 的应用和解密我会放到我的下一篇文章,这里我们先揭秘这个方法选择。

揭秘神奇的提示词

其实 LangChain Tool 里并没有什么 Rocket Science,依然还是提示词工程,我们来看一下下面这段提示词:

prompt = """
  According to user requirement and operation description, select the operation. Please, return the operation's name, 
  and if there's no suitable method, then return "none"
  1. operation name:  get_cluster_info, description: get the info about the cluster, and the info can be used to detect the problems of the cluster;
  2. operation name:  get_node_info, description: get the info about the node, and the info can be used to detect the problems of the node; , in our context node is equals to machine or virtual machine/VM
  3. operation name:  get_pod_info, description: get the info about the pod, and the info can be used to detect the problems of the pod; """

通过阅读提示词你会发现,我们做的只是简单告诉 GPT,根据当前用户的要求及 operation 的描述,返回 operation 的名字,就这么简单。下面我们来测试一下。

response = openai.ChatCompletion.create(
    engine="gpt4", # engine = "deployment_name".
    messages=[
        {"role": "system", "content": "You are an operator."+prompt},
        {"role": "user", "content": "我的机器怎么了"}]
  )
print(response.choices[0].message.content)

上面调用的输出是:

get_node_info

到这里我想你已经了解他的原理。

结束语

上面我们通过提示词就扩展了 GPT 的能力,当然你可通过方法来让 GPT 获取实时的事件和数据,甚至让他来执行语言模型做不了的事情,如调用其他模型来生成一张图片。在很多 AI 专业人士眼中,这些场景的扩展可能需要通过对模型 fine tuning 来完成,实际在大语言模型时代我们只需通过提示词工程(Prompt Engineering)就可以做到了。LLM 的成功,需要我们重新思考很多曾经的应用构建方式及 AI 模型的扩展方式。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值