• Try YugabyteDB Managed Free Cloud database managed by us
  • Download YugabyteDB Free open source database
  • Book a Demo Personalized demo

Building an Autonomous AI Agent with LangChain and PostgreSQL pgvector

Brett Hoyer

Brett Hoyer

May 27, 2024

In this blog, we’ll create a fully autonomous AI travel agent, capable of finding listings and managing bookings. This OpenAI tools agent uses LangChain, OpenAI, PostgreSQL and pgvector , with YugabyteDB as the underlying database.

Why You Should Use an AI Agent

AI agents have knowledge of the environment in which they operate, which allows them to perform specific tasks to achieve their goals. By providing an agent with custom tools, we can build a system that is robust within a particular domain.

Large language models (LLMs) are able to provide text outputs from a message prompt, by predicting the next word or sentence based on those preceding. However, an LLM’s knowledge base is limited to what it was trained on. An LLM cannot inherently answer prompts that require up-to-date or proprietary information. However, by providing an LLM with tools that can interface with active data sources, through the use of an AI agent, new goals can be achieved.

Building Blocks

Before diving into the code, let’s first outline the role that each component plays in creating an effective agent.

LangChain: A framework for constructing AI applications, with the ability to create sequential chains of actions, or in the case of agents, autonomous actions with ordering based on logical reasoning. LangChain provides multiple agent types based on application needs.

OpenAI: The LLM of choice, with many models to choose from based on the application’s needs. These models can be used to generate responses to prompts, and also to create text embeddings to perform queries, such as similarity search.

PostgreSQL: The general-purpose relational database for a wide array of applications, equipped with extensions for storing and querying vector embeddings in AI applications.

pgvector: A PostgreSQL extension for handling vector similarity search.

Tools: Interfaces that the agent can use to perform specific tasks. For instance, a weather tool could provide the agent with the ability to fetch the current weather forecast.

Foundational Components

Now that we’ve covered the foundational components, let’s begin building our autonomous agent.

Creating an AI Agent with Internet Search Functionality

We begin by walking through the steps required to create our first AI agent in LangChain. Many LLMs are supported, such as those by Google, Anthropic, and Mistral, but we’ll default to using OpenAI in this example.

  • Install dependencies. pip install -qU langchain-openai
  • Create an OpenAI API Key and store it as an environment variable. OPENAI_API_KEY=sk-...
  • Select an OpenAI model to use with the ChatOpenAI interface. This interface provides user-friendly methods for building chatbot related applications. from langchain_openai import ChatOpenAI llm = ChatOpenAI(model="gpt-3.5-turbo-0125")
  • Create a tool for searching the web from the agent. Tavily is a search engine optimized for use in AI applications. from langchain_community.tools.tavily_search import TavilySearchResults tools = [TavilySearchResults(max_results=1)]
  • Create and run the agent. from langchain.agents import AgentExecutor, create_tool_calling_agent from langchain_core.prompts import ChatPromptTemplate prompt = ChatPromptTemplate.from_messages( [ ( "system", "You are a helpful assistant. Make sure to use the tavily_search_results_json tool for information.", ), ("placeholder", "{chat_history}"), ("human", "{input}"), ("placeholder", "{agent_scratchpad}"), ] ) # Construct the Tools agent agent = create_tool_calling_agent(llm, tools, prompt) # Create an agent executor by passing in the agent and tools agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True) agent_executor.invoke({"input": "What is the weather in San Francisco?"}) # Output > Entering new AgentExecutor chain... Invoking: `tavily_search_results_json` with `{'query': 'weather in San Francisco'}` [{'url': 'https://www.weatherapi.com/', 'content': "{'location': {'name': 'San Francisco', 'region': 'California', 'country': 'United States of America', 'lat': 37.78, 'lon': -122.42, 'tz_id': 'America/Los_Angeles', 'localtime_epoch': 1713832009, 'localtime': '2024-04-22 17:26'}, 'current': {'last_updated_epoch': 1713831300, 'last_updated': '2024-04-22 17:15', 'temp_c': 17.8, 'temp_f': 64.0, 'is_day': 1, 'condition': {'text': 'Partly cloudy', 'icon': '//cdn.weatherapi.com/weather/64x64/day/116.png', 'code': 1003}, 'wind_mph': 12.5, 'wind_kph': 20.2, 'wind_degree': 320, 'wind_dir': 'NW', 'pressure_mb': 1010.0, 'pressure_in': 29.81, 'precip_mm': 0.0, 'precip_in': 0.0, 'humidity': 70, 'cloud': 25, 'feelslike_c': 17.8, 'feelslike_f': 64.0, 'vis_km': 16.0, 'vis_miles': 9.0, 'uv': 5.0, 'gust_mph': 17.8, 'gust_kph': 28.7}}"}]The current weather in San Francisco is partly cloudy with a temperature of 64.0°F (17.8°C). The wind speed is 20.2 km/h coming from the northwest direction. The humidity is at 70%, and the visibility is 9.0 miles. > Finished chain.

With the addition of the TavilySearchResults tool, the AI agent is able to access the internet to retrieve up-to-date information, rather than simply relying on the knowledge base on which the LLM was trained.

This simple example displays the power of enhancing core LLM chat functionality with additional tools. Now, let’s examine how we can set up a PostgreSQL database to further enhance our AI agent’s functionality for use in a sample travel booking application.

Vector Embeddings in PostgreSQL

LangChain can be used to query SQL databases directly, by gaining knowledge of the database schema and executing the appropriate queries to respond to a particular prompt. However, this functionality can be extended with the use of pgvector and vector embeddings in PostgreSQL.

For instance, when building a travel booking application, the listing_description_embeddings column of the listings table could hold a vector representation of the listing description. This column could then be used for similarity search.

Here’s how the pgvector extension can be used in PostgreSQL.

  • Install the extension. CREATE EXTENSION IF NOT EXISTS vector;
  • Include a column of type vector in the database schema. The number of dimensions varies based on the model. OpenAI’s text-embedding-3-small model contains 1536 dimensions. CREATE TABLE listings ( id SERIAL PRIMARY KEY, listing_name varchar(255), listing_description text, price DECIMAL(10,2), neighborhood varchar(255), listing_description_embeddings vector (1536), );
  • Generate text embeddings for each listing description. from langchain_openai import OpenAIEmbeddings embeddings = OpenAIEmbeddings(model="text-embedding-3-small") cursor.execute("SELECT listing_description from listings") listings = cursor.fetchall() for listing in listings: listing_description = listing[0] # OpenAI's model will return a vector array with 1536 dimensions to be added to the database as embeddings text_embeddings = embeddings.embed_query(listing_description)
  • Add records to the database. query = "INSERT INTO listings(listing_name, listing_description, price, neighborhood, listing_description_embeddings) VALUES (%s, %s, %s, %s, %s)" cursor.execute(query, (listing_name, listing_description, price, neighborhood, text_embeddings, )) db_connection.commit()
  • Test the database using similarity search. In this example, we order results using the cosine distance between vectors (text embeddings). SELECT * FROM listings ORDER BY listing_description_embeddings <=> '[3,1,2,...]' LIMIT 5;

After generating embeddings and storing the appropriate data, we’re ready to create a tool to query our PostgreSQL database.

Adding Tools for Database Interaction

There are many ways to connect to a SQL database from LangChain . For direct access, users can access the LangChain SQL database toolkit, which provides functionality to ingest the database schema, query the database, and recover from any potential errors. However, this toolkit is in active development and does come with some issues. For instance, do we really want to give a SQL agent direct access to create and delete records in our database?

