? Megabotsは、最先端のプロダクションReady LLMアプリをMega Eastyにしているので、ゼロから構築する必要はありませんか?今、ボットを作成しますか?
Megabotsライブラリを使用して、次のボットを作成できます。
?メガボットは、AIを生産するための最も有名なツールのいくつかに支えられています。 LANGCHAINを使用して、LLMチェーン、Langchain-Serveを管理して、Production 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タイプのボットでは、 context (インデックスから検索されたknwoledge)とquestion (人間の質問)に2つの変数を渡す必要があります。
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にメモリを簡単に追加できます。使用するメモリのタイプを持つ文字列を受け入れます。これは、いくつかの正気なダフォールトにデフォルトです。より多くの構成が必要な場合は、 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ボットの場合、メモリを使用してカスタムプロンプトを渡す場合、チャット履歴を容易にするために、カスタムプロンプトにもう1つの変数を渡すことを忘れないでください。変数名は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 Composeファイルをダウンロードして実行する必要があります。コマンドは次のとおりです。
wget https://raw.githubusercontent.com/milvus-io/pymilvus/v2.2.7/examples/hello_milvus.pyその後、Milvusの管理ツールとしてATTUをインストールできます
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 Factory機能を使用します
from megabots import bot , vectorstore
milvus = vectorstore ( "milvus" , host = "localhost" , port = 19530 )
bot = bot ( "qna-over-docs" , index = "./examples/files/" , vectorstore = milvus ) Langchain-serveを使用して、ボットエンドポイントをローカルに公開することもできます。サンプルファイルapi.pyは、 megabotsフォルダーに提供されています。
APIをローカルに公開するには、できます
lc-serve deploy local megabots.apiその後、 http://localhost:8000/docsにアクセスして、APIドキュメントを表示およびやり取りできるはずです。
APIをクラウドに展開するには、出力で提供されているエンドポイントを使用してAPIに接続できます。
lc-serve deploy jcloud megabots.apicreate_interface関数を使用して、ボットのグラデーションUIを公開できます。ファイルがui.py run gradio qnabot/ui.pyと呼ばれると仮定して、uiをローカルに実行します。その後、 http://127.0.0.1:7860 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 | インデックスに使用されるベクトルストア。データベースの名前を持つ文字列にすることも、 vectorstore Factory機能を使用できます。サポートされているDBS: milvus 。 |
大規模な言語モデル(LLM)は強力ですが、見たことのないドキュメントに関する質問に答えることはできません。 LLMを使用して、訓練されていないドキュメントに関する質問に答えたい場合は、それらのドキュメントに関する情報を提供する必要があります。これを解決するには、「検索拡張生成」を使用します。
簡単に言えば、質問がある場合は、まず関連文書を検索します。次に、文書と質問を言語モデルに提供して、回答を生成します。この作業を行うには、検索可能な形式(インデックス)でドキュメントが必要です。このプロセスには、2つの主要な手順が含まれます。(1)簡単なクエリのためにドキュメントを準備し、(2)検索拡張生成法を使用します。
qna-over-docs FAISSを使用してドキュメントのインデックスを作成し、GPTのインデックスを作成して回答を生成します。
Sequendediagram
俳優ユーザー
参加者API
参加者LLM
参加者VectorStore
参加者のIngestionEngine
参加者のDatalake
Autonumber
API、Datalake:摂取段階に注意してください
x時間ごとにループします
IngestionEngine- >> Datalake:ドキュメントを読み込みます
Datalake- >> IngestionEngine:データを返します
IngestionEngine- >> IngestionEngine:ドキュメントを分割し、埋め込みを作成します
IngestionEngine- >> VectorStore:ドキュメントと埋め込みを保存します
終わり
API、Datalake:Generation Phaseに注意してください
ユーザー - >> API:ユーザーの質問を受け取ります
API- >> VectorStore:質問に関連するインデックスのルックアップドキュメント
API- >> API:質問と関連するドキュメントからプロンプトを作成します
API- >> LLM:プロンプトをモデルに渡します
LLM- >> API:モデルから応答を取得します
API- >>ユーザー:応答を返します