Add custom generation options for language models#50
Merged
Conversation
GenerationOptions generic on LanguageModelThere was a problem hiding this comment.
Pull request overview
This PR introduces a flexible system for model-specific generation options by adding a CustomGenerationOptions protocol and subscript-based API to GenerationOptions. This allows each language model implementation to define its own extended parameters without modifying the core options structure.
Key Changes:
- Added
CustomGenerationOptionsprotocol and type-erased storage mechanism toGenerationOptionswith subscript access pattern - Implemented custom options for OpenAI (sampling, reasoning, service tiers), Anthropic (thinking, tool choice, service tiers), Gemini (thinking, server tools), Ollama (flexible dictionary), and Llama (mirostat, penalties)
- Deprecated model-level configuration in GeminiLanguageModel in favor of per-request options
- Added comprehensive test coverage for custom options functionality
Reviewed changes
Copilot reviewed 12 out of 13 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| Sources/AnyLanguageModel/LanguageModel.swift | Added CustomGenerationOptions associated type to LanguageModel protocol |
| Sources/AnyLanguageModel/GenerationOptions.swift | Implemented custom options protocol, type-erased storage, and subscript accessor |
| Sources/AnyLanguageModel/Models/OpenAILanguageModel.swift | Added comprehensive CustomGenerationOptions struct with 20+ parameters; integrated into API request building |
| Sources/AnyLanguageModel/Models/AnthropicLanguageModel.swift | Added CustomGenerationOptions with Anthropic-specific parameters (topK, thinking, tool choice) |
| Sources/AnyLanguageModel/Models/GeminiLanguageModel.swift | Moved thinking and serverTools from model properties to CustomGenerationOptions; deprecated old initializer |
| Sources/AnyLanguageModel/Models/OllamaLanguageModel.swift | Used typealias to [String: JSONValue] for flexible custom options |
| Sources/AnyLanguageModel/Models/LlamaLanguageModel.swift | Added CustomGenerationOptions with mirostat sampling and penalty parameters |
| Tests/AnyLanguageModelTests/CustomGenerationOptionsTests.swift | Comprehensive unit tests for all custom options implementations |
| Tests/AnyLanguageModelTests/OpenAILanguageModelTests.swift | Added integration tests demonstrating custom options usage |
| Tests/AnyLanguageModelTests/GeminiLanguageModelTests.swift | Updated tests to use new custom options pattern |
| Tests/AnyLanguageModelTests/LlamaLanguageModelTests.swift | Added tests for mirostat and penalty custom options |
| Tests/AnyLanguageModelTests/SystemLanguageModelTests.swift | Updated test prompt for variety |
| Package.resolved | Added llama.swift dependency |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Resolves #39