An alternative approach is to create custom tools for database interaction. These tools will handle specific tasks and can even make calls to external services to further abstract the agent from our data, while providing the same end-user experience.

Querying the Database with an AI Agent

Here’s an example of how we can augment our existing agent to interact with the listings table.

  • Create a new tool which calls this API service. from langchain.tools import StructuredTool class GetListingsInput(BaseModel): data: object = Field(description="has two keys, 'query_params' and 'embedding_text'") def get_listings(data): """this function makes an API call to a REST server to search listings. """ # The URL for the API endpoint url = 'http://localhost:8000/api/listings' # Making a POST request to the Flask API response = requests.post(url, json=data) if response.status_code == 200: data = response.json() return data else: print("Failed to retrieve data from API") get_listings_tool = StructuredTool.from_function( func=get_listings, name="GetListings", description="retrieves listings from an API call", args_schema=GetListingsInput ) tools = [ TavilySearchResults(max_results=1), get_listings_tool ]
  • Update the system message with more detailed instructions to include in the chat prompt. formatted_system_message = """You are a friendly travel agent. You are helping customers book travel accomodations. Below is the database schema: CREATE TABLE listings ( id SERIAL PRIMARY KEY, listing_name varchar(255), listing_description text, bedrooms NUMERIC(3,1), price NUMERIC(10,2), neighborhood varchar(255), listing_description_embeddings vector (1536), ); If a user asks to find listings on their behalf, use the get_listings_tool and respond accordingly. Return results in JSON format with 'summary' and 'results_to_display' keys. """ prompt = ChatPromptTemplate.from_messages([ SystemMessage(formatted_system_message), MessagesPlaceholder(variable_name="chat_history", optional=True), ("human", "{input}"), MessagesPlaceholder(variable_name="agent_scratchpad") ])
  • Execute the agent to verify the database is being queried from the API server to respond appropriately. Here, we can see that the agent invokes the GetListings tool, which responds with relevant listings based on the user prompt. agent_executor.invoke({"input": "Find listings in The Mission District with enough space for a large family."}) > Entering new AgentExecutor chain... Invoking: `GetListings` with `{'data': {'query_params': {'neighbourhood': {'value': 'Mission', 'type': 'text'}, 'bedrooms': {'value': 3, 'type': 'number', 'symbol': '>='}}, 'embedding_text': 'spacious place for a family getaway.'}}` {'data': '[{"listing_id": 26638684, "name": "Big Bright House Sleeps 9 W/D BBQ Parking", "description": "Cheerful and warm, this spacious two-story house features large common areas with loads of space and privacy, and is ideal for groups and families. Sleeps nine people with lots of elbow-room indoors and out. Enjoy a BBQ and a ping-pong game or two in the large, private back yard. Easy street parking with one spot in front guaranteed. Lovely, quiet neighborhood. Nearby parks, dining, retail and public transit. Welcome home to generous rooms filled with light and a formal floor plan which allows for privacy. Features include a living room with fireplace, piano and cityscape views; fully equipped chef\'\'s style kitchen that opens to dining room with seating for ten; three full bathrooms with tubs and showers; and private backyard with BBQ grill, plenty of seating, and ping pong table. Sofabed available to allow to sleep nine people total. The Main level includes bedroom with three twin beds and adjacent full bath, plus the master suite with a king-sized bed and full bath. The Lower level f", "price": "$250.00 ", "neighbourhood": "Mission Terrace"}, {"listing_id": 19872447, "name": "New building luxury 3BR/2BTH-near Mission St", "description": "My place is good for couples, solo adventurers, business travelers, families(with kids), and big groups. The building was construted in 2008, updated electrical and plumbing, fresh paint. lights fill in every rooms. you will have the 2nd floor for your own privacy.", "price": "$275.00 ", "neighbourhood": "Mission Terrace"}, ...]', 'status': 'this is the response from the get listings endpoint'} {"summary": "Here are the results I found. Can I help you with anything else?", "results_to_display": [{"listing_id": 26638684, "name": "Big Bright House Sleeps 9 ‚≠ê∂∏é W/D ‚≠ê∂∏é BBQ ‚≠ê∂∏é Parking", "description": "Cheerful and warm, this spacious two-story house features large common areas with loads of space and privacy, and is ideal for groups and families. Sleeps nine people with lots of elbow-room indoors and out. Enjoy a BBQ and a ping-pong game or two in the large, private back yard. Easy street parking with one spot in front guaranteed. Lovely, quiet neighborhood. Nearby parks, dining, retail and public transit. Welcome home to generous rooms filled with light and a formal floor plan which allows for privacy. Features include a living room with fireplace, piano and cityscape views; fully equipped chef's style kitchen that opens to dining room with seating for ten; three full bathrooms with tubs and showers; and private backyard with BBQ grill, plenty of seating, and ping pong table. Sofabed available to allow to sleep nine people total. The Main level includes bedroom with three twin beds and adjacent full bath, plus the master suite with a king-sized bed and full bath. The Lower level f", "price": "$250.00 ", "neighbourhood": "Mission Terrace"}, {"listing_id": 19872447, "name": "New building luxury 3BR/2BTH-near Mission St", "description": "My place is good for couples, solo adventurers, business travelers, families(with kids), and big groups. The building was construted in 2008, updated electrical and plumbing, fresh paint. lights fill in every rooms. you will have the 2nd floor for your own privacy.", "price": "$275.00 ", "neighbourhood": "Mission Terrace"}, ...]} > Finished chain.

After retrieving results from the API via the GetListings function, the AI Agent is able to autonomously synthesize the results and return them in the appropriate format defined in the system message.

Writing to the Database with an AI Agent

Let’s add another tool to autonomously create bookings on the user’s behalf.

The tool calls an API endpoint to insert records into the bookings table on behalf of the user.

Now, we can run the agent to create a booking.

After running the agent, we can verify that the booking was successfully written to the database.

This functionality can be augmented by equipping the agent with additional tools to edit and delete bookings, as well as a number of other tasks.

Scaling the Agent With Distributed PostgreSQL

We’ve discovered the power of building an AI agent backed by PostgreSQL. Now, let’s explore how we can leverage distributed SQL to make our applications more scalable and resilient.

Here are some key reasons that AI applications benefit from distributed PostgreSQL databases, such as YugabyteDB :

  • Embeddings consume a lot of storage and memory. For instance, an OpenAI model with 1536 dimensions takes up ~57GB of space for 10 million records. Scaling horizontally provides the space required to store vectors.
  • Vector similarity search is very compute-intensive. By scaling out to multiple nodes, applications have access to unbound CPU and GPU limits.
  • Service interruptions won’t be an issue. The database will be resilient to node, data center or regional outages, meaning AI applications will never experience downtime due to the database tier.

YugabyteDB is a distributed SQL database built on PostgreSQL. It’s feature and runtime compatible with Postgres, allowing you to reuse the libraries, drivers, tools and frameworks that were created for the standard version of Postgres.

YugabyteDB has pgvector compatibility, and provides all of the functionality found in native PostgreSQL. This makes it ideal for those looking to level-up their AI applications.

