定义一种风格
我们如何仅通过提示来约束模型的输出?
为了约束模型的响应以符合我们任务的界限,我们可以指定一种风格。
风格约束可以包括
- 写作风格: 写一首华丽的诗
- 语调: 写一首戏剧性的诗
- 情绪: 写一首快乐的诗
- 类型: 写一首悬疑诗
实现¶
import instructor
from pydantic import BaseModel
import openai
class Email(BaseModel):
subject: str
message: str
client = instructor.from_openai(openai.OpenAI())
def generate_email(subject, to, sender, tone):
return client.chat.completions.create(
model="gpt-4o",
messages=[
{
"role": "user",
"content": f"""
Write an email about {subject} to {to} from {sender}.
The email should be {tone}.
""",
}
],
response_model=Email,
)
if __name__ == "__main__":
email = generate_email(
subject="invitation to all-hands on Monday at 6pm",
to="John Smith",
sender="Jane Doe",
tone="formal",
)
print(email.subject)
#> Invitation to All-Hands Meeting
print(email.message)
"""
Dear Mr. Smith,
I hope this message finds you well. I am writing to formally invite you to our upcoming all-hands meeting scheduled for Monday at 6:00 PM. This meeting is an important opportunity for us to come together, discuss key updates, and align on our strategic goals.
Please confirm your availability at your earliest convenience. Your presence and contributions to the discussion would be greatly valued.
Thank you and I look forward to your confirmation.
Warm regards,
Jane Doe
"""
风格约束示例¶
约束 | 可能的短语 |
---|---|
写作风格 | 功能性, 华丽, 坦率, 平淡, 华美, 诗意 |
语调 | 戏剧性, 幽默, 乐观, 悲伤, 正式, 非正式 |
情绪 | 愤怒, 恐惧, 快乐, 悲伤 |
类型 | 历史小说, 文学小说, 科幻小说, 悬疑, 反乌托邦, 恐怖 |
更多风格约束
要查看这些风格约束和附加约束(性格刻画、节奏和情节)的更多示例,请查阅这篇论文。
参考资料¶
1: Bounding the Capabilities of Large Language Models in Open Text Generation with Prompt Constraints