Skip to content

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.

2 participants