模拟视角
我们如何鼓励模型关注相关信息?
SimToM(模拟心智理论)是一种两步提示技术,鼓励模型从特定视角进行思考。
这对于涉及多个实体的复杂问题很有用。例如,如果提示包含关于两个人的信息,我们可以要求模型从其中一个人的视角回答我们的查询。
这通过两个步骤实现。给定一个实体,
- 识别并分离与该实体相关的信息
- 要求模型从该实体的视角回答查询
示例模板
步骤 1: 给定以下上下文,列出 <实体> 会知道的事实。上下文:<上下文>
步骤 2: 你是 <实体>。仅根据你了解的这些事实来回答以下问题:<事实>。问题:<查询>
实现¶
import openai
import instructor
from pydantic import BaseModel, Field
from typing import Iterable
client = instructor.from_openai(openai.OpenAI())
class KnownFact(BaseModel):
fact: str = Field(description="A fact that the given entity would know")
class Response(BaseModel):
location: str
def generate_known_facts(entity, context, query) -> Iterable[KnownFact]:
return client.chat.completions.create(
model="gpt-4o",
response_model=Iterable[KnownFact],
messages=[
{
"role": "user",
"content": f"""Given the following context, list
the facts that {entity} would know:
Context:
{context}
{query}
List only the facts relevant to {entity}.
""",
}
],
)
def answer_question_based_on_facts(entity, query, known_facts) -> Response:
return client.chat.completions.create(
model="gpt-4o",
response_model=Response,
messages=[
{
"role": "system",
"content": f"""You are {entity}. Answer the following question
based only on these facts you know:
{" ".join([str(fact) for fact in known_facts])}""",
},
{
"role": "user",
"content": f"Question: {query}",
},
],
)
if __name__ == "__main__":
entity = "Alice"
context = """Alice puts the book on the table.
Alice leaves the room.
Bob moves the book to the shelf.
"""
query = f"Where does {entity} think the book is?"
known_facts = generate_known_facts(entity, context, query)
response = answer_question_based_on_facts(entity, query, known_facts)
for fact in known_facts:
print(fact)
#> fact='Alice puts the book on the table.'
#> fact='Alice leaves the room. Bob moves the book to the shelf.'
print(response.location)
#> On the table
参考¶
1: Think Twice: Perspective-Taking Improves Large Language Models' Theory-of-Mind Capabilities