Skip to content

Improve ergonomics of prompt and sampling message initialization #120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 26, 2025

Conversation

mattt
Copy link
Contributor

@mattt mattt commented May 26, 2025

Follow-up to #119

This PR improves the ergonomics for constructing prompt and sampling messages in the following ways:

  • Replace Message(role:content:) initializers with .user(_ content:) and .assistant(_ content:) factory methods
  • Making Message.Content expressible by string literals (with support for interpolation).
Before

Prompt Messages:

let messages = [
    Prompt.Message(role: .user, content: .text(text: "Tell me about yourself")),
    Prompt.Message(role: .assistant, content: .text(text: "I'm a software engineer with 5 years of experience"))
]

// Interview scenario
let candidateName = "Bob"
let position = "Data Scientist"
let interviewMessage = Prompt.Message(
    role: .assistant,
    content: .text(text: "Welcome \(candidateName)! Tell me about your experience as a \(position)")
)

Sampling Messages:

let messages = [
    Sampling.Message(role: .user, content: .text("What's the weather like?")),
    Sampling.Message(role: .assistant, content: .text("I'd be happy to help you check the weather!"))
]


// Mixed content types
let conversation = [
    Sampling.Message(role: .user, content: "Can you analyze this image?"),
    Sampling.Message(role: .assistant, content: .image(data: "base64data", mimeType: "image/png")),
    Sampling.Message(role: .user, content: "What does it show?")
]
After

Prompt Messages:

let messages: [Prompt.Message] = [
    .user("Tell me about yourself"),
    .assistant("I'm a software engineer with 5 years of experience")
]

// Interview scenario with string interpolation
let candidateName = "Bob"
let position = "Data Scientist"
let interviewMessage: Prompt.Message = .assistant("Welcome \(candidateName)! Tell me about your experience as a \(position)")

Sampling Messages:

let messages: [Sampling.Message] = [
    .user("What's the weather like?"),
    .assistant("I'd be happy to help you check the weather!")
]

// Mixed content types
let conversation: [Sampling.Message] = [
    .user("Can you analyze this image?"),
    .assistant(.image(data: "base64data", mimeType: "image/png")),
    .user("What does it show?")
]

For backwards compatibility, the existing Message initializers are deprecated. In the future, they'll be made internal.

@mattt mattt merged commit 3b505ee into main May 26, 2025
6 checks passed
@mattt mattt deleted the mattt/improve-message-ergonomics branch May 26, 2025 12:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant