AIエージェントにおける参照URL表示のためのJSON構造設計
AIエージェントが検索結果や参照情報を確実に表示するためには、適切なJSON構造が不可欠です。以下に、実装に最適なスキーマ設計と実装方法を詳しく解説します。
基本的なJSON構造
最も一般的で効果的なスキーマは、以下のような階層構造を持つJSONです:
json
{
"answer": "AIが生成した回答テキスト...",
"references": [
{
"url": "https://example.com/source1",
"title": "参照元のタイトル",
"snippet": "引用された部分のテキスト",
"timestamp": "2025-04-22T12:34:56Z"
},
{
"url": "https://example.com/source2",
"title": "別の参照元のタイトル",
"snippet": "別の引用部分",
"timestamp": "2025-04-20T10:20:30Z"
}
],
"citations": [
{
"text": "引用されたテキスト部分",
"reference_index": 0
},
{
"text": "別の引用部分",
"reference_index": 1
}
]
}
この構造では、回答本文と参照情報が明確に分離されており、UIでの表示が容易になります[1][8]。
OpenAI Agents SDKでの実装例
OpenAI Agents SDKを使用する場合、以下のようなスキーマ定義が可能です:
python
from pydantic import BaseModel
from typing import List
class Reference(BaseModel):
url: str
title: str
snippet: str
timestamp: str
class Citation(BaseModel):
text: str
reference_index: int
class AgentResponse(BaseModel):
answer: str
references: List[Reference]
citations: List[Citation]
# エージェント定義時にoutput_typeとして指定
agent = Agent(
name='search agent',
instructions='検索結果を元に回答し、必ず参照情報を含めてください',
output_type=AgentResponse,
tools=[WebSearchTool()]
)
このようにoutput_typeを指定することで、AIの出力がこのスキーマに従うよう強制できます[1][7]。
参照情報の詳細フィールド
より詳細な参照情報を含めるには、以下のフィールドを追加すると効果的です:
- relevance_score: 参照の関連性スコア(0.0〜1.0)
- publication_date: 元コンテンツの公開日
- domain: URLのドメイン部分(信頼性の判断に役立つ)
- content_type: "article", "blog", "academic", "news"などのコンテンツタイプ