Introduction
In this lesson, we’ll recap the concepts of tools and agents in LangChain and its main applications. We’ll also learn about LangChain OpenGPTs, an open-source effort to create AI assistants similar to the OpenAI GPTs.
Agents
LangChain agents integrate chains, prompts, memory, and tools to complete tasks.
Agents can be used to perform a wide range of tasks, from executing a series of steps in a specific order to interacting with external systems like Gmail or SQL databases and more.
They can be customized to fit a variety of use cases, and LangChain provides a suite of tools and functionalities to facilitate the process.
Let’s review the relevant LangChain concepts:
- Chain: A sequential application of models or tools, such as passing a prompt to an LLM and then parsing it.
- Tool: In LangChain, a tool is a function that performs a specific task that an LLM can leverage to get relevant information for its task completion. It can be a Google Search, a Database lookup, a Python REPL, or other chains.
- Memory: Memory keeps track of past interactions with an LLM to be used as context for the next interactions.
Agent types
LangChain has several agent types. Find a comprehensive list below.
- Zero-shot ReAct: It leverages the ReAct framework to make tool decisions based exclusively on the descriptions of the available tools. It’s called “zero-shot” because it only leverages the tool descriptions without using specific examples.
- Structured Input ReAct: Optimized for handling tools that require multiple inputs.
- OpenAI Functions Agent: Tailored for models specifically fine-tuned for function calls, this agent is compatible with advanced models like
gpt-3.5-turbo
andgpt-4
. - Conversational Agent: Focuses on conversational contexts, utilizing ReAct for tool selection and incorporating memory to recall past interactions.
- Self-Ask with Search Agent: Centering around the "Intermediate Answer" tool, it excels at finding factual answers to queries, mirroring the approach in the original self-ask with search study.
- ReAct Document Store Agent: This agent requires "Search" and "Lookup" tools, simultaneously using both features while providing a stream of thoughts.
LangChain’s agents essentially provide the ‘reasoning’ behind the choice of the to-do action, deciding whether to involve multiple tools, just one, or none at all in the process.
Plan-and-Execute Agents
The plan-and-execute agents first make a plan with multiple actions and then execute each action sequentially. They are more suited for complex or long-running tasks as they maintain focus on long-term objectives and focus. However, they may lead to more latency.
Available tools and custom tools
You can find the list of tools that integrate LangChain with other tools here.
Some examples are:
- The Python tool: It’s used to execute generated Python code to answer a question.
- The JSON tool: It’s used when interacting with a JSON blob that is too large to fit in the LLM context window.
- The CSV tool: It’s used to interact with CSV files.
Custom tools extend the capabilities of agents, making them adaptable to a wide range of specialized tasks and interactions.
Custom tools provide task-specific functionality and flexibility for precise behaviors tailored to unique use cases.
The level of customization depends on the creation of advanced interactions, where tools can be orchestrated to perform complex behaviors, such as generating questions, searching the web for answers, and summarizing the gathered information.
LangChain OpenGPT
LangChain OpenGPT is an open-source effort to create an experience similar to OpenAI’s assistants and GPTs.
In contrast to OpenAI GPTs, LangChain OpenGPT allows you to configure the LLM used, the tools, the vector DB, the retrieval algorithm, and the chat history DB.
Let’s see how to use them:
- Clone the Repository:
To interact with Langchain's OpenGPTs, follow these steps detailed in their GitHub repository. The easiest way to launch OpenGPTs locally is through Docker and “docker compose”.
First, clone the repo locally and cd
into it.
git clone https://github.com/langchain-ai/opengpts.git
cd opengpts
You should now see a .env
file with the following content.
OPENAI_API_KEY=placeholder
ANTHROPIC_API_KEY=placeholder
YDC_API_KEY=placeholder
TAVILY_API_KEY=placeholder
AZURE_OPENAI_DEPLOYMENT_NAME=placeholder
AZURE_OPENAI_API_KEY=placeholder
AZURE_OPENAI_API_BASE=placeholder
AZURE_OPENAI_API_VERSION=placeholder
By default, the app will be using the OpenAI models, so replace the placeholder
of OPENAI_API_KEY
with your OpenAI key.
You can now launch everything with the following command.
docker compose up
Now, visit http://localhost:8100/; you should see the following page.
Creating OpenGPTs
We are now ready to create our first OpenGPT!
To set a custom prompt in LangChain's OpenGPTs, you begin by defining a specific role or persona for the AI, like a 'Career Counselor'.
You guide the models' responses to fit the desired context and ensure that its advice, insights, or recommendations are aligned with the defined role.
This System Message
outlines the OpenGPTs' responsibilities, tone, and the type of interactions it should engage in:
You are a Career Counselor. Your role is to provide insightful and
personalized guidance as I navigate my professional path. Whether I'm
facing career uncertainties, seeking job advancement, or contemplating a
career shift, your expertise is aimed at offering constructive, individualized
advice that helps me make informed decisions.
Our sessions will be a platform for discussing my professional
aspirations, skills, and potential barriers.
In our interactions, I expect a supportive environment where I can share
my professional experiences, goals, and concerns. Your role is to motivate and
provide clear, practical strategies that align with my career objectives.
By understanding my unique circumstances, you offer tailored advice and plans
to aid my professional growth. This collaboration is crucial for my career
development, with your guidance being a cornerstone of my journey
towards achieving my career goals.
This prompt acts as a foundational script, directing the model's behavior to meet the specific needs of your application or service.
Click on “New Bot”, select GPT-3.5-Turbo (or another default that you want to use), name the bot “Career Counselor” and provide the System Message we created.
Click Save, and you are ready to chat!
Conclusion
In this lesson, we covered LangChain’s tools and agents, their applications, and the creation of LangChain OpenGPTs, an open-source initiative for AI assistants. We explored agent types, including Zero-shot ReAct and Conversational Agents, along with plan-and-execute agents. We discussed the integration of various tools like Python and CSV and the customization of these tools. Finally, we set up and used LangChain OpenGPTs, emphasizing their reconfigurability and role in facilitating AI interactions.
RESOURCES:
- OpenGPTs
- tools docs