Langchain Tool/Agent → ChatGPT plugin functionality

March 25, 2023
Shrikar Archak

Imagine a world where your virtual assistant can not only answer your questions but also perform complex tasks on your behalf. A world where your AI-powered chatbot can seamlessly integrate with external platforms, services, and tools to provide you with a truly interactive and dynamic experience. This was one of the powerful feature OpenAI release a couple of days back.

With ChatGPT plugins ChatGPT can access interet and take actions on behalf of user. From making reservations at your favorite restaurant to creating shopping lists for your next culinary adventure, ChatGPT plugins are revolutionizing the way we interact with AI-powered chatbots. So, buckle up and join us as we delve into the cool and futuristic world of ChatGPT plugins, where the possibilities are endless and the future is now!

By the end of this blog you will learn how to take a certain action based on a user's input. You will also learn how to use the Langchain Agents and tools to provide ChatGPT plugin functionality.

What is langchain

LangChain is a framework built around LLMs. The core idea of the library is that we can “chain” together different components to create more advanced use cases around LLMs. Some of the core features of LangChain are:

  • Prompt Templates: A prompt template refers to a reproducible way to generate a prompt. It contains a text string (“the template”), that can take in a set of parameters from the end user and generate a prompt. The prompt template may contain:
    • instructions to the language model
    • a set of few shot examples to help the language model generate a better response,
    • a question to the language model.
  • Agents/Tools: An agent is a set of tools that can be used to perform a specific task
  • Chains: Using an LLM in isolation is fine for some simple applications, but many more complex ones require chaining LLMs - either with each other or with other experts. LangChain provides a standard interface for Chains, as well as some common implementations of chains for ease of use.
  • Memory: Chains and Agents are stateless by default, meaning they handle each query independently without remembering past interactions. However, in certain applications, such as chatbots, it's crucial to recall previous interactions in both the short and long term. To address this, LangChain introduces the concept of "Memory" to retain past interactions. LangChain offers memory components in two ways: (1) helper utilities to manage and manipulate previous chat messages in a modular manner, and (2) easy methods to integrate these utilities into chains.

Let's look at the Agents/Tools in detail that can help augment or provide new features Like a math calculator, calling a web api etc in the chain/flow

To begin with let's import the packages

from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.llms import OpenAI
import time
from langchain.agents import initialize_agent, Tool

Let's say you are building an agent which gets called when a user wants to know the price of the stock. We can use the tool DataClass to build out custom GetStockPrice tool. Similarly if we want to find the ingredients of a recipe we could build another tool called GetRecipeIngredients.

import requests
def get_stock_price(symbol):
  print("\nGet Stock price for stock : ", symbol)
  data = requests.get(f"https://finnhub.io/api/v1/quote?symbol={symbol}&token=cg8evdhr01qgf5t90b50cg8evdhr01qgf5t90b5g").json()
  return data['c']

def get_recipe_ingredients(recipe):
  print("\nGet Recipe Ingredients: ", recipe)
  return "You can take any custom action for this user action"

llm = OpenAI(temperature=0)
tools = [
    Tool(
        name = "GetStockPrice",
        func=get_stock_price,
        description="""This is useful if we want to get the price of the stock. Make sure to pass the symbol of the stock.
        If user ask for Apple Stock price pass 'AAPL' to the tool. Similarly if user ask for microsoft pass MSFT.
        """
    ),
    Tool(
        name = "GetRecipeIngredients",
        func=get_recipe_ingredients,
        description="""Call this tool when a user's intent is to find the ingredients of a given recipe. If a user ask for vegetable biryani
        pass "Vegetable Biryani" to the tool.
        """
    )
]
action_chain = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)

We can think of the description as similar to what description_for_model is for the ChatGPT plugin. For more information checkout this link → ChatGPT Writing Descriptions

When a user makes a query that might be a potential request that goes to a plugin, the model looks through the descriptions of the endpoints in the OpenAPI specification along with the description_for_model in the manifest file. Just like with prompting other language models, you will want to test out multiple prompts and descriptions to see what works best.

The OpenAPI spec itself is a great place to give the model information about the diverse details of your API – what functions are available, with what parameters, etc. Besides using expressive, informative names for each field, the spec can also contain “description” fields for every attribute. These can be used to provide natural language descriptions of what a function does or what information a query field expects, for example. The model will be able to see these, and they will guide it in using the API. If a field is restricted to only certain values, you can also provide an “enum” with descriptive category names.

In the above code we build the tools and initialize the agent with the tools. Also we are use the OpenAI LLM to generate the response.

Now that we have our tools ready, let's call the agent and see how it works.

In [15]: action_chain.run("how to make carrot cake")


> Entering new AgentExecutor chain...
 I need to find the ingredients for a carrot cake
Action: GetRecipeIngredients
Action Input: Carrot Cake
Get Recipe Ingredients:  Carrot Cake

Observation: You can take any custom action for this user action
Thought: I now know the ingredients for the carrot cake
Final Answer: The ingredients for carrot cake are: all-purpose flour, baking powder, baking soda, salt, ground cinnamon, ground nutmeg, eggs, granulated sugar, vegetable oil, vanilla extract, grated carrots, and chopped walnuts.

> Finished chain.
Out[15]: 'The ingredients for carrot cake are: all-purpose flour, baking powder, baking soda, salt, ground cinnamon, ground nutmeg, eggs, granulated sugar, vegetable oil, vanilla extract, grated carrots, and chopped walnuts.'

In [16]: action_chain.run("What is amazon's  price")


> Entering new AgentExecutor chain...
 I need to get the stock price
Action: GetStockPrice
Action Input: AMZN
Get Stock price for stock :  AMZN

Observation: 98.13
Thought:^[[A I now know the final answer
Final Answer: Amazon's price is 98.13.

> Finished chain.
Out[16]: "Amazon's price is 98.13."

In [17]: action_chain.run("What is square's  price")


> Entering new AgentExecutor chain...
 I need to get the stock price
Action: GetStockPrice
Action Input: SQ
Get Stock price for stock :  SQ

Observation: 60.68
Thought: I now know the final answer
Final Answer: Square's price is 60.68.

> Finished chain.
Out[17]: "Square's price is 60.68."

Please let me know if you have any feedback.

Subscribe to the newsletter

Get notified when new content or topic is released.

You won't receive any spam! ✌️