OceanBaseはV4.3.3からベクトル型データの格納、ベクトルインデックス、そしてembeddingベクトル検索機能をサポートしています。これにより、ベクトル化したデータをOceanBaseに保存し、その後の検索処理で利用できるようになりました。
LlamaIndexは、LLM(エージェントやワークフローを含む)を活用して、コンテキストを強化した生成AIアプリケーションを構築するためのフレームワークです。データコネクタ、データインデックス、エージェント、観測性/評価の統合、ワークフローなどの機能を提供します。
本記事では、Qwen APIと組み合わせて、OceanBase Cloudのベクトル検索機能、Qwen、およびLlamaIndexを連携させ、ドキュメント質問応答を実現する方法を紹介します。
前提条件
環境に利用可能なトランザクション型(MySQL)クラスタインスタンスがあります。テナントの作成を参照してテナント作成を完了した後、以下の手順を実行してください。
環境に使用可能なMySQL互換モードのテナント、MySQLデータベース、およびアカウントが既に存在し、データベースアカウントに対して読み書き権限が付与されていること。作成が必要な場合は、詳細についてはアカウントの作成およびデータベースの作成(MySQL専用)をご参照ください。
プロジェクト管理者またはインスタンス管理者ロールを持ち、プロジェクト内のインスタンスに対して読み書き操作を実行できること。権限がない場合は、組織管理者に連絡して権限の追加を依頼してください。
Python 3.9以降のバージョンおよび対応するpipをインストールしていること。マシンのPythonバージョンが低い場合は、Minicondaを使用して新しいPython 3.9以降の環境を作成できます。詳細については、Minicondaインストールガイドをご参照ください。
LlamaIndexをインストールしていること。
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-dashscope
ステップ1:データベース接続情報を取得する
ドロップダウンリストから、ID でクラスタインスタンスを選択します。
**概要**ページに移動します。
接続をクリックし、**接続文字列を取得**を選択します。
ポップアップウィンドウで、**パブリックネットワークを使用**を選択します。
アクセスアドレスを取得し、**現在のブラウザIPアドレスを追加**を選択します。
データベース関連情報を入力し、**接続文字列をコピー**します。
ステップ2:LLMプラットフォームアカウントを登録する
Alibaba Cloud Model Studioアカウントに登録し、Model Studioサービスを有効化してAPIキーを取得します。
注意
Alibaba Cloud Model Studioサービスを有効化するには、第三者プラットフォームに移動して完了する必要があります。この操作は第三者プラットフォームの料金規則に従い、該当する費用が発生する可能性があります。続行する前に、その公式ウェブサイトを訪問するか関連ドキュメントを確認し、料金体系を確認して同意してください。同意しない場合は、操作を続けないでください。
APIキーを環境変数に設定します:
export DASHSCOPE_API_KEY="YOUR_DASHSCOPE_API_KEY"
ステップ3:AIアシスタントを構築する
サンプルデータをダウンロードする
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.'