How Custom LangChain Agents Work - 5 Real Examples

How Custom LangChain Agents Work - 5 Real Examples

LangChain has emerged as an essential framework for developing powerful LLM-powered AI applications. This open source framework, with its ability to chain LLMs with other tools, enhances the scope of what can be achieved with natural language processing.

In this article, we'll embark on a detailed journey through the mechanics of LangChain Agents. But, more than just understanding how they work, we'll showcase 5 real-world examples that illustrate their transformative capabilities and how AI innovators across industries are harnessing their power to redefine what's possible.

Why Use LangChain?

LangChain can be used to develop a range of LLM-powered applications. At its core, LangChain is used to “chain” together a series of components to build more advanced AI applications.

As an open-source framework, it offers a standard interface for chains, integrations with tools like GPT-4 and other LLMs, and packages for both Python and JavaScript. LangChain can be leveraged to build applications such as:

  • AI chatbots
  • Document summarization
  • Code analysis
  • Data augmentation
  • Custom QA systems

How Do LangChain Agents Work?

In this article we will focus on LangChain Agents specifically. LangChain agents sit at the heart of LangChain, acting as decision makers when an application requires not just a predetermined chain of calls to a large language model (LLM) or other tools, but potentially an unknown chain of calls that are dependent on the user input.

It’s important to know that LangChain applications can be built without Agents. Agents are optional and are only used when an “on-the-fly” decision is needed.

There are several key components of LangChain that you need to understand to make sense to LangChain Agents:

  • Tools - Tools streamline interactions between language models and functions. They accept text inputs, process them, and yield text outputs. Tools could be something standard like performing a Google search, or they could be custom tools that perform some specific tasks like looking up information in a database.
  • Toolkits - Curated sets of tools designed to tackle specific challenges. They're an assembly of specialized instruments that, when combined, efficiently accomplish set tasks, optimizing complex processes.
  • Agents - Acts as the reasoning engine, choosing a list of tools (chain) to use and actions to take with said tools based on the user input.
  • Agent executor - Responsible for calling the agent, executing the action that the agent chose, and passing the output of the action back to the agent using the prompt template.

PRO TIP: Memory is key for LangChain agents. It lets them remember and pull up previous interactions, whether with other agents, people, or when using tools. There are two main kinds: short-term and long-term. Short-term memory holds onto the newest details, like the last few tools used.

Long-term memory saves information that's important for what's happening now, often based on how often something comes up or how similar it is to current tasks. Thanks to this memory feature, LangChain agents can learn and adjust based on what they've done before.

5 Real Word Examples - How Do Custom LangChain Agents Work?

How Do Custom Langchain Agents Work

LangChain Agents, with their dynamic and adaptive capabilities, have opened up a new frontier in the development of LLM and AI-powered applications. These agents, rather than following a static sequence, tailor their responses by intelligently selecting and chaining tools based on real-time input.

This flexibility enables the creation of solutions that are more responsive, versatile, and suited to specific tasks. In this section, we'll delve into some real examples of custom LangChain Agents at work:

Assistance for Online Shopping

Chatbot

A custom Agent can be used to provide automated assistance for online shoppers. They are able to understand customer inquiries in natural language and suggest products and services that are most relevant to their needs.

Leveraging advanced linguistic processing, LangChain Agents efficiently decode and respond to real-time queries, streamlining the shopping process. Their capabilities go beyond mere product suggestions, offering users detailed insights that aid in making informed decisions.

For example, imagine that a customer on an electronics retail website needs help finding the right laptop. They open the shopping assistant chatbot and say "I'm looking for a good laptop for gaming."

The LangChain Agent would then analyze the user’s input and decide on the action to take and the tool to use to meet their needs. In this case, it would create a personalized search to find the best laptops for their needs. It would also suggest related peripherals and accessories that could enhance the gaming experience. In addition, the Agent could provide detailed product information, such as technical specs, pricing, and customer reviews to help finalize the purchase.

Assistance For Online Shopping

Example:

If you want to implement a shopping assistant chatbot like this, use this sample code as a starting point:

import langchain as lc

# Create a LangChain Agent object
agent = lc.Agent()

