Repository navigation
Expand file tree
/
Copy pathapitype.go
More file actions
123 lines (107 loc) · 3.32 KB
/
Copy pathapitype.go
File metadata and controls
123 lines (107 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package llmproxy
import (
"encoding/json"
)
type APIType string
const (
APITypeChatCompletions APIType = "chat_completions"
APITypeResponses APIType = "responses"
APITypeCompletions APIType = "completions"
APITypeMessages APIType = "messages"
APITypeGenerateContent APIType = "generate_content"
APITypeStreamGenerateContent APIType = "stream_generate_content"
APITypePredictLongRunning APIType = "predict_long_running"
APITypeEmbeddings APIType = "embeddings"
APITypeImagesGenerations APIType = "images_generations"
APITypeAudioSpeech APIType = "audio_speech"
APITypeAudioTranscriptions APIType = "audio_transcriptions"
APITypeConverse APIType = "converse"
)
func DetectAPIType(body []byte) APIType {
var raw map[string]any
if err := json.Unmarshal(body, &raw); err != nil {
return APITypeChatCompletions
}
if _, hasInput := raw["input"]; hasInput {
if _, hasMessages := raw["messages"]; !hasMessages {
return APITypeResponses
}
}
if _, hasPrompt := raw["prompt"]; hasPrompt {
if _, hasMessages := raw["messages"]; !hasMessages {
return APITypeCompletions
}
}
return APITypeChatCompletions
}
func DetectAPITypeFromPath(path string) APIType {
switch {
case containsPath(path, "/v1/chat/completions"):
return APITypeChatCompletions
case containsPath(path, "/v1/responses"):
return APITypeResponses
case containsPath(path, "/v1/completions"):
return APITypeCompletions
case containsPath(path, "/v1/embeddings"):
return APITypeEmbeddings
case containsPath(path, "/v1/images/generations"):
return APITypeImagesGenerations
case containsPath(path, "/v1/audio/speech"):
return APITypeAudioSpeech
case containsPath(path, "/v1/audio/transcriptions"):
return APITypeAudioTranscriptions
case containsPath(path, "/v1/messages"):
return APITypeMessages
case containsPath(path, ":streamGenerateContent"):
return APITypeStreamGenerateContent
case containsPath(path, ":predictLongRunning"):
return APITypePredictLongRunning
case containsPath(path, ":generateContent"):
return APITypeGenerateContent
case containsPath(path, "/converse"):
return APITypeConverse
default:
return ""
}
}
func DetectAPITypeFromBodyAndProvider(body []byte, provider string) APIType {
var raw map[string]any
if err := json.Unmarshal(body, &raw); err != nil {
return APITypeChatCompletions
}
if _, hasInput := raw["input"]; hasInput {
if _, hasMessages := raw["messages"]; !hasMessages {
return APITypeResponses
}
}
if _, hasPrompt := raw["prompt"]; hasPrompt {
if _, hasMessages := raw["messages"]; !hasMessages {
return APITypeCompletions
}
}
if _, hasContents := raw["contents"]; hasContents {
return APITypeGenerateContent
}
if _, hasMessages := raw["messages"]; hasMessages {
switch provider {
case "anthropic":
return APITypeMessages
case "googleai":
if _, hasContents := raw["contents"]; hasContents {
return APITypeGenerateContent
}
return APITypeMessages
case "bedrock":
return APITypeConverse
}
}
if _, hasSystem := raw["system"]; hasSystem {
if _, hasMessages := raw["messages"]; hasMessages {
return APITypeMessages
}
}
return APITypeChatCompletions
}
func containsPath(path, substr string) bool {
return len(path) >= len(substr) && path[len(path)-len(substr):] == substr
}