ACT LIKE 提示
提示工程:指示 LLM “扮演”某种角色
Section titled “提示工程:指示 LLM “扮演”某种角色”现代自然语言处理 (NLP) 模型,例如驱动 ChatGPT 的模型,能够生成极其接近人类的文本。提示工程中一个指导其响应的关键技术是指示模型“扮演”(act like)特定的角色(persona)、人物或实体。这通常被称为角色提示(persona prompting)或角色扮演(role-playing)。
本章探讨“扮演”提示的概念,提供示例,并重点介绍它们在各种场景中的应用,以调整 LLM 的行为、语气和知识。
理解“扮演”提示
Section titled “理解“扮演”提示”- 定义:“扮演”提示明确指示 LLM 在生成响应时采用特定的身份、角色或人物。这不仅仅是回答一个问题;它关乎扮演一个身份。
- 角色扮演:这些提示使用户能够通过让模型模拟不同角色,以更具沉浸感、更有针对性或更具创意的方式与模型互动。
- 影响响应:通过指定角色、职业,甚至是一个抽象概念(例如,“扮演一个谨慎乐观主义者”),用户可以显著影响模型的语言风格、语气、词汇以及它所依赖的知识领域。例如,让它扮演海盗会产生与扮演理论物理学家不同的语言。
“扮演”提示的示例
Section titled ““扮演”提示的示例”以下是一些展示这项技术多功能性的示例:
扮演历史人物:
- 提示:“你将扮演莱昂纳多·达·芬奇。描述你对鸟类飞行的观察,以及它如何启发了飞行机器的设计。”
- 预期响应:模型将尝试生成一种风格和知识与莱昂纳多·达·芬奇一致的响应,侧重于观察、力学和发明性思维。
模仿虚构人物:
- 提示:“假设你扮演夏洛克·福尔摩斯。一幅珍贵画作从一间上锁的画廊中消失了,没有明显的入口。你的初步想法和问题是什么?”
- 预期响应:模型将采用福尔摩斯标志性的演绎推理、对细节的关注以及追根究底的语气。
模拟专家或专业人士:
- 提示:“扮演一位经验丰富的财务顾问。向新手投资者解释平均成本法(dollar-cost averaging)的概念,重点说明其优点和潜在缺点。”
- 预期响应:模型将提供清晰、专业的解释,使用恰当的金融术语,但同时让初学者也能理解。
“扮演”提示的应用
Section titled ““扮演”提示的应用”- 创意写作与讲故事:作家可以使用“扮演”提示来生成特定角色的对话、内心独白或叙事段落,增加深度和真实性。
- 教育与学习:学生可以与扮演历史人物、科学家或文学角色的 LLMs 互动,以便更具沉浸感地理解不同学科。
- 内容生成:营销人员可以使用此方法以特定的品牌声音或针对特定的目标受众角色生成内容。
- 头脑风暴与解决问题:采用不同的专家角色有助于从多个角度探索问题(例如,“扮演工程师,然后扮演设计师,为…提出解决方案”)。
- 娱乐与游戏:“扮演”提示是为基于聊天的游戏或虚拟助手创建互动体验的基础,用户可以与不同的虚拟角色互动。
示例:使用 OpenAI API 的 Python 实现
Section titled “示例:使用 OpenAI API 的 Python 实现”让我们看看如何使用 Python 和 OpenAI API 实现“扮演”提示。
from openai import OpenAIimport os
# client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))client = OpenAI(api_key='YOUR_OPENAI_API_KEY') # Replace with your key
def generate_persona_response(persona, query): try: response = client.chat.completions.create( model="gpt-3.5-turbo", messages=[ { "role": "system", "content": f"You are to act as {persona}. Respond to the user's query in character, maintaining the persona's typical tone, style, and knowledge." }, { "role": "user", "content": query } ], max_tokens=250, # Adjust based on expected response length n=1, stop=None, temperature=0.7 # Allows for some creativity while staying in character ) return response.choices[0].message.content.strip() except Exception as e: return f"An error occurred: {e}"
# Example usage:persona_to_adopt = "Sherlock Holmes"mystery_query = ("A valuable diamond necklace has been stolen from a locked room. " "The room has no windows, and the only entrance is a solid wooden door, " "locked from the inside. There are no signs of forced entry. " "The owner claims nobody else had access. How might the thief have managed this?")
# Using a more direct prompt within the query can also reinforce the personafull_query_for_sherlock = f"Mr. Holmes, consider this perplexing case: {mystery_query} What are your deductions?"
sherlock_response = generate_persona_response(persona_to_adopt, full_query_for_sherlock)print(f"Response from {persona_to_adopt}:\n")print(sherlock_response)在这个 Python 代码中,generate_persona_response 函数接受一个 persona(角色)和一个 query(查询)。系统消息(system message)为 LLM 设置了扮演的角色。用户的查询随后由扮演该角色的 LLM 进行处理。
运行时,LLM 的响应应反映夏洛克·福尔摩斯分析敏锐和观察入微的特质:
Response from Sherlock Holmes:
"Intriguing, my dear Watson, positively intriguing! A classic locked-room mystery, if ever I saw one. Let us dissect the elements before us.
First, 'locked from the inside' and 'no signs of forced entry' are key. This immediately suggests several possibilities, ruling out brutish methods.
1. **The Owner is Not Being Entirely Truthful:** This is always a primary consideration. Could the owner have staged the theft for insurance purposes or some other motive? We must not dismiss this.2. **A Hidden Mechanism or Passage:** While the room is described as having 'no windows' and a 'solid wooden door,' one must investigate thoroughly. Are there old chimneys, dumbwaiters, or cleverly concealed panels? The term 'solid' can be deceptive.3. **The Thief Never Left, or Was Never 'In' in the Conventional Sense:** Could the thief have hidden within the room prior to it being locked? Or, perhaps, the necklace was removed via some ingenious contraption from outside, through a small, overlooked aperture – a keyhole, a gap under the door, a loose floorboard.4. **The 'Locked from the Inside' is a Deception:** Was the door truly locked from the inside *after* the theft? Or was it locked in a way that *appears* to be from the inside? There are mechanisms – strings, specific tools – that could achieve this.5. **Collusion:** If the owner claims 'nobody else had access,' this must be verified. Could a servant, or someone with a duplicate key, be involved, and the 'locked from inside' part of a more elaborate ruse?
My immediate line of inquiry would be:* A meticulous examination of the room itself – every inch of the walls, floor, ceiling, and the door mechanism.* The nature of the lock: Is it simple or complex? Could it be manipulated?* The owner's character, financial situation, and recent behavior.* The exact timing of when the necklace was last seen and when it was discovered missing.
The game is afoot! We must eliminate the impossible, and whatever remains, however improbable, must be the truth."注意:具体的响应会随每次生成和模型版本而有所不同,但风格和推理应与请求扮演的角色保持一致。
“扮演”提示是提示工程师工具箱中的有力工具。它们允许对 LLM 的输出进行细粒度控制,使用户能够生成不仅信息丰富,而且在各种创意、教育和专业应用场景中风格恰当的文本。理解如何有效定义和调用角色,为与这些先进 AI 模型互动打开了广阔的可能性空间。