avatar01coder

Summary

This context demonstrates how to integrate LangChain capabilities with AutoGen agents to build a Super AI Agent that can execute tasks requiring specific knowledge, such as the Uniswap protocol.

Abstract

The content of this context revolves around integrating LangChain capabilities with AutoGen agents to create a Super AI Agent that can handle tasks requiring specific knowledge, such as understanding the Uniswap protocol. The author provides step-by-step instructions on how to build an AI agent that can execute tasks related to the Uniswap V3 protocol. The process involves building a vector store with the Uniswap V3 whitepaper, setting up a conversational retrieval QA chain using LangChain, defining a function to answer Uniswap-related questions, and setting up AutoGen user and assistant agents with function calling enabled. The author also shares a Python notebook version of this showcase on Github.

Bullet points

  • AutoGen and LangChain are key components in building Super AI Agents.
  • AutoGen does not support connecting to various external data sources natively, which is where LangChain can help.
  • The context provides a step-by-step guide to building an AI agent that can execute tasks related to the Uniswap V3 protocol.
  • The process involves building a vector store with the Uniswap V3 whitepaper, setting up a conversational retrieval QA chain using LangChain, defining a function to answer Uniswap-related questions, and setting up AutoGen user and assistant agents with function calling enabled.
  • The author shares a Python notebook version of this showcase on Github.

AutoGen + LangChain + ChromaDB = Super AI Agents

AutoGen + LangChain + ChromaDB

de>AutoGen is a versatile framework that facilitates the creation of LLM applications by employing multiple agents capable of interacting with one another to tackle tasks. These AutoGen agents can be tailored to specific needs, engage in conversations, and seamlessly integrate human participation. They are adaptable to different operation modes that encompass the utilization of LLMs, human inputs, and various tools.

de>LangChain is an open-source framework designed for software developers engaged in AI and ML. It enables them to seamlessly integrate LLM with external components, facilitating the creation of LLM-driven applications. The primary aim of LangChain is to establish connections between LLMs such as OpenAI's GPT-3.5 and GPT-4 and various external data sources, enabling the development and utilization of NLP applications.

Both of them are playing key roles in the LLM application development.

AutoGen doesn't support connecting to various external data sources natively. This is exactly where LangChain can come into play.

In this post, I will show you how to integrate LangChain capabilities to AutoGen agents and build a cool AI tool.

I produced a video talking about the same content on Youtube. Feel free to watch if you are more comfortable with Video stream. Btw, it’s in Chinese. But I have uploaded subtitle so that you should be able to use the auto-translate to view in your preferred language.

This tool will act as an AI agent that knows well about Uniswap protocol, and more specifically the version 3.

Uniswap is a decentralized exchange that allows users to trade Ethereum-based tokens.

Firstly, let me walk you through the steps to build such an AI agent that can execute tasks that require Uniswap knowledge.

  1. Build up a vector store with Uniswap V3 whitepaper.
  2. Set up a conversational retrieval QA chain by LangChain, based on the vector store.
  3. Define a function answer_uniswap_question by taking a parameter question, calling the QA chain to answer it.
  4. Set up AutoGen user agent and assistant agent with function calling enabled.

Notices for step 4:

In the function mapping, the function defined in step 3 is included.

The assistant agent can be instructed by the following message to call the function answer_uniswap_question to answer Uniswap related questions.

I'm writing a blog to introduce the version 3 of Uniswap protocol. 

Find the answers to the 3 questions below and write an introduction based on them.  
  1. What is Uniswap? 
  2. What are the main changes in Uniswap version 3? 
  3. How to use Uniswap?

Start the work now.

The function call is done by user agent.

Now, it’s time to start coding.

Environment Preparation

1. Install python dependencies and download the PDF file for demo.

pip install pyautogen~=0.1.0 docker langchain openai tiktoken chromadb pypdf -q -U
wget -O uniswap_v3.pdf https://uniswap.org/whitepaper-v3.pdf

2. Load the AutoGen supported configuration.

import autogen

config_list = autogen.config_list_from_json(
    "OAI_CONFIG_LIST",
    filter_dict={
        "model": ["gpt-4"],
    },
)
#
# Sample content of OAI_CONFIG_LIST file below:
#
# [
#   {
#     "model": "gpt-4",
#     "api_key": "your openai api key"
#   }
# ]
#

3. Import required classes and set OPENAI_API_KEY environmental variable.

gpt4_api_key = config_list[0]["api_key"]

import os
os.environ['OPENAI_API_KEY'] = gpt4_api_key

from langchain.vectorstores import Chroma
from langchain.embeddings import OpenAIEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.document_loaders import PyPDFLoader
from langchain.memory import ConversationBufferMemory
from langchain.llms import OpenAI
from langchain.chains import ConversationalRetrievalChain

Coding the Main Feature

docs = PyPDFLoader('./uniswap_v3.pdf').load()
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000)
docs = text_splitter.split_documents(docs)

vectorstore = Chroma(
    collection_name="full_documents",
    embedding_function=OpenAIEmbeddings()
)
vectorstore.add_documents(docs)

qa = ConversationalRetrievalChain.from_llm(
    OpenAI(temperature=0),
    vectorstore.as_retriever(),
    memory=ConversationBufferMemory(memory_key="chat_history", return_messages=True)
)

# Test the QA chain
result = qa(({"question": "What is uniswap?"}))
print(result['answer'])

