目次
この記事について
本記事は、Agent Builder を使用したデータストア参照型対話エージェントの作成に関する調査記録です。
やりたいこと
Agent Builder を用いたデータストア参照型対話エージェントの作成において、
データストアのファイル数が増加することで回答率が低下する問題への対処を進める。
前回の調査日記
					あわせて読みたい
					
 					
			
						AI Agent開発日記 2025/03/25
						この記事について 本記事は、Agent Builder を使用したデータストア参照型対話エージェントの作成に関する調査記録です。 やりたいこと Agent Builder を用いたデータス...					
				Agent BuilderのツールであるOpenAPIのスキーマを適切に設定し、Cloud RunにデプロイしたElasticsearchで検索できるか調べる。
- 試したこと
- Agent BuilderのツールであるOpenAPIのyaml形式のスキーマを設定して動作を見る前に、jupyterでスキーマを試し、様子を見る。
 - 下記形式のコードを使ってスキーマが正常に動作するのか調査した
 
 - 現状
- Elasticsearchで検索する為のスキーマを調査中。
 
 
jupyterでTool OpenAPIのyaml形式のスキーマを実行するコード
このコードは、Elasticsearchとは関係なく、Jupyter Notebook上で、Tool OpenAPIのYAML形式を使用してCloud Run上の別の物件検索APIへリクエストをするコードです。
import requests
import yaml
scima = """
openapi: 3.0.0
info:
  title: Warehouse Address API
  version: v1
servers:
  - url: 'https://api.example.com'  # エンドポイントurl
paths:
  /:
    get:
      summary: Get Warehouse Address
      description: Retrieves the address of a warehouse by its ID.
      parameters:
        - in: query
          name: warehouseId
          schema:
            type: integer
            format: int64
          required: true
          description: The ID of the warehouse.
      responses:
        '200':
          description: Successful response with the warehouse address.
          content:
            application/json:
              schema:
                type: object
                properties:
                  address:
                    type: string
                    description: The full address of the warehouse.
        '404':
          description: Warehouse not found.
        '400':
          description: Bad Request. warehouseId is missing or not an integer.
"""
# OpenAPI Specification を YAML として読み込む (関数の外で一度だけ実行)
spec = yaml.safe_load(scima)
# サーバーURLを取得 (関数の外で一度だけ実行)
server_url = spec['servers'][0]['url']
# パスを取得 (関数の外で一度だけ実行)
path = spec['paths']['/']['get']
def get_warehouse_address(warehouse_id):
    """
    倉庫IDに基づいて倉庫の住所を取得する関数
    Args:
        warehouse_id (int): 取得したい倉庫のID
    Returns:
        str: 倉庫の住所 (成功時)
        None: 倉庫が見つからない場合、またはエラーが発生した場合
    """
    try:
        # エンドポイントURLを構築 (クエリパラメータ付き)
        endpoint_url = f"{server_url}/?warehouseId={warehouse_id}"
        # GETリクエストを送信
        response = requests.get(endpoint_url)
        # レスポンスステータスコードを確認
        if response.status_code == 200:
            data = response.json()
            return data.get('address')
        elif response.status_code == 404:
            print(f"倉庫ID {warehouse_id} は見つかりませんでした。")
            return None
        elif response.status_code == 400:
            print(f"リクエストエラー: warehouseId が不正です (ステータスコード: {response.status_code})")
            return None
        else:
            print(f"エラーが発生しました (ステータスコード: {response.status_code})")
            return None
    except requests.exceptions.RequestException as e:
        print(f"リクエストエラー: {e}")
        return None
if __name__ == "__main__":
    warehouse_id_to_lookup = 123  # サンプルIDに変更
    address = get_warehouse_address(warehouse_id_to_lookup)
    if address:
        print(f"倉庫ID {warehouse_id_to_lookup} の住所: {address}")
    else:
        print(f"倉庫ID {warehouse_id_to_lookup} の住所を取得できませんでした。")
		
            