Documentation
¶
Overview ¶
Package tools provides Anthropic-specific tool conversion utilities.
Package tools provides Gemini-specific tool conversion utilities.
Package tools provides Ollama-specific tool conversion utilities. Ollama uses OpenAI-compatible format but requires JSON schema normalization for compatibility with its validation.
Package tools provides shared tool choice conversion utilities for AI provider implementations.
Package tools provides shared tool conversion utilities for AI provider implementations. This consolidates common patterns for converting between universal tool formats and provider-specific formats, reducing code duplication across providers.
Index ¶
- func ConvertAnthropicContentToToolCalls(content []AnthropicContentBlock) []types.ToolCall
- func ConvertFromOllamaToolCalls(toolCalls []OllamaToolCall) []types.ToolCall
- func ConvertFromOpenAIToProviderFormat(openaiTools []OpenAIFormat) []types.Tool
- func ConvertFromOpenAIToolCalls(toolCalls []OpenAIToolCall) []types.ToolCall
- func ConvertGeminiFunctionCallsToUniversal(parts []GeminiPart) []types.ToolCall
- func ConvertToolChoice(toolChoice *types.ToolChoice, format ToolChoiceFormat) interface{}
- func ConvertToolChoiceToAnthropic(toolChoice *types.ToolChoice) interface{}
- func ConvertToolChoiceToOpenAI(toolChoice *types.ToolChoice) interface{}
- func NormalizeJSONSchema(schema map[string]interface{}) map[string]interface{}
- type AnthropicContentBlock
- type AnthropicTool
- type GeminiFunctionCall
- type GeminiFunctionDeclaration
- type GeminiFunctionResp
- type GeminiPart
- type GeminiProperty
- type GeminiSchema
- type GeminiTool
- type OllamaFunctionDef
- type OllamaTool
- type OllamaToolCall
- type OllamaToolCallFunction
- type OpenAIFormat
- type OpenAIFunctionDef
- type OpenAIToolCall
- type OpenAIToolCallFunction
- type ToolChoiceFormat
- type ToolFormat
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ConvertAnthropicContentToToolCalls ¶
func ConvertAnthropicContentToToolCalls(content []AnthropicContentBlock) []types.ToolCall
ConvertAnthropicContentToToolCalls converts Anthropic content blocks to universal tool calls
func ConvertFromOllamaToolCalls ¶
func ConvertFromOllamaToolCalls(toolCalls []OllamaToolCall) []types.ToolCall
ConvertFromOllamaToolCalls converts Ollama tool calls to universal format
func ConvertFromOpenAIToProviderFormat ¶
func ConvertFromOpenAIToProviderFormat(openaiTools []OpenAIFormat) []types.Tool
ConvertFromOpenAIToProviderFormat converts OpenAI format tools to provider-specific format This is useful when you have OpenAI format tools and need to convert to another provider's format
func ConvertFromOpenAIToolCalls ¶
func ConvertFromOpenAIToolCalls(toolCalls []OpenAIToolCall) []types.ToolCall
ConvertFromOpenAIToolCalls converts OpenAI format tool calls to universal format
func ConvertGeminiFunctionCallsToUniversal ¶
func ConvertGeminiFunctionCallsToUniversal(parts []GeminiPart) []types.ToolCall
ConvertGeminiFunctionCallsToUniversal converts Gemini function calls to universal format
func ConvertToolChoice ¶
func ConvertToolChoice(toolChoice *types.ToolChoice, format ToolChoiceFormat) interface{}
ConvertToolChoice converts universal ToolChoice to provider-specific format
func ConvertToolChoiceToAnthropic ¶
func ConvertToolChoiceToAnthropic(toolChoice *types.ToolChoice) interface{}
ConvertToolChoiceToAnthropic converts universal ToolChoice to Anthropic format
func ConvertToolChoiceToOpenAI ¶
func ConvertToolChoiceToOpenAI(toolChoice *types.ToolChoice) interface{}
ConvertToolChoiceToOpenAI converts universal ToolChoice to OpenAI format
func NormalizeJSONSchema ¶
NormalizeJSONSchema normalizes a JSON schema for Ollama compatibility Converts array-typed "type" fields to single string types For example: {"type": ["string", "null"]} becomes {"type": "string"}
Types ¶
type AnthropicContentBlock ¶
type AnthropicContentBlock struct {
Type string `json:"type"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Input map[string]interface{} `json:"input,omitempty"`
Text string `json:"text,omitempty"`
}
AnthropicContentBlock represents a content block in Anthropic's format
type AnthropicTool ¶
type AnthropicTool struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
InputSchema map[string]interface{} `json:"input_schema,omitempty"`
Type string `json:"type,omitempty"` // For native tools (e.g., "web_search_20250305")
// Native tool fields (for Anthropic's built-in tools)
MaxUses *int `json:"max_uses,omitempty"`
AllowedDomains []string `json:"allowed_domains,omitempty"`
BlockedDomains []string `json:"blocked_domains,omitempty"`
UserLocation map[string]interface{} `json:"user_location,omitempty"`
}
AnthropicTool represents a tool in Anthropic's format
func ConvertToAnthropicFormat ¶
func ConvertToAnthropicFormat(tools []types.Tool) []AnthropicTool
ConvertToAnthropicFormat converts universal tools to Anthropic format Handles both custom tools and Anthropic's native tools
type GeminiFunctionCall ¶
type GeminiFunctionCall struct {
Name string `json:"name"`
Args map[string]interface{} `json:"args"`
}
GeminiFunctionCall represents a function call in Gemini's format
type GeminiFunctionDeclaration ¶
type GeminiFunctionDeclaration struct {
Name string `json:"name"`
Description string `json:"description"`
Parameters GeminiSchema `json:"parameters"`
}
GeminiFunctionDeclaration represents a function declaration in Gemini's format
type GeminiFunctionResp ¶
type GeminiFunctionResp struct {
Name string `json:"name"`
Response map[string]interface{} `json:"response"`
}
GeminiFunctionResp represents a function response in Gemini's format
type GeminiPart ¶
type GeminiPart struct {
Text string `json:"text,omitempty"`
FunctionCall *GeminiFunctionCall `json:"functionCall,omitempty"`
FunctionResp *GeminiFunctionResp `json:"functionResponse,omitempty"`
}
GeminiPart represents a part in Gemini's content format
func ConvertUniversalToolCallsToGeminiParts ¶
func ConvertUniversalToolCallsToGeminiParts(toolCalls []types.ToolCall) []GeminiPart
ConvertUniversalToolCallsToGeminiParts converts universal tool calls to Gemini parts
type GeminiProperty ¶
type GeminiProperty struct {
Type string `json:"type,omitempty"`
Description string `json:"description,omitempty"`
Enum []string `json:"enum,omitempty"`
}
GeminiProperty represents a property in Gemini's schema format
type GeminiSchema ¶
type GeminiSchema struct {
Type string `json:"type"`
Properties map[string]GeminiProperty `json:"properties,omitempty"`
Required []string `json:"required,omitempty"`
}
GeminiSchema represents JSON Schema in Gemini's format
type GeminiTool ¶
type GeminiTool struct {
FunctionDeclarations []GeminiFunctionDeclaration `json:"function_declarations,omitempty"`
}
GeminiTool represents a tool in Gemini's format
func ConvertToGeminiFormat ¶
func ConvertToGeminiFormat(tools []types.Tool) []GeminiTool
ConvertToGeminiFormat converts universal tools to Gemini's function_declarations format
type OllamaFunctionDef ¶
type OllamaFunctionDef struct {
Name string `json:"name"`
Description string `json:"description"`
Parameters map[string]interface{} `json:"parameters"` // JSON Schema
}
OllamaFunctionDef represents a function definition in Ollama's format
type OllamaTool ¶
type OllamaTool struct {
Type string `json:"type"` // Always "function"
Function OllamaFunctionDef `json:"function"`
}
OllamaTool represents a tool in Ollama's format (OpenAI-compatible)
func ConvertToOllamaFormat ¶
func ConvertToOllamaFormat(tools []types.Tool) []OllamaTool
ConvertToOllamaFormat converts universal tools to Ollama format with schema normalization Ollama requires that JSON schema "type" fields be strings, not arrays
type OllamaToolCall ¶
type OllamaToolCall struct {
ID string `json:"id"`
Type string `json:"type"` // "function"
Function OllamaToolCallFunction `json:"function"`
}
OllamaToolCall represents a tool call in Ollama's format (OpenAI-compatible)
func ConvertToOllamaToolCalls ¶
func ConvertToOllamaToolCalls(toolCalls []types.ToolCall) []OllamaToolCall
ConvertToOllamaToolCalls converts universal tool calls to Ollama format
type OllamaToolCallFunction ¶
type OllamaToolCallFunction struct {
Name string `json:"name"`
Arguments string `json:"arguments"` // JSON string
}
OllamaToolCallFunction represents a function call in a tool call
type OpenAIFormat ¶
type OpenAIFormat struct {
Type string `json:"type"` // Always "function"
Function OpenAIFunctionDef `json:"function"`
}
OpenAIFormat represents the OpenAI tool format structure
func ConvertToOpenAIFormat ¶
func ConvertToOpenAIFormat(tools []types.Tool) []OpenAIFormat
ConvertToOpenAIFormat converts universal tools to OpenAI format This is the most common format used by OpenAI-compatible providers
type OpenAIFunctionDef ¶
type OpenAIFunctionDef struct {
Name string `json:"name"`
Description string `json:"description"`
Parameters map[string]interface{} `json:"parameters"` // JSON Schema
}
OpenAIFunctionDef represents a function definition in OpenAI format
type OpenAIToolCall ¶
type OpenAIToolCall struct {
ID string `json:"id"`
Type string `json:"type"` // "function"
Function OpenAIToolCallFunction `json:"function"`
}
OpenAIToolCall represents a tool call in OpenAI format
func ConvertToOpenAIToolCalls ¶
func ConvertToOpenAIToolCalls(toolCalls []types.ToolCall) []OpenAIToolCall
ConvertToOpenAIToolCalls converts universal tool calls to OpenAI format
type OpenAIToolCallFunction ¶
type OpenAIToolCallFunction struct {
Name string `json:"name"`
Arguments string `json:"arguments"` // JSON string
}
OpenAIToolCallFunction represents a function call in a tool call
type ToolChoiceFormat ¶
type ToolChoiceFormat string
ToolChoiceFormat represents how tool choice should be formatted for different providers
const ( // ToolChoiceFormatString uses simple string values ("auto", "required", "none") ToolChoiceFormatString ToolChoiceFormat = "string" // ToolChoiceFormatObject uses object/map format (e.g., {"type": "auto"}) ToolChoiceFormatObject ToolChoiceFormat = "object" // ToolChoiceFormatOpenAI uses OpenAI's specific format ToolChoiceFormatOpenAI ToolChoiceFormat = "openai" // ToolChoiceFormatAnthropic uses Anthropic's specific format ToolChoiceFormatAnthropic ToolChoiceFormat = "anthropic" )
type ToolFormat ¶
type ToolFormat string
ToolFormat represents the format used for tool definitions
const ( // ToolFormatOpenAI is the OpenAI-compatible tool format (used by OpenAI, Cerebras, OpenRouter, Qwen, etc.) ToolFormatOpenAI ToolFormat = "openai" // ToolFormatAnthropic is the Anthropic-specific tool format ToolFormatAnthropic ToolFormat = "anthropic" // ToolFormatGemini is the Google Gemini-specific tool format ToolFormatGemini ToolFormat = "gemini" // ToolFormatOllama is the Ollama-specific tool format (OpenAI-compatible with schema normalization) ToolFormatOllama ToolFormat = "ollama" )