跳转到主要内容

category

目前在LLM上开发的方法正在迅速发展,即时工程在某种程度上被链接和代理的概念所吸收。

在最近的一篇文章中,我写到了即时工程的演变,以及即时工程是如何被吸收到更大的开发结构中的。

这些更大的开发结构允许:

  1. 更长、更复杂的用户交互
  2. 进程可以串行或并行运行
  3. 提示可以编程、共享、存储和模板化
  4. 链接是创建提示流或提示序列的概念。

对于现有流退出并且可以创建预先确定的会话或工作流的情况,Chaining工作得很好。

另一方面,在流量未知或高度不可预测的情况下,链接并不能很好地服务于场景。在这些情况下,预定的流程将不能很好地工作,并且需要一定程度的自主权。

 

什么是代理?

代理可以通过使用其可支配的一组工具或资源来接收查询。这些工具可以包括访问维基百科、网络搜索、数学库、LLM等。

  • 链可以包含代理。
  • 代理还包含一个链。

但是,在代理的情况下,它是一系列步骤,这些步骤是在代理循环使用其可支配的工具来为请求提供服务时动态编译的。

LangChain有一个广泛的工具列表,可以将这些工具合并到代理创建的自组织链中。

下图显示了LangChain代理所遵循的事件序列:

在下面的笔记本摘录中,可以看到一个模棱两可的问题和事件的顺序。从进入链条、动作输入、观察、思考等。

另一个实际例子;考虑到下面的图片,这个笔记本是一个LangChain代理的,它可以访问网络搜索和GPT4来回答这个问题。

注意经纪人是如何进入一个链的,回答我提出的问题:SpaceX和特斯拉的创始人是什么年出生的,他创立的第一家公司叫什么名字?并将问题转化为行动。

特工进入搜索动作,搜索输入为:SpaceX和特斯拉的创始人出生年份和第一家公司。

这是基于代理接收到的初始输入的观察结果。特工有一个想法,最终的答案已经达成。链已经完成,并给出了回应:太空探索技术公司和特斯拉的创始人埃隆·马斯克出生于1971年。他创办的第一家公司是Zip2。

LangChain Agent example

以下是Langchain代理的完整工作代码,它可以执行:

  • 网络搜索
  • 数学
  • 并且可以访问GPT4

考虑到下面的代码,向代理提交了一个模棱两可的问题:

太空探索技术公司和特斯拉的创始人是哪一年出生的,他创立的第一家公司叫什么名字?

pip install langchain
pip install google-search-results
pip install openai

from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.agents import AgentType
from langchain.llms import OpenAI

import os
os.environ['OPENAI_API_KEY'] = str("xxxxxxxxxxxxxxxxxxx")
os.environ["SERPAPI_API_KEY"] = str("xxxxxxxxxxxxxxxxxxx")
llm = OpenAI(temperature=0,model_name='gpt-4-0314')

tools = load_tools(["serpapi", "llm-math"], llm=llm)

agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)

agent.run("What year was the founder of SpaceX and Tesla born and what is the name of the first company he founded?")

以及代理响应:

> Entering new AgentExecutor chain...
I need to search for the birth year of the founder of SpaceX and Tesla and the name of his first company.
Action: Search
Action Input: founder of SpaceX and Tesla birth year and first company
Observation: Elon Musk ; Born: June 28, 1971 (age 51) Pretoria South Africa ; Founder: PayPal SpaceX Zip2 ; Early life. Musk was born to a South African father and a Canadian ...
Thought:I now know the final answer
Final Answer: Elon Musk, the founder of SpaceX and Tesla, was born in 1971. The first company he founded was Zip2.

> Finished chain.
'Elon Musk, the founder of SpaceX and Tesla, was born in 1971. The first company he founded was Zip2.

此示例显示了代理有权访问的实例:

  • 维基百科
  • GPT4
pip install langchain
pip install wikipedia
pip install openai

from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.agents import AgentType
from langchain.llms import OpenAI

import os
os.environ['OPENAI_API_KEY'] = str("xxxxxxxxxxxxxxxxxxxxxxxxxxx")
llm = OpenAI(temperature=0,model_name='gpt-4-0314')

tools = load_tools(["wikipedia"], llm=llm)
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)

agent.run("What year was the founder of SpaceX and Tesla born and what is the name of the first company he founded?")

注意响应中的差异:


