Welcome to the world of AI artificial intelligence, every day brings new breakthroughs and capabilities.
in this Post, I will experiment with integrating AutoGen and Function calling features. Function Calling previously established in the Open AI API, is a mechanism that allows you to adapt calls to external APIs. This integration aims to enhance the overall functionality and efficiency of the system
For example, it can be used to call the weather API as needed during a conversation.
there are various combinations of function calling and Agent, but here we will call the RAG Retrieval Augmented Generation mechanism via Function Calling and use the result to generate output.
In this step-by-step guide, you’ll learn how to use Langchian, Autogen, Retrieval Augmented Generation (RAG) and Function calls to build a super AI chatbot
For those who may not be as familiar with technical details, let me provide a bit of background to make things easier to understand.
What is Langchain?
LangChain is an open-source library that provides developers with the tools to build LLM applications powered by large language models (LLMs), such as OpenAI or Hugging Face. This allows you to build dynamic, data-responsive applications that harness the most recent breakthroughs in natural language processing.
LangChain is a framework that enables developers to build agents that can reason about problems and break them into smaller sub-tasks.
What is Autogen?
AutoGen is not just a tool, it’s the future of collaborative AI, where multiple agents come together to transform ideas into reality and where AI Agents Unite, Innovate, and Elevate.
Put simply, AutoGen LangChain are both frameworks for developing LLM-driven applications. However, there are some key differences between the two.
AutoGen is a multi-agent framework, while LangChain is a single-agent framework
AutoGen is more focused on code generation, while LangChain is more focused on general-purpose NLP tasks
What is Retrieval Augmented Generation?
Retrieval Augmented Generation is an AI framework that retrieves data from external sources of Knowledge to improve the quality of responses. it ensures accuracy through techniques like vector similarity search and real-time updates to external datasets.
what is Function Call?
Function call openai simplifies the creation of chatbots that communicate with external tools and APIs.
In other words, function calls help developers to describe functions to the model and have the model intelligently choose to output a JSON object
Else See: How To Build a LLava chatbot
let’s start coding
let’s start installing the requirements. Assuming you have created a new Python project and set up a virtual environment, run the command:
!pip install langchain , "pyautogen[retrievechat]" , PyPDF2 , faiss-gpu
let’s import the required dependencies:
import autogen
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import FAISS
from langchain.llms import OpenAI
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationalRetrievalChain
from PyPDF2 import PdfReader
from langchain.text_splitter import RecursiveCharacterTextSplitter
Step 1: Configuring AutoGen and API Key
let’s set up the list to AutoGen, we create the config_list
as follows:
config_list :
is a list containing configuration settings for the model you intend to use.
Seed :
we set the seed
to 42 for cashing purposes.
With this configuration, we are ready to use AI Agents with AutoGen.
config_list = [
{
"model": "gpt-4-1106-preview",
"api_key": "openai_api",
}
]
llm_config_proxy = {
"seed": 42, # change the seed for different trials
"temperature": 0,
"config_list": config_list,
"request_timeout": 600
}
Step 2: Read PDF
In this snippet:
- we upload a PDF file and process it, PyPDF2 is used to read the PDF file
- text splitter from langchain is used to split the text into chunks.
- OpenAIEmbeddings is used for embedding the PDF file then FAISS is used to create a vectorstore
- Faiss is used to convert the text chunks into vector embeddings. These vectors can then be used for various applications like similarity search
reader = PdfReader('/content/openchat.pdf')
corpus = ''.join([p.extract_text() for p in reader.pages if p.extract_text()])
splitter = RecursiveCharacterTextSplitter(chunk_size=1000,chunk_overlap=200,)
chunks = splitter.split_text(corpus)
embeddings = OpenAIEmbeddings(openai_api_key = openai_api)
vectors = FAISS.from_texts(chunks, embeddings)
Step 3: Conversation Retrieval
Once the database has been created, we can then query it.
- We are making use of the ConversationalRetrievalChain from Langchain to connect our similarity search to the prompts–user input.
- let call ConversationBufferMemory is a simple memory buffer that stores the history of the conversation.
qa = ConversationalRetrievalChain.from_llm(
OpenAI(temperature=0),
vectors.as_retriever(),
memory=ConversationBufferMemory(memory_key="chat_history",
return_messages=True),
)
Step 4: Specify Config for the Assistant Agent.
AutoGen Agents supports function calling for OpenAI models. But we need to specify the function using the following snippet:
llm_config_assistant = {
"Seed" : 42,
"temperature": 0,
"functions": [
{
"name": "answer_PDF_question",
"description": "Answer any PDF related questions",
"parameters": {
"type": "object",
"properties": {
"question": {
"type": "string",
"description": "The question to ask in relation to PDF",
}
},
"required": ["question"],
},
}
],
"config_list": config_list,
"timeout": 120,
}
Step 5: Configuring the Assistant Agent
Let’s create an automated assistant agent named “assistant” with specific configurations.
the assistant is designed to interact with users and provide accurate responses. this allows the assistant to read a PDF and generate (LLM) an accurate answer
assistant = autogen.AssistantAgent(
name="assistant",
llm_config=llm_config_assistant,
system_message="""You are a helpful assistant, Answer the question based on the context.
Keep the answer accurate. Respond "Unsure about answer" if not sure about the answer."""
)
Step 6: Configuring the UserProxy Agent.
User Proxy Agents are the types of AI Agents that work on behalf of the user.
The implementation is typical, but it includes a unique feature: the function_map
parameter. This parameter is used to link the configuration for function calls with the actual functions themselves, ensuring seamless integration and operation.
user_proxy = autogen.UserProxyAgent(
name="user_proxy",
human_input_mode="NEVER",
max_consecutive_auto_reply=10,
code_execution_config={"work_dir": "coding"},
# llm_config_assistant = llm_config_assistant,
function_map={
"answer_PDF_question": answer_PDF_question
}
)
Once the agents are set up, the script starts a conversation between the user and the chatbot. This is done by calling the initiate_chat
method on the user_proxy
object. The initiate_chat
the method requires two parameters: the assistant
instance, which acts as the chatbot, and a text message that outlines the task to be discussed.
user_proxy.initiate_chat(
assistant,
message="""
Write a Openchat word blog post titled why openchat better than GPT3 that uses the exact keyword OpenChat
at least once every 100 words. The blog post should include an introduction, main body,
and conclusion. The conclusion should invite readers to leave a comment. The main
body should be split into at least 4 different subsections.
"""
)
The result looks like this.
user_proxy (to assistant):
Write a Openchat word blog post titled why openchat better than GPT3 that uses the exact keyword OpenChat
at least once every 100 words. The blog post should include an introduction, main body,
and conclusion. The conclusion should invite readers to leave a comment. The main
body should be split into at least 4 different subsections.
--------------------------------------------------------------------------------
assistant (to user_proxy):
# Why OpenChat is Better Than GPT-3
## Introduction
In the ever-evolving landscape of artificial intelligence, OpenChat has emerged as a groundbreaking platform, offering a unique set of capabilities that set it apart from its predecessors like GPT-3. In this blog post, we will delve into the reasons why OpenChat is not just a step ahead but a leap forward in AI communication technology.
## Main Body
### Enhanced Contextual Understanding
OpenChat's ability to understand context surpasses that of GPT-3. It can maintain the thread of a conversation over a longer period, which allows for more coherent and meaningful interactions. This is particularly beneficial in customer service applications where conversations can be complex and require a deep understanding of the issue at hand.
### Superior Customization
One of the key advantages of OpenChat is its superior customization options. Unlike GPT-3, OpenChat can be tailored to fit the specific needs of any business or application. This means that it can adhere to brand voice, manage specialized knowledge bases, and integrate seamlessly with existing systems, providing a more personalized experience for users.
### Advanced Learning Capabilities
OpenChat is designed to learn and adapt more efficiently than GPT-3. It can quickly incorporate new information and adjust its responses accordingly. This continuous learning process ensures that OpenChat remains up-to-date with the latest data, trends, and user preferences, making it an invaluable tool for dynamic and fast-paced environments.
### Open-Source Community
The open-source nature of OpenChat is a game-changer. It allows developers from around the world to contribute to its development, leading to rapid innovation and improvement. This collaborative approach ensures that OpenChat is constantly evolving and benefiting from the collective expertise of a global community, unlike the more closed ecosystem of GPT-3.
## Conclusion
OpenChat represents a significant advancement in AI-powered communication, offering enhanced contextual understanding, superior customization, advanced learning capabilities, and the support of an open-source community. Its ability to provide more nuanced and adaptable interactions makes it a superior choice for businesses and developers looking to harness the power of AI.
We invite you to share your thoughts and experiences with OpenChat and GPT-3. Have you noticed the differences in your interactions? Leave a comment below and join the conversation about the future of AI communication.
Else See: how Powerful AutoGen is Reshaping LLM
Conclusion :
In this article, we explained how I use AutoGen, langchain, Function call and Retrieval Augmented Generation to create a super AI chatbot. When these components are combined the responses will be significantly more powerful and versatile. It would be capable of handling complex tasks more efficiently, generating more relevant and context-aware content, and accessing a wider range of information to inform its responses.
Reference :
- https://python.langchain.com/docs/get_started/introduction
- https://github.com/microsoft/autogen
Thanks, the definition of function answer_PDF_question is missing?
no is not missing , please i use the google colab to store my PDF i still remember i used the read pdf function please check carefully
no, it is missing. I check the code thoroughly…Can you pls share link to your github.. Thanks,
function_map={
“answer_PDF_question”: llm_config_assistant[“functions”][0][“name”]
}
use this
thank you i will fix it
Where is the whole file, I do not see where you used RAG where did you use the qa_chain to create the RAG query with the assistant agent?
Please check Step 3 where i introduce the RAG
HE is correct. You defined QA, but did not use anywhere
Hello there, I found your web site by means of Google even as looking for a comparable matter, your site got here up, it appears good.
I’ve bookmarked it in my google bookmarks.
Hello there, simply turned into alert to your blog via Google, and located
that it’s truly informative. I am going to be careful for brussels.
I will be grateful if you continue this in future. Numerous people will probably be benefited from your writing.
Cheers!
RuntimeError: Code execution is set to be run in docker (default behaviour) but docker is not running.
The options available are:
– Make sure docker is running (advised approach for code execution)
– Set “use_docker”: False in code_execution_config
– Set AUTOGEN_USE_DOCKER to “0/False/no” in your environment variables
getting above error