Documentation
¶
Index ¶
- Constants
- Variables
- func ContextResults() []string
- func ConvertClaudeToOpenAI(claudeRequest *ClaudeMessagesRequest, modelManager ModelManager, ...) (map[string]interface{}, error)
- func ConvertOpenAIToClaudeResponse(openaiResponse map[string]interface{}, originalRequest *ClaudeMessagesRequest) (map[string]interface{}, error)
- type AnthropicConfig
- type ClaudeContentBlock
- type ClaudeContentBlockImage
- type ClaudeContentBlockText
- type ClaudeContentBlockToolResult
- type ClaudeContentBlockToolUse
- type ClaudeMessage
- func (m *ClaudeMessage) GetContentAsBlocks() ([]ClaudeContentBlock, bool)
- func (m *ClaudeMessage) GetContentAsString() (string, bool)
- func (m ClaudeMessage) MarshalJSON() ([]byte, error)
- func (m *ClaudeMessage) SetContentAsBlocks(blocks []ClaudeContentBlock)
- func (m *ClaudeMessage) SetContentAsString(content string)
- func (m *ClaudeMessage) UnmarshalJSON(data []byte) error
- type ClaudeMessagesRequest
- func (r *ClaudeMessagesRequest) GetStream() bool
- func (r *ClaudeMessagesRequest) GetSystemAsContents() ([]ClaudeSystemContent, bool)
- func (r *ClaudeMessagesRequest) GetSystemAsString() (string, bool)
- func (r *ClaudeMessagesRequest) GetTemperature() float64
- func (r *ClaudeMessagesRequest) GetTopK() *int
- func (r *ClaudeMessagesRequest) GetTopP() *float64
- func (r *ClaudeMessagesRequest) SetStream(stream bool)
- func (r *ClaudeMessagesRequest) SetSystemAsContents(contents []ClaudeSystemContent)
- func (r *ClaudeMessagesRequest) SetSystemAsString(system string)
- func (r *ClaudeMessagesRequest) SetTemperature(temp float64)
- func (r *ClaudeMessagesRequest) SetTopK(topK int)
- func (r *ClaudeMessagesRequest) SetTopP(topP float64)
- func (r *ClaudeMessagesRequest) UnmarshalJSON(data []byte) error
- type ClaudeSystemContent
- type ClaudeThinkingConfig
- type ClaudeTokenCountRequest
- func (r *ClaudeTokenCountRequest) GetSystemAsContents() ([]ClaudeSystemContent, bool)
- func (r *ClaudeTokenCountRequest) GetSystemAsString() (string, bool)
- func (r *ClaudeTokenCountRequest) SetSystemAsContents(contents []ClaudeSystemContent)
- func (r *ClaudeTokenCountRequest) SetSystemAsString(system string)
- func (r *ClaudeTokenCountRequest) UnmarshalJSON(data []byte) error
- type ClaudeTool
- type Context
- func (c *Context) AddCallBack(cb func(fc *FinishContext))
- func (c *Context) Callbacks() []func(fc *FinishContext)
- func (c *Context) GetResponse() *Response
- func (c *Context) IsStopped() bool
- func (c *Context) Result() ResultError
- func (c *Context) SetResponse(resp *Response)
- func (c *Context) Stop(result ResultError)
- type DefaultModelManager
- type FinishContext
- type ModelManager
- type ProviderSpec
- type Response
- type ResponseType
- type ResultError
- type StreamingConversionState
- type ToolCallState
Constants ¶
const ( ContentText = "text" ContentImage = "image" ContentToolUse = "tool_use" ContentToolResult = "tool_result" RoleSystem = "system" RoleUser = "user" RoleAssistant = "assistant" RoleTool = "tool" ToolFunction = "function" // Stop reasons StopEndTurn = "end_turn" StopMaxTokens = "max_tokens" StopToolUse = "tool_use" // Event types for streaming EventMessageStart = "message_start" EventContentBlockStart = "content_block_start" EventContentBlockDelta = "content_block_delta" EventContentBlockStop = "content_block_stop" EventMessageDelta = "message_delta" EventMessageStop = "message_stop" EventPing = "ping" // Delta types DeltaText = "text_delta" )
Constants for content types (matching Python Constants class)
Variables ¶
var GlobalAnthropicConfig = NewAnthropicConfig()
var GlobalModelManager = NewDefaultModelManager()
Global instance following the Python pattern: model_manager = ModelManager(config)
Functions ¶
func ContextResults ¶
func ContextResults() []string
func ConvertClaudeToOpenAI ¶ added in v2.9.4
func ConvertClaudeToOpenAI(claudeRequest *ClaudeMessagesRequest, modelManager ModelManager, config *AnthropicConfig) (map[string]interface{}, error)
ConvertClaudeToOpenAI converts Claude API request format to OpenAI format This is a precise translation of the Python convert_claude_to_openai function
func ConvertOpenAIToClaudeResponse ¶ added in v2.9.4
func ConvertOpenAIToClaudeResponse(openaiResponse map[string]interface{}, originalRequest *ClaudeMessagesRequest) (map[string]interface{}, error)
ConvertOpenAIToClaudeResponse converts OpenAI response to Claude format This is a precise translation of the Python convert_openai_to_claude_response function
Types ¶
type AnthropicConfig ¶ added in v2.9.4
type AnthropicConfig struct {
// Token limits configuration
MinTokensLimit int
MaxTokensLimit int
// Model mapping configuration
SmallModel string
MiddleModel string
BigModel string
}
AnthropicConfig represents the configuration system, mirroring Python config.py This provides centralized configuration for AI Gateway functionality
func NewAnthropicConfig ¶ added in v2.9.4
func NewAnthropicConfig() *AnthropicConfig
func (*AnthropicConfig) GetBigModel ¶ added in v2.9.4
func (c *AnthropicConfig) GetBigModel() string
GetBigModel returns the big model mapping
func (*AnthropicConfig) GetMaxTokensLimit ¶ added in v2.9.4
func (c *AnthropicConfig) GetMaxTokensLimit() int
GetMaxTokensLimit returns the maximum token limit
func (*AnthropicConfig) GetMiddleModel ¶ added in v2.9.4
func (c *AnthropicConfig) GetMiddleModel() string
GetMiddleModel returns the middle model mapping
func (*AnthropicConfig) GetMinTokensLimit ¶ added in v2.9.4
func (c *AnthropicConfig) GetMinTokensLimit() int
GetMinTokensLimit returns the minimum token limit
func (*AnthropicConfig) GetSmallModel ¶ added in v2.9.4
func (c *AnthropicConfig) GetSmallModel() string
GetSmallModel returns the small model mapping
type ClaudeContentBlock ¶ added in v2.9.4
type ClaudeContentBlock struct {
Type string `json:"type"`
Text string `json:"text,omitempty"`
Source map[string]interface{} `json:"source,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Input map[string]interface{} `json:"input,omitempty"`
ToolUseID string `json:"tool_use_id,omitempty"`
Content interface{} `json:"content,omitempty"`
}
ClaudeContentBlock represents any content block (union type)
type ClaudeContentBlockImage ¶ added in v2.9.4
type ClaudeContentBlockImage struct {
Type string `json:"type"`
Source map[string]interface{} `json:"source"`
}
ClaudeContentBlockImage represents an image content block
type ClaudeContentBlockText ¶ added in v2.9.4
ClaudeContentBlockText represents a text content block
type ClaudeContentBlockToolResult ¶ added in v2.9.4
type ClaudeContentBlockToolResult struct {
Type string `json:"type"`
ToolUseID string `json:"tool_use_id"`
Content interface{} `json:"content"` // Can be string, []map[string]interface{}, or map[string]interface{}
}
ClaudeContentBlockToolResult represents a tool result content block
type ClaudeContentBlockToolUse ¶ added in v2.9.4
type ClaudeContentBlockToolUse struct {
Type string `json:"type"`
ID string `json:"id"`
Name string `json:"name"`
Input map[string]interface{} `json:"input"`
}
ClaudeContentBlockToolUse represents a tool use content block
type ClaudeMessage ¶ added in v2.9.4
type ClaudeMessage struct {
Role string `json:"role"` // "user" or "assistant"
Content interface{} `json:"content"` // Can be string or []ClaudeContentBlock
}
ClaudeMessage represents a message in the conversation
func (*ClaudeMessage) GetContentAsBlocks ¶ added in v2.9.4
func (m *ClaudeMessage) GetContentAsBlocks() ([]ClaudeContentBlock, bool)
GetContentAsBlocks returns the content as content blocks if it's an array type
func (*ClaudeMessage) GetContentAsString ¶ added in v2.9.4
func (m *ClaudeMessage) GetContentAsString() (string, bool)
GetContentAsString returns the content as a string if it's a string type
func (ClaudeMessage) MarshalJSON ¶ added in v2.9.4
func (m ClaudeMessage) MarshalJSON() ([]byte, error)
MarshalJSON implements custom JSON marshaling for ClaudeMessage
func (*ClaudeMessage) SetContentAsBlocks ¶ added in v2.9.4
func (m *ClaudeMessage) SetContentAsBlocks(blocks []ClaudeContentBlock)
SetContentAsBlocks sets the content as content blocks
func (*ClaudeMessage) SetContentAsString ¶ added in v2.9.4
func (m *ClaudeMessage) SetContentAsString(content string)
SetContentAsString sets the content as a string
func (*ClaudeMessage) UnmarshalJSON ¶ added in v2.9.4
func (m *ClaudeMessage) UnmarshalJSON(data []byte) error
UnmarshalJSON implements custom JSON unmarshaling for ClaudeMessage
type ClaudeMessagesRequest ¶ added in v2.9.4
type ClaudeMessagesRequest struct {
Model string `json:"model"`
MaxTokens int `json:"max_tokens"`
Messages []ClaudeMessage `json:"messages"`
System interface{} `json:"system,omitempty"` // Can be string or []ClaudeSystemContent
StopSequences []string `json:"stop_sequences,omitempty"`
Stream *bool `json:"stream,omitempty"` // Default: false in Python
Temperature *float64 `json:"temperature,omitempty"` // Default: 1.0 in Python
TopP *float64 `json:"top_p,omitempty"` // Default: None in Python
TopK *int `json:"top_k,omitempty"` // Default: None in Python
Metadata map[string]interface{} `json:"metadata,omitempty"`
Tools []ClaudeTool `json:"tools,omitempty"`
ToolChoice map[string]interface{} `json:"tool_choice,omitempty"`
Thinking *ClaudeThinkingConfig `json:"thinking,omitempty"`
}
ClaudeMessagesRequest represents the request for Claude messages API
func NewClaudeMessagesRequest ¶ added in v2.9.4
func NewClaudeMessagesRequest(model string, maxTokens int, messages []ClaudeMessage) *ClaudeMessagesRequest
NewClaudeMessagesRequest creates a new ClaudeMessagesRequest with default values
func (*ClaudeMessagesRequest) GetStream ¶ added in v2.9.4
func (r *ClaudeMessagesRequest) GetStream() bool
GetStream returns the stream value with default (false)
func (*ClaudeMessagesRequest) GetSystemAsContents ¶ added in v2.9.4
func (r *ClaudeMessagesRequest) GetSystemAsContents() ([]ClaudeSystemContent, bool)
GetSystemAsContents returns the system as content blocks if it's an array type
func (*ClaudeMessagesRequest) GetSystemAsString ¶ added in v2.9.4
func (r *ClaudeMessagesRequest) GetSystemAsString() (string, bool)
GetSystemAsString returns the system as a string if it's a string type
func (*ClaudeMessagesRequest) GetTemperature ¶ added in v2.9.4
func (r *ClaudeMessagesRequest) GetTemperature() float64
GetTemperature returns the temperature value with default (1.0)
func (*ClaudeMessagesRequest) GetTopK ¶ added in v2.9.4
func (r *ClaudeMessagesRequest) GetTopK() *int
GetTopK returns the top_k value (no default, can be nil)
func (*ClaudeMessagesRequest) GetTopP ¶ added in v2.9.4
func (r *ClaudeMessagesRequest) GetTopP() *float64
GetTopP returns the top_p value (no default, can be nil)
func (*ClaudeMessagesRequest) SetStream ¶ added in v2.9.4
func (r *ClaudeMessagesRequest) SetStream(stream bool)
SetStream sets the stream value
func (*ClaudeMessagesRequest) SetSystemAsContents ¶ added in v2.9.4
func (r *ClaudeMessagesRequest) SetSystemAsContents(contents []ClaudeSystemContent)
SetSystemAsContents sets the system as content blocks
func (*ClaudeMessagesRequest) SetSystemAsString ¶ added in v2.9.4
func (r *ClaudeMessagesRequest) SetSystemAsString(system string)
SetSystemAsString sets the system as a string
func (*ClaudeMessagesRequest) SetTemperature ¶ added in v2.9.4
func (r *ClaudeMessagesRequest) SetTemperature(temp float64)
SetTemperature sets the temperature value
func (*ClaudeMessagesRequest) SetTopK ¶ added in v2.9.4
func (r *ClaudeMessagesRequest) SetTopK(topK int)
SetTopK sets the top_k value
func (*ClaudeMessagesRequest) SetTopP ¶ added in v2.9.4
func (r *ClaudeMessagesRequest) SetTopP(topP float64)
SetTopP sets the top_p value
func (*ClaudeMessagesRequest) UnmarshalJSON ¶ added in v2.9.4
func (r *ClaudeMessagesRequest) UnmarshalJSON(data []byte) error
UnmarshalJSON implements custom JSON unmarshaling for ClaudeMessagesRequest
type ClaudeSystemContent ¶ added in v2.9.4
ClaudeSystemContent represents system content
type ClaudeThinkingConfig ¶ added in v2.9.4
type ClaudeThinkingConfig struct {
Enabled bool `json:"enabled"` // Default: true in Python
}
ClaudeThinkingConfig represents thinking configuration
func NewClaudeThinkingConfig ¶ added in v2.9.4
func NewClaudeThinkingConfig() *ClaudeThinkingConfig
NewClaudeThinkingConfig creates a new ClaudeThinkingConfig with default values
type ClaudeTokenCountRequest ¶ added in v2.9.4
type ClaudeTokenCountRequest struct {
Model string `json:"model"`
Messages []ClaudeMessage `json:"messages"`
System interface{} `json:"system,omitempty"` // Can be string or []ClaudeSystemContent
Tools []ClaudeTool `json:"tools,omitempty"`
Thinking *ClaudeThinkingConfig `json:"thinking,omitempty"`
ToolChoice map[string]interface{} `json:"tool_choice,omitempty"`
}
ClaudeTokenCountRequest represents the request for Claude token count API
func NewClaudeTokenCountRequest ¶ added in v2.9.4
func NewClaudeTokenCountRequest(model string, messages []ClaudeMessage) *ClaudeTokenCountRequest
NewClaudeTokenCountRequest creates a new ClaudeTokenCountRequest
func (*ClaudeTokenCountRequest) GetSystemAsContents ¶ added in v2.9.4
func (r *ClaudeTokenCountRequest) GetSystemAsContents() ([]ClaudeSystemContent, bool)
GetSystemAsContents returns the system as content blocks if it's an array type
func (*ClaudeTokenCountRequest) GetSystemAsString ¶ added in v2.9.4
func (r *ClaudeTokenCountRequest) GetSystemAsString() (string, bool)
GetSystemAsString returns the system as a string if it's a string type
func (*ClaudeTokenCountRequest) SetSystemAsContents ¶ added in v2.9.4
func (r *ClaudeTokenCountRequest) SetSystemAsContents(contents []ClaudeSystemContent)
SetSystemAsContents sets the system as content blocks
func (*ClaudeTokenCountRequest) SetSystemAsString ¶ added in v2.9.4
func (r *ClaudeTokenCountRequest) SetSystemAsString(system string)
SetSystemAsString sets the system as a string
func (*ClaudeTokenCountRequest) UnmarshalJSON ¶ added in v2.9.4
func (r *ClaudeTokenCountRequest) UnmarshalJSON(data []byte) error
UnmarshalJSON implements custom JSON unmarshaling for ClaudeTokenCountRequest
type ClaudeTool ¶ added in v2.9.4
type ClaudeTool struct {
Name string `json:"name"`
Description *string `json:"description,omitempty"`
InputSchema map[string]interface{} `json:"input_schema"`
}
ClaudeTool represents a tool definition
type Context ¶
type Context struct {
Ctx *context.Context
Provider *ProviderSpec
// Req is the original request from the user. It's body is in ReqBody.
Req *httpprot.Request
ReqBody []byte
ReqInfo *protocol.GeneralRequest
OpenAIReq map[string]any
RespType ResponseType
ClaudeMessagesRequest *ClaudeMessagesRequest
// ParseMetricFn is a function that parses the response body to a metric.
// If it is sent, it will be called to parse the response body to a metric.
// Otherwise, default ParseMetricFn will be used.
ParseMetricFn func(fc *FinishContext) *metricshub.Metric
// contains filtered or unexported fields
}
func (*Context) AddCallBack ¶
func (c *Context) AddCallBack(cb func(fc *FinishContext))
AddCallBack adds a callback function to the context. The callback will be called when the reponse is sent to the user.
func (*Context) Callbacks ¶
func (c *Context) Callbacks() []func(fc *FinishContext)
CallBacks returns all callback functions registered in the context.
func (*Context) GetResponse ¶
GetResponse returns the response of the context.
func (*Context) Result ¶
func (c *Context) Result() ResultError
Result returns the result of the context execution.
func (*Context) SetResponse ¶
SetResponse sets the response of the context. If you need to close the response body, you should add a callback function to the context using AddCallBack method.
func (*Context) Stop ¶
func (c *Context) Stop(result ResultError)
Stop stops the context execution and sets the result to be returned.
type DefaultModelManager ¶ added in v2.9.4
type DefaultModelManager struct {
// contains filtered or unexported fields
}
DefaultModelManager implements the ModelManager interface This is a precise translation of the Python ModelManager class
func NewDefaultModelManager ¶ added in v2.9.4
func NewDefaultModelManager() *DefaultModelManager
NewDefaultModelManager creates a new DefaultModelManager with the given configuration
func NewModelManagerWithConfig ¶ added in v2.9.4
func NewModelManagerWithConfig(config *AnthropicConfig) *DefaultModelManager
NewModelManagerWithConfig creates a new DefaultModelManager with custom configuration
func (*DefaultModelManager) MapClaudeModelToOpenAI ¶ added in v2.9.4
func (m *DefaultModelManager) MapClaudeModelToOpenAI(claudeModel string) string
MapClaudeModelToOpenAI maps Claude model names to OpenAI model names based on BIG/SMALL pattern This is a precise translation of the Python map_claude_model_to_openai method
type FinishContext ¶
type ModelManager ¶ added in v2.9.4
ModelManager interface to map Claude models to OpenAI models
type ProviderSpec ¶
type ProviderSpec struct {
Name string `json:"name"`
ProviderType string `json:"providerType"`
BaseURL string `json:"baseURL"`
APIKey string `json:"apiKey"`
Headers map[string]string `json:"headers,omitempty"`
// Optional parameters for specific providers, such as Azure.
Endpoint string `json:"endpoint,omitempty"` // It is used for Azure OpenAI.
DeploymentID string `json:"deploymentID,omitempty"` // It is used for Azure OpenAI.
APIVersion string `json:"apiVersion,omitempty"` // It is used for Azure OpenAI.
}
ProviderSpec defines the specification for an AI provider.
type Response ¶
type Response struct {
StatusCode int
// ContentLength records the length of the associated content. The
// value -1 indicates that the length is unknown. Unless Request.Method
// is "HEAD", values >= 0 indicate that the given number of bytes may
// be read from Body.
ContentLength int64
Header http.Header
// BodyReader is the response body reader. Only one of BodyReader or BodyBytes should be set.
BodyReader io.Reader
// BodyBytes is the response body bytes. Only one of BodyReader or BodyBytes should be set.
BodyBytes []byte
}
type ResponseType ¶
type ResponseType string
ResponseType defines the type of response for AI requests.
const ( // Anthropic Response Types. // ResponseTypeMessage is used for message requests. ResponseTypeMessage ResponseType = "/v1/messages" // OpenAI Response Types. // ResponseTypeCompletions is used for standard completion requests. ResponseTypeCompletions ResponseType = "/v1/completions" // ResponseTypeChatCompletions is used for chat completion requests. ResponseTypeChatCompletions ResponseType = "/v1/chat/completions" // ResponseTypeEmbeddings is used for embedding requests. ResponseTypeEmbeddings ResponseType = "/v1/embeddings" // ResponseTypeModels is used for model listing requests. ResponseTypeModels ResponseType = "/v1/models" // ResponseTypeImageGenerations is used for image generation requests. ResponseTypeImageGenerations ResponseType = "/v1/images/generations" )
type ResultError ¶
type ResultError string
const ( // ResultOk indicates a successful request with no errors. // In easegress, it is represented as an empty string. ResultOk ResultError = "" ResultInternalError ResultError = "internalError" ResultClientError ResultError = "clientError" ResultServerError ResultError = "serverError" ResultFailureCodeError ResultError = "failureCodeError" ResultProviderError ResultError = "providerError" ResultMiddlewareError ResultError = "middlewareError" )
type StreamingConversionState ¶ added in v2.9.4
type StreamingConversionState struct {
MessageID string
TextBlockIndex int
ToolBlockCounter int
CurrentToolCalls map[int]*ToolCallState
FinalStopReason string
}
StreamingConversionState holds state for streaming conversion