-
-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathmain.go
More file actions
350 lines (312 loc) · 8.68 KB
/
Copy pathmain.go
File metadata and controls
350 lines (312 loc) · 8.68 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// Package main provides a CLI tool to record real API responses for contract tests.
// Usage:
//
// OPENAI_API_KEY=sk-xxx go run ./cmd/recordapi \
// -provider=openai \
// -endpoint=chat \
// -output=tests/contract/testdata/openai/chat_completion.json
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"time"
)
const oracleDefaultModel = "openai.gpt-oss-120b"
// Provider configurations
var providerConfigs = map[string]struct {
baseURL string
baseURLEnv string
envKey string
authHeader string
contentType string
}{
"openai": {
baseURL: "https://api.openai.com",
envKey: "OPENAI_API_KEY",
authHeader: "Authorization",
contentType: "application/json",
},
"anthropic": {
baseURL: "https://api.anthropic.com",
envKey: "ANTHROPIC_API_KEY",
authHeader: "x-api-key",
contentType: "application/json",
},
"gemini": {
baseURL: "https://generativelanguage.googleapis.com/v1beta/openai",
envKey: "GEMINI_API_KEY",
authHeader: "Authorization",
contentType: "application/json",
},
"groq": {
baseURL: "https://api.groq.com/openai",
envKey: "GROQ_API_KEY",
authHeader: "Authorization",
contentType: "application/json",
},
"xai": {
baseURL: "https://api.x.ai",
envKey: "XAI_API_KEY",
authHeader: "Authorization",
contentType: "application/json",
},
"oracle": {
baseURLEnv: "ORACLE_BASE_URL",
envKey: "ORACLE_API_KEY",
authHeader: "Authorization",
contentType: "application/json",
},
}
// Endpoint configurations
var endpointConfigs = map[string]struct {
path string
method string
requestBody map[string]any
}{
"chat": {
path: "/v1/chat/completions",
method: http.MethodPost,
requestBody: map[string]any{
"model": "gpt-4o-mini",
"messages": []map[string]string{
{"role": "user", "content": "Say 'Hello, World!' and nothing else."},
},
"max_tokens": 50,
},
},
"chat_stream": {
path: "/v1/chat/completions",
method: http.MethodPost,
requestBody: map[string]any{
"model": "gpt-4o-mini",
"messages": []map[string]string{
{"role": "user", "content": "Say 'Hello, World!' and nothing else."},
},
"max_tokens": 50,
"stream": true,
},
},
"models": {
path: "/v1/models",
method: http.MethodGet,
},
"responses": {
path: "/v1/responses",
method: http.MethodPost,
requestBody: map[string]any{
"model": "gpt-4o-mini",
"input": "Say 'Hello, World!' and nothing else.",
},
},
"responses_stream": {
path: "/v1/responses",
method: http.MethodPost,
requestBody: map[string]any{
"model": "gpt-4o-mini",
"input": "Say 'Hello, World!' and nothing else.",
"stream": true,
},
},
}
var providerCapabilities = map[string]map[string]bool{
"openai": {
"responses": true,
},
"anthropic": {
"responses": false,
},
"gemini": {
"responses": false,
},
"groq": {
"responses": false,
},
"xai": {
"responses": true,
},
"oracle": {
"responses": true,
},
}
func endpointRequiresResponsesCapability(endpoint string) bool {
return endpoint == "responses" || endpoint == "responses_stream"
}
func providerSupportsResponses(provider string) bool {
capabilities, ok := providerCapabilities[provider]
if !ok {
return false
}
return capabilities["responses"]
}
func main() {
provider := flag.String("provider", "openai", "Provider to test (openai, anthropic, gemini, groq, xai, oracle)")
endpoint := flag.String("endpoint", "chat", "Endpoint to test (chat, chat_stream, models, responses, responses_stream)")
output := flag.String("output", "", "Output file path (required)")
model := flag.String("model", "", "Override model in request")
flag.Parse()
if *output == "" {
fmt.Fprintln(os.Stderr, "Error: -output flag is required")
flag.Usage()
os.Exit(1)
}
pConfig, ok := providerConfigs[*provider]
if !ok {
fmt.Fprintf(os.Stderr, "Error: unknown provider %q\n", *provider)
os.Exit(1)
}
baseURL := pConfig.baseURL
if pConfig.baseURLEnv != "" {
baseURL = os.Getenv(pConfig.baseURLEnv)
if baseURL == "" {
fmt.Fprintf(os.Stderr, "Error: %s environment variable is required\n", pConfig.baseURLEnv)
os.Exit(1)
}
}
eConfig, ok := endpointConfigs[*endpoint]
if !ok {
fmt.Fprintf(os.Stderr, "Error: unknown endpoint %q\n", *endpoint)
os.Exit(1)
}
if endpointRequiresResponsesCapability(*endpoint) && !providerSupportsResponses(*provider) {
fmt.Fprintf(os.Stderr, "Error: provider %q is missing responses capability (/v1/responses)\n", *provider)
os.Exit(1)
}
apiKey := os.Getenv(pConfig.envKey)
if apiKey == "" {
fmt.Fprintf(os.Stderr, "Error: %s environment variable is required\n", pConfig.envKey)
os.Exit(1)
}
// Build request body
var bodyReader io.Reader
if eConfig.requestBody != nil {
reqBody := eConfig.requestBody
// Oracle's OpenAI-compatible endpoint expects OCI-hosted model IDs,
// so use a provider-specific default instead of the generic gpt-4o-mini fixture.
if *model != "" {
reqBody["model"] = *model
} else if *provider == "oracle" {
reqBody["model"] = oracleDefaultModel
}
// Adjust request for different providers
if *provider == "anthropic" {
reqBody = adjustForAnthropic(reqBody)
}
bodyBytes, err := json.Marshal(reqBody)
if err != nil {
fmt.Fprintf(os.Stderr, "Error marshaling request body: %v\n", err)
os.Exit(1)
}
bodyReader = bytes.NewReader(bodyBytes)
}
// Build URL
url := baseURL + eConfig.path
// Create request
req, err := http.NewRequest(eConfig.method, url, bodyReader)
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating request: %v\n", err)
os.Exit(1)
}
req.Header.Set("Content-Type", pConfig.contentType)
// Add auth header (except for Gemini which uses query param)
if pConfig.authHeader != "" {
if pConfig.authHeader == "Authorization" {
req.Header.Set(pConfig.authHeader, "Bearer "+apiKey)
} else {
req.Header.Set(pConfig.authHeader, apiKey)
}
}
// Add Anthropic-specific headers
if *provider == "anthropic" {
req.Header.Set("anthropic-version", "2023-06-01")
}
// Send request
client := &http.Client{Timeout: 60 * time.Second}
fmt.Printf("Sending request to %s %s...\n", eConfig.method, url)
resp, err := client.Do(req)
if err != nil {
fmt.Fprintf(os.Stderr, "Error sending request: %v\n", err)
os.Exit(1)
}
defer resp.Body.Close()
fmt.Printf("Response status: %d %s\n", resp.StatusCode, resp.Status)
// Read response body
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading response: %v\n", err)
os.Exit(1)
}
// Handle streaming responses differently
if strings.HasSuffix(*endpoint, "_stream") {
if err := writeStreamOutput(*output, body); err != nil {
fmt.Fprintf(os.Stderr, "Error writing output: %v\n", err)
os.Exit(1)
}
fmt.Printf("Streaming response saved to %s\n", *output)
return
}
// Pretty print JSON
var prettyJSON bytes.Buffer
if err := json.Indent(&prettyJSON, body, "", " "); err != nil {
// If it's not valid JSON, write raw
if err := writeOutput(*output, body); err != nil {
fmt.Fprintf(os.Stderr, "Error writing output: %v\n", err)
os.Exit(1)
}
fmt.Printf("Raw response saved to %s\n", *output)
return
}
if err := writeOutput(*output, prettyJSON.Bytes()); err != nil {
fmt.Fprintf(os.Stderr, "Error writing output: %v\n", err)
os.Exit(1)
}
fmt.Printf("Response saved to %s\n", *output)
// Print response summary
var respMap map[string]any
if err := json.Unmarshal(body, &respMap); err == nil {
if id, ok := respMap["id"].(string); ok {
fmt.Printf("Response ID: %s\n", id)
}
if model, ok := respMap["model"].(string); ok {
fmt.Printf("Model: %s\n", model)
}
}
}
// adjustForAnthropic converts OpenAI-style request to Anthropic format
func adjustForAnthropic(req map[string]any) map[string]any {
result := make(map[string]any)
// Copy model
if model, ok := req["model"].(string); ok {
result["model"] = model
}
// Convert max_tokens
if maxTokens, ok := req["max_tokens"].(int); ok {
result["max_tokens"] = maxTokens
} else {
result["max_tokens"] = 1024 // Default for Anthropic
}
// Convert messages
if messages, ok := req["messages"].([]map[string]string); ok {
result["messages"] = messages
}
return result
}
// writeOutput writes data to the output file, creating directories as needed.
func writeOutput(path string, data []byte) error {
dir := filepath.Dir(path)
if err := os.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf("failed to create directory: %w", err)
}
return os.WriteFile(path, data, 0644)
}
// writeStreamOutput writes streaming response data to a text file.
func writeStreamOutput(path string, data []byte) error {
// For streaming responses, save as-is (SSE format)
return writeOutput(path, data)
}