# Define a function to handle customer inquiries
def handle_inquiry(inquiry):
  # Parse the inquiry and generate suggestions
  parsed_inquiry = agent.nlu(inquiry)
  suggestions = agent.suggest(parsed_inquiry)
  insights = agent.insight(suggestions)

  # Return the suggestions and insights as a response
  response = agent.response(suggestions, insights)
  return response

# Test the function with an example inquiry
example_inquiry = "I'm looking for a laptop that can run games smoothly"
example_response = handle_inquiry(example_inquiry)

print(example_response)

Customer Service Agents

Agent Tools

LangChain Agents can be used to provide automated customer service answers to customers. Harnessing their pre-programmed natural language understanding capabilities, these agents are adept at swiftly grasping the essence of customer inquiries, ensuring they respond with precise and relevant information.

This not only enhances customer satisfaction but also streamlines the support process, making it more efficient and timely for both businesses and customers.

For example, imagine a customer needs some support before completing their purchase and reaches out to a customer service chatbot, posing the question, "What are the available shipping options for my order?"

The LangChain Agent would then analyze the customer's question, identifying the order number and the destination address. It would access its database to retrieve the available shipping methods for that specific order and destination.

It would compose a clear and concise final answer, providing information on each shipping option, such as delivery time and cost. If the customer were to follow up with, "Can I expedite my delivery?" the agent would smoothly proceed, explaining how to select priority shipping at checkout and what additional charges would apply.

Example:

If you want to implement a customer service agent chatbot like this, use this sample code as a starting point:

# Define a function to handle customer inquiries about shipping options
def shipping_options(customer_question):
    # Initialize a LangChain Agent with NLP capabilities
    agent = LangChainAgent(nlu=True)

    # Parse the customer question and extract the relevant information
    parsed_question = agent.parse(customer_question)
    order_id = parsed_question["order_id"]
    destination = parsed_question["destination"]

    # Query the database for the available shipping methods
    shipping_methods = agent.query_database(order_id, destination)

    # Generate a response with details on each shipping option
    response = agent.generate_response(shipping_methods)
    return response

# Example of calling the function with a customer question
customer_question = "What are the available shipping options for my order?"
response = shipping_options(customer_question)
print(response)

# Output:
There are three shipping options for your order:
- Standard shipping costs $5 and takes 5-7 business days.
- Express shipping costs $10 and takes 3-5 business days.
- Priority shipping costs $15 and takes 1-3 business days.
To expedite your delivery, please select priority shipping at checkout.

Automated Order Processing

Agent Automations

LangChain Agents can be used to streamline and automate crucial aspects of an e-commerce system. By integrating advanced linguistic and transactional algorithms, these agents can interpret and execute customer orders with unparalleled efficiency.

Beyond merely collecting order details, they are adept at validating payment methods, ensuring transaction security, and initiating the order process, all stemming directly from a customer's request. This not only enhances the user experience by offering swift and intuitive purchasing options but also optimizes backend operations for businesses.

For example, let’s imagine that a customer wants to make a purchase using a voice assistant on an app. They say, "I'd like to order the new edition hardcover book and pay using my credit card."

The conversational agent would first extract the item name and then converse with the user in natural language, asking for purchase details like their address and credit card number. After receiving this information, it would verify the payment using a predefined function or custom tool.

If the payment is successful, it would use another function to process the order for the hardcover book and set up the delivery. It would then send a confirmation message to the customer using another function. However, if there is a problem with the payment verification, it would send a notification message to the customer informing them that the transaction could not be completed.

Example:

If you want to implement a similar bot that handles automated order processing, use this sample code as a starting point:

# This is a possible example of code for the LangChain agent
# It assumes that there are following functions:
# - verify_payment that returns True/False
# - process_order that returns a confirmation message
# - called send_message that sends a message it to the customer
import re

# Extract the item name and the credit card number from the order_text
item_pattern = r"order the (.+?) and"
credit_card_pattern = r"pay using my (.+)"
item_name = re.search(item_pattern, order_text).group(1)
credit_card_number = re.search(credit_card_pattern, order_text).group(1)

# Verify the payment using the credit card number
payment_status = verify_payment(credit_card_number)

if payment_status:
  confirmation_message = process_order(item_name)
  send_message(confirmation_message)
else:
  notification_message = "Sorry, your transaction could not be completed."
  send_message(notification_message)

Automated Supplier Discovery

Assistant