Here’s how you can run a 3-node YugabyteDB cluster locally in Docker.

  • Create a directory to store data locally. mkdir ~/yb_docker_data
  • Create a Docker network docker network create custom-network
  • Deploy a 3-node cluster to this network. docker run -d --name yugabytedb-node1 --net custom-network \ -p 15433:15433 -p 7001:7000 -p 9001:9000 -p 5433:5433 \ -v ~/yb_docker_data/node1:/home/yugabyte/yb_data --restart unless-stopped \ yugabytedb/yugabyte:latest \ bin/yugabyted start \ --base_dir=/home/yugabyte/yb_data --background=false docker run -d --name yugabytedb-node2 --net custom-network \ -p 15434:15433 -p 7002:7000 -p 9002:9000 -p 5434:5433 \ -v ~/yb_docker_data/node2:/home/yugabyte/yb_data --restart unless-stopped \ yugabytedb/yugabyte:latest \ bin/yugabyted start --join=yugabytedb-node1 \ --base_dir=/home/yugabyte/yb_data --background=false docker run -d --name yugabytedb-node3 --net custom-network \ -p 15435:15433 -p 7003:7000 -p 9003:9000 -p 5435:5433 \ -v ~/yb_docker_data/node3:/home/yugabyte/yb_data --restart unless-stopped \ yugabytedb/yugabyte:latest \ bin/yugabyted start --join=yugabytedb-node1 \ --base_dir=/home/yugabyte/yb_data --background=false

Local Cluster

With YugabyteDB running locally, we can now run the travel booking agent application, pointing to this database instead of native PostgreSQL.

By visiting the UI and interacting with the AI agent, we can verify that the switch to YugabyteDB was a success!

Get Listings

Building an AI agent backed by PostgreSQL is easy with the components now available in today’s ecosystem.

By using tools to either directly or indirectly interface with the database, we can build robust agents, capable of autonomously acting on our behalf. With the help of pgvector, it’s easy to store and query text embeddings for similarity search. Additionally, by moving our data to YugabyteDB, we can add scalability and resilience without sacrificing the familiar PostgreSQL syntax.

If you’d like to learn more about building AI applications with YugabyteDB, check out some of our AI tutorials .

If you’re interested in building this project on your own, check out the application on GitHub .

Related Posts

Simplifying documentation updates with ai-generated release notes.

Simplifying Documentation Updates with AI-Generated Release Notes

Three Database Predictions That Will Shape 2024

Three Database Predictions That Will Shape 2024

The Future of Money and Financial Services

The Future of Money and Financial Services

Explore Distributed SQL and YugabyteDB in Depth

DEV Community

DEV Community

TimeSurge Labs profile image

Posted on Aug 15, 2023 • Updated on Feb 19

How I Made an AI Agent in 10 Minutes with LangChain

LangChain is a powerful library for Python and Javascript/Typescript that allows you to quickly prototype large language model applications. It allows you to chain together LLM tasks (hence the name) and even allows you to run autonomous agents quickly and easily. In this blog post, we'll explore how to create agents and define custom tools that those agents can use.

Prerequisites

  • 3.10 and up have some issues with some of LangChain’s modules.
  • An OpenAI API Key

Getting Started

We’re going to create a Python virtual environment and install the dependencies that way.

Once that is done we can install dependencies. The only ones we need for this tutorial are LangChain and OpenAI. Finally, python-dotenv will be used to load the OpenAI API keys into the environment.

LangChain is a very large library so that may take a few minutes. While this is downloading, create a new file called .env and paste your API key in. Here is an example:

Once that is complete we can make our first chain!

Quick Concepts

  • Agent Type - This defines how the Agent acts and reacts to certain events and inputs. For this tutorial we will focus on the ReAct Agent Type.
  • LLM - The AI that actually runs your prompts.
  • Many tools make a Toolkit . There are many toolkits already available built-in to LangChain, but for this example we’ll make our own.

Agents use a combination of an LLM (or an LLM Chain) as well as a Toolkit in order to perform a predefined series of steps to accomplish a goal. For this example, we’ll create a couple of custom tools as well as LangChain’s provided DuckDuckGo search tool to create a research agent.

1. Importing Necessary Libraries

Here's a breakdown of the imports:

  • requests : A popular Python library for making HTTP requests.
  • BeautifulSoup : A library for web scraping purposes to pull the data out of HTML and XML files.
  • load_dotenv : A method to load environment variables from a .env file.
  • LangChain specific imports: These are specific to the LangChain framework and are used to define tools, prompts, chat models, chains, and agents.

2. Loading Environment Variables

This line loads environment variables from a .env file. This is useful if you have API keys or other sensitive information that you don't want to hard-code into your script.

3. Setting Up the DuckDuckGo Search Tool

This initializes the DuckDuckGo search tool provided by LangChain. It allows you to search the web using DuckDuckGo and retrieve the results.

4. Defining Headers for Web Requests

This sets a user-agent header for our web requests. Some websites might block requests that don't have a user-agent set, thinking they're from bots.

5. Parsing HTML Content

This function takes in HTML content, uses BeautifulSoup to parse it, and then extracts all the text from it.

6. Fetching Web Page Content

This function fetches the content of a web page using the requests library and then parses the HTML to extract the text.

7. Creating the Web Fetcher Tool

Here, we're creating a new tool using the Tool.from_function method. This tool will use our fetch_web_page function to fetch and parse web pages.

8. Setting Up the Summarizer

This section sets up a summarizer using the ChatOpenAI model from LangChain. We define a prompt template for summarization, create a chain using the model and the prompt, and then define a tool for summarization. We use ChatGPT 3, 5 16k context as most web pages will exceed the 4k context of ChatGPT 3.5.

9. Initializing the Agent

Here, we're initializing an agent with the tools we've defined. This agent will be able to search the web, fetch web pages, and summarize them. Notice how we can re-use the LLM from the summarize tool.

10. Running the Agent

Finally, we define a prompt for our agent and run it. The agent will search the web for information about the Python’s Requests Library, fetch the content fetch some of the content, and then summarize it.

Experimentation

In this section, we'll explore how to modify the code from the blog post draft to use the experimental Plan and Execute agent. The Plan and Execute agent accomplishes an objective by first planning what to do, then executing the sub-tasks. The planning is almost always done by an LLM, while the execution is usually done by a separate agent equipped with tools.

First, install the LangChain experimental package.

Then you can import the necessary modules from the langchain_experimental.plan_and_execute package:

Load the planner and executor:

Initialize the Plan and Execute agent:

Run the agent with a prompt:

In this example, the agent will first plan the steps needed to accomplish the objective, then execute the sub-tasks using the tools provided. The agent will search the web for information about the requests library in Python, fetch the content of the relevant results, and then summarize them into a guide.

Note that the Plan and Execute agent is experimental and may not work as expected in all cases. However, it can be a powerful tool for automating complex tasks that require multiple steps and interactions with external tools.

LangChain is a game-changer for anyone looking to quickly prototype large language model applications. In just a few minutes, we’ve walked through the process of creating agents, defining custom tools, and even experimenting with the experimental Plan and Execute agent to automate complex tasks.

The power of LangChain lies in its simplicity and flexibility. Whether you’re a seasoned developer or just starting out, LangChain’s intuitive design allows you to harness the capabilities of large language models like never before. From generating creative content to running autonomous agents, the possibilities are endless.

So why wait? Dive into LangChain today and unleash the potential of AI in your projects. If you’re looking to integrate AI into your existing workflow or products, TimeSurge Labs is here to help. Specializing in AI consulting, development, internal tooling, and LLM hosting, our team of passionate AI experts is dedicated to building the future of AI and helping your business thrive in this rapidly changing industry. Contact us today!

Top comments (0)

pic

Templates let you quickly answer FAQs or store snippets for re-use.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink .

Hide child comments as well

For further actions, you may consider blocking this person and/or reporting abuse

opensourcee profile image

