本記事では、DBMS.AI_SERVICEパッケージを使用してAIモデルとそのアクセスエンドポイントを登録し、OceanBaseデータベース内でAI_COMPLETE、AI_EMBED、AI_RERANKなどのAI関数を使用する方法について説明します。
前提条件
- AIモデル関連の権限を保有していること。詳細については、AI関数サービス権限を参照してください。
- サードパーティモデルサービスのAPIキーを取得済みであること。
モデルとエンドポイントテンプレートの登録
CREATE_AI_MODEL を使用してモデルを登録し、次に CREATE.AI_MODEL_ENDPOINT を使用してそのモデルのアクセスエンドポイントを登録します。以下の例では、テキスト生成(AI_COMPLETE)、ベクトル埋め込み(AI_EMBED)、および再ランキング(AI_RERANK)にそれぞれ使用される3つのモデルキー ob_complete、ob_embed、ob_rerank を登録します。access_key は実際のAPIキーに置き換えてください。
説明
特定の種類のAI機能のみを使用する場合は、対応するモデルとエンドポイントのみを登録することができます。例えば、AI_COMPLETE のみを使用する場合は、以下の テキスト生成モデルとエンドポイントの登録 ステップを実行するだけで済みます。
埋め込みモデルとエンドポイントの登録
AI_EMBEDに使用されます。
この例は、Alibaba Cloud(OpenAI形式互換)の埋め込みモデルとエンドポイント登録の例です。
CALL DBMS.AI_SERVICE.DROP_AI_MODEL ('ob_embed');
CALL DBMS.AI_SERVICE.DROP_AI_MODEL_ENDPOINT ('ob_embed_endpoint');
CALL DBMS.AI_SERVICE.CREATE_AI_MODEL(
'ob_embed', '{
"type": "dense_embedding",
"model_name": "BAAI/bge-m3"
}');
CALL DBMS.AI_SERVICE.CREATE_AI_MODEL_ENDPOINT (
'ob_embed_endpoint', '{
"ai_model_name": "ob_embed",
"url": "https://dashscope.aliyuncs.com/compatible-mode/v1/embeddings",
-- 実際のaccess_keyに置き換えてください
"access_key": "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxx",
"provider": "aliyun-openAI"
}};
この例は、Alibaba Cloud DashScope(OpenAI形式とは互換性がありません)の埋め込みモデルとエンドポイントの登録方法を示しています。
CALL DBMS.AI_SERVICE.DROP_AI_MODEL ('ob_embed');
CALL DBMS.AI_SERVICE.DROP_AI_MODEL_ENDPOINT ('ob_embed_endpoint');
CALL DBMS_AI_SERVICE.CREATE_AI_MODEL(
'ob_embed', '{
"type": "dense_embedding",
"model_name": "BAAI/bge-m3"
}');
CALL DBMS_AI_SERVICE.CREATE_AI_MODEL_ENDPOINT (
'ob_embed_endpoint', '{
"ai_model_name": "ob_embed",
"url": "https://dashscope.aliyuncs.com/api/v1/services/embeddings/text-embedding/text-embedding",
-- 実際のaccess_keyに置き換えてください
"access_key": "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxx",
"provider": "aliyun-dashscope"
}};
この例は、SiliconFlowの埋め込みモデルとエンドポイント登録の例です。
CALL DBMS.AI_SERVICE.DROP_AI_MODEL ('ob_embed');
CALL DBMS.AI_SERVICE.DROP_AI_MODEL_ENDPOINT ('ob_embed_endpoint');
CALL DBMS_AI_SERVICE.CREATE_AI_MODEL(
'ob_embed', '{
"type": "dense_embedding",
"model_name": "BAAI/bge-m3"
}');
CALL DBMS_AI_SERVICE.CREATE_AI_MODEL_ENDPOINT (
'ob_embed_endpoint', '{
"ai_model_name": "ob_embed",
"url": "https://api.siliconflow.cn/v1/embeddings",
-- 実際のaccess_keyに置き換えてください
"access_key": "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxx",
"provider": "siliconflow"
}};
この例は、テンセントのHunyuan(OpenAI形式互換)の埋め込みモデルとエンドポイント登録の例です。
CALL DBMS.AI_SERVICE.DROP_AI_MODEL ('ob_embed');
CALL DBMS_AI_SERVICE.DROP_AI_MODEL_ENDPOINT ('ob_embed_endpoint');
CALL DBMS_AI_SERVICE.CREATE_AI_MODEL(
'ob_embed', '{
"type": "dense_embedding",
"model_name": "BAAI/bge-m3"
}');
CALL DBMS_AI_SERVICE.CREATE_AI_MODEL_ENDPOINT (
'ob_embed_endpoint', '{
"ai_model_name": "ob_embed",
"url": "https://api.hunyuan.cloud.tencent.com/v1/embeddings",
-- 実際のaccess_keyに置き換える必要があります
"access_key": "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxx",
"provider": "hunyuan-openAI"
}};
テキスト生成モデルとエンドポイントの登録
AI_COMPLETEに使用します。
この例は、Alibaba Cloud(OpenAI形式互換)のテキスト生成モデルとエンドポイント登録の例です。
CALL DBMS.AI_SERVICE.DROP_AI_MODEL ('ob_complete');
CALL DBMS_AI_SERVICE.DROP_AI_MODEL_ENDPOINT ('ob_complete_endpoint');
CALL DBMS_AI_SERVICE.CREATE_AI_MODEL(
'ob_complete', '{
"type": "completion",
"model_name": "THUDM/GLM-4-9B-0414"
}');
CALL DBMS_AI_SERVICE.CREATE_AI_MODEL_ENDPOINT (
'ob_complete_endpoint', '{
"ai_model_name": "ob_complete",
"url": "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions",
-- 実際のaccess_keyに置き換えてください
"access_key": "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxx",
"provider": "aliyun-openAI"
}};
この例は、Alibaba Cloud DashScope(OpenAI形式と互換性なし)のテキスト生成モデルとエンドポイント登録の例です。
CALL DBMS.AI_SERVICE.DROP_AI_MODEL ('ob_complete');
CALL DBMS.AI_SERVICE.DROP_AI_MODEL_ENDPOINT ('ob_complete_endpoint');
CALL DBMS_AI_SERVICE.CREATE_AI_MODEL(
'ob_complete', '{
"type": "completion",
"model_name": "THUDM/GLM-4-9B-0414"
}');
CALL DBMS_AI_SERVICE.CREATE_AI_MODEL_ENDPOINT (
'ob_complete_endpoint', '{
"ai_model_name": "ob_complete",
"url": "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation",
-- 実際のaccess_keyに置き換える必要があります
"access_key": "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxx",
"provider": "aliyun-dashscope"
}};
この例は、deepseekのテキスト生成モデルとエンドポイント登録の例です。
CALL DBMS.AI_SERVICE.DROP_AI_MODEL ('ob_complete');
CALL DBMS_AI_SERVICE.DROP_AI_MODEL_ENDPOINT ('ob_complete_endpoint');
CALL DBMS_AI_SERVICE.CREATE_AI_MODEL(
'ob_complete', '{
"type": "completion",
"model_name": "THUDM/GLM-4-9B-0414"
}');
CALL DBMS_AI_SERVICE.CREATE_AI_MODEL_ENDPOINT (
'ob_complete_endpoint', '{
"ai_model_name": "ob_complete",
"url": "https://api.deepseek.com/chat/completions",
-- 実際のaccess_keyに置き換えてください
"access_key": "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxx",
"provider": "deepseek"
}};
この例は、SiliconFlowのテキスト生成モデルとエンドポイント登録の例です。
CALL DBMS.AI_SERVICE.DROP_AI_MODEL ('ob_complete');
CALL DBMS_AI_SERVICE.DROP_AI_MODEL_ENDPOINT ('ob_complete_endpoint');
CALL DBMS_AI_SERVICE.CREATE_AI_MODEL(
'ob_complete', '{
"type": "completion",
"model_name": "THUDM/GLM-4-9B-0414"
}');
CALL DBMS_AI_SERVICE.CREATE_AI_MODEL_ENDPOINT (
'ob_complete_endpoint', '{
"ai_model_name": "ob_complete",
"url": "https://api.siliconflow.cn/v1/chat/completions",
-- 実際のaccess_keyに置き換えてください
"access_key": "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxx",
"provider": "siliconflow"
}};
この例は、テンセントのHunyuan(OpenAI形式互換)テキスト生成モデルとエンドポイント登録のサンプルです。
CALL DBMS.AI_SERVICE.DROP_AI_MODEL ('ob_complete');
CALL DBMS.AI_SERVICE.DROP_AI_MODEL_ENDPOINT ('ob_complete_endpoint');
CALL DBMS.AI_SERVICE.CREATE_AI_MODEL(
'ob_complete', '{
"type": "completion",
"model_name": "THUDM/GLM-4-9B-0414"
}');
CALL DBMS.AI_SERVICE.CREATE_AI_MODEL_ENDPOINT (
'ob_complete_endpoint', '{
"ai_model_name": "ob_complete",
"url": "https://api.hunyuan.cloud.tencent.com/v1/chat/completions",
-- 実際のaccess_keyに置き換えてください
"access_key": "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxx",
"provider": "hunyuan-openAI"
}};
重順位付けモデルとエンドポイントの登録
AI_RERANKに使用されます。
この例は、Alibaba Cloud DashScope(OpenAI形式とは互換性がありません)のリランキングモデルとエンドポイント登録の例です。
CALL DBMS_AI_SERVICE.DROP.AI_MODEL ('ob_rerank');
CALL DBMS_AI_SERVICE.DROP_AI_MODEL_ENDPOINT ('ob_rerank_endpoint');
CALL DBMS_AI_SERVICE.CREATE_AI_MODEL(
'ob_rerank', '{
"type": "rerank",
"model_name": "BAAI/bge-reranker-v2-m3"
}');
CALL DBMS_AI_SERVICE.CREATE_AI_MODEL_ENDPOINT (
'ob_rerank_endpoint', '{
"ai_model_name": "ob_rerank",
"url": "https://dashscope.aliyuncs.com/api/v1/services/rerank/text-rerank/text-rerank",
-- 実際のaccess_keyに置き換えてください
"access_key": "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxx",
"provider": "aliyun-dashscope"
}};
この例は、SiliconFlowのリランキングモデルとエンドポイント登録の例です。
CALL DBMS.AI_SERVICE.DROP_AI_MODEL ('ob_rerank');
CALL DBMS_AI_SERVICE.DROP_AI_MODEL_ENDPOINT ('ob_rerank_endpoint');
CALL DBMS_AI_SERVICE.CREATE_AI_MODEL(
'ob_rerank', '{
"type": "rerank",
"model_name": "BAAI/bge-reranker-v2-m3"
}');
CALL DBMS_AI_SERVICE.CREATE_AI_MODEL_ENDPOINT (
'ob_rerank_endpoint', '{
"ai_model_name": "ob_rerank",
"url": "https://api.siliconflow.cn/v1/rerank",
-- 実際のaccess_keyに置き換えてください
"access_key": "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxx",
"provider": "siliconflow"
}};
関連ドキュメント
- AI関数サービスのクイックスタート:登録後に最初の例を実行する手順。
- AI関数サービスの使用と例:各AI関数の構文とその他の例。
- DBMS.AI_SERVICEパッケージ:AIモデルとエンドポイントの完全なパラメータ説明。