NSDT工具推荐: Three.js AI纹理开发包 - YOLO合成数据生成器 - GLTF/GLB在线编辑 - 3D模型格式在线转换 - 可编程3D场景编辑器 - REVIT导出3D模型插件 - 3D模型语义搜索引擎 - AI模型在线查看 - Three.js虚拟轴心开发包 - 3D模型在线减面 - STL模型在线切割 - 3D道路快速建模
LLM 代理(Agent)是使用大型语言模型来决定如何以及何时使用工具来完成任务的程序。
为什么LLM代理对于任务至关重要? LLM 很强大; 但是,他们可能无法执行某些任务。 让我们探讨一下原因。
大型语言模型是经过大量文本数据训练的强大神经网络。 他们可以生成文本、翻译语言、编写不同类型的创意内容并以信息丰富的方式回答您的问题,但不是为了执行任务。
代理的核心思想是使用语言模型来选择要采取的一系列操作。 在链中,一系列操作被硬编码(在代码中)。 在代理中,语言模型被用作推理引擎来确定要采取哪些操作以及按什么顺序。
简而言之,LLM就像为汽车提供动力的发动机,而LLM代理就是汽车本身,具有使其可用于现实世界任务的附加功能。
2、LLM代理和工具
要使用代理,我们需要三件事:
- 基础LLM,
- 我们将与之交互的工具(Tools),
- 控制交互的代理(Agent)。
3、LLM代理实现
首先安装依赖包:
!pip install -q openai py_expression_eval google-api-python-client
导入依赖包:
from openai import OpenAI
from googleapiclient.discovery import build
from py_expression_eval import Parser
import re, time, os
按照这些说明设置自定义搜索引擎和 Google API 密钥。
从上一步中获取 API Key 和自定义搜索引擎 ID,并将它们分别设置为环境变量 GOOGLE_API_KEY
和 GOOGLE_CSE_ID
。
client = OpenAI(api_key='Openai_api_key')
os.environ["GOOGLE_CSE_ID"] = "Google CSE ID"
os.environ["GOOGLE_API_KEY"] = "Google API Key"
3.1 工具
search
:使用 Google 官方自定义搜索引擎进行搜索引擎查询。 在免费套餐中,你每天可以发送 100 个请求。
calculator
:我使用 py_expression_eval
作为计算器(在能够运行复杂的数学表达式之间取得了良好的平衡,并且没有尝试引入完整的 Python REPL/eval 的许多风险)。
我们在演示中使用了两个工具。 你可以根据你的要求使用任何想要的工具。
#Google search engine
def search(search_term):
search_result = ""
service = build("customsearch", "v1", developerKey=os.environ.get("GOOGLE_API_KEY"))
res = service.cse().list(q=search_term, cx=os.environ.get("GOOGLE_CSE_ID"), num = 10).execute()
for result in res['items']:
search_result = search_result + result['snippet']
return search_result
#Calculator
parser = Parser()
def calculator(str):
return parser.parse(str).evaluate({})
3.2 提示
首先,我们来设置一下系统提示:
System_prompt = """
Answer the following questions and obey the following commands as best you can.
You have access to the following tools:
Search: Useful for when you need to answer questions about current events. You should ask targeted questions.
Calculator: Useful for when you need to answer questions about math. Use python code, eg: 2 + 2
Response To Human: When you need to respond to the human you are talking to.
You will receive a message from the human, then you should start a loop and do one of two things
Option 1: You use a tool to answer the question.
For this, you should use the following format:
Thought: you should always think about what to do
Action: the action to take, should be one of [Search, Calculator]
Action Input: "the input to the action, to be sent to the tool"
After this, the human will respond with an observation, and you will continue.
Option 2: You respond to the human.
For this, you should use the following format:
Action: Response To Human
Action Input: "your response to the human, summarizing what you did and what you learned"
Begin!
"""
那么上面的循环能做什么呢?
告诉模型它将在循环中运行。 在这个循环中,LLM有两个选择:它可以“使用工具”,为该工具提供输入,或者它可以响应人类。 我们为模型提供了一份工具列表以及何时/如何使用每个工具的描述。 思想-行动模式创建了一个“思想链”,告诉模型思考它正在做什么。
我们在此演示中使用了 GPT-4 模型,但你也可以使用 Llama、Mistral、Zephyr 等开源模型。
def Stream_agent(prompt):
messages = [
{ "role": "system", "content": System_prompt },
{ "role": "user", "content": prompt },
]
def extract_action_and_input(text):
action_pattern = r"Action: (.+?)\n"
input_pattern = r"Action Input: \"(.+?)\""
action = re.findall(action_pattern, text)
action_input = re.findall(input_pattern, text)
return action, action_input
while True:
response = client.chat.completions.create(
model="gpt-4",
messages=messages,
temperature=0,
top_p=1,)
response_text = response.choices[0].message.content
print(response_text)
#To prevent the Rate Limit error for free-tier users, we need to decrease the number of requests/minute.
time.sleep(20)
action, action_input = extract_action_and_input(response_text)
if action[-1] == "Search":
tool = search
elif action[-1] == "Calculator":
tool = calculator
elif action[-1] == "Response To Human":
print(f"Response: {action_input[-1]}")
break
observation = tool(action_input[-1])
print("Observation: ", observation)
messages.extend([
{ "role": "system", "content": response_text },
{ "role": "user", "content": f"Observation: {observation}" },
])
让我们测试一下我们的代理。
Stream_agent("who is new openai board members")
Output
Thought: I need to find the current information about the new board members of OpenAI.
Action: Search
Action Input: "New OpenAI board members"
Observation: 7 days ago ... I am returning to OpenAI as CEO. Mira will return to her role as CTO. The new initial board will consist of Bret Taylor (Chair), Larry Summers, ...Nov 21, 2023 ... Altman were jettisoned from the board, whose members now include Bret Taylor, an early Facebook officer and former co-chief executive of ...OpenAI is governed by the board of the OpenAI Nonprofit, currently comprised of Independent Directors Bret Taylor (Chair), Larry Summers, and Adam D'Angelo.Nov 22, 2023 ... Here are the newest members of OpenAI's board · Bret Taylor, board chair · Larry Summers · Adam D'Angelo.Nov 22, 2023 ... The company will have an initial board consisting of Bret Taylor, the former chair of Twitter's board before its takeover by Elon Musk; Larry ...Nov 23, 2023 ... Who are OpenAI's new board members as Sam Altman returns? ... Nov 22 (Reuters) - ChatGPT-maker OpenAI on Tuesday said it reached an agreement for ...Nov 22, 2023 ... https://www.bloomberg.com/news/articles/2023-11-22/sam-altman-to-return-as-openai-ceo-with-a-new-board What are your thoughts?6 days ago ... Who is Bret Taylor? New OpenAI Board Member is Former Salesforce CEO · Bret Taylor is a 43-year-old American computer programmer, entrepreneur ...The new initial board included former Salesforce co-CEO Bret Taylor as chairman. OpenAI also announced that Microsoft would have a non-voting board seat at ...Nov 22, 2023 ... The company that developed ChatGPT has added new members to its board as it welcomes Sam Altman back as CEO · Sam Altman is the value in OpenAI: ...
Action: Response To Human
Action Input: "The new board members of OpenAI include Bret Taylor, who is serving as the Chair, Larry Summers, and Adam D'Angelo."
Response: The new board members of OpenAI include Bret Taylor, who is serving as the Chair, Larry Summers, and Adam D'Angelo.
4、结束语
这就是基于本文构建的LLM代理的基本思想。 与 Langchain 和 Llamaindex 代理相比,输出非常好。
原文链接:How to Create your own LLM Agent from Scratch: A Step-by-Step Guide
BimAnt翻译整理,转载请标明出处