MCPcopy Create free account
hub / github.com/aiprodcoder/MIXAPI / ClaudeToOpenAIRequest

Function ClaudeToOpenAIRequest

service/convert.go:14–189  ·  view source on GitHub ↗
(claudeRequest dto.ClaudeRequest, info *relaycommon.RelayInfo)

Source from the content-addressed store, hash-verified

12)
13
14func ClaudeToOpenAIRequest(claudeRequest dto.ClaudeRequest, info *relaycommon.RelayInfo) (*dto.GeneralOpenAIRequest, error) {
15 openAIRequest := dto.GeneralOpenAIRequest{
16 Model: claudeRequest.Model,
17 MaxTokens: claudeRequest.MaxTokens,
18 Temperature: claudeRequest.Temperature,
19 TopP: claudeRequest.TopP,
20 Stream: claudeRequest.Stream,
21 }
22
23 isOpenRouter := info.ChannelType == constant.ChannelTypeOpenRouter
24
25 if claudeRequest.Thinking != nil && claudeRequest.Thinking.Type == "enabled" {
26 if isOpenRouter {
27 reasoning := openrouter.RequestReasoning{
28 MaxTokens: claudeRequest.Thinking.GetBudgetTokens(),
29 }
30 reasoningJSON, err := json.Marshal(reasoning)
31 if err != nil {
32 return nil, fmt.Errorf("failed to marshal reasoning: %w", err)
33 }
34 openAIRequest.Reasoning = reasoningJSON
35 } else {
36 thinkingSuffix := "-thinking"
37 if strings.HasSuffix(info.OriginModelName, thinkingSuffix) &&
38 !strings.HasSuffix(openAIRequest.Model, thinkingSuffix) {
39 openAIRequest.Model = openAIRequest.Model + thinkingSuffix
40 }
41 }
42 }
43
44 // Convert stop sequences
45 if len(claudeRequest.StopSequences) == 1 {
46 openAIRequest.Stop = claudeRequest.StopSequences[0]
47 } else if len(claudeRequest.StopSequences) > 1 {
48 openAIRequest.Stop = claudeRequest.StopSequences
49 }
50
51 // Convert tools
52 tools, _ := common.Any2Type[[]dto.Tool](claudeRequest.Tools)
53 openAITools := make([]dto.ToolCallRequest, 0)
54 for _, claudeTool := range tools {
55 openAITool := dto.ToolCallRequest{
56 Type: "function",
57 Function: dto.FunctionRequest{
58 Name: claudeTool.Name,
59 Description: claudeTool.Description,
60 Parameters: claudeTool.InputSchema,
61 },
62 }
63 openAITools = append(openAITools, openAITool)
64 }
65 openAIRequest.Tools = openAITools
66
67 // Convert messages
68 openAIMessages := make([]dto.Message, 0)
69
70 // Add system message if present
71 if claudeRequest.System != nil {

Callers 1

ConvertClaudeRequestMethod · 0.92

Calls 14

SetStringContentMethod · 0.95
SetMediaContentMethod · 0.95
SetToolCallsMethod · 0.95
ParseContentMethod · 0.95
toJSONStringFunction · 0.85
GetBudgetTokensMethod · 0.80
IsStringSystemMethod · 0.80
GetStringSystemMethod · 0.80
ParseSystemMethod · 0.80
GetTextMethod · 0.80
ParseMediaContentMethod · 0.80
IsStringContentMethod · 0.45

Tested by

no test coverage detected