LlamaIndex(GPT Index)を試す

jerryjliu/gpt_index: LlamaIndex (GPT Index) is a project that provides a central interface to connect your LLM's with external data.

LlamaIndex(GPT Index)を使うことで、大量の独自知識を詰め込んだオリジナルChatGPTを作る的なことができるらしい(?)ので素振りしてみる。

環境

インストール

普段全くPython書かないマンなのでライブラリの入れ方からドキュメントを見る。パッケージマネージャにpoetryとか使ったほうが良い気がするが、とりあえずスピード重視でREADME通りpip installする。

pip3 install llama-index

しばらく待つと、

ERROR: Command errored out with exit status 1:

無事こける。Warningでpipが古いと出ていた(WARNING: You are using pip version 19.2.3, however version 23.0.1 is available.)ので上げてみる。

pip3 install --upgrade pip

再チャレンジすると別のエラー。

error: package `rayon-core v1.11.0` cannot be built because it requires rustc 1.59 or newer, while the currently active rustc version is 1.58.1

rustcのバージョンが古いようなのでこちらも上げる。

rustup update

rustc 1.67.1 になった。

さらに再チャレンジ。今度はいくつかWarningは出たが通った。

OpenAI APIキーとdirenv設定

OpenAI APIキーを払い出す。

APIキーを .py ファイルにハードコードしたくないので、direnvを使って環境変数セットする。

touch .envrc

ファイルを以下のように編集する。

export OPENAI_API_KEY='sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxx'

以下コマンドを実行。

direnv allow

LlamaIndexのコードを書いて動かす

つい最近自分が書いた記事を読み込ませて質問するコードを書いて実行する

from llama_index import GPTSimpleVectorIndex, SimpleWebPageReader

documents = SimpleWebPageReader().load_data(['https://dev.classmethod.jp/articles/chatgpt-api-line-bot-aws-serverless/'])
index = GPTSimpleVectorIndex(documents)
print(index.query("ChatGPT APIでLINEボットを作るにあたり、文脈を保持するにはどうすればいい?")) # 質問をここに書く
python3 main.py

するとエラー。

ValueError: `html2text` package not found, please run `pip install html2text`

html2textパッケージをインストールする:

pip3 install html2text

再実行し、下記のエラー。

INFO:openai:error_code=None error_message='You exceeded your current quota, please check your plan and billing details.' error_param=None error_type=insufficient_quota message='OpenAI API error received' stream_error=False

Error Code 429 - You exceeded your current quota, please check your plan and billing details. | OpenAI Help Center

これはOpenAIで支払い設定してないことが原因っぽい。

Billing Overviewから支払い方法の設定をする。使いすぎることがないよう、Usage limitsからHardLimitを20ドルにしておく(何これすごい。AWSにもこの機能つけてほしい)。

ところがまだ同じエラーが出る。OpenAI APIキーを作り直してみた。

それからまた再実行すると、

INFO:root:> [query] Total LLM token usage: 4363 tokens
INFO:root:> [query] Total embedding token usage: 44 tokens

ChatGPT APIでLINEボットを作るにあたり、文脈を保持するには、DynamoDBを使用して会話の履歴を保存し、OpenAIからリリースされたChatGPTとWhisperのAPIを使用して、会話履歴を把握しながら回答する必要があります。

おおおおお。言ってることは若干不自然だけどポイントは抑えられていそう。

今日はここまで。

参考