Open AI with Vercel: Solution to Gateway Timeouts

OpenSource - May 31

the_greatbonnie profile image

How to build: a To-Do list app with an embedded AI copilot (Next.js, GPT4, & CopilotKit)

Bonnie - May 29

crusty0gphr profile image

Tricky Golang interview questions - Part 3: nil receivers

Harutyun Mardirossian - May 31

davidmezzetti profile image

RAG with llama.cpp and external API services

David Mezzetti - May 31

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Codementor Community

  • Data Engineering
  • Machine Learning
  • RESTful API
  • React Native
  • Elasticsearch
  • Ruby on Rails

Codementor Events

How to Build Your First AI Agent with LangChain and GPT-4.

How to Build Your First AI Agent with LangChain and GPT-4.

I'm really excited about the future of AI Agents.

The field is so new but we've already seen so many great projects, such as AutoGPT, HuggingGPT, MetaGPT, or Microsoft's AutoGen. And we're only starting!

After reading this article, you will:

  • understand the difference between the standard Large Language Models and AI Agents,
  • learn how AI Agents "reason & act" with the ReAct type of prompting,
  • know how to implement a basic AI Agent with LangChain for OpenAI language models

Are you ready?

What Are AI Agents in Plain English?

Let me explain it with an example.

Imagine you want to create a design with Canva but you don't know how to do it.

You go to ChatGPT and explain what you want to create. ChatGPT will give you instructions on:

  • how to sign up to Canva
  • how to create an empty design
  • how to add elements to the design
  • how to edit those elements, etc.

But you'll have do everything by yourself.

ChatGPT won't do anything for you in Canva, right?

It won't unless you're a ChatGPT Plus user and have access to the Canva Plugin!

Because now, you can explain ChatGPT what you need and it'll create several designs in Canva!

GPT-4 with the Canva plugin is an example of an AI Agent! Technically, GPT-4 with any plugin is an AI Agent.

So what's the difference?

GPT-4 with plugins has 2 things that standard ChatGPT is missing:

  • A toolkit (plugins).
  • The power to decide when and which tool to use.

So instead of asking ChatGPT "how to do XYZ," you tell ChatGPT "using the tools I give you, do XYZ."

So GPT-4 as an AI Agent will "do it for you."

But how do the Agents decide?

Let's talk about ReAct.

ReAct stands for "Reasoning & Acting."

It's a type of prompting in which Large Language Models work in a loop containing 3 steps:

  • Thought - deciding what to do.
  • Action - doing "it".
  • Observation - analyzing the results of the action from step 2.

Now, let's move to the project itself!

Step-by-Step Guide to Building Your First AI Agent

The idea for the project was to expose GPT-4 limitations and then fix them using GPT-4 as an AI agent.

So I came up the query: "Who is the NBA all-time leading scorer? What's his total points to the power of 0.42?"

The query contains questions that reveal 2 GPT-4 problems:

  • It has outdated information.
  • It's bad at math.

And the questions are easy to answer if you have access to Google Search and a calculator.

Let's find out how to arm GPT-4 with a practical toolkit!

Prerequisites

Before diving in, install these Python packages:

You will also need an OpenAI API key, which you can get on the  OpenAI Website  (you need to sign up first).

Then, copy the generated API Key to the  .env  file like this:

OPENAI_API_KEY=<Your-API-Key>

Replace  <Your-API-Key>  with the actual key.

Then, you need to get the SerpAPI API key, that you'll get on the SerpAPI Website .

SERPAPI_API_KEY=<Your-SerpAPI-Key>

Replace  <Your-SerpAPI-Key>  with the actual key.

Step 1: Load API Keys.

Let’s start by securely loading your OpenAI API key from a  .env  file.

Why do we need the API keys?

  • The OpenAI API key is required to use OpenAI’s models, such as GPT-4.
  • The SerpAPI API key is required to use Google Search.

Step 2: Testing the Standard GPT-4.

First, we need to initialize the model, and specify we want to use GPT-4:

Then, we ask our question:

Let's see what we get as the result:

Both answers are wrong!

As I mentioned, we're exposing 2 problems:

  • Outdated information . In 2023, LeBron James surpassed Kareem Abdul-Jabbar as the NBA all-time leading scorer.
  • Bad math . Kareem's total points to the power of 0.42 is approximately 84.2.

So let's fix the answers!

Step 3: Giving GPT-4 Tools: Google Search & Calculator.

AI Agents have tools and the power to use them.

So let's provide GPT-4 with some tools:

This code snippet contains several important parts:

  • Initializing the GPT-4 model.
  • Importing and initializing the SerpAPIWrapper
  • Creating the serp_tool and giving it a description on when it should be used.
  • Initializing the toolkit with llm-math for calculations.
  • Adding the SERP tool to the toolkit.

As a result, we defined 2 tools: Google Search and a calculator.

Step 4: Initializing the Agent.

It's time to initialize the AI Agent. Here's how:

We need the initialize_agent function with the following parameters:

  • Tools . We defined them in the previous step.
  • Model . Again, it's our GPT-4.
  • Agent Type . Here's where we define we want to use the ReAct framework.
  • We also set verbose=True to see every step of our ReAct chain.

Awesome! Time to play around!

Step 5: Testing our AI Agent.

To run the agent, we just need the run() function:

Let's analyze the Agent's ReAct process.

  • Thought 1 : I need to find out who the NBA all-time leading scorer is and how many total points he scored. I will use the Search tool to find this information.
  • Action 1 : Using Google to find the NBA all-time leading scorer and his total points.
  • Observation 1 : LeBron James points tracker: NBA's all-time top scorer closing in on 47,000 career points. Team USA's two-time Olympic gold medallist broke the career NBA regular season points scoring record held by Kareem Abdul-Jabbar. Basketball hero LeBron James is now the NBA's all-time top scorer.
  • Thought 2 : The NBA all-time leading scorer is LeBron James. But I need to find the exact total points scored by LeBron James. I will use the Search tool again to find this information.
  • Action 2 : Using Google to find LeBron's total points .
  • Observation 2 : 38,652
  • Thought 3 : LeBron James has scored a total of 38,652 points in the NBA. Now I need to calculate 38,652 to the power of 0.42. I will use the Calculator tool for this.
  • Action 3 : Using calculator to get the result of 38,652^0.42
  • Observation 3 : Answer: 84.45244506356971
  • Thought 4 : I now know the final answer. The NBA all-time leading scorer is LeBron James and 38,652 (his total points) to the power of 0.42 is approximately 84.45.
  • Final Answer : The NBA all-time leading scorer is LeBron James and 38,652 (his total points) to the power of 0.42 is approximately 84.45.

So the Agent went through 3 iterations.

I especially love how it improved the search query to get the exact points!

What's your favorite part?

Conclusions

It wasn’t too bad, was it? LangChain makes our lives easier.

AI Agents aren’t much different from the standard Large Language Models. We just give them some tools and let them decide what to do.

But this simple concept powers all the great projects I mentioned at the beginning of the article!

What else would you like to read about? Leave a comment ✍️

Enjoy this post? Give Krzysztof Ograbek a like if it's helpful.

post comments

Leave a like and comment for Krzysztof

Markdown

You can pass a Runnable into an agent.

Building an agent from a runnable usually involves a few things:

Data processing for the intermediate steps ( agent_scratchpad ). These need to represented in a way that the language model can recognize them. This should be pretty tightly coupled to the instructions in the prompt. For this reason, in the below example with an XML agent, we use the built in util formatXml to format the steps as XML.

