OceanBaseデータベースは、ベクトル型データの格納、ベクトルインデックス、そしてembeddingベクトル検索機能を提供します。これにより、ベクトル化したデータをOceanBaseデータベースに保存し、次の検索処理で利用できます。
LlamaIndexは、LLM(エージェントやワークフローを含む)を使用して、コンテキストを強化した生成AIアプリケーションを構築するためのフレームワークです。データコネクタ、データインデックス、エージェント、観測性/評価の統合、ワークフローなどの機能を提供します。
このチュートリアルでは、Qwen APIを組み合わせて、OceanBaseデータベースのベクトル検索機能、Qwen、およびLlamaIndexを統合し、ドキュメント質問応答を実現する方法を説明します。
前提条件
OceanBaseデータベースV4.4.0以降をデプロイし、MySQLモードのテナントを作成していること。テナントの作成後、以下の手順を参照して操作してください。
- 環境には使用可能なMySQLテナント、MySQLデータベース、アカウントが存在し、データベースアカウントに対して読み書き権限が付与されていること。
- ベクトル検索機能を有効にするために、テナントで
ob_vector_memory_limit_percentageパラメータを設定していること。V4.4.1より前のバージョンでは値を30に設定することを推奨しますが、V4.4.1バージョン以降はデフォルト値の0のままにしておくことを推奨します。このパラメータをより正確に設定する必要がある場合は、ob_vector_memory_limit_percentageを参照してこの値を計算してください。
Python 3.9以降をインストールしていること。
必要な依存関係をインストールしていること:
python3 -m pip install llama-index-vector-stores-oceanbase llama-index python3 -m pip install llama-index-embeddings-dashscope python3 -m pip install llama-index-llms-dashscopeQwen APIキーを準備しておくこと。
ステップ1:データベース接続情報を取得する
OceanBaseデータベースのデプロイ担当者または管理者から、該当するデータベース接続文字列を取得します。例:
obclient -h$host -P$port -u$user_name -p$password -D$database_name
パラメータ説明:
$host:OceanBaseデータベースへの接続IPアドレス。OceanBaseデータベースプロキシ(OceanBase Database Proxy、ODP)接続方式ではODPアドレスを使用し、直接接続方式ではOBServerノードのIPアドレスを使用します。$port:OceanBaseデータベースへの接続ポート。ODP接続方式のデフォルトポートは2883で、ODPデプロイ時にカスタマイズ可能です。直接接続方式のデフォルトポートは2881で、OceanBaseデータベースのデプロイ時にカスタマイズ可能です。$database_name:アクセスするデータベース名。注意
テナントに接続するユーザーは、データベースに対する
CREATE、INSERT、DROP、SELECT権限が付与されていなければなりません。ユーザー権限の詳細については、MySQLモードの権限分類を参照してください。$user_name:テナントの接続アカウント。ODP接続の一般的な形式:ユーザー名@テナント名#クラスタ名またはクラスタ名:テナント名:ユーザー名。直接接続方式の形式:ユーザー名@テナント名。$password:アカウントのパスワード。
接続文字列の詳細については、OBClientを使用してOceanBaseテナントに接続するを参照してください。
ステップ2:AIアシスタントを構築する
Qwen APIキーの環境変数を設定する
Qwen APIキーを取得し、APIキーを環境変数に設定します。
export DASHSCOPE_API_KEY="YOUR_DASHSCOPE_API_KEY"
サンプルデータをダウンロードする
mkdir -p '/root/llamaindex/paul_graham/'
wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt' -O '/root/llamaindex/paul_graham/paul_graham_essay.txt'
データテキストを読み込む
import os
from pyobvector import ObVecClient
from llama_index.core import Settings
from llama_index.embeddings.dashscope import DashScopeEmbedding
from llama_index.core import (
SimpleDirectoryReader,
load_index_from_storage,
VectorStoreIndex,
StorageContext,
)
from llama_index.vector_stores.oceanbase import OceanBaseVectorStore
from llama_index.llms.dashscope import DashScope, DashScopeGenerationModels
#set ob client
client = ObVecClient(uri="127.0.0.1:2881", user="root@test",password="",db_name="test")
# Global Settings
Settings.embed_model = DashScopeEmbedding()
# config llm model
dashscope_llm = DashScope(
model_name=DashScopeGenerationModels.QWEN_MAX,
api_key=os.environ.get("DASHSCOPE_API_KEY", ""),
)
# load documents
documents = SimpleDirectoryReader("/root/llamaindex/paul_graham/").load_data()
oceanbase = OceanBaseVectorStore(
client=client,
dim=1536,
drop_old=True,
normalize=True,
)
storage_context = StorageContext.from_defaults(vector_store=oceanbase)
index = VectorStoreIndex.from_documents(
documents, storage_context=storage_context
)
ベクトル検索
この手順では、ドキュメント paul_graham_essay.txt から “What did the author do growing up?” を検索する方法を示します。
# set Logging to DEBUG for more detailed outputs
query_engine = index.as_query_engine(llm=dashscope_llm)
res = query_engine.query("What did the author do growing up?")
res.response
期待される結果:
'Growing up, the author worked on writing and programming outside of school. In terms of writing, he wrote short stories, which he now considers to be awful, as they had very little plot and focused mainly on characters with strong feelings. For programming, he started in 9th grade by trying to write programs on an IBM 1401 at his school, using an early version of Fortran. Later, after getting a TRS-80 microcomputer, he began to write more practical programs, including simple games, a program to predict the flight height of model rockets, and a word processor that his father used for writing.'