Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions internal/extproc/translator/openai_gcpanthropic.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ const (
tempNotSupportedError = "temperature %.2f is not supported by Anthropic (must be between 0.0 and 1.0)"
)

// anthropicInputSchemaKeysToSkip defines the keys from an OpenAI function parameter map
// that are handled explicitly and should not go into the ExtraFields map.
var anthropicInputSchemaKeysToSkip = map[string]struct{}{
"required": {},
"type": {},
"properties": {},
}

// NewChatCompletionOpenAIToGCPAnthropicTranslator implements [Factory] for OpenAI to GCP Anthropic translation.
// This translator converts OpenAI ChatCompletion API requests to GCP Anthropic API format.
func NewChatCompletionOpenAIToGCPAnthropicTranslator(apiVersion string, modelNameOverride internalapi.ModelNameOverride) OpenAIChatCompletionTranslator {
Expand Down Expand Up @@ -163,16 +171,6 @@ func translateOpenAItoAnthropicTools(openAITools []openai.Tool, openAIToolChoice

inputSchema := anthropic.ToolInputSchemaParam{}

// Dereference json schema
// If the paramsMap contains $refs we need to dereference them
var dereferencedParamsMap any
if dereferencedParamsMap, err = jsonSchemaDereference(paramsMap); err != nil {
return nil, anthropic.ToolChoiceUnionParam{}, fmt.Errorf("failed to dereference tool parameters: %w", err)
}
if paramsMap, ok = dereferencedParamsMap.(map[string]any); !ok {
return nil, anthropic.ToolChoiceUnionParam{}, fmt.Errorf("failed to cast dereferenced tool parameters to map[string]interface{}")
}

var typeVal string
if typeVal, ok = paramsMap["type"].(string); ok {
inputSchema.Type = constant.Object(typeVal)
Expand All @@ -194,6 +192,21 @@ func translateOpenAItoAnthropicTools(openAITools []openai.Tool, openAIToolChoice
inputSchema.Required = requiredSlice
}

// ExtraFieldsMap to construct
ExtraFieldsMap := make(map[string]any)

// Iterate over the original map from openai
for key, value := range paramsMap {
// Check if the current key should be skipped
if _, found := anthropicInputSchemaKeysToSkip[key]; found {
continue
}

// If not skipped, add the key-value pair to extra field map
ExtraFieldsMap[key] = value
}
inputSchema.ExtraFields = ExtraFieldsMap

toolParam.InputSchema = inputSchema
}

Expand Down
Loading
Loading