-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.go
More file actions
294 lines (251 loc) · 8.94 KB
/
types.go
File metadata and controls
294 lines (251 loc) · 8.94 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
package bedrockify
import (
"context"
"fmt"
)
// --- Core interfaces ---
// Converser handles chat completion requests against a backend model API.
type Converser interface {
Converse(ctx context.Context, req *ChatRequest) (*ChatResponse, error)
ConverseStream(ctx context.Context, req *ChatRequest) (<-chan StreamEvent, error)
ListModels(ctx context.Context) ([]ModelInfo, error)
}
// Embedder generates embedding vectors from text.
type Embedder interface {
Embed(ctx context.Context, text string) ([]float64, error)
}
// --- OpenAI-compatible chat request types ---
// ChatRequest is the OpenAI /v1/chat/completions request body.
type ChatRequest struct {
Model string `json:"model"`
Messages []Message `json:"messages"`
MaxTokens int `json:"max_tokens,omitempty"`
MaxCompletionTokens int `json:"max_completion_tokens,omitempty"`
Temperature *float64 `json:"temperature,omitempty"`
TopP *float64 `json:"top_p,omitempty"`
Stream bool `json:"stream,omitempty"`
StreamOptions *StreamOptions `json:"stream_options,omitempty"`
Stop []string `json:"stop,omitempty"`
Tools []Tool `json:"tools,omitempty"`
ToolChoice interface{} `json:"tool_choice,omitempty"`
N int `json:"n,omitempty"`
ReasoningEffort string `json:"reasoning_effort,omitempty"`
ExtraBody map[string]interface{} `json:"extra_body,omitempty"`
}
// StreamOptions controls streaming behavior.
type StreamOptions struct {
IncludeUsage bool `json:"include_usage,omitempty"`
}
// Message is a single chat message.
type Message struct {
Role string `json:"role"`
Content interface{} `json:"content"` // string or []ContentPart
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
ToolCallID string `json:"tool_call_id,omitempty"`
Name string `json:"name,omitempty"`
ReasoningContent string `json:"reasoning_content,omitempty"`
}
// ContentPart is a typed content element (text, image_url, tool_result, etc.)
type ContentPart struct {
Type string `json:"type"`
Text string `json:"text,omitempty"`
ImageURL *ImageURL `json:"image_url,omitempty"`
ToolCallID string `json:"tool_call_id,omitempty"`
Content interface{} `json:"content,omitempty"`
}
// ImageURL holds image data for vision messages.
type ImageURL struct {
URL string `json:"url"`
Detail string `json:"detail,omitempty"`
}
// Tool defines a function the model may call.
type Tool struct {
Type string `json:"type"`
Function ToolFunction `json:"function"`
}
// ToolFunction is the function definition within a tool.
type ToolFunction struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Parameters interface{} `json:"parameters,omitempty"`
}
// ToolCall is a function invocation requested by the model.
type ToolCall struct {
ID string `json:"id"`
Type string `json:"type"`
Function ToolCallFunction `json:"function"`
}
// ToolCallFunction holds the function name and stringified arguments.
type ToolCallFunction struct {
Name string `json:"name"`
Arguments string `json:"arguments"`
}
// --- OpenAI-compatible chat response types ---
// ChatResponse is the non-streaming /v1/chat/completions response.
type ChatResponse struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
Choices []Choice `json:"choices"`
Usage Usage `json:"usage"`
}
// Choice is a single completion option.
type Choice struct {
Index int `json:"index"`
Message Message `json:"message"`
FinishReason string `json:"finish_reason"`
}
// Usage reports token consumption (shared by chat and embeddings).
type Usage struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens,omitempty"`
TotalTokens int `json:"total_tokens"`
PromptTokensDetails *PromptTokensDetails `json:"prompt_tokens_details,omitempty"`
CompletionTokensDetails *CompletionTokensDetails `json:"completion_tokens_details,omitempty"`
}
// PromptTokensDetails holds granular prompt token breakdown.
type PromptTokensDetails struct {
CachedTokens int `json:"cached_tokens,omitempty"`
}
// CompletionTokensDetails holds granular completion token breakdown.
type CompletionTokensDetails struct {
ReasoningTokens int `json:"reasoning_tokens,omitempty"`
}
// --- Streaming types ---
// StreamChunk is the SSE delta payload for streaming responses.
type StreamChunk struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
Choices []StreamChoice `json:"choices"`
Usage *Usage `json:"usage,omitempty"`
}
// StreamChoice is a single delta choice in a streaming chunk.
type StreamChoice struct {
Index int `json:"index"`
Delta Delta `json:"delta"`
FinishReason *string `json:"finish_reason"`
}
// Delta carries the incremental content for a streaming chunk.
type Delta struct {
Role string `json:"role,omitempty"`
Content string `json:"content,omitempty"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
ReasoningContent string `json:"reasoning_content,omitempty"`
}
// StreamEvent is an internal event emitted during streaming.
type StreamEvent struct {
// Text delta content
Text string
// Reasoning/thinking content delta
ReasoningContent string
// Reasoning signature (feature 5.3)
ReasoningSignature string
// Tool call being built up
ToolCallID string
ToolName string
ToolArgs string
// Finish signal
FinishReason string
// Usage stats (sent at end)
Usage *Usage
// Error
Err error
}
// --- Models list ---
// ModelInfo is a single model in the /v1/models list.
type ModelInfo struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created,omitempty"`
OwnedBy string `json:"owned_by"`
}
// ModelsResponse is the /v1/models response.
type ModelsResponse struct {
Object string `json:"object"`
Data []ModelInfo `json:"data"`
}
// --- Embedding types ---
// EmbeddingRequest is a single-string input request.
type EmbeddingRequest struct {
Input string `json:"input"`
Model string `json:"model"`
EncodingFormat string `json:"encoding_format,omitempty"`
Dimensions int `json:"dimensions,omitempty"`
}
// EmbeddingRequestBatch is an array input request.
type EmbeddingRequestBatch struct {
Input []string `json:"input"`
Model string `json:"model"`
EncodingFormat string `json:"encoding_format,omitempty"`
Dimensions int `json:"dimensions,omitempty"`
}
// EmbeddingResponse is the top-level response envelope.
type EmbeddingResponse struct {
Object string `json:"object"`
Data []EmbeddingData `json:"data"`
Model string `json:"model"`
Usage Usage `json:"usage"`
}
// EmbeddingData is a single embedding result.
// Embedding field is interface{} to support both []float64 and base64 string.
type EmbeddingData struct {
Object string `json:"object"`
Index int `json:"index"`
Embedding interface{} `json:"embedding"`
}
// --- API response types ---
// HealthResponse is returned by GET /.
type HealthResponse struct {
Status string `json:"status"`
Model string `json:"model,omitempty"`
EmbedModel string `json:"embed_model,omitempty"`
Version string `json:"version,omitempty"`
}
// ErrorResponse wraps errors in OpenAI-compatible format.
type ErrorResponse struct {
Error ErrorDetail `json:"error"`
}
// ErrorDetail is the error payload.
type ErrorDetail struct {
Message string `json:"message"`
Type string `json:"type"`
Code string `json:"code,omitempty"`
}
// --- Internal helpers ---
// ProxyError represents a proxy-level error.
type ProxyError struct {
Message string
Code int
}
func (e *ProxyError) Error() string {
return fmt.Sprintf("proxy error %d: %s", e.Code, e.Message)
}
// EmbedError represents an embedding failure.
type EmbedError struct {
Message string
}
func (e *EmbedError) Error() string {
return fmt.Sprintf("embed error: %s", e.Message)
}
// MessageContent extracts the string content from a Message.Content (which may be
// a string or []ContentPart). Returns the concatenated text.
func MessageContent(m Message) string {
switch v := m.Content.(type) {
case string:
return v
case []interface{}:
var sb string
for _, part := range v {
if p, ok := part.(map[string]interface{}); ok {
if t, ok := p["text"].(string); ok {
sb += t
}
}
}
return sb
}
return ""
}