The prompt itself. Below, this is the default XML agent prompt, which includes variables for the tool list and user question. It also contains examples of inputs and outputs for the agent to learn from.

The model, complete with stop tokens if needed (in our case, needed).

The output parser - should be in sync with how the prompt specifies things to be formatted. In our case, we'll continue with the theme of XML and use the default XMLAgentOutputParser

See this section for general instructions on installing integration packages .

For this example we'll define a custom tool for simplicity. You may use our built in tools, or define tools yourself, following the format you see below.

Help us out by providing feedback on this documentation page:

LangChain Blog

CrewAI Unleashed: Future of AI Agent Teams

Editor's Note: this blog is from Joao Moura, maintainer of CrewAI. CrewAI is a multi-agent framework built on top of LangChain, and we're incredibly excited to highlight this cutting edge work.

AI Agents Crews are game-changing

AI agents are emerging as game-changers, quickly becoming partners in problem-solving, creativity, and innovation and that's where CrewAI comes in.

Imagine turning a single line of thought into a comprehensive landing page in just minutes. It's a reality we're creating today with CrewAI. 

Recently, I demonstrated this in a tweet , using CrewAI's and LangChain with OpenHermes2.5, powered by Ollama , transforming a one-liner into a complete landing page. The result was a revelation of the untapped potential in AI collaboration and the ability to early market test ideas faster than ever before, and that is only one use case ( play with the code in Replit ).

CrewAI's vision is clear, to allow engineers to harness the collective power of AI agents, moving beyond traditional automation, by bringing Agents together and allowing for streamlined decision-making, enhanced creativity, and solving complex challenges.

Simplicity through Modular Design

CrewAI champions a principle that resonates with every engineer: simplicity through modularity. This means it's like a set of building blocks (much like LangChain).

CrewAI’s main components:

  • Agents: These are like your dedicated team members, each with their role, background story, goal and memory
  • Tools: The equipment our agents use to perform their tasks efficiently, you can use any existing ones from LangChain or quickly write your own
  • Tasks: Small and focused mission a given Agent should accomplish
  • Process: This is the workflow or strategy the crew follows to complete tasks.
  • Crew: Where Agents, Tasks and a Process meet, this is the container layer where the work happens

We believe in the power of simplicity to unlock complexity. By breaking down the intricate world of Agents into these modular components, we make it approachable, manageable, and fun to work with.

travel agent langchain

While individual tasks are important, CrewAI truly excels when multiple agents come together to form a crew. It’s in teamwork – agents collaborating, sharing goals, and following a process to achieve a common objective – where you go from average results to very impressive ones!

One example of collaboration within CrewAI is delegation, thanks to it, agents can ask for assistance or allocate portions of their task to others, much like we do in our workplaces. This ability for spontaneous collaboration is one example of what sets this library apart.

I designed CrewAI for practical, real-world use. And we strive to make it a library you can integrate into your applications and use it almost as easily as you would call a function.

Building with CrewAI

Disclaimer: The full code examples are available at Replit as templates you can customize and run in the cloud yourself but also at Github.

Let's explore the idea of a crew that can build a landing page from a single-line idea. The best way to go about a new crew is to first map out what would the process you, as a human, would go through to do this yourself, and then we can translate that into a crew.

My need for landing pages came from the fact I want to quickly experiment with ideas and see if they have any traction, so I want to be able to quickly build a landing page and gauge people’s interest on it, so that only then I can spend more time on it. More than a landing page I really need to do a good job at communicating the product or service I'm offering to make sure it's compelling.

So this is what I would do as a human:

  • Start by writing a single line idea, something like "A healthy snack for dogs to lose weight".
  • Expand that with some research, understand the market and why this is a good idea.
  • Find the appropriate template for this idea's landing page.
  • Write the copy for the landing page.
  • Build the landing page using the appropriate template and copy.

So if I had to put together a team for this what would be the ideal hires I would make?

  • Senior idea analyst to understand and expand upon the essence of ideas, make sure they are great and focus on real pain points.
  • Senior communications strategist to craft compelling stories to captivate and engage people around an idea.
  • Senior react engineer to build an intuitive, aesthetically pleasing, and high-converting landing page.
  • Senior content editor to ensure the landing page content is clear, concise, and captivating.

Now that we have our crew, let's see how we can translate part of it into an initial CrewAI crew that can expand our one-liner into a fully fledged researched idea (full code at Replit and Github).

For that you’ll need to create each agent you wanna use, create the tasks you want them to perform and then put them together in a Crew.

Make it Custom

The example we just explored is one of the most basic setups in CrewAI, and look how powerful it is! But that's just scratching the surface. Imagine the possibilities when you start customizing things (I’ll link to some fully working examples later in the post)

Think about mixing and matching different AI brains (LLMs) in your crew. Some might be based in the cloud, others right on your computer, up to you. And what if you have a unique challenge? No problem, you can always have a fine tuned model to be the brain of one of an Agent. What about integrations with external systems? You can craft your own tools or tap into the vast tools that LangChain offers , like in this example bellow:

Disclaimer: A little heads-up for those diving into local models: they can be a bit tricky right out of the box, especially the smaller ones. To get the best out of them, you might need to tweak a few settings. You should add 'Observation' as a stop word, and play around with parameters like 'top_p', 'repeat_last_n', and 'temperature' to get it working as you need. These little adjustments can make a world of difference in how your AI agents perform.

How it Works

Behind the scenes, each CrewAI Agent is essentially a LangChain Agent, but enhanced with a ReActSingleInputOutputParser. This parser is specially modified to better support role-playing, incorporates a binding stop word for contextual focus, and integrates a memory mechanism (using LangChain’s ConversationSummaryMemory) for continuity in tasks.

The fact that Agents are built on top of LangChain creates flywheel effects, the main one being that you get access to all LangChain tools and toolkits out of the box, and that alone already unlocks many use cases.

In the current iteration, Agents function autonomously, engaging in self-dialogue to determine the use of tools. Future versions of CrewAI, however, are planned to introduce diverse process types. These will enable collaborative executions in various group settings, allowing for dynamic task assignment among Agents during runtime

Tasks are assigned to Agents from their creation on the current version, and offer the ability to override the available tools that Agent will have at its disposal when performing work, this makes it easier to steer the same Agent in performing slightly different tasks while not overwhelming it with too many tools.

The Crew serves as a framework that encapsulates both Agents and Tasks, facilitating their sequential execution of work. In practice, you'll likely find it more effective to deploy multiple standalone Crews, each with a few Agents. This modular approach allows for diverse outcomes from each crew, as opposed to a singular, large Crew handling many tasks and Agents for a single output.

Real Use Cases and Code

Other than building landing pages I’ve also been using Crews for many other cases, some of those are below with links for their Replit templates so you modify and try them yourself.

  • Assist in travel planning , finding places, messaging, schedule, costs.
  • Build a landing page from one-liner idea
  • Individual Stock analysis (disclaimer: not actual investment recommendation)

Currently, CrewAI champions the 'sequential' process. Think of it as a relay race where each agent completes their part and then hands off their work to the next one. It's straightforward yet effective.

But this is not taking full advantage of all the different ways in which AI agents can work together, so we're not stopping there – we’re working on introducing more complex processes like 'consensual' and 'hierarchical' to unlock even more potential uses. We are super open for contributions through both PRs and Issues in Github , if you like it also send me a post in X I’d love to hear more from people enjoying it!

CrewAI represents a shift in AI agents by offering a thin framework that leverages collaboration and roleplaying, based on versatility and efficiency. It stands as a tool for engineers and creatives alike, enabling the seamless assembly of AI agents into cohesive, high-performing teams.