LangChain Agents can be used to automate the process of supplier discovery. Through their natural language understanding and a query-based search tool, they can identify and suggest suppliers that meet specific business criteria.

This automation significantly enhances the efficiency and precision of the sourcing process, helping businesses to quickly identify and collaborate with optimal suppliers for their requirements.

For instance, suppose a procurement officer needs to find a reliable source of organic cotton fabric. They can simply ask the chatbot, "Can you find me a trustworthy supplier for organic cotton fabric?"

The agent will then use its natural language understanding and query-based search to scan its database and find suppliers that match the criteria. It will then return a list of names, along with their contact details and ratings, of suppliers that are known for their quality and affordability in the organic cotton sector. The officer can then easily contact and negotiate with the best suppliers for their needs.

Example:

If you want to implement a similar supplier discovery chatbot, use this sample code as a starting point:

# Define a function to find a supplier for organic cotton fabric
def find_supplier():
    # Initialize a LangChain Agent object
    agent = LangChainAgent()
    # Define the criteria for the query
    criteria = {"trustworthy": True, "organic_cotton_fabric": True}
    # Perform the query and get the results
    results = agent.query(criteria)
    # Return the results as a list of supplier names
    return [result["name"] for result in results]

# Call the function and print the output
suppliers = find_supplier()
print("Here are some trustworthy suppliers for organic cotton fabric:")
for supplier in suppliers:
    print(supplier)

Document Analysis

Document Analysis

LangChain Agents can also be used to analyze legal contracts and automatically detect any non-standard phrases or clauses. Their sophisticated algorithms combined with vast legal knowledge bases ensure comprehensive and precise analysis. By doing so, they protect businesses and individuals from potential legal pitfalls, safeguarding against unintentional breaches or unfavorable terms.

Imagine a company has just received a non-disclosure agreement (NDA) from a potential partner. Before signing, they run the document through their document analysis assistant.

The assistant scrutinizes the NDA using custom tools, identifying any terms or clauses that deviate from the typical framework of such agreements. If it detects any suspicious or non-standard terms, it promptly alerts the company, recommending a closer review or consultation. This proactive approach ensures that the company remains protected and fully informed throughout their contractual engagements.

Example:

If you want to build a similar document analysis tool, use this sample code as a starting point:

import langchain

# Load the NDA document as a string
nda = open("nda.txt").read()

# Create a LangChain Agent object with the document analysis mode
agent = langchain.Agent(mode="document_analysis")

# Analyze the document and get a report
report = agent.analyze(nda)

# Print the report summary
print(report.summary)

# Check if there are any non-standard terms or clauses
if report.non_standard:
    # Print a warning message and the list of non-standard items
    print("Warning: The document contains non-standard terms or clauses.")
    print(report.non_standard)
else:
    # Print a confirmation message
    print "The document is standard and does not contain any suspicious\
           or non-standard terms or clauses."

Common Challenges When Using LangChain Agents

While the potential of LangChain Agents in revolutionizing various applications is undeniable, like any advanced technological solution, they come with their own set of challenges. Before diving deep into its myriad of uses, it's vital to be aware of and address the hurdles that might be encountered in the practical implementation of these agents.

Let's explore some of the common obstacles faced by developers and businesses alike when utilizing LangChain Agents:

  • Data privacy and security: Data should be handled in a transparent and ethical manner, respecting the users’ consent and preferences.
  • Reliability of the data sets: LLMs are trained on large-scale data sets that may contain errors, inconsistencies, or biases. These may affect the quality and accuracy of the agents’ outputs and decisions.
  • Potential for bias in the algorithms: LLMs use complex algorithms that may exhibit bias or discrimination towards certain groups or individuals based on their characteristics, such as gender, race, ethnicity, etc.
  • Scalability and efficiency: These agents require a lot of computational resources to process and generate natural language. This may limit their scalability and efficiency in handling large volumes of tasks or requests.

Conclusion

As the world leans more into AI, LangChain is undoubtedly at the forefront, shaping the future of natural language processing. The versatility of custom LangChain Agents, from enhancing customer service to streamlining document analysis, underscores their importance.

If you're looking for help leveraging LangChain and LLMs to develop a custom AI solution, consider our AI development services. Our team can work with you to put together a comprehensive AI strategy and guide you through the integration and development process. Reach out for a free quote!