プロンプトにおけるsystem と human の関係
`system` と `human` の部分は、
**`ChatPromptTemplate.from_messages`** を使って
プロンプトを構築する際の役割を明確にするためのラベルです。
これは OpenAI の `ChatPromptTemplate` に準拠した形式になっています。
—
## **1. `system` メッセージ**
("system", "You are extracting organization and person entities from the text.")
### **役割**
– **システムメッセージ (`system`)** は、AI の振る舞いを指定するために使用される。
– ここでは **「組織や人物のエンティティを抽出する役割のAI」** であることを明示。
– 通常、モデルの動作を方向づける基本的な指示を含む。
### **意味**
> 「あなた(AI)はテキストから組織と人物のエンティティを抽出する役割を持ちます。」
—
## **2. `human` メッセージ**
("human", "Use the given format to extract information from the following input: {question}")
### **役割**
– **ユーザーからの入力を表すメッセージ** (`human`)。
– `{question}` はプレースホルダーで、実際の入力がここに挿入される。
– AI が処理するための具体的な指示を与える。
### **意味**
> 「以下の入力から、指定されたフォーマットを使って情報を抽出してください:{question}」
—
## **`system` と `human` の関係**
– **`system` メッセージ** → AI の振る舞いやタスクの定義
– **`human` メッセージ** → 実際の入力データと実行すべき処理の具体的な指示
この二つを組み合わせることで、AI に **「何をするべきか(system)」** と **「何を元に処理するか(human)」** を伝える。
—
## **実行の流れ**
1. **`system`**:「AI はテキストから組織と人物のエンティティを抽出する」
2. **`human`**:「具体的な入力 `{question}` に基づいて、決まったフォーマットで情報を抽出せよ」
3. AI はこのプロンプトを解釈し、テキスト内のエンティティを抽出する。
—
## **補足**
このプロンプトの後に `convert_to_openai_function(Entities)` を適用して、結果を `Entities` クラスのスキーマに適合させているため、出力は `names` のリスト形式で返される。
この形式を使うことで、AI はエンティティ抽出タスクを **明確なルール** のもとで実行することができる。
他の商品検索などにも使えるらしい
GPTによれば
はい、その通りです。
**`Entities` のスキーマ定義とプロンプトの `system` / `human` メッセージを変更すれば、別のケースに適用可能** です。
—
## **具体的な変更方法**
### **1. スキーマ (`Entities` クラス) を変更**
抽出したい情報が異なる場合、`names` の部分を適切なフィールド名・型・説明に変更すれば対応可能です。
#### **例1:地名(場所)を抽出する場合**
class Locations(BaseModel):
"""Identifying information about locations."""
locations: list[str] = Field(
...,
description="All the locations, cities, or countries that appear in the text",
)
#### **例2:商品名を抽出する場合**
class Products(BaseModel):
"""Identifying information about products."""
products: list[str] = Field(
...,
description="All the product names that appear in the text",
)
—
### **2. プロンプト (`system` / `human`) を変更**
スキーマ変更に合わせて、AI に適切な指示を与えるため `system` と `human` のメッセージも修正する。
#### **例1:地名(場所)を抽出する場合**
prompt = ChatPromptTemplate.from_messages(
[
("system", "You are extracting location names from the text."),
("human", "Use the given format to extract location names from the following input: {question}"),
]
)
#### **例2:商品名を抽出する場合**
prompt = ChatPromptTemplate.from_messages(
[
("system", "You are extracting product names from the text."),
("human", "Use the given format to extract product names from the following input: {question}"),
]
)
—
### **3. そのまま適用**
スキーマとプロンプトを変更すれば、あとは **同じロジックで処理可能** です。
dict_schema = convert_to_openai_function(Locations) # スキーマ変更
location_chain = prompt | llm.with_structured_output(dict_schema)
locations = location_chain.invoke({"question": question})
location_names = eval(locations['locations'])
または、商品名の場合:
dict_schema = convert_to_openai_function(Products)
product_chain = prompt | llm.with_structured_output(dict_schema)
products = product_chain.invoke({"question": question})
product_names = eval(products['products'])
—
## **結論**
> **スキーマ (`Entities` クラスの `names` 部分) とプロンプト (`system` / `human` メッセージ) を変更すれば、別の用途でもそのまま使える。**
`BaseModel` の型 (`list[str]`) や `description` を変更することで、抽出する対象を自由にカスタマイズ可能です。