Azure OpenAI dataSourcesパラメータによるRAGリクエストについてまとめた。
RAG(Retrieval-Augmented Generation, 外部知識ベースから検索して正確な情報に基づいてLLMに回答させるIcLを応用した手法)についてはLangchainを使う場合、RetrievalQAやRetrievalQAWithSourcesChainを利用する。
https://docs.pinecone.io/docs/langchain
しかしこれはAPIクライアント(エージェント)側処理であるが、LLMのAPI側の仕様にてこれを設定して、関連する質問の回答をしてしまうことができるのがdataSourcesパラメータである。
dataSourcesパラメータ
APIバージョン 2023-06-01-preview よりAzure OpenAI ChatCompletion(入力候補)の拡張機能でdataSourcesパラメータが追加されており、RAGによるドキュメント参照回答を1リクエストで行うことが可能となっている。
コマンドでの利用方法
RESOURCE_ENDPOINT="https://[].openai.azure.com/"
DEPLOY_NAME=""
KEY=""
curl -X POST ${RESOURCE_ENDPOINT}openai/deployments/${DEPLOY_NAME}/extensions/chat/completions?api-version=2023-06-01-preview \
-H "Content-Type: application/json" \
-H "api-key: ${KEY}" \
-d \
'{ "dataSources": [ {
"type": "AzureCognitiveSearch",
"parameters": {
"endpoint": "https://[].search.windows.net",
"key": "[cognitive searchのkey]",
"indexName": "[インデックス名]",
"semanticConfiguration": "[セマンティック構成名]"
}}],
"messages": [
{"role": "system", "content": "You are an AI assistant that primarily uses Japanes. You answer users questions."},
{"role": "user", "content": "gpt-4のトークンサイズは?"} ] }' -s |jq '.choices[0].messages[1].content'
モデルはgpt-4, gpt-35-turbo-16kのみ対応しており、4kトークンのgpt-35-turboを指定すると下記のエラーが出る。(gpt-35-turboでもモデルバージョン0301なら対応しているかもしれない?)
{"error": {"requestid": "d1b0c8ab-c6fe-48a7-b151-b8656fea7493", "code": 400, "message": "Unsupported Model. Model Name: 'gpt-35-turbo' Model Version '0613'. Please retry with supported model: gpt-35-turbo (0301),gpt-35-turbo-16k (0613),gpt-4 (0613),gpt-4 (0314),gpt-4-32k (0613),gpt-4-32k (0314)"}}
Cognitive Searchインデックス
Cognitive Search側へPDFドキュメントをチャンクサイズ1k tokenにてインデックス作成している。
比較検証のため、以下のAzure OpenAIリファレンスの英語版と日本語版を登録した。またセマンティック構成としsemanticConfigurationに構成名を設定した。
https://learn.microsoft.com/en-us/azure/ai-services/openai/reference
https://learn.microsoft.com/ja-jp/azure/ai-services/openai/reference
検証
日本語での質問
「gpt-4のトークンサイズは?」
〇英語ドキュメントインデックス
"The token size for GPT-4 is 8,192 tokens ."
×日本語ドキュメントインデックス
"I'm sorry, but I couldn't find any information about the token size of GPT-4 in the retrieved documents. The requested information is not available in the retrieved data. Please try another query or topic."
- 日本語の質問クエリは英語に翻訳され、インデックスを検索する。
- このため日本語ドキュメントインデックスを(翻訳された英語で)検索してもヒットせず。
- クエリ翻訳のオプションは今のところなさそう。
英語での質問
「What is the token size of GPT-4?」
× 英語ドキュメントインデックス
"The requested information is not found in the retrieved data. Please try another query or topic."
×日本語ドキュメントインデックス
"The token size of GPT-4 is not mentioned in the retrieved documents. The requested information is not available in the retrieved data. Please try another query or topic."
- 英語ドキュメントインデックスは正しく回答できてもよさそうだが、なぜかNG
まとめ
Cognitive Searchを利用したRAGが1リクエストで行えるのは魅力的だが、日本語対応がまだ不十分。今後のバージョンアップに期待。