LMQL(Language Model Query Language)とLlamaIndexを接続してみる
LMQL(Language Model Query Language)のクエリ内ではかなり自由にPythonのコードを組み合わせることができます。
前回は最後の方でLangChainとの連携例について書いたので、今回はLlamaIndexとの連携例について書いてみたいと思います。
前回の記事はコチラ↓
コード例
LlamaIndexとの連携例で定番であるPaul Grahamのエッセイ(テキストデータ)を元データにしたクエリをLMQLで書いてみたいと思います。
まずはPaul GrahamのエッセイをLlamaIndexでインデックス化します。テキストデータは配下のdataディレクトリ内に格納しておきます。
from llama_index import GPTSimpleVectorIndex, SimpleDirectoryReader, ServiceContext
import lmql
documents = SimpleDirectoryReader('data').load_data()
service_context = ServiceContext.from_defaults(chunk_size_limit=512)
index = GPTSimpleVectorIndex.from_documents(documents, service_context=service_context)
次にLMQLクエリを記述します。ユーザーからのクエリから関連情報を引っ張ってくるようなLlamaIndexのコードと組み合わせています。
@lmql.query
async def index_query(query_input: str):
'''
argmax(max_len=4096)
"You are a QA bot that helps people find answers to their questions.\n"
response = index.query(query_input, similarity_top_k=3)
"Question: {query_input}\n"
"Relevant Information: {str(response)}\n"
"Your response based on relevant information: [RESPONSE]"
from
"chatgpt"
where
STOPS_AT(RESPONSE, "\n")
'''
このクエリを実行すると以下のようなレスポンスを取得できます。ちなみにLMQLのoutput_writerオプションにlmql.stream()を指定すると、指定した出力変数をストリーミングで出力してくれます。
response = await index_query(
"What did the author do growing up?",
output_writer=lmql.stream(variable="RESPONSE")
)
ちなみにマルチバイト対応は…
ちなみに気になるマルチバイト対応ですが、LMQLクエリを以下のように書き換え、日本語で応答してもらえるように変更したところ・・・
@lmql.query
async def index_query(query_input: str):
'''
argmax(max_len=4096)
"You are a QA bot that helps people find answers to their questions.\n"
response = index.query(query_input, similarity_top_k=3)
"Question: {query_input}\n"
"Relevant Information: {str(response)}\n"
"Your response based on relevant information in Japanese: [RESPONSE]\n"
from
"chatgpt"
where
STOPS_AT(RESPONSE, "\n")
'''
response = await index_query("著者はどのようにして育ちましたか?")
print(response[0].variables["RESPONSE"])
文字化けしてしまう様子でした。
中のコードは結構複雑で、サクッとプルリクを送るようなカッコ良いことができなかったので、イシューを作成しておきました。
現場からは以上です。
この記事が気に入ったらサポートをしてみませんか?