Skip to content
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

Fix Perplexity API Role Compatibility (Issue #448) #454

Merged
merged 3 commits into from
Mar 10, 2025

Conversation

minkiha
Copy link
Contributor

@minkiha minkiha commented Mar 4, 2025

Fix Perplexity API Role Compatibility (Issue #448)

This fix maintains compatibility with legacy o1 models while solving the Perplexity error.

The Problem

A previous update (commit e5e4bdd) added support for o1-preview and o1-mini models by converting system messages to user messages. However, this breaks Perplexity models because their API doesn't allow multiple consecutive messages with the same role.

How to See the Issue

Try this command and you'll see an error:

mods --model llama-3.1-sonar-small-128k-online --role shell list all files

Solution

  • Only remap system messages for the two models that need it (o1-preview and o1-mini)
  • Keep the original message roles for all other models that go through the function createOpenAIStream (stream.go)

Note

The system message remapping is not needed for these models, which handle system messages correctly without modification:

  • o1
  • o3-mini
  • gpt-4.5-preview

@minkiha minkiha requested a review from caarlos0 as a code owner March 4, 2025 23:33
stream.go Outdated
Comment on lines 25 to 36
messages := m.messages
if mod.Name == "o1-preview" || mod.Name == "o1-mini" {
messages = []openai.ChatCompletionMessage{}
for _, message := range m.messages {
if message.Role == openai.ChatMessageRoleSystem {
messages = append(messages, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleUser,
Content: message.Content,
})
} else {
messages = append(messages, message)
}
Copy link
Member

@caarlos0 caarlos0 Mar 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could probably simplify this to something like:

if mod.Name == "o1-preview" || mod.Name == "o1-mini" {
	for i := range m.messages {
		if message.Role != openai.ChatMessageRoleSystem {
			continue
		}
		m.messages[i].Role = openai.ChatMessageRoleUser
	}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah you're right, simplified it.

Copy link
Member

@caarlos0 caarlos0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the PR!

made one one small suggestion

@caarlos0
Copy link
Member

thanks!

@caarlos0 caarlos0 added the bug Something isn't working label Mar 10, 2025
@caarlos0 caarlos0 merged commit f0daa27 into charmbracelet:main Mar 10, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants