Building LangChain Agents With LangFlow

LLM based Agents maintain autonomy by accessing LLMs and Tools at its disposal.

Cobus Greyling
5 min readMay 8, 2023

--

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.

More About Agents

LangChain Agents are autonomous within the context of a suite of available tools. Now you can build LangChain agents in a GUI by making use of LangFlow.

The LangChain Agent utilises a variety of Actions when receiving a request. After taking an Action, the Agent enters the Observation step, where they share a Thought. If a Final Answer is not reached, the Agent cycles back to choose a different Action in order to move closer to the Final Answer.

Agents are attractive due to their ability to act independently and not follow a predetermined path.

They are equipped with a set of tools, allowing them to respond to any request that falls within the scope of these tools.

This execution pipeline enables the Agent to independently work through the problem, with potentially multiple iterations, until the desired result is achieved.

Building A Simple Agent Using LangFlow

Building a LangChain Agent via a pro-code approach can seem daunting and abstract at first. However, LangFlow demystifies this process to a large degree.

Below you can see the six components used to build the Agent.

The ZeroShotPrompt component holds the prompt template, which is discussed in detail later in this article.

The OpenAI component holds model name, Temperature setting and the API key.

The LLM Chain component merely connects the Prompt and the LLM with the Agent. The agent has two tools which it can make use of in this instance, PAL-MATH and Search.

In this example, you can see an ambiguous and complex question is posed to the agent: What is the square root of the birth year of the father of the iPhone? .

The answer is given, and clicking on the chat bubble, displays the agent’s chain-of-thought reasoning.

For a detailed high resolution video of the flow, click here.

The chain-of thought reasoning in detail:

> Entering new AgentExecutor chain...
I need to figure out the father of the iPhone and the year of his birth.
Action: Search
Action Input: "Father of the iphone"
Observation: Steve Jobs ; Pioneer of the personal computer revolution with Steve Wozniak; Co-creator of the Apple II, Macintosh, iPod, iPhone, iPad, and first Apple Stores.
Thought: I now need to find the year of birth of Steve Jobs
Action: Search
Action Input: "Steve Jobs birth year"
Observation: February 24, 1955
Thought: I now need to find the square root of 1955
Action: PAL-MATH
Action Input: "What is the square root of 1955?"
Observation: name 'math' is not defined
Thought: I need to use a different language model
Action: PAL-MATH
Action Input: "What is the square root of one thousand nine hundred and fifty-five?"
Observation: 44.21538193886829
Thought: I now know the final answer
Final Answer: The square root of the year of birth of the father of the iphone is 44.21538193886829.

> Finished chain.

The prompt template used in the Prompt component, with the prefix, suffix and instructions.

Prompt Prefix:

Answer the following questions as best you can. You have access to the following tools:

Prompt Suffix:

Begin!

Question: {input}
Thought:{agent_scratchpad}

Prompt Instructions:

Use the following format:

Question: the input question you must answer
Thought: you should always think about what to do
Action: the action to take, should be one of [{tool_names}]
Action Input: the input to the action
Observation: the result of the action
... (this Thought/Action/Action Input/Observation can repeat N times)
Thought: I now know the final answer
Final Answer: the final answer to the original input question

Search is added as a tool via the SerpApi as shown below.

Source

In Closing

Agents are effective even in cases where the question is ambiguous and demands a multihop approach. This can be considered as an automated process of decomposing a complex question or instruction into a chain-of-thought process.

⭐️ 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.

https://www.linkedin.com/in/cobusgreyling
https://www.linkedin.com/in/cobusgreyling

--

--