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

news2024/9/24 3:26:18

       本文我们将尝试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/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1313032.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Windows 7如何将分区标记为活动分区?

引导分区,也称为引导卷,是包含引导加载程序和Windows操作系统所需文件的磁盘分区,是正常启动计算机的前提。一个正确的引导分区不只要包含相关的可引导数据,还需要将分区标记为活动分区。那么,如何将Windows 7分区标记…

网络安全——基于Snort的入侵检测实验

一、实验目的要求: 二、实验设备与环境: 三、实验原理: 四、实验步骤: 五、实验现象、结果记录及整理: 六、分析讨论与思考题解答: 七、实验截图: 一、实验目的要求: 1、掌握…

SuperMap iPortal权限介绍

作者:yx 文章目录 前言一、内置权限二、自定义权限(11.1.0及以后版本)1、修改配置文件2、页面展示3、api调用4、结果验证5、实际应用 前言 iPortal 用户访问和使用门户中资源的能力取决于其用户类型与在门户中拥有的权限,权限通过…

深入分析ClassLocader工作机制

文章目录 一、ClassLoader简介1. 概念2. ClassLoader类结构分析 二、ClassLoader的双亲委派机制三、Class文件的加载流程1. 简介2. 加载字节码到内存3. 验证与解析4. 初始化Class对象 四、常见加载类错误分析1. ClassNotFoundException2. NoClassDefFoundError3. UnsatisfiledL…

2023.12.13 关于 MySQL 复杂查询

目录 聚合查询 聚合函数 group by 子句 执行流程图 联合查询 笛卡尔积 内连接 外连接 左外连接 右外连接 自连接 子查询 单行子查询 多行子查询 EXISTS 关键字 合并查询 union on 和 union 的区别 聚合查询 聚合函数 函数说明COUNT([DISTINCT] expr)返回查询到…

wpf TelerikUI使用DragDropManager

首先,我先创建事务对象ApplicationInfo,当暴露出一对属性当例子集合对于构成ListBoxes。这个类在例子中显示如下代码: public class ApplicationInfo { public Double Price { get; set; } public String IconPath { get; set; } public …

【【ZYNQ 7020显示 图片 实验 】】

ZYNQ 7020显示 图片 实验 关键配置 BRAM 因为本次 我想显示的 图片是 400*400 所以在 内部 的 ROM 存储单元选择 了160000 ZYNQ7020的内部资源 最多是 大概 200000左右的 大小 大家可以根据 资源选择合适的像素 此处存放 内部的 图片转文字的COE文件 PLL设置 我选用的是按…

详解wmvcore.dll丢失的解决方法

wmvcore.dll是一款由Microsoft开发的Windows系统文件,主要用于存储和处理多媒体文件,尤其是Windows媒体视频。该文件对于音频和视频的播放至关重要。如果电脑上缺少这个文件,可能会出现播放问题或者相关的应用程序运行错误。在本文中&#xf…

Netty详解

目录标题 1、前期知识科普1.1 NIO 基本概念1.2 java BIO与NIO对比1.3 Reactor 模型 2、Netty 基础概念2.1 Netty 简介2.2 Netty 执行流程2.3 Netty 核心组件 3、Netty Demo编写3.1 总体框架3.2 具体代码 4、交流群 1、前期知识科普 1.1 NIO 基本概念 阻塞(Block&a…

0x21 树与图的遍历

0x21 树与图的遍历 树与图最常见的储存方式就是使用一个邻接表保存它们的边集。邻接表以head数组为表头,使用ver和edge数组分别存储边的终点和权值,使用next数组模拟链表指针(就像我们在0x13节中讲解邻接表所给出的代码那样)。 …

科技铸就企业转型钢筋铁骨,群硕获评2023年度数字化影响力企业

12月15日,STIF2023第四届国际科创节暨DSC2023国际数字服务大会在北京顺利举行,本次大会以“数实融合 推动高质量发展”为主题,各大科技服务企业齐聚一堂,共同探讨2023科技发展新趋势。 大会上,群硕软件继2022年后再度…

Java版商城:Spring Cloud+SpringBoot b2b2c实现多商家入驻、直播带货及免 费小程序商城搭建

1. 涉及平台 平台管理、商家端(pc端、手机端)、买家平台(h5/公众号、小程序、app端(ios/android)、微服务平台(业务服务) 2. 核心架构 spring cloud、spring boot、mybatis、redis 3. 前端框架…

如何远程访问Axure RP制作的本地web站点实现协同办公

文章目录 前言1.在AxureRP中生成HTML文件2.配置IIS服务3.添加防火墙安全策略4.使用cpolar内网穿透实现公网访问4.1 登录cpolar web ui管理界面4.2 启动website隧道4.3 获取公网URL地址4.4. 公网远程访问内网web站点4.5 配置固定二级子域名公网访问内网web站点4.5.1创建一条固定…

浅析AI视频分析与视频管理系统EasyCVR平台及场景应用

人工智能的战略重要性导致对视频智能分析的需求不断增加。鉴于人工智能视觉技术的巨大潜力,人们的注意力正在从传统的视频监控转移到计算机视觉的监控过程自动化。 1、什么是视频分析? 视频分析或视频识别技术,是指从视频片段中提取有用信息…

java.lang.UnsupportedOperationException

一、背景 记录一次小坑… 最近在写一个关于Excel导出的小需求,由于系统都有一些工具类,还有原来已经做好的导出,直接拿过来改了改就用了,没想到直接报错,尴尬。 还是那句话,别人都能用,我复制…

innovus:ccopt_design流程

我正在「拾陆楼」和朋友们讨论有趣的话题,你⼀起来吧? 拾陆楼知识星球入口 ccopt完整的流程包括如下几个步骤: spec文件可以只创建一次,无需多次创建。 1)clustering阶段 set_ccopt_property balance_mode cluster …

产品经理之Axure的元件库使用详细案例

⭐⭐ 产品经理专栏:产品专栏 ⭐⭐ 个人主页:个人主页 ​ 目录 前言 一.Axure的元件库的使用 1.1 元件介绍 1.2 基本元件的使用 1.2.1 矩形、按钮、标题的使用 1.2.2 图片及热区的使用 1.3 表单元件及表格元件的使用 1.3.1表单元件的使用 1.3.…

NFS|在linux环境下的安装和配置NFS

简介 NFS全称网络文件系统,可用于不同服务器之间的文件共享。 接下来介绍下NFS在linux环境下安装和配置。主要分为服务端和客户端。 服务端安装 开启rpcbind/portmap和nfs服务 # service portmaper start [rootlocalhost java]# service portmap start Redirectin…

低代码平台浅析:引迈JNPF

低代码平台能够改变应用交付和管理的模式,大幅缩减交付周期,最终帮助业务加速创新。引迈JNPF作为当中的一个低代码平台,其在用户体系方面做得怎样呢?我针对引迈JNPF进行了相关体验与测评,一起来看下。 低代码平台体验简…

SpringBoot Starter机制 ——自动化配置

目录 一、Starter机制 1.1 什么是 SpringBoot Starter 1.2 SpringBoot Starter 的作用 1.3 Starter的应用场景 二、案例 2.1 模拟短信发送模版 2.2 AOP实现日志切面模版 一、Starter机制 1.1 什么是 SpringBoot Starter Spring Boot Starter是Spring Boot框架提供的一种…