OpenSearchおよびOCI生成AIを使用したAIアシスタントの作成
データ分析を簡素化するために、OpenSearchおよびOCI生成AIを使用してAIアシスタントを作成します。
このトピックでは、会話型エージェントを使用して、OpenSearchに独自のチャットボットを作成する方法について説明します。大規模言語モデル(LLM)では、必要な独自のデータがナレッジ・ベースに含まれていないため、質問に回答できない場合があります。ここでは、あとでLLMに詳細情報を提供するためにエージェントが必要な場合があります。会話型エージェントはツールを実行して、構成済のデータ・ソースから詳細情報にアクセスし、追加情報をコンテキストとしてLLMに送信できます。
前提条件
- テナンシは、米国中西部(シカゴ)リージョンまたはドイツ中央部(フランクフルト)リージョンにサブスクライブする必要があります。これらのリージョンのいずれかにクラスタを作成する必要はありません。テナンシがいずれかのリージョンにサブスクライブされていることを確認してください。
- OpenSearchでAIアシスタント・ツールキットを使用するには、OpenSearchイメージ・バージョン2.15以降のクラスタが必要です。デフォルトでは、新しいクラスタはバージョン2.15を使用するように構成されています。クラスタを作成するには、「OpenSearchクラスタの作成」を参照してください。
バージョン2.3またはバージョン2.11用に構成された既存のクラスタの場合、バージョン2.15へのインライン・アップグレードを実行できます。詳細は、OpenSearchクラスタ・ソフトウェアのアップグレードを参照してください。
バージョン1.2.3用に構成された既存のクラスタを2.15にアップグレードするには、「OpenSearchクラスタの手動アップグレード」で説明されているアップグレード・プロセスを使用する必要があります。
- 生成AIリソースへのアクセス権を付与するポリシーを作成します。次のポリシーの例には、必要な権限が含まれています。ポリシーの作成の詳細は、ポリシーの作成を参照してください。
ALLOW ANY-USER to manage generative-ai-family in tenancy WHERE ALL {request.principal.type='opensearchcluster', request.resource.compartment.id='<cluster_compartment_OCID>'} -
クラスタAPIの設定操作を使用して、コネクタを作成できる推奨クラスタ設定を構成します。次の例は、推奨される設定を示しています。
PUT _cluster/settings { "persistent": { "plugins": { "ml_commons": { "only_run_on_ml_node": "false", "model_access_control_enabled": "true", "native_memory_threshold": "99", "rag_pipeline_feature_enabled": "true", "memory_feature_enabled": "true", "allow_registering_model_via_local_file": "true", "allow_registering_model_via_url": "true", "model_auto_redeploy.enable":"true", "model_auto_redeploy.lifetime_retry_times": 10 } } } }レスポンスの例:
{ "acknowledged": true, "persistent": { "plugins": { "ml_commons": { "rag_pipeline_feature_enabled": "true", "memory_feature_enabled": "true", "only_run_on_ml_node": "false", "model_access_control_enabled": "true", "native_memory_threshold": "99", "allow_registering_model_via_local_file": "true", "model_auto_redeploy": { "lifetime_retry_times": "10", "enable": "true" }, "allow_registering_model_via_url": "true" } } }, "transient": {} }
生成AIチャット・モデルの作成
生成AIアシスタントを使用するには、生成AIチャット・モデルを作成してユーザーの指示を解釈し、要求された情報で応答する必要があります。生成AIモデルの作成には、次のステップが含まれます。
- モデル・グループを登録します。
- コネクタを作成します。
- モデルを登録します。
- モデルをデプロイします。
モデル・グループの登録
次の例に示すように、モデル・グループAPIの登録操作を使用してモデル・グループを登録します。
POST /_plugins/_ml/model_groups/_register
{
"name": "demo_model_group",
"description": "This is a model group"
}
レスポンスで返されたmodel_group_idを書き留めます。
{
"model_group_id": "<model_group_id>",
"status": "CREATED"
}
生成AIへのコネクタの作成
生成AIを使用してモデルを作成するには、コネクタが必要です。コネクタを作成するコマンドは、モデルに固有です。その他の例は、「コネクタの作成」を参照してください。
生成AIサービスは、使用可能なモデルを定期的に更新します。サポートされているチャット・モデルの情報を取得するには、生成AIの事前トレーニング済基本モデルを確認してください。生成AIサービス・チームは、通常、モデルを非推奨にしようとしているときに通知を送信します。オンデマンド・モデルを使用している場合は、エージェント・サービスの中断を回避するために、これらの通知を遵守し、必要に応じてエージェント・モデルを更新します。
Cohere.command-r-plus-08-2024
次の例は、Cohere.command-r-plus-08-2024チャット・モデルを使用した作成コネクタの構成を示しています。
POST _plugins/_ml/connectors/_create
{
"name": "Cohere Commndr-R-Plus Chat Connector",
"description": "demo genai connector",
"version": 2,
"protocol": "oci_sigv1",
"parameters": {
"endpoint": "inference.generativeai.us-chicago-1.oci.oraclecloud.com",
"auth_type": "resource_principal"
},
"credential": {
},
"actions": [
{
"action_type": "predict",
"method": "POST",
"url": "https://${parameters.endpoint}/20231130/actions/chat",
"request_body": "{\"compartmentId\":\"<compartment_OCID>\",\"servingMode\":{\"modelId\":\"cohere.command-r-plus-08-2024\",\"servingType\":\"ON_DEMAND\"},\"chatRequest\":{\"message\":\"${parameters.prompt}\",\"maxTokens\":600,\"temperature\":1,\"frequencyPenalty\":0,\"presencePenalty\":0,\"topP\":0.75,\"topK\":0,\"isStream\":false,\"chatHistory\":[],\"apiFormat\":\"COHERE\"}}",
"post_process_function": "def text = params['chatResponse']['text'].replace('\n', '\\\\n').replace('\"','');\n return text"
}
]
}
<compartment_OCID>をコンパートメントのOCIDに置き換えます。
レスポンスで返されたconnector_idを書き留めます。
{
"connector_id": "<connector_ID>",
}
OpenAI AIアシスタント・コネクタ・ペイロード
この項では、次のOpenAI AIアシスタント・コネクタ・ペイロードのコネクタおよびエージェントを示します。
40
POST _plugins/_ml/connectors/_create
{
"name": "4o-agent-connector",
"description": "OpenAI 4o connector for AI assistant",
"version": 2,
"protocol": "oci_sigv1",
"parameters": {
"endpoint": "inference.generativeai.us-chicago-1.oci.oraclecloud.com",
"auth_type": "resource_principal"
},
"credential": {
},
"actions": [
{
"action_type": "predict",
"method": "POST",
"url": "https://${parameters.endpoint}/20231130/actions/chat",
"request_body": "{\"compartmentId\":\"<compartment_ocid>\",\"servingMode\":{\"modelId\":\"openai.gpt-4o\",\"servingType\":\"ON_DEMAND\"},\"chatRequest\":{\"maxCompletionTokens\":600,\"temperature\":0.5,\"frequencyPenalty\":0,\"presencePenalty\":0,\"topP\":1.0,\"isStream\":false,\"apiFormat\":\"GENERIC\",\"messages\":[{\"role\":\"USER\",\"content\":[{\"type\":\"TEXT\",\"text\":\"${parameters.prompt}\"}]}]}}",
"post_process_function": "def text = params['chatResponse']['choices'][0]['message']['content'][0]['text'];\n return text"
}
]
}
4o- ミニ
POST _plugins/_ml/connectors/_create
{
"name": "4o-mini-agent-connector",
"description": "OpenAI 4o-mini connector for AI Assistant",
"version": 2,
"protocol": "oci_sigv1",
"parameters": {
"endpoint": "inference.generativeai.us-chicago-1.oci.oraclecloud.com",
"auth_type": "resource_principal"
},
"credential": {
},
"actions": [
{
"action_type": "predict",
"method": "POST",
"url": "https://${parameters.endpoint}/20231130/actions/chat",
"request_body": "{\"compartmentId\":\"<compartment_ocid>\",\"servingMode\":{\"modelId\":\"openai.gpt-4o-mini\",\"servingType\":\"ON_DEMAND\"},\"chatRequest\":{\"maxCompletionTokens\":600,\"temperature\":0.5,\"frequencyPenalty\":0,\"presencePenalty\":0,\"topP\":1.0,\"isStream\":false,\"apiFormat\":\"GENERIC\",\"messages\":[{\"role\":\"USER\",\"content\":[{\"type\":\"TEXT\",\"text\":\"${parameters.prompt}\"}]}]}}",
"post_process_function": "def text = params['chatResponse']['choices'][0]['message']['content'][0]['text'];\n return text"
}
]
}
o3-mini/o1
{
"name": "agent-connector",
"description": "OpenAI connector for AI Assitant",
"version": "2",
"protocol": "oci_sigv1",
"parameters": {
"endpoint": "inference.generativeai.us-chicago-1.oci.oraclecloud.com",
"auth_type": "resource_principal"
},
"actions": [
{
"action_type": "PREDICT",
"method": "POST",
"url": "https://${parameters.endpoint}/20231130/actions/chat",
"request_body": """{"compartmentId":"<compartment_ocid>","servingMode":{"modelId":"<MODEL NAME>","servingType":"ON_DEMAND"},"chatRequest":{"isStream":false,"apiFormat":"GENERIC","reasoningEffort":"LOW","messages":[{"role":"USER","content":[{"type":"TEXT","text":"${parameters.prompt}"}]}]}}""",
"post_process_function": """def text = params['chatResponse']['choices'][0]['message']['content'][0]['text'].replace('\n', '\\\n');\nreturn text"""
}
]
}
モデルを登録する
次の例に示すように、モデルを登録します。
POST /_plugins/_ml/models/_register
{
"name": "demo_genai_model",
"function_name": "remote",
"model_group_id": "<model_group_ID>",
"description": "demo genai model",
"connector_id": "<connector_ID>"
}
レスポンスで返されたmodel_idを書き留めます。
{
"task_id": "<task_ID>",
"status": "CREATED",
"model_id": "<genAI_model_ID>"
}
モデルのデプロイ
次の例に示すように、モデルをデプロイします。
POST /_plugins/_ml/models/<genAI_model_ID>/_deploy
次のようなレスポンスが返されます。
{
"task_id": "-C1m4pQBpfOUfebnTqCc",
"task_type": "DEPLOY_MODEL",
"status": "COMPLETED"
}
モデルをテストします
次の例に示すように、必要に応じてモデルをテストできます。
POST /_plugins/_ml/models/<genAI_model_ID>/_predict
{
"parameters": {
"prompt": "\n\nHuman: how are you? \n\nAssistant:"
}
}
次のようなレスポンスが返されます。
{
"inference_results": [
{
"output": [
{
"name": "response",
"dataAsMap": {
"inferenceResponse": {
"generatedTexts": [
{
"text": "I'm an AI language model, so I don't have feelings or emotions as
humans do. However, I'm functioning properly and ready to assist you with any questions
or tasks you may have! How can I help you today?"
}
]
}
}
}
],
"status_code": 200
}
]
}
会話型エージェントの作成
生成AIモデルを作成した後、これを使用して会話型エージェントを作成できます。このエージェントは、生成AIモデルとユーザー指定のツールを組み合せて、ユーザー・テキストをツールの起動に解釈し、結果の解釈をユーザーに返すことができます。その後、モデルは解釈された結果を返します。詳細は、独自のチャットボットの構築およびエージェントおよびツールを参照してください。
この例では、組込みのopensearch_dashboards_sample_data_ecommerce索引を使用します。この索引は、ダッシュボードを介してOpenSearchクラスタに追加できます。この例では、エージェントにPPLToolおよびCatIndexToolが指定されています。PPLToolは、ユーザー指示をPiped Processing Language (PPL)に変換します。これにより、エージェントは索引データに対してリクエストを実行できます。CatIndexToolを使用すると、エージェントはクラスタ内の索引をリストできます。このツールは、有用なアシスタントに必要です。独自のユースケースの構成の詳細は、AIエージェントのOpenSearchドキュメントを参照してください。
これらのOpenSearch機能の詳細は、次のリンクを選択してください。
POST _plugins/_ml/agents/_register
{
"name": "Chat Agent with Llama",
"type": "conversational",
"description": "this is a test agent",
"app_type": "os_chat",
"llm": {
"model_id": "<genAI_model_ID>",
"parameters": {
"max_iteration": 5,
"message_history_limit": 5,
"disable_trace": false
}
},
"memory": {
"type": "conversation_index"
},
"tools": [
{
"type": "PPLTool",
"description": "Use this tool to transfer natural language to generate PPL and execute PPL to query inside. Use this tool after you know the index name, otherwise, call IndexRoutingTool first. The input parameters are: {index:IndexName, question:UserQuestion}.",
"parameters": {
"model_id": "<genAI_model_ID>",
"model_type": "OPENAI",
"execute": true
},
"include_output_in_agent_response": true
},
{
"type": "CatIndexTool",
"description": "Use this tool to get OpenSearch index information: (health, status, index, uuid, primary count, replica count, docs.count, docs.deleted, store.size, primary.store.size). \nIt takes 2 optional arguments named `index` which is a comma-delimited list of one or more indices to get information from (default is an empty list meaning all indices), and `local` which means whether to return information from the local node only instead of the cluster manager node (default is false)."
}
]
}
次のようなレスポンスが返されます。返されたagent_idを書き留めます。
{
"agent_id": "<conversational_agent_ID>"
}
<genAI_model_ID>
会話型エージェントのテスト
次の例に示すように、必要に応じて会話型エージェントをテストできます。
POST _plugins/_ml/agents/<conversational_agent_ID>/_execute
{
"parameters": {
"question": "Can you query with index opensearch_dashboards_sample_data_ecommerce to find out how many orders happened between 4 AM and 9 PM on 3/24/2025?",
"verbose": false,
"selected_tools": ["PPLTool", "CatIndexTool"]
}
}
次のようなレスポンスが返されます。
{
"inference_results": [
{
"output": [
{
"name": "memory_id",
"result": "11lPppUBsqYDrOfXM-Uf"
},
{
"name": "parent_interaction_id",
"result": "2FlPppUBsqYDrOfXM-Ur"
},
{
"name": "response",
"dataAsMap":
{
"response": "The count of orders between 4 AM and 9 PM on 3/24/2025 is 102.",
"additional_info":
{
"TransferQuestionToPPLAndExecuteTool.output": [
"Failed to run the tool TransferQuestionToPPLAndExecuteTool with the error message execute ppl:source=opensearch_dashboards_sample_data_ecommerce | where type = 'order' AND order_date >= '2025-03-24T04:00:00+00:00' AND order_date < '2025-03-24T21:00:00+00:00' | stats COUNT() AS count, get error: timestamp:2025-03-24T04:00:00+00:00 in unsupported format, please use 'yyyy-MM-dd HH:mm:ss[.SSSSSSSSS]'.",
"""{"ppl":"source\u003dopensearch_dashboards_sample_data_ecommerce | where order_date \u003e\u003d \u00272025-03-24 04:00:00\u0027 AND order_date \u003c\u003d \u00272025-03-24 21:00:00\u0027 | stats COUNT() AS count","executionResult":"{\n \"schema\": [\n {\n \"name\": \"count\",\n \"type\": \"integer\"\n }\n ],\n \"datarows\": [\n [\n 102\n ]\n ],\n \"total\": 1,\n \"size\": 1\n}"}"""]
}
}
}]
}]
}
日付書式が不適切なため、PPLToolは当初問合せを拒否しましたが、エージェントはそのレスポンスを受信して問合せを再フォーマットできるため、PPLツールの2回目の起動に成功します。
OpenSearchダッシュボードでのルート・チャットボット・エージェントの作成
会話型エージェントを作成した後、チャットボット・エージェントを作成し、クラスタのOpenSearchダッシュボードでルート・チャットボット・エージェントとして構成します。この例のチャットボット・エージェントは、会話型エージェントを使用して質問を解釈して回答します。また、MLModelToolを使用して新しい質問を提案します。
チャットボット・エージェントの作成
次の例に示すように、チャットボット・エージェントを作成します。
POST /_plugins/_ml/agents/_register
{
"name": "Chatbot agent",
"type": "flow",
"description": "this is a test chatbot agent",
"tools": [
{
"type": "AgentTool",
"name": "LLMResponseGenerator",
"parameters": {
"agent_id": "<conversational_agent_ID>"
},
"include_output_in_agent_response": true
}
],
"memory": {
"type": "conversation_index"
}
}
次のようなレスポンスが返されます。返されたagent_idを書き留めます。
{
"agent_id": "-i2m4pQBpfOUfebnxKAf"
}
OpenSearchダッシュボードでのルート・チャットボット・エージェントの構成
チャットボット・エージェントを作成した後、次の例に示すように、OpenSearchダッシュボードでエージェントをルート・エージェントとして構成できます。
PUT .plugins-ml-config/_doc/os_chat
{
"type":"os_chat_root_agent",
"configuration":{
"agent_id": "<root_conversational_agent_ID>"
}
}
次のようなレスポンスが返されます。
{
"_index": ".plugins-ml-config",
"_id": "os_chat",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1
}
ルート・エージェントのテスト
ルート・エージェントをテストするには、OpenSearchクラスタのOpenSearchダッシュボードでOpenSearchアシスタントの質問を行います。