97e91a83f3
Ruff / Ruff (push) Has been cancelled
Test / Core Tests (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.10) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.11) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.12) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.13) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.9) (push) Has been cancelled
Test / Full Coverage (Python 3.11) (push) Has been cancelled
Test / Core Provider Tests (OpenAI) (push) Has been cancelled
Test / Core Provider Tests (Anthropic) (push) Has been cancelled
Test / Core Provider Tests (Google) (push) Has been cancelled
Test / Core Provider Tests (Other) (push) Has been cancelled
Test / Anthropic Tests (push) Has been cancelled
Test / Gemini Tests (push) Has been cancelled
Test / Google GenAI Tests (push) Has been cancelled
Test / Vertex AI Tests (push) Has been cancelled
Test / OpenAI Tests (push) Has been cancelled
Test / Writer Tests (push) Has been cancelled
Test / Auto Client Tests (push) Has been cancelled
ty / type-check (push) Has been cancelled
2.9 KiB
2.9 KiB
title, description
| title | description |
|---|---|
| Style Prompting | To contrain a model's response to fit the boundaries of our task, we can specify a style. |
How can we constrain model outputs through prompting alone?
To contrain a model's response to fit the boundaries of our task, we can specify a style.
Stylistic constraints can include:
- writing style: write a flowery poem
- tone: write a dramatic poem
- mood: write a happy poem
- genre: write a mystery poem
Implementation
import instructor
from pydantic import BaseModel
import openai
class Email(BaseModel):
subject: str
message: str
client = instructor.from_provider("openai/gpt-5-nano")
def generate_email(subject, to, sender, tone):
return client.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
"""
Stylistic Constraint Examples
| Constraint | Possible Phrases |
|---|---|
| Writing Style | Functional, Flowery, Candid, Prosaic, Ornate, Poetic |
| Tone | Dramatic, Humorous, Optimistic, Sad, Formal, Informal |
| Mood | Angry, Fearful, Happy, Sad |
| Genre | Historical Fiction, Literary Fiction, Science Fiction, Mystery, Dystopian, Horror |
!!! info "More Stylistic Constraints"
To see even more examples of these stylistic constraints and additional constraints (**characterization**, **pacing**, and **plot**), check out [this](https://arxiv.org/abs/2302.09185) paper.
References
1: Bounding the Capabilities of Large Language Models in Open Text Generation with Prompt Constraints