Skip to content

Add custom generation options for language models#50

Merged
mattt merged 16 commits intomainfrom
mattt/generic-generation-options
Dec 1, 2025
Merged

Add custom generation options for language models#50
mattt merged 16 commits intomainfrom
mattt/generic-generation-options

Conversation

@mattt
Copy link
Collaborator

@mattt mattt commented Dec 1, 2025

Resolves #39

@mattt mattt changed the title Make GenerationOptions generic on LanguageModel Add custom generation options for language models Dec 1, 2025
@mattt mattt marked this pull request as ready for review December 1, 2025 11:09
@mattt mattt requested a review from Copilot December 1, 2025 11:09
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 CustomGenerationOptions protocol and type-erased storage mechanism to GenerationOptions with 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.

@mattt mattt merged commit a3febf6 into main Dec 1, 2025
3 checks passed
@mattt mattt deleted the mattt/generic-generation-options branch December 1, 2025 11:44
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.

Support 'extra_body' in OpenAI spec. (to better support OpenRouter and vendor-specific parameters)

2 participants