如何从头开始创建自己的LLM代理:分步指南

2023年11月29日 由 alex 发表 374 0

在本文中,我们将使用Python从零开始构建一个LLM代理。不使用Langchain,不使用Llama Index等。


1


什么是代理?


LLM代理是使用大型语言模型来决定如何以及何时使用工具来完成任务的程序。


大型语言模型是训练有素的巨大神经网络,它们基于大量的文本数据。它们可以生成文本、翻译语言、创作不同类型的创意内容,并以有益的方式回答你的问题,但不适用于执行任务。


代理的核心思想是使用语言模型来选择要采取的一系列动作。在链条中,一系列动作是硬编码(编入代码)的。在代理中,使用语言模型作为推理引擎来确定哪些动作要采取,以及采取的顺序。


简单来说,LLM就像是汽车的引擎,而LLM代理就像是一辆有了额外功能、适用于现实世界任务的汽车本身。


代理和工具


要使用代理,我们需要三样东西:


  • 基础LLM,
  • 我们将要互动的工具,
  • 控制交互的代理。


2


代理实现


!pip install -q openai langchain py_expression_eval google-api-python-client


from openai import OpenAI
from langchain.utilities import GoogleSearchAPIWrapper
from langchain.tools import Tool
from py_expression_eval import Parser
import re, time, os


  • 根据这些指导建立一个自定义搜索引擎和谷歌API密钥。
  • 从上一步获取API密钥和自定义搜索引擎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"


工具


搜索:使用谷歌官方的自定义搜索引擎进行搜索引擎查询。在免费层中,你每天可以发送100次请求。这里,我们使用LangChain谷歌搜索引擎工具作为演示。


计算器:我使用py_expression_eval作为计算器(在能够运行复杂的数学表达式与避免引入完整的Python REPL/eval的风险之间有一个很好的平衡)。


我们为演示使用了两种工具。根据你的需求,你可以使用任何你想要的工具。


#Calculator
parser = Parser()
def calculator(str):
    return parser.parse(str).evaluate({})
#Google search engine
search = GoogleSearchAPIWrapper()
goog = Tool(
    name="Google Search",
    description="Search Google for recent results.",
    func=search.run
    )
def search(str):
    return goog(str)


首先,让我们设置系统提示。


System_prompt = """
Answer the following questions and obey the following commands as best you can.
You have access to the following tools:
Search: 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,
            max_tokens=1000,
            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 current CEO of OpenAI")


Output
Thought: I need to find out who the current CEO of OpenAI is.
Action: Search
Action Input: "current CEO of OpenAI"
Observation:  2 days ago ... Mira Murati: Who is the new OpenAI interim CEO? Murati now heads the company ... Now, Musk can say he also used to employ OpenAI's current CEO. ^ Mok, Aaron (September 25, 2023). "OpenAI CEO Sam Altman says he worked so hard on building his first startup with his ex-boyfriend that he got scurvy". 2 days ago ... Chief technology officer Mira Murati appointed interim CEO to lead OpenAI; Sam Altman departs the company. CEO and co-founder: Sam Altman, former president of the startup accelerator Y Combinator (2015–2023); President and co-founder: Greg Brockman, former CTO, 3rd ... OpenAI is an AI research and deployment company. Our mission is to ensure that artificial general intelligence benefits all of humanity. Jun 21, 2023 ... You ever watch Star Trek?” Sam Altman, the CEO who has become the most visible face of the current artificial-intelligence boom, has just called ... the quality of airbnb product launches sets the current high water mark in the tech industry: ... i am pro-regulation on frontier systems, which is what openai ... Mar 16, 2023 ... OpenAI CEO Sam Altman says AI will reshape society, acknowledges risks ... This feature is currently only accessible to a small set of users ... 1 day ago ... For years, Mira Murati has worked behind the scenes at OpenAI, overseeing the development and delivery of revolutionary products such as ... Mar 19, 2023 ... Sam Altman, CEO of OpenAI, the co that created ChatGPT, believes that artificial intelligence technology will reshape society as we know it, ...
Thought: The search results indicate that Mira Murati is the current interim CEO of OpenAI, taking over from Sam Altman.
Action: Response To Human
Action Input: "The current interim CEO of OpenAI is Mira Murati, who took over from Sam Altman."
Response: The current interim CEO of OpenAI is Mira Murati, who took over from Sam Altman.


结论


这是基于本文构建的一个LLM代理的基本概念。与Langchain和Llamaindex代理相比,其输出的结果非常好。


文章来源:https://medium.com/@gathnex/how-to-create-your-own-llm-agent-from-scratch-a-step-by-step-guide-14b763e5b3b8
欢迎关注ATYUN官方公众号
商务合作及内容投稿请联系邮箱:bd@atyun.com
评论 登录
热门职位
Maluuba
20000~40000/月
Cisco
25000~30000/月 深圳市
PilotAILabs
30000~60000/年 深圳市
写评论取消
回复取消