LLM之Agent(六)| 使用AutoGen、LangChian、RAG以及函数调用构建超级对话系统

LLM之Agent(六)| 使用AutoGen、LangChian、RAG以及函数调用构建超级对话系统_第1张图片

       本文我们将尝试AutoGen集成函数调用功能。函数调用最早出现在Open AI API中,它允许用户调用外部API来增强系统的整体功能和效率。例如,在对话过程中根据需要调用天气API。

      函数调用和Agent有各种组合,在这里我们将通过函数调用调用RAG检索增强生成机制,并使用结果生成输出。

     本文将介绍如何使用LangchianAutogenRetrieval Augmented Generation(RAG)函数调用来构建超级AI聊天机器人。

一、什么是Langchain?

      LangChain是一个开源库,为开发人员提供了构建由大型语言模型(LLM)支持的LLM应用程序的工具,如OpenAI或Hugging Face。可以构建动态的、响应数据的应用程序,利用自然语言处理方面的最新突破。

       LangChain是一个框架,使开发人员能够构建能够推理问题并将其分解为较小子任务的代理。

二、什么是Autogen?

       AutoGen不仅仅是一种工具,它也是协作人工智能的未来,多个智能体聚集在一起,将想法转化为现实,人工智能智能体团结、创新和提升。

      简单地说,AutoGen和LangChain都是用于开发LLM驱动的应用程序的框架。然而,两者之间存在一些关键区别:

  • AutoGen是一个多智能体框架,而LangChain是一个单智能体框架;
  • AutoGen更专注于代码生成,而LangChain更专注于通用NLP任务

三、什么是检索增强生成?

       检索增强生成RAG是一种人工智能框架,它从外部知识来源检索数据,以提高响应质量。它通过矢量相似性搜索和对外部数据集的实时更新等技术来确保准确性。

四、什么是函数调用?

       函数调用简化了与外部工具和API通信的聊天机器人的创建。

       换句话说,函数调用帮助开发人员向模型描述函数,并让模型智能地选择输出JSON对象。

五、搭建超级对话系统

安装环境以及所需要的包,命令如下:

!pip install langchain , "pyautogen[retrievechat]" , PyPDF2 , faiss-gpu

导入相关包

import autogenfrom langchain.embeddings import OpenAIEmbeddingsfrom langchain.vectorstores import FAISSfrom langchain.llms import OpenAIfrom langchain.memory import ConversationBufferMemoryfrom langchain.chains import ConversationalRetrievalChainfrom PyPDF2 import PdfReaderfrom langchain.text_splitter import RecursiveCharacterTextSplitter

步骤1:配置AutoGen和API密钥

AutoGen的配置文件是一个名为config_list的list:

config_list:是一个列表,其中包含使用的模型的配置;

seed:设置为42;

有了这个配置,下面看一下如何使用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}

步骤2:读取PDF文件

  1. 我们上传一个PDF文件并进行处理,使用PyPDF2读取PDF文件;

  2. 使用langchain中的text splitter将文本分割成chunk;

  3. 使用OpenAIEmbeddings嵌入PDF文件,然后FAISS存储在向量数据库中;

  4. Faiss可以将文本chunk转换为embedding。然后,这些向量可以用于各种应用,如相似性搜索。

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)

步骤3:会话检索

一旦创建了数据库,我们就可以对其进行查询。

  1. 我们就可以使用Langchain的ConversationalRetrievalChain对用户的Prompt进行相似性搜索;

  2. let call ConversationBufferMemory是一个简单的内存缓冲区,用于存储会话的历史记录。

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

步骤4:指定Assistant代理的配置

       AutoGen Agent支持对OpenAI模型的函数调用,但我们需要使用以下代码段指定函数:

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,}

步骤5:配置Assistant Agent

        让我们创建一个名为“assistant”的具有特定配置的自动化助理代理。我们使用该assistant阅读PDF并生成准确的答案。

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."""                    )

步骤6:配置UserProxy代理。

       User Proxy代理包括一个独特的功能:function_map参数,此参数用于将函数调用的配置与实际函数本身链接起来,确保无缝集成和操作。

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            }        )

       一旦设置了代理,该脚本就会启动用户和聊天机器人之间的对话。这是通过调用user_proxy对象上的initiate_chat方法来完成的。initiate_chat方法需要两个参数:充当聊天机器人的assistant实例和描述任务的文本消息。

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.""")

结果如下所示:

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## IntroductionIn 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 UnderstandingOpenChat'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 CustomizationOne 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 CapabilitiesOpenChat 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 CommunityThe 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.## ConclusionOpenChat 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.

结论:

       在这篇文章中,我们解释了如何使用AutoGen、langchain、函数调用和检索增强生成来创建一个超级AI聊天机器人。当这些组件结合在一起时,能够更有效地处理复杂的任务,生成更相关和更了解上下文的内容,响应将更加强大和通用。

参考文献:

[1] https://levelup.gitconnected.com/autogen-langchian-rag-function-call-super-ai-chabot-3951911607f2

[2] https://www.microsoft.com/en-us/research/blog/autogen-enabling-next-generation-large-language-model-applications/

[3] https://github.com/microsoft/autogen

[4] https://python.langchain.com/docs/get_started/introduction

[5] https://www.microsoft.com/en-us/research/blog/autogen-enabling-next-generation-large-language-model-applications/

你可能感兴趣的:(ChatGPT,笔记,NLP,chatgpt,人工智能,AIGC)