def answer_uniswap_question(question):
  response = qa({"question": question})
  return response["answer"]

llm_config={
    "request_timeout": 600,
    "seed": 42,
    "config_list": config_list,
    "temperature": 0,
    "functions": [
        {
            "name": "answer_uniswap_question",
            "description": "Answer any Uniswap related questions",
            "parameters": {
                "type": "object",
                "properties": {
                    "question": {
                        "type": "string",
                        "description": "The question to ask in relation to Uniswap protocol",
                    }
                },
                "required": ["question"],
            },
        }
    ],
}

assistant = autogen.AssistantAgent(
    name="assistant",
    llm_config=llm_config,
)

user_proxy = autogen.UserProxyAgent(
    name="user_proxy",
    human_input_mode="NEVER",
    max_consecutive_auto_reply=10,
    code_execution_config={"work_dir": "."},
    llm_config=llm_config,
    system_message="""Reply TERMINATE if the task has been solved at full satisfaction.
Otherwise, reply CONTINUE, or the reason why the task is not solved yet.""",
    function_map={"answer_uniswap_question": answer_uniswap_question}
)

Now it’s time to submit a task.

user_proxy.initiate_chat(
    assistant,
    message="""
I'm writing a blog to introduce the version 3 of Uniswap protocol. Find the answers to the 3 questions below and write an introduction based on them.

1. What is Uniswap?
2. What are the main changes in Uniswap version 3?
3. How to use Uniswap?

Start the work now.
"""
)

You should be able to see the similar output as below:

user_proxy (to assistant):


I'm writing a blog to introduce the version 3 of Uniswap protocol. Find the answers to the 3 questions below and write an introduction based on them.

1. What is Uniswap?
2. What are the main changes in Uniswap version 3?
3. How to use Uniswap?

Start the work now.


--------------------------------------------------------------------------------
assistant (to user_proxy):

***** Suggested function Call: answer_uniswap_question *****
Arguments: 

{
  "question": "What is Uniswap?"
}
************************************************************

--------------------------------------------------------------------------------

>>>>>>>> EXECUTING FUNCTION answer_uniswap_question...
user_proxy (to assistant):

***** Response from calling function "answer_uniswap_question" *****

Uniswap is a decentralized cryptocurrency exchange that allows users to trade Ethereum-based tokens without the need for a centralized exchange. It is built on the Ethereum blockchain and uses automated market makers to facilitate trades.
********************************************************************

--------------------------------------------------------------------------------
assistant (to user_proxy):

***** Suggested function Call: answer_uniswap_question *****
Arguments: 

{
  "question": "What are the main changes in Uniswap version 3?"
}
************************************************************

--------------------------------------------------------------------------------

>>>>>>>> EXECUTING FUNCTION answer_uniswap_question...
user_proxy (to assistant):

***** Response from calling function "answer_uniswap_question" *****


Uniswap version 3 introduces a number of new features, including a new fee structure, improved liquidity provisioning, and a new liquidity mining program. It also includes a new user interface, improved gas efficiency, and a new governance system.
********************************************************************

--------------------------------------------------------------------------------
assistant (to user_proxy):

***** Suggested function Call: answer_uniswap_question *****
Arguments: 

{
  "question": "How to use Uniswap?"
}
************************************************************

--------------------------------------------------------------------------------

>>>>>>>> EXECUTING FUNCTION answer_uniswap_question...
user_proxy (to assistant):

***** Response from calling function "answer_uniswap_question" *****


To use Uniswap, you will need to have a web3 wallet such as MetaMask installed and connected to the Ethereum network. Once you have your wallet set up, you can go to the Uniswap website and connect your wallet. From there, you can select the tokens you want to trade and execute the swap.
********************************************************************

--------------------------------------------------------------------------------
assistant (to user_proxy):

Based on the answers to your questions, here is a draft introduction for your blog:

Uniswap is a revolutionary decentralized cryptocurrency exchange that operates on the Ethereum blockchain. It allows users to trade Ethereum-based tokens directly from their wallets, bypassing the need for a centralized exchange. Uniswap uses automated market makers to facilitate trades, providing a seamless and efficient trading experience.

The latest version, Uniswap version 3, brings a host of new features and improvements. It introduces a new fee structure, making transactions more cost-effective for users. The liquidity provisioning has been enhanced, allowing for more efficient use of capital. A new liquidity mining program has been introduced, providing incentives for users to provide liquidity to the platform. Additionally, Uniswap V3 boasts a new user interface, improved gas efficiency, and a revamped governance system, making it more user-friendly and democratic.

Using Uniswap is straightforward. All you need is a web3 wallet, such as MetaMask, connected to the Ethereum network. Once your wallet is set up, you can visit the Uniswap website, connect your wallet, select the tokens you wish to trade, and execute the swap. It's as simple as that!

Uniswap V3 is a significant step forward in the world of decentralized finance, providing users with more control, flexibility, and efficiency in their trading activities.

TERMINATE

--------------------------------------------------------------------------------
user_proxy (to assistant):

TERMINATE

--------------------------------------------------------------------------------

The Python notebook version of this showcase is available on Github: https://github.com/sugarforever/LangChain-Advanced/blob/main/Integrations/AutoGen/autogen_langchain_uniswap_ai_agent.ipynb

Looking forward to your feedback.

Have a nice day!

Langchain
Autogen
OpenAI
Llm
AI
Recommended from ReadMedium