メインコンテンツまでスキップ
バージョン: V1.0.0

LlamaIndexとの連携

seekdbは、ベクトル型データの格納、ベクトルインデックス、そしてembeddingベクトル検索機能を提供します。これにより、ベクトル化したデータをseekdbに保存し、次の検索処理で利用できるようになります。

LlamaIndexは、LLM(エージェントやワークフローを含む)を使用して、コンテキストを強化した生成AIアプリケーションを構築するためのフレームワークです。データコネクタ、データインデックス、エージェント、観測性/評価の統合、ワークフローなどの機能を提供します。

このチュートリアルでは、Qwen APIと組み合わせて、seekdbのベクトル検索機能、Qwen、LlamaIndexを統合し、ドキュメント質問応答を実現する方法を説明します。

前提条件​

  • seekdbデータベースのデプロイが完了していること。

    • 環境に使用可能なデータベースとアカウントが存在し、データベースアカウントに対して読み書き権限が付与されていること。
    • ベクトル検索機能を有効にするために、ob_vector_memory_limit_percentage構成パラメータを設定できること。デフォルト値の0(適応モード)を推奨します。この構成パラメータをより正確に設定する必要がある場合は、関連する設定ドキュメントを参照してください。
  • 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-dashscope
  • Qwen APIキーを準備しておくこと。

ステップ1:データベース接続情報を取得する​

seekdbデータベースのデプロイ担当者または管理者から、該当するデータベース接続文字列を取得します。例:

mysql -h$host -P$port -u$user_name -p$password -D$database_name

パラメータ説明:

  • $host:seekdbデータベースへの接続IPアドレス。

  • $port:seekdbデータベースへの接続ポート。デフォルトは2881で、デプロイ時にカスタマイズ可能です。

  • $database_name:アクセスするデータベース名。

    ヒント

    データベースに接続するユーザーは、そのデータベースに対するCREATE、INSERT、DROP、SELECT権限を持っている必要があります。ユーザー権限の詳細については、MySQLモードの権限分類を参照してください。

  • $user_name:データベース接続アカウント。形式:ユーザー名。

  • $password:アカウントのパスワード。

ステップ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.'