feat: add model-specific prompt formatting with conversation history support #1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR enhances the OpenAI-compatible API server by adding model-specific prompt formatting with full conversation history support. Different AI models require different prompt structures and special tokens to function correctly, especially when handling multi-turn conversations. This change ensures that prompts are properly formatted based on the model being used while preserving the entire conversation context.
Changes
Added model-specific formatting functions with context support:
formatLlama3()- Handles multi-turn conversations with<|begin_of_text|>,<|start_header_id|>tokensformatLlama2()- Preserves conversation history using[INST],<<SYS>>tokensformatMistral()- Maintains context across multiple user/assistant interactionsformatClaude()- Formats entire conversation history in Claude's conversational styleformatGrok()- Supports system instructions and conversation contextformatSimpleChat()- Fallback that preserves all messages for OpenAI/GeminiformatDefault()- Generic format maintaining full message historyMain function:
formatPromptForModel()- Processes entire message array while preserving conversation flowKey Features
🔄 Full Conversation History Support
🎯 Model-Specific Context Handling
Each model family has unique requirements for handling conversation history:
Motivation
When using the OpenAI-compatible API endpoint with different models through Raycast AI, maintaining conversation context is crucial. Without proper formatting:
Testing
Tested with multi-turn conversations on:
Example
Before this change:
After this change:
Breaking Changes
None. This change is backwards compatible and only enhances the existing functionality.
Benefits
✅ Better context understanding: Models can reference previous messages
✅ Improved response quality: Full conversation context leads to more coherent responses
✅ System instruction support: Properly handles system messages for each model
✅ Multi-turn conversations: Enables complex, contextual interactions
✅ Model compatibility: Each model receives its optimal format
Related Issues
Improves model compatibility for OpenAI-compatible API usage
Addresses prompt formatting issues with conversation history
Enables proper context handling for multi-turn conversations
Checklist
Disclosure
This code enhancement was developed with AI assistance. The PR description and documentation were also generated using AI to ensure comprehensive coverage of the changes and their implications.