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.
Overview
Implements comprehensive OpenAI-compatible tool calling functionality for Swama's chat completions API, enabling AI models to interact with external functions and tools.
Key Features
🔧 OpenAI API Compatibility
tools,tool_choice, and tool response formats"none","auto","required", and function-specific🚀 Streaming & Non-Streaming Support
🔄 MLX Integration
ToolSpecformat💬 Complete Message Support
system,user,assistant,toolrole: "tool"Implementation Details
New API Structures
Tool&Function: OpenAI-compatible tool definitionsToolChoice: Flexible tool selection controlResponseToolCall&ResponseFunction: Standardized tool call responsesCompletionRequestwith tool parametersCompletionChoicewith tool call resultsCore Functions
convertToolsToMLX(): OpenAI → MLX tool spec conversionconvertToMLXChatMessages(): Message role mapping with tool supportModel Runner Improvements
ModelRunnerwithrunChat/runChatNonStreamseparationAPI Changes
Request Format
{ "model": "qwen3", "messages": [...], "tools": [ { "type": "function", "function": { "name": "get_weather", "description": "Get current weather", "parameters": {...} } } ], "tool_choice": "auto" }Response Format
{ "choices": [ { "message": { "role": "assistant", "content": "I'll get the weather for you." }, "finish_reason": "tool_calls", "tool_calls": [ { "id": "call_123", "type": "function", "function": { "name": "get_weather", "arguments": "{\"location\": \"Tokyo\"}" } } ] } ] }Streaming Format
Testing
Test Coverage
toolroleBuild Compatibility
swift build)Files Modified
Sources/SwamaKit/Server/CompletionsHandler.swift- Main implementationSources/SwamaKit/Model/ModelRunner.swift- Stream/non-stream separationSources/SwamaKit/Model/ModelPool.swift- Memory managementTests/SwamaKitTests/ToolCallingTests.swift- New test suitePackage.swift&Package.resolved- Dependency updatesDependencies
ToolCalltypes