-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathopenai.go
More file actions
252 lines (228 loc) · 6.79 KB
/
openai.go
File metadata and controls
252 lines (228 loc) · 6.79 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
package mockllm
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"github.com/openai/openai-go/v3"
)
// OpenAIProvider handles OpenAI Chat Completions API request/response mocking
type OpenAIProvider struct {
mocks []OpenAIMock
}
// NewOpenAIProvider creates a new OpenAI provider with the given mocks
func NewOpenAIProvider(mocks []OpenAIMock) *OpenAIProvider {
return &OpenAIProvider{
mocks: mocks,
}
}
// Handle processes an OpenAI chat completion request
func (p *OpenAIProvider) Handle(w http.ResponseWriter, r *http.Request) {
// Parse request body to check for stream
// This is due to a limitation on the ChatCompletionNewParams type in the SDK
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, fmt.Sprintf("Failed to read body: %v", err), http.StatusBadRequest)
return
}
r.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
// Check for stream: true in raw JSON
var rawMap map[string]interface{}
if err := json.Unmarshal(bodyBytes, &rawMap); err != nil {
http.Error(w, fmt.Sprintf("Invalid JSON: %v", err), http.StatusBadRequest)
return
}
// Parse the incoming request into SDK type
var requestBody openai.ChatCompletionNewParams
if err := json.NewDecoder(bytes.NewBuffer(bodyBytes)).Decode(&requestBody); err != nil {
http.Error(w, fmt.Sprintf("Invalid JSON: %v", err), http.StatusBadRequest)
return
}
// Find a matching mock
mock := p.findMatchingMock(requestBody, r.Header)
if mock == nil {
requestBodyBytes, err := json.MarshalIndent(requestBody, "", " ")
if err != nil {
http.Error(w, fmt.Sprintf("Failed to encode request body: %v", err),
http.StatusInternalServerError)
return
}
http.Error(w, fmt.Sprintf("No matching mock found. Request: %s",
string(requestBodyBytes)), http.StatusNotFound)
return
}
// Return the response
isStream := false
if streamVal, ok := rawMap["stream"].(bool); ok {
isStream = streamVal
}
if isStream {
p.handleStreamingResponse(w, mock.Response)
} else {
p.handleNonStreamingResponse(w, mock.Response)
}
}
// findMatchingMock finds the first mock that matches the request
func (p *OpenAIProvider) findMatchingMock(request openai.ChatCompletionNewParams, headers http.Header) *OpenAIMock {
for _, mock := range p.mocks {
if p.requestsMatch(mock.Match, request) && headersMatch(mock.Match.Headers, headers) {
return &mock
}
}
return nil
}
// requestsMatch checks if two requests are equivalent
func (p *OpenAIProvider) requestsMatch(expected OpenAIRequestMatch, actual openai.ChatCompletionNewParams) bool {
// Simple deep equal comparison for now
// In the future, we could add more sophisticated matching
switch expected.MatchType {
case MatchTypeExact:
// get Last message from actual
if len(actual.Messages) == 0 {
return false
}
lastMessage := actual.Messages[len(actual.Messages)-1]
// Check json is equal
jsonExpected, err := json.Marshal(expected.Message)
if err != nil {
return false
}
jsonActual, err := json.Marshal(lastMessage)
if err != nil {
return false
}
return bytes.Equal(jsonExpected, jsonActual)
case MatchTypeContains:
// Check if the last message contains the expected message
if len(actual.Messages) == 0 {
return false
}
lastMessage := actual.Messages[len(actual.Messages)-1]
if *lastMessage.GetRole() != *expected.Message.GetRole() {
return false
}
strExpected, ok := expected.Message.GetContent().AsAny().(*string)
if !ok {
return false
}
strActual, ok := lastMessage.GetContent().AsAny().(*string)
if !ok {
return false
}
return strings.Contains(*strActual, *strExpected)
default:
return false
}
}
// handleNonStreamingResponse sends a JSON response
func (p *OpenAIProvider) handleNonStreamingResponse(w http.ResponseWriter, response any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(response); err != nil {
http.Error(w, fmt.Sprintf("Failed to encode response: %v", err), http.StatusInternalServerError)
}
}
// handleStreamingResponse sends a streaming SSE response
func (p *OpenAIProvider) handleStreamingResponse(w http.ResponseWriter, response openai.ChatCompletion) {
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.WriteHeader(http.StatusOK)
flusher, ok := w.(http.Flusher)
if !ok {
http.Error(w, "Streaming not supported by server", http.StatusInternalServerError)
return
}
// Helper to send a chunk
sendChunk := func(chunk openai.ChatCompletionChunk) {
jsonBytes, err := json.Marshal(chunk)
if err != nil {
fmt.Printf("Failed to encode chunk: %v\n", err)
return
}
fmt.Fprintf(w, "data: %s\n\n", jsonBytes)
flusher.Flush()
}
// 1. Initial chunk with role
chunk1 := openai.ChatCompletionChunk{
ID: response.ID,
Object: "chat.completion.chunk",
Created: response.Created,
Model: response.Model,
Choices: []openai.ChatCompletionChunkChoice{
{
Index: 0,
Delta: openai.ChatCompletionChunkChoiceDelta{
Role: string(response.Choices[0].Message.Role),
},
},
},
}
sendChunk(chunk1)
// 2. Content chunk
chunk2 := openai.ChatCompletionChunk{
ID: response.ID,
Object: "chat.completion.chunk",
Created: response.Created,
Model: response.Model,
Choices: []openai.ChatCompletionChunkChoice{
{
Index: 0,
Delta: openai.ChatCompletionChunkChoiceDelta{
Content: response.Choices[0].Message.Content,
},
},
},
}
sendChunk(chunk2)
// 2.5 ToolCalls chunk
if len(response.Choices[0].Message.ToolCalls) > 0 {
var toolCallDeltas []openai.ChatCompletionChunkChoiceDeltaToolCall
for i, tc := range response.Choices[0].Message.ToolCalls {
toolCallDeltas = append(toolCallDeltas, openai.ChatCompletionChunkChoiceDeltaToolCall{
Index: int64(i),
ID: tc.ID,
Type: tc.Type,
Function: openai.ChatCompletionChunkChoiceDeltaToolCallFunction{
Name: tc.Function.Name,
Arguments: tc.Function.Arguments,
},
})
}
chunkTool := openai.ChatCompletionChunk{
ID: response.ID,
Object: "chat.completion.chunk",
Created: response.Created,
Model: response.Model,
Choices: []openai.ChatCompletionChunkChoice{
{
Index: 0,
Delta: openai.ChatCompletionChunkChoiceDelta{
ToolCalls: toolCallDeltas,
},
},
},
}
sendChunk(chunkTool)
}
// 3. Finish reason chunk
chunk3 := openai.ChatCompletionChunk{
ID: response.ID,
Object: "chat.completion.chunk",
Created: response.Created,
Model: response.Model,
Choices: []openai.ChatCompletionChunkChoice{
{
Index: 0,
Delta: openai.ChatCompletionChunkChoiceDelta{},
FinishReason: response.Choices[0].FinishReason,
},
},
}
sendChunk(chunk3)
// 4. DONE
fmt.Fprint(w, "data: [DONE]\n\n")
flusher.Flush()
}