> Entering new AgentExecutor chain...
I need to find information about the founder of SpaceX and Tesla, including his birth year and the first company he founded.
Action: Wikipedia
Action Input: Elon Musk
Observation: Page: Elon Musk
Summary: Elon Reeve Musk  ( EE-lon; born June 28, 1971) is a business magnate and investor. He is the founder, CEO and chief engineer of SpaceX; angel investor, CEO and product architect of Tesla, Inc.; owner and CEO of Twitter; founder of the Boring Company; co-founder of Neuralink and OpenAI; and president of the philanthropic Musk Foundation. With an estimated net worth of around $192 billion as of March 27, 2023, primarily from his ownership stakes in Tesla and SpaceX, Musk is the second-wealthiest person in the world, according to both the Bloomberg Billionaires Index and Forbes's real-time billionaires list.Musk was born in Pretoria, South Africa, and briefly attended at the University of Pretoria before moving to Canada at age 18, acquiring citizenship through his Canadian-born mother. Two years later, he matriculated at Queen's University and transferred to the University of Pennsylvania, where he received bachelor's degrees in economics and physics. He moved to California in 1995 to attend Stanford University. After two days, he dropped out and, with his brother Kimbal, co-founded the online city guide software company Zip2. In 1999, Zip2 was acquired by Compaq for $307 million and Musk co-founded X.com, a direct bank. X.com merged with Confinity in 2000 to form PayPal, which eBay acquired for $1.5 billion in 2002. Musk received an EB-5 investor green card in 1997, which led to his U.S. citizenship in 2002.With $175.8 million, Musk founded SpaceX in 2002, a spaceflight services company. In 2004, he was an early investor in the electric vehicle manufacturer Tesla Motors, Inc. (Tesla, Inc.). He became its chairman and product architect, assuming the position of CEO in 2008. In 2006, he helped create SolarCity, a solar energy company that was later acquired by Tesla and became Tesla Energy. In 2015, he co-founded OpenAI, a nonprofit artificial intelligence research company. The following year, he co-founded Neuralink—a neurotechnology company developing brain–computer interfaces—and the Boring Company, a tunnel construction company. Musk has also proposed a hyperloop high-speed vactrain transportation system. In 2022, his acquisition of Twitter for $44 billion was completed. 
Musk has made controversial statements on politics and technology, particularly on Twitter, and is a polarizing figure. He has been criticized for making unscientific and misleading statements, including spreading COVID-19 misinformation. In 2018, the U.S. Securities and Exchange Commission (SEC) sued Musk for falsely tweeting that he had secured funding for a private takeover of Tesla. Musk stepped down as chairman of Tesla and paid a $20 million fine as part of a settlement agreement with the SEC.

Page: Acquisition of Twitter by Elon Musk
Summary: Business magnate Elon Musk initiated an acquisition of American social media company Twitter, Inc. on April 14, 2022, and concluded it on October 27, 2022. Musk had begun buying shares of the company in January 2022, becoming its largest shareholder by April with a 9.1 percent ownership stake. Twitter invited Musk to join its board of directors, an offer he initially accepted before declining. On April 14, Musk made an unsolicited offer to purchase the company, to which Twitter's board initially responded with a "poison pill" strategy to resist a hostile takeover, before unanimously accepting Musk's buyout offer of $44 billion on April 25. Musk stated that he planned to introduce new features to the platform, make its algorithms open-source, combat spambot accounts, and promote free speech.
In July, Musk announced his intention to terminate the agreement, asserting that Twitter had breached their agreement by refusing to crack down on spambot accounts. The company filed a lawsuit against Musk in the Delaware Court of Chancery shortly thereafter, with a trial scheduled for the week of October 17. Weeks before the trial was set to begin, Musk reversed course, announcing that he would move forward with the acquisition. The deal was closed on October 27, with Musk immediately becoming Twitter's new owner and CEO. Twitter was taken private and merged into a new parent company named X Corp. Upon acquiring Twitter, Musk promptly fired several top executives, including previous CEO Parag Agrawal. Musk has since proposed several reforms to Twitter and laid off half of the company's workforce. Hundreds of employees then resigned from the company after Musk issued an ultimatum demanding they commit to "extremely hardcore" work.
Reactions to the buyout have been mixed, with praise for Musk's planned reforms and vision for the company, particularly his calls for greater free speech, but criticism over fears of a potential rise in misinformation, disinformation, harassment, and hate speech on the platform. Within the United States, conservatives have largely supported the acquisition, while many liberals and former Twitter employees have voiced concerns about Musk's intentions. Since becoming owner, Musk has faced backlash for his handling of the company and account suspensions.

Page: Views of Elon Musk
Summary: Elon Musk, one of the richest men in the world and the CEO of Tesla, has expressed many views on a wide variety of subjects, ranging from politics to science.
Within the context of American politics, Musk has described himself as being politically moderate. Musk supported George W. Bush in 2004, Barack Obama in 2008 and 2012, Hillary Clinton in 2016, and Joe Biden in 2020. In 2022, Musk said he would start voting for Republican Party candidates. He has stated support for universal basic income, gun rights, freedom of speech, a tax on carbon emissions and opposes government subsidies.Musk is also a prominent critic of short-selling. Musk has expressed concern about issues such as artificial intelligence, climate change and population decline. He has also criticized COVID-19 lockdowns, public transportation, and labor unions.
Thought:I now know the final answer.
Final Answer: Elon Musk, the founder of SpaceX and Tesla, was born on June 28, 1971. The first company he founded was Zip2, which he co-founded with his brother Kimbal in 1995.

> Finished chain.
'Elon Musk, the founder of SpaceX and Tesla, was born on June 28, 1971. The first company he founded was Zip2, which he co-founded with his brother Kimbal in 1995.

最后

链和代理可以互换使用。代理人最有价值的组成部分是:

  1. 默认情况下,执行思维链推理和提示的能力。
  2. 选择和排序适当工具的自主性水平。
  3. 由代理保持的逻辑水平以及达到答案的清晰度。
  4. 代理的上下文窗口非常重要,这可以从代码中使用的问题中看出。这些问题都是隐晦的、模棱两可的、需求链推理和多跳方法。

 

文章链接