? 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- >>用戶:返迴響應