Whether it's transforming a single thought into a fully-fledged landing page or conducting complex idea analysis, CrewAI is adept at handling a diverse range of tasks through its processes.

The real-world applications of CrewAI, from boosting social media presence to building interactive landing pages, underscore its practicality and adaptability. Looking forward, CrewAI is set to evolve further, introducing more intricate processes and continuing to redefine the landscape of AI teamwork. With its user-friendly integration and customization options, CrewAI is not just a concept but a tangible, powerful tool to harness the power of AI Agents.

Join our newsletter

Updates from the LangChain team and community

Processing your application...

Success! Please check your inbox and click the link to confirm your subscription.

Sorry, something went wrong. Please try again.

travel agent langchain

Stochastic Coder

travel agent langchain

Effortlessly Add Traces to Your LangChain Agent with LangSmith

After developing your LangChain Agent or LLM application, ensuring its ongoing performance is crucial. LangSmith provides seamless trace capabilities, bridging the gap between prototypes and production-grade applications. This article demonstrates how to implement LangSmith tracing to monitor and optimize your agent’s behavior effectively.

After completing the development of your LangChain Agent or LLM application, a crucial question arises: “How do I ensure the LLM application continues to perform as expected?” This is where a platform like LangSmith becomes invaluable. LangSmith seamlessly adds trace capabilities to your LLM application, bridging the gap between your development prototype and a production-grade enterprise application. In this article we will utilize the LangChain Agent developed in the article “ Empower Your AI Agent with Azure Cosmos DB ” and implement tracing with LangSmith to monitor the agent’s behavior effectively. We will walk through the steps required to get started with LangSmith and add tracing to an existing application.

What is LangSmith?

LangSmith is a specialized platform that provides comprehensive monitoring and trace capabilities for LLM applications. It empowers developers with the tools needed to gain deep insights into their AI agent’s behavior, performance, and operational health. By integrating LangSmith, you can bridge the gap between development prototypes and robust, production-grade enterprise applications.

Why Use LangSmith?

Transitioning an LLM application from a prototype to a production environment involves numerous challenges, including maintaining performance, ensuring reliability, and scaling effectively. LangSmith addresses these challenges by providing a robust set of tools that:

  • Enhance visibility into the AI agent’s operations, making it easier to understand and optimize performance.
  • Facilitate proactive maintenance and issue resolution, reducing downtime and improving user satisfaction.
  • Support continuous improvement through detailed analytics and reporting, driving better decision-making and strategic planning.

Practical Application: Integrating LangSmith with Your LangChain Agent

To illustrate the power of LangSmith, consider a practical example where we leverage the LangChain Agent developed in the article “ Empower Your AI Agent with Azure Cosmos DB “. By applying LangSmith, we can add advanced tracing capabilities to monitor the agent’s behavior. This integration helps us ensure that the agent performs as expected in a production environment, maintaining high standards of reliability and efficiency.

Setting Up Your LangSmith Project

To begin, you will need to create an account with LangSmith. For the purposes of this article, the ‘Developer’ plan is recommended, as it provides ample trace capacity to monitor and test our AI Travel Agent.

LangSmith Developer Plan

Now that your Developer plan has been set up, you will be able to initiate a new project directly from the LangSmith landing page.

LangSmith create new project

Give your project a meaningful name, for example “Travel Agent,” then proceed to click Submit .

LangSmith new project - Travel Agent

When accessing the “Projects” section, you may observe that “Travel Agent” is not listed. Only projects with ‘runs’ will be displayed. To view all projects, simply click on “Sort by Name.”

LangSmith Projects

If you’ve reached this point, you have successfully set up your LangSmith project and are prepared to integrate it with your LangChain Agent.

Integrating Tracing with Your LangChain Agent

Updating the LangChain Agent developed in the article “ Empower Your AI Agent with Azure Cosmos DB ” to integrate LangSmith is a straightforward process. It involves three simple steps: installing LangSmith, updating the environment variables, and utilizing the context manager to initiate logging.

Installing Python Requirements

We will need to add LangSmith to the requirements.txt file in the api directory. The updated requirements file should look as follows:

Activate your environment and install dependencies by running the following command from the api directory:

Updating the Environment Variables

Update the ‘.env’ file in the api directory to include the four LangSmith variables.

LangSmith API Key

If you do not possess an API key for LangSmith, please navigate to settings (the gear icon) from the LangSmith landing page.

LangSmith select settings

Then select Create API Key .

LangSmith create API Key

Create a new key for our AI Travel Agent, then click Create API Key .

LangSmith Travel Agent API Key

Copy your key and paste it into your environment variable file, LANGCHAIN_API_KEY .

LangSmith Copy API Key

Initiate Logging with Context Manager

With LangSmith installed and configured, we are able to proceed with updating our code to initiate logging. Given that we utilized LangChain for our AI Agent, the necessary adjustments to incorporate LangSmith are minimal, as LangSmith seamlessly integrates into LangChain.

We will only be working with the FastAPI code from Empower Your AI Agent with Azure Cosmos DB , which is located in the api directory.

Original Code without LangSmith

Below is the original agent invocation code from our Python FastAPI application. The TravelAgent.py file is straightforward, as our agent, agent_with_chat_history , and its dependencies (tools, prompt, and LLM) are initialized and configured in the init.py file. In TravelAgent.py , we invoke our agent using the user’s input along with the session ID to maintain conversation memory. The agent’s output and response time are then returned encapsulated in a PromptResponse (model/prompt).

service/TravelAgent.py

Code with LangSmith

Introducing the LangSmith code follows a straightforward process. We begin by incorporating the tracing_v2_enabled tracer context. The next step involves enclosing our agent invoke with the tracer, accomplished by adding the line: with tracing_v2_enabled() . Additionally, metadata is included to enable the filtering of each agent session, thereby capturing the session_id.

These changes are all that’s needed to begin capturing run details with LangSmith!

Capturing Trace Data

In order to commence capturing our trace data with LangSmith, the first step is to initialize the FastAPI server. Please execute the following command from the api directory to initiate the server.

The FastAPI server launches on the localhost loopback 127.0.0.1 port 8000 by default. You can access the Swagger documents using the following localhost address:  http://127.0.0.1:8000/docs

Python FastAPI Travel Agent API Swagger Docs

With the API running, we have the ability to execute the following command from the  web  directory to initiate the React web user interface.

Running the command above will launch the React JS web application.

AI Travel Agent React JS Web Application

Click on ‘Effortlessly plan your voyage’ to launch the travel assistant.

AI Travel Agent - Launch Agent

Finding a ‘Relaxing Vacation’ with our LangChain Agent

Ask the agent for assistance finding a relaxing vacation.

AI Travel Agent looking for relaxing vacation

Asking for a ‘relaxing’ vacation will result in a recommendation for the  Tranquil Breeze Cruise  and the  Fantasy Seas Adventure Cruise  as they are anticipated to be the most ‘relaxing’ cruises available through the vector search. 

AI Travel Agent relaxing vacation vector search results

When reviewing our ‘Travel Agent’ project in LangSmith, we’ll notice that a transaction has logged this agent interaction, showing a latency of approximately 14 seconds and utilizing around 1,000 tokens (prompt and completion).

LangSmith Tracing Travel Agent Project run for vector search

Upon further examination of the operation, it becomes evident that the agent utilized the ‘vacation_lookup’ function to address this request. Analyzing each trace component of the AgentExecutor will provide valuable insights into the handling of this interaction.

LangSmith Tracing vacation lookup details

Refining Results through Memory

We can enhance our search by identifying the cruise that is most suitable for children, in order to evaluate our agent’s handling of the request.

AI Travel Agent chat history interaction

We completed the second task much more rapidly, taking only 1.60 seconds.

LangSmith Tracing Travel Agent project run results

This request is simply using the chat history (memory) from the conversation to answer the question thus resulting in a low latency.

LangSmith tracing chat history details

Tracing Additional LangChain Agent Function

As you search for your destination and book your cruise, you will be able to view the traces for each corresponding function and tool utilized.

AI Travel Agent LangChain Agent Itinerary Lookup

Asking inquiries regarding destinations and accommodations will prompt the agent to invoke the ‘itinerary_lookup’ function.

LangSmith Tracing Itinerary Lookup Details

Filtering by Metadata

During ongoing testing, you will likely start new sessions with the React JS web application. When this occurs, you can filter your LangSmith project runs by selecting the metadata session_id

LangSmith metadata filter

Transitioning an LLM application from prototype to production involves challenges like maintaining performance, ensuring reliability, and scaling effectively. LangSmith addresses these by enhancing visibility into AI operations, facilitating proactive maintenance, and supporting continuous improvement through detailed analytics. In this article we demonstrated how to quickly integrate LangSmith into an existing LangChain Agent. This integration ensures your agent performs reliably and efficiently in a production environment.

Share this:

Discover more from stochastic coder.

Subscribe to get the latest posts to your email.

Type your email…

Leave a Reply Cancel reply

LangChain Vector Search with Cosmos DB for MongoDB

Subscribe now to keep reading and get access to the full archive.

Continue reading

LangChain has a SQL Agent which provides a more flexible way of interacting with SQL Databases than a chain. The main advantages of using the SQL Agent are:

  • It can answer questions based on the databases' schema as well as on the databases' content (like describing a specific table).
  • It can recover from errors by running a generated query, catching the traceback and regenerating it correctly.
  • It can query the database as many times as needed to answer the user question.
  • It will save tokens by only retrieving the schema from relevant tables.

To initialize the agent we'll use the create_sql_agent constructor. This agent uses the SQLDatabaseToolkit which contains tools to:

  • Create and execute queries
  • Check query syntax
  • Retrieve table descriptions
  • ... and more

First, get required packages and set environment variables:

We default to OpenAI models in this guide, but you can swap them out for the model provider of your choice.

The below example will use a SQLite connection with Chinook database. Follow these installation steps to create Chinook.db in the same directory as this notebook:

  • Save this file as Chinook_Sqlite.sql
  • Run sqlite3 Chinook.db
  • Run .read Chinook_Sqlite.sql
  • Test SELECT * FROM Artist LIMIT 10;

Now, Chinhook.db is in our directory and we can interface with it using the SQLAlchemy-driven SQLDatabase class:

API Reference:

  • SQLDatabase

We'll use an OpenAI chat model and an "openai-tools" agent, which will use OpenAI's function-calling API to drive the agent's tool selection and invocations.

As we can see, the agent will first choose which tables are relevant and then add the schema for those tables and a few sample rows to the prompt.

  • create_sql_agent

Now, I will execute this query to get the total sales per country.

 [('USA', 523.0600000000003), ('Canada', 303.9599999999999), ('France', 195.09999999999994), ('Brazil', 190.09999999999997), ('Germany', 156.48), ('United Kingdom', 112.85999999999999), ('Czech Republic', 90.24000000000001), ('Portugal', 77.23999999999998), ('India', 75.25999999999999), ('Chile', 46.62)] The total sales per country are as follows:

  • USA: $523.06
  • Canada: $303.96
  • France: $195.10
  • Brazil: $190.10
  • Germany: $156.48
  • United Kingdom: $112.86
  • Czech Republic: $90.24
  • Portugal: $77.24
  • India: $75.26
  • Chile: $46.62

To answer the second question, the country whose customers spent the most is the USA, with a total sales of $523.06.

> Finished chain.

CREATE TABLE "PlaylistTrack" ( "PlaylistId" INTEGER NOT NULL, "TrackId" INTEGER NOT NULL, PRIMARY KEY ("PlaylistId", "TrackId"), FOREIGN KEY("TrackId") REFERENCES "Track" ("TrackId"), FOREIGN KEY("PlaylistId") REFERENCES "Playlist" ("PlaylistId") )

PlaylistId TrackId 1 3402 1 3389 1 3390

Using a dynamic few-shot prompt ​

To optimize agent performance, we can provide a custom prompt with domain-specific knowledge. In this case we'll create a few shot prompt with an example selector, that will dynamically build the few shot prompt based on the user input. This will help the model make better queries by inserting relevant queries in the prompt that the model can use as reference.

First we need some user input \< > SQL query examples:

Now we can create an example selector. This will take the actual user input and select some number of examples to add to our few-shot prompt. We'll use a SemanticSimilarityExampleSelector, which will perform a semantic search using the embeddings and vector store we configure to find the examples most similar to our input:

  • SemanticSimilarityExampleSelector
  • OpenAIEmbeddings

Now we can create our FewShotPromptTemplate, which takes our example selector, an example prompt for formatting each example, and a string prefix and suffix to put before and after our formatted examples:

  • ChatPromptTemplate
  • FewShotPromptTemplate
  • MessagesPlaceholder
  • PromptTemplate
  • SystemMessagePromptTemplate

Since our underlying agent is an OpenAI tools agent , which uses OpenAI function calling, our full prompt should be a chat prompt with a human message template and an agent_scratchpad MessagesPlaceholder . The few-shot prompt will be used for our system message:

And now we can create our agent with our custom prompt:

Let's try it out:

Dealing with high-cardinality columns ​

In order to filter columns that contain proper nouns such as addresses, song names or artists, we first need to double-check the spelling in order to filter the data correctly.

We can achieve this by creating a vector store with all the distinct proper nouns that exist in the database. We can then have the agent query that vector store each time the user includes a proper noun in their question, to find the correct spelling for that word. In this way, the agent can make sure it understands which entity the user is referring to before building the target query.

First we need the unique values for each entity we want, for which we define a function that parses the result into a list of elements:

Now we can proceed with creating the custom retriever tool and the final agent:

  • create_retriever_tool

As we can see, the agent used the search_proper_nouns tool in order to check how to correctly query the database for this specific artist.

Next steps ​

Under the hood, create_sql_agent is just passing in SQL tools to more generic agent constructors. To learn more about the built-in generic agent types as well as how to build custom agents, head to the Agents Modules .

The built-in AgentExecutor runs a simple Agent action -> Tool call -> Agent action... loop. To build more complex agent runtimes, head to the LangGraph section .

Help us out by providing feedback on this documentation page:

  • Using a dynamic few-shot prompt
  • Dealing with high-cardinality columns

Navigation Menu

Search code, repositories, users, issues, pull requests..., provide feedback.

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly.

To see all available qualifiers, see our documentation .

  • Notifications You must be signed in to change notification settings

06-langchain-agents.ipynb

Latest commit, file metadata and controls.

IMAGES

  1. LangChain Agents: Simply Explained!

    travel agent langchain

  2. LangChain Agents

    travel agent langchain

  3. Introduction to LangChain: A Framework for LLM Powered Applications

    travel agent langchain

  4. Revolutionizing Insurance With LangChain: Enhancing Security

    travel agent langchain

  5. LangChain Agents Deep Dive with GPT 3.5

    travel agent langchain

  6. LangChain 101: Build Your Own GPT-Powered Applications

    travel agent langchain

VIDEO

  1. Setting up Your Dev Environment for a Wild Ride on Langchain

  2. OpenGPTs:本地化构建AI Agent、RAG、AI聊天助手,langchain出品的gpts构建框架,兼容langchain的100+款工具、60多个模型、60多个向量库无缝兼容

  3. Agents in Langchain

  4. Slim Version of ChatGPT Code-Interpreter with LangChain

  5. Deploy a WebHook/API running a Langchain Agent in 23 Minutes [With SSL]

  6. Langchain Agents

COMMENTS

  1. Build an Agent

    Build an Agent. By themselves, language models can't take actions - they just output text. A big use case for LangChain is creating agents . Agents are systems that use LLMs as reasoning engines to determine which actions to take and the inputs to pass them. After executing actions, the results can be fed back into the LLM to determine whether ...

  2. Building an Autonomous AI Agent with LangChain and PostgreSQL pgvector

    In this blog, we'll create a fully autonomous AI travel agent, capable of finding listings and managing bookings. This OpenAI tools agent uses LangChain, OpenAI, ... Before diving into the code, let's first outline the role that each component plays in creating an effective agent. LangChain: A framework for constructing AI applications, ...

  3. Building a Smart Travel Itinerary Suggester with LangChain, Google Maps

    The goal is more to get acquainted with the tools needed to build a service like this rather than actually deploy the application, but along the way we'll learn a bit about prompt engineering, LLM orchestration with LangChain, using the Google Maps API to extract directions and displaying the results with leafmap and gradio.It's amazing how quickly these tools allow you to build a POC for ...

  4. How I Made an AI Agent in 10 Minutes with LangChain

    Finally, python-dotenv will be used to load the OpenAI API keys into the environment. pip install langchain openai python-dotenv requests duckduckgo-search. LangChain is a very large library so that may take a few minutes. While this is downloading, create a new file called .env and paste your API key in.

  5. Building an AI driven travel agent with Langchain and OpenAI

    Building an AI driven travel agent with Langchain and OpenAI I recently did an experiment to create a AI driven travel agent. So far the backend is done. Learn more about how I built it in the following link. Feel free to reuse the code for your personal projects 😉 ... Hey, I'm looking for an AI travel agent and was sent here.

  6. Learn the LangChain Basics by Building a Berlin Travel Guide

    Agents. Agents are one of the most interesting components of LangChain. With agents, we are even more flexible. The agents can decide on their own which and how they solve a problem based on a set ...

  7. Agents

    LangChain comes with a number of built-in agents that are optimized for different use cases. Read about all the available agent types here. For this example, let's try out the OpenAI tools agent, which makes use of the new OpenAI tool-calling API (this is only available in the latest OpenAI models, and differs from function-calling in that ...

  8. GitHub

    The LangChain libraries themselves are made up of several different packages. langchain-core: Base abstractions and LangChain Expression Language. langchain-community: Third party integrations. langchain: Chains, agents, and retrieval strategies that make up an application's cognitive architecture.

  9. Build an Agent

    A big use case for LangChain is creating agents. Agents are systems that use an LLM as a reasoning enginer to determine which actions to take and what the inputs to those actions should be. The results of those actions can then be fed back into the agent and it determine whether more actions are needed, or whether it is okay to finish.

  10. How to Build Your First AI Agent with LangChain and GPT-4

    AI Agents have tools and the power to use them. So let's provide GPT-4 with some tools: from langchain.utilities import SerpAPIWrapper. from langchain.agents import Tool, load_tools. from langchain.chat_models import ChatOpenAI. search = SerpAPIWrapper() #initialize GPT-4. gpt4 = ChatOpenAI(model="gpt-4", temperature=0) # create the serp tool.

  11. Kabeer Singh Thockchom on LinkedIn: TravelAgent ai demo

    Update! I've just created a quick and simple demo of an AI-powered travel agent using LangChain, Duffel, and OpenAI.I go through an example user journey in the demo. This initial MVP can search ...

  12. AI Travel Agent Powered by Azure Cosmos DB and LangChain

    This repository explores the implementation of a LangChain Agent using Azure Cosmos DB for MongoDB vCore to handle traveler inquiries and bookings. The project provides detailed instructions for setting up the environment and loading travel data, aiming to empower developers to integrate similar agents into their solutions.

  13. Intro to LLM Agents with Langchain: When RAG is Not Enough

    Introduction to the agents. Illustration by author. LLMs are often augmented with external memory via RAG architecture. Agents extend this concept to memory, reasoning, tools, answers, and actions. Let's begin the lecture by exploring various examples of LLM agents. While the topic is widely discussed, few are actively utilizing agents; often ...

  14. Using Agents as Tools in LangChain

    return price_tool. we can then go on and define an agent that uses this agent as a tool. As always, getting the prompt right for the agent to do what it's supposed to do takes a bit of tweaking ...

  15. Agents

    Agents. You can pass a Runnable into an agent. Building an agent from a runnable usually involves a few things: Data processing for the intermediate steps ( agent_scratchpad ). These need to represented in a way that the language model can recognize them. This should be pretty tightly coupled to the instructions in the prompt.

  16. Build an Agent

    Build an Agent. By themselves, language models can't take actions - they just output text. A big use case for LangChain is creating agents . Agents are systems that use an LLM as a reasoning engine to determine which actions to take and what the inputs to those actions should be. The results of those actions can then be fed back into the agent ...

  17. LangGraph: Multi-Agent Workflows

    LangGraph: Multi-Agent Workflows. Links. Last week we highlighted LangGraph - a new package (available in both Python and JS) to better enable creation of LLM workflows containing cycles, which are a critical component of most agent runtimes. As a part of the launch, we highlighted two simple runtimes: one that is the equivalent of the ...

  18. How to Build the Ultimate AI Automation with Multi-Agent Collaboration

    Introducing LangGraph. LangGraph is an extension of LangChain aimed at creating agent and multi-agent flows. It adds in the ability to create cyclical flows and comes with memory built in - both important attributes for creating agents. LangGraph provides developers with a high degree of controllability and is important for creating custom ...

  19. CrewAI Unleashed: Future of AI Agent Teams

    CrewAI is a multi-agent framework built on top of LangChain, and we're incredibly excited to highlight this cutting edge work. AI Agents Crews are game-changing. AI agents are emerging as game-changers, quickly becoming partners in problem-solving, creativity, ... Assist in travel planning , finding places, messaging, schedule, costs.

  20. Effortlessly Add Traces to Your LangChain Agent with LangSmith

    After developing your LangChain Agent or LLM application, ensuring its ongoing performance is crucial. LangSmith provides seamless trace capabilities, bridging the gap between prototypes and production-grade applications. This article demonstrates how to implement LangSmith tracing to monitor and optimize your agent's behavior effectively.

  21. Agents

    Agents. LangChain has a SQL Agent which provides a more flexible way of interacting with SQL Databases than a chain. The main advantages of using the SQL Agent are: It can answer questions based on the databases' schema as well as on the databases' content (like describing a specific table). It can recover from errors by running a generated ...

  22. examples/learn/generation/langchain/handbook/06-langchain-agents.ipynb

    You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window.

  23. Master Advanced Prompt Engineering with LangChain

    In advanced prompt engineering, we craft complex prompts and use LangChain's capabilities to build intelligent, context-aware applications. This includes dynamic prompting, context-aware prompts, meta-prompting, and using memory to maintain state across interactions. These techniques can significantly enhance the performance and reliability ...