Introducing LangChain Agents

An implementation with Azure OpenAI and Python

Valentina Alto
Microsoft Azure
Published in
8 min readMay 14

Large Language Models (LLMs) like GPT-3.5-turbo (the model behind ChatGPT) and GPT-4 have been proving their generative power in last few months.

However, as they are today they suffer from a limitation: they are limited to the knowledge on which they have been trained and the additional knowledge provided as context; as a result, if a useful piece of information is missing the provided knowledge, the model cannot “go around” and try to find it in other sources.

This is the reason why we need to introduce the concept of Agents. Agents can be seen as applications powered by LLMs and integrated with a set of tools like search engines, databases, websites, and so on. Within an agent, the LLM is the reasoning engine that, based on the user input, is able to plan and execute a set of actions that are needed to fulfill the request.

The concept of reasoning and acting is also the basis of a new prompt engineering technique called ReAct (Reason and Act), introduced by Yao et al (you can read the full paper here)

In this article, we are going to see an implementation of an Agent powered by Azure OpenAI chat models. To do so, we will use LangChain, a powerful lightweight SDK which makes it easier to integrate and manage LLMs within applications.

Building your first Agent

According to LangChain’s documentation, there are two main types of Agents we can build, which also corresponds to two different types of prompt engineering techniques:

  • Action Agent →this type of agent decide an action and then executes it one step at a time. For example, given user input, the agent will reason that it will probably find the required information within Wikipedia, hence it will search for that site; then, if the information required is not found in the parsed site, it will decide to parse another site and so on, until it finds the required information to generate a response. This approach of retrieving documents, reading, and deciding whether retrieve more documents is called Retrieve-Read-Retrieve;
  • Plan and Execute Agent → this type of agent follows a more sophisticated approach that is useful for complex…

--

--

Valentina Alto
Microsoft Azure

Data&AI Specialist at @Microsoft | MSc in Data Science | AI, Machine Learning and Running enthusiast