Agents & Large Language Models
The current methods of developing on LLMs are evolving rapidly, and prompt engineering is being absorbed to some degree by the concepts of chaining and agents.
In a recent post I wrote about the evolution of prompt engineering, and how prompt engineering is being absorbed into bigger development structures.
These bigger development structures allow for:
- Longer and more complex user interactions
- Processes can be run series or parallel
- Prompts can be programmed, shared, stored and templated
- Chaining is the notion of creating a flow or sequence of prompts.
Chaining works well for instances where existing flows exit and predetermined conversational or work flows can be created.
On the other hand, chaining does not serve scenarios well where the flow is unknown or highly unpredictable. In these instances a predetermined flow will not work well and a level of autonomy is required.
⭐️ Please follow me on LinkedIn for updates on Conversational AI ⭐️
What are Agents?
Agents can receive a query by making use of a set of tools or resources at its disposal. These tools can include access to Wikipedia, web search, mathematical libraries, LLMs, etc.
Chains can contain agents.
Agents also contain a chain.
But in the case of an Agent, it is a sequence of steps which are compiled on-the-fly as the agent cycles through the tools at its disposal to service the request.
LangChain has an extensive list of tools which can be incorporated into an ad-hoc chain created by the agent.
The image below shows the sequence of events followed by the LangChain agent:
And in the notebook extract below an ambiguous question can be seen with the sequence of events. From entering the chain, action input, observation, thought, etc.
Another practical example; considering the image below, this notebook snipped is of a LangChain agent which has access to web search and GPT4 to answer the question.
Notice how the agent enters a chain, takes the question posed by me: What year was the founder of SpaceX and Tesla born and what is the name of the first company he founded?
And converts the question into an action.
The agent access an action of search, with the search input: fonder of SpaceX and Tesla birth year and first company.
This is an observation based on the initial input the agent receives. The agent has a Thought
, that the final answer has been reached. The chain is completed and the response is given: Elon Musk, the founder of SpaceX and Tesla, was born in 1971. The first company he founded was Zip2
.
Below is the full working code for a Langchain Agent which can perform:
- Web search
- Math
- And has access to GPT4
Considering the code below, an ambiguous question is submitted to the agent:
What year was the founder of SpaceX and Tesla born and what is the name of the first company he founded?
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?")
And the agent response:
> 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.
This example shows an instance were the agent has access to:
- Wikipedia
- 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?")
Notice the difference in the response:
> 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.
And Finally
Chains and Agents can be used interchangeably. The most valuable component of agents are:
- The ability to execute, by default, chain-of-thought reasoning & prompting.
- The level of autonomy with which the appropriate tools are selected and sequenced.
- The level of logic which is maintained by the agent and the clarity with which an answer is reached.
- The context window of agents are quite significant, as can be seen in the questions used in the code. These questions are all cryptic & ambiguous, demand chain-of-thought reasoning and a multi-hop approach.
⭐️ Please follow me on LinkedIn for updates on Conversational AI ⭐️
I’m currently the Chief Evangelist @ HumanFirst. I explore and write about all things at the intersection of AI and language; ranging from LLMs, Chatbots, Voicebots, Development Frameworks, Data-Centric latent spaces and more.