? Megabots提供了最先进的生产Ready LLM应用程序,使您不必从头开始构建它们?现在创建一个机器人?
Megabots库可用于创建以下机器人:
? Megabots得到了一些最著名的生产AI的工具。它使用Langchain来管理LLM链,Langchain-Serve创建生产Ready API,Gradio来创建UI。目前,它使用OpenAI产生答案,但我们计划将来支持其他LLM。
注意:这是一项正在进行的工作。 API可能会改变。
pip install megabots from megabots import bot
import os
os . environ [ "OPENAI_API_KEY" ] = "my key"
# Create a bot with one line of code. Automatically loads your data from ./index or index.pkl.
# Keep in mind that you need to have one or another.
qnabot = bot ( "qna-over-docs" )
# Ask a question
answer = qnabot . ask ( "How do I use this bot?" )
# Save the index to save costs (GPT is used to create the index)
qnabot . save_index ( "index.pkl" )
# Load the index from a previous run
qnabot = bot ( "qna-over-docs" , index = "./index.pkl" )
# Or create the index from a directory of documents
qnabot = bot ( "qna-over-docs" , index = "./index" )
# Change the model
qnabot = bot ( "qna-over-docs" , model = "text-davinci-003" )您可以更改机器人启示,以根据您的需求将其自定义。在qna-over-docs类型的bot类型中,您需要通过2个context (从索引搜索)和question (人类问题)。
from megabots import bot
prompt = """
Use the following pieces of context to answer the question at the end.
If you don't know the answer, just say that you don't know, don't try to make up an answer.
Answer in the style of Tony Stark.
{context}
Question: {question}
Helpful humorous answer:"""
qnabot = bot ( "qna-over-docs" , index = "./index.pkl" , prompt = prompt )
qnabot . ask ( "what was the first roster of the avengers?" )您可以使用memory参数轻松地将内存添加到bot 。它接受一个带有要使用的内存类型的字符串。这默认为某些理智的dafaults。如果您需要更多的配置,则可以使用memory功能并传递内存类型和所需的配置。
from megabots import bot
qnabot = bot ( "qna-over-docs" , index = "./index.pkl" , memory = "conversation-buffer" )
print ( qnabot . ask ( "who is iron man?" ))
print ( qnabot . ask ( "was he in the first roster?" ))
# Bot should understand who "he" refers to.或使用memory工厂功能
from megabots import bot , memory
mem ( "conversation-buffer-window" , k = 5 )
qnabot = bot ( "qna-over-docs" , index = "./index.pkl" , memory = mem )
print ( qnabot . ask ( "who is iron man?" ))
print ( qnabot . ask ( "was he in the first roster?" ))注意:对于qna-over-docs bot时,使用内存并传递自定义提示时,请记住要将一个变量传递给自定义提示,以促进聊天历史记录。变量名称是history 。
from megabots import bot
prompt = """
Use the following pieces of context to answer the question at the end.
If you don't know the answer, just say that you don't know, don't try to make up an answer.
{context}
{history}
Human: {question}
AI:"""
qnabot = bot ( "qna-over-docs" , prompt = prompt , index = "./index.pkl" , memory = "conversation-buffer" )
print ( qnabot . ask ( "who is iron man?" ))
print ( qnabot . ask ( "was he in the first roster?" )) Megabots bot还可以将Milvus用作其搜索引擎的后端。您可以在下面找到一个示例。
为了运行Milvus,您需要遵循本指南以下载Docker撰写文件并运行它。命令是:
wget https://raw.githubusercontent.com/milvus-io/pymilvus/v2.2.7/examples/hello_milvus.py然后,您可以安装ATTU作为Milvus的管理工具
from megabots import bot
# Attach a vectorstore by passing the name of the database. Default port for milvus is 19530 and default host is localhost
# Point it to your files directory so that it can index the files and add them to the vectorstore
bot = bot ( "qna-over-docs" , index = "./examples/files/" , vectorstore = "milvus" )
bot . ask ( "what was the first roster of the avengers?" )或使用vectorstore工厂功能进行更多自定义
from megabots import bot , vectorstore
milvus = vectorstore ( "milvus" , host = "localhost" , port = 19530 )
bot = bot ( "qna-over-docs" , index = "./examples/files/" , vectorstore = milvus )您也可以使用兰班链守路在本地露出机器人端点。 megabots文件夹中提供了示例文件api.py
要在本地公开API,您可以做
lc-serve deploy local megabots.api然后,您应该可以访问http://localhost:8000/docs以查看和与API文档进行交互。
要将API部署到云中,您可以使用输出中提供的端点进行操作并连接到API。
lc-serve deploy jcloud megabots.api您可以使用create_interface函数将gradio ui曝光。假设您的文件称为ui.py run gradio qnabot/ui.py可以在本地运行UI。然后,您应该可以访问http://127.0.0.1:7860查看API文档。
from megabots import bot , create_interface
demo = create_interface ( bot ( "qna-over-docs" ))bot功能应作为创建和自定义机器人的起点。以下是bot中可用参数的列表。
| 争论 | 描述 |
|---|---|
| 任务 | 要创建的机器人的类型。可用选项: qna-over-docs 。更快地开始 |
| 指数 | 指定用于机器人的索引。它可以是保存的索引文件(例如, index.pkl )或文档目录(例如, ./index )。在目录的情况下,索引将自动创建。如果未指定索引bot将寻找index.pkl或./index |
| 模型 | 用于机器人的模型名称。您可以通过提供其名称来指定不同的模型,例如“ Text-Davinci-003”。受支持的模型: gpt-3.5-turbo (默认), text-davinci-003即将开始。 |
| 迅速的 | 提示的字符串模板,该模板定义了传递给模型的问题和上下文的格式。模板应包括占位符变量,例如: context , {question} ,在使用内存history的情况下。 |
| 记忆 | 机器人要使用的内存类型。可以是具有内存类型的字符串,也可以使用memory工厂功能。支持的回忆: conversation-buffer , conversation-buffer-window |
| 矢量店 | 用于索引的矢量店。可以是带有数据库名称的字符串,也可以使用vectorstore Factory函数。支持的DBS: milvus 。 |
大型语言模型(LLM)很强大,但他们无法回答有关他们从未见过的文档的问题。如果您想使用LLM回答有关未经培训的文档的问题,则必须提供有关这些文件的信息。为了解决这个问题,我们使用“检索增强产生”。
简而言之,当您有问题时,您首先搜索相关文档。然后,您将文档和问题提供给语言模型以生成答案。为了完成这项工作,您需要以可搜索格式的文档(索引)。此过程涉及两个主要步骤:(1)准备文档以易于查询,以及(2)使用检索增强生成方法。
qna-over-docs使用FAISS创建文档和GPT索引来生成答案。
测序图
演员用户
参与者API
参与者LLM
参与者矢量店
参与者摄取
参与者数据纳克
自动化器
请注意API,数据级:摄入阶段
每X时间循环
摄入ENGENGINE- >> DataLake:加载文档
DataLake- >> IngestionEngine:返回数据
摄入EngestionEngine- >>摄入EngestionEngine:拆分文档并创建嵌入式
摄入EngestionEngine- >>矢量店:存储文档和嵌入
结尾
注意API,数据纳克:生成阶段
用户 - >> API:接收用户问题
API- >>向量店:与问题相关的索引中的查找文档
API- >> API:从问题和任何相关文档中构造提示
API- >> LLM:将提示传递给模型
LLM- >> API:从模型中获取响应
API- >>用户:返回响应