-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathanthropic.go
More file actions
483 lines (444 loc) · 14.6 KB
/
Copy pathanthropic.go
File metadata and controls
483 lines (444 loc) · 14.6 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
// Copyright Built On Envoy
// SPDX-License-Identifier: Apache-2.0
// The full text of the Apache license is available in the LICENSE file at
// the root of the repo.
package llmproxy
import (
"bytes"
"encoding/json"
"fmt"
"sort"
"strings"
)
// anthropicRequest is the subset of an Anthropic Messages request body
// needed for routing and richer observability extraction.
type anthropicRequest struct {
Model string `json:"model"`
Stream bool `json:"stream"`
System json.RawMessage `json:"system"`
Messages []anthropicRequestMessage `json:"messages"`
}
// anthropicRequestMessage models one request message in the Anthropic payload.
type anthropicRequestMessage struct {
Role string `json:"role"`
Content any `json:"content"`
}
// anthropicUsage holds token-usage and cache-related fields from an Anthropic response.
type anthropicUsage struct {
InputTokens uint32 `json:"input_tokens"`
OutputTokens uint32 `json:"output_tokens"`
CacheCreationInputTokens uint32 `json:"cache_creation_input_tokens"`
CacheReadInputTokens uint32 `json:"cache_read_input_tokens"`
}
// anthropicResponse is the subset of a non-streaming Anthropic response
// needed for richer observability extraction.
type anthropicResponse struct {
Content []struct {
Type string `json:"type"`
Text string `json:"text,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Input json.RawMessage `json:"input,omitempty"`
} `json:"content"`
Usage anthropicUsage `json:"usage"`
}
// anthropicToolCall represents a tool use block in Anthropic responses.
type anthropicToolCall struct {
ID string `json:"id"`
Name string `json:"name"`
Input string `json:"input"`
}
// anthropicMessageStartData is the payload of a "message_start" SSE event.
type anthropicMessageStartData struct {
Message struct {
Usage anthropicUsage `json:"usage"`
} `json:"message"`
}
// anthropicMessageDeltaData is the payload of a "message_delta" SSE event.
type anthropicMessageDeltaData struct {
Usage struct {
OutputTokens uint32 `json:"output_tokens"`
} `json:"usage"`
}
// anthropicContentBlockStartData is the payload of a "content_block_start" SSE event.
type anthropicContentBlockStartData struct {
Index int `json:"index"`
ContentBlock struct {
Type string `json:"type"`
Text string `json:"text,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Input json.RawMessage `json:"input,omitempty"`
} `json:"content_block"`
}
// anthropicContentBlockDeltaData is the payload of a "content_block_delta" SSE event.
type anthropicContentBlockDeltaData struct {
Index int `json:"index"`
Delta struct {
Type string `json:"type"`
Text string `json:"text,omitempty"`
PartialJSON string `json:"partial_json,omitempty"`
} `json:"delta"`
}
// anthropicLLMRequest implements LLMRequest for the Anthropic Messages API.
type anthropicLLMRequest struct {
model string
stream bool
question string
system string
}
func (r *anthropicLLMRequest) GetModel() string { return r.model }
func (r *anthropicLLMRequest) IsStream() bool { return r.stream }
func (r *anthropicLLMRequest) GetQuestion() string {
return r.question
}
func (r *anthropicLLMRequest) GetSystem() string { return r.system }
// anthropicLLMResponse implements LLMResponse for the Anthropic Messages API.
type anthropicLLMResponse struct {
usage LLMUsage
answer string
reasoning string
toolCalls []anthropicToolCall
reasoningTokens uint32
cachedTokens uint32
inputTokenDetails any
outputTokenDetails any
}
func (r *anthropicLLMResponse) GetUsage() LLMUsage { return r.usage }
func (r *anthropicLLMResponse) GetAnswer() string { return r.answer }
func (r *anthropicLLMResponse) GetReasoning() string {
return r.reasoning
}
func (r *anthropicLLMResponse) GetToolCalls() any { return r.toolCalls }
func (r *anthropicLLMResponse) GetReasoningTokens() uint32 { return r.reasoningTokens }
func (r *anthropicLLMResponse) GetCachedTokens() uint32 { return r.cachedTokens }
func (r *anthropicLLMResponse) GetInputTokenDetails() any { return r.inputTokenDetails }
func (r *anthropicLLMResponse) GetOutputTokenDetails() any { return r.outputTokenDetails }
// anthropicLLMResponseChunk implements LLMResponseChunk for Anthropic streaming SSE.
type anthropicLLMResponseChunk struct {
usage LLMUsage
hasTextToken bool
cachedTokens uint32
inputTokenDetails any
}
func (c *anthropicLLMResponseChunk) GetUsage() LLMUsage { return c.usage }
func (c *anthropicLLMResponseChunk) GetAnswer() string { return "" }
func (c *anthropicLLMResponseChunk) GetReasoning() string {
return ""
}
func (c *anthropicLLMResponseChunk) GetToolCalls() any { return nil }
func (c *anthropicLLMResponseChunk) HasTextToken() bool {
return c.hasTextToken
}
// parseAnthropicRequest parses an Anthropic Messages request body and returns
// an LLMRequest with routing and observability fields.
func parseAnthropicRequest(body []byte) (LLMRequest, error) {
var req anthropicRequest
if err := json.Unmarshal(body, &req); err != nil {
return nil, err
}
return &anthropicLLMRequest{
model: req.Model,
stream: req.Stream,
question: extractAnthropicQuestion(req.Messages),
system: extractAnthropicSystem(req.System),
}, nil
}
// parseAnthropicResponse parses a non-streaming Anthropic response and extracts
// usage plus richer observability fields.
func parseAnthropicResponse(body []byte) (LLMResponse, error) {
var resp anthropicResponse
if err := json.Unmarshal(body, &resp); err != nil {
return nil, err
}
answer := ""
toolCalls := make([]anthropicToolCall, 0)
for _, item := range resp.Content {
if item.Type == "text" {
answer += item.Text
}
if item.Type == "tool_use" {
toolCalls = append(toolCalls, anthropicToolCall{
ID: item.ID,
Name: item.Name,
Input: string(item.Input),
})
}
}
return &anthropicLLMResponse{
usage: anthropicUsageToLLM(resp.Usage),
answer: answer,
toolCalls: toolCalls,
cachedTokens: resp.Usage.CacheReadInputTokens,
inputTokenDetails: buildAnthropicInputTokenDetails(resp.Usage),
}, nil
}
// parseAnthropicChunk parses a single Anthropic SSE event payload.
func parseAnthropicChunk(eventType string, data []byte) (anthropicLLMResponseChunk, error) {
switch eventType {
case "message_start":
var msg anthropicMessageStartData
if err := json.Unmarshal(data, &msg); err != nil {
return anthropicLLMResponseChunk{}, err
}
return anthropicLLMResponseChunk{
usage: anthropicUsageToLLM(msg.Message.Usage),
cachedTokens: msg.Message.Usage.CacheReadInputTokens,
inputTokenDetails: buildAnthropicInputTokenDetails(msg.Message.Usage),
}, nil
case "message_delta":
var delta anthropicMessageDeltaData
if err := json.Unmarshal(data, &delta); err != nil {
return anthropicLLMResponseChunk{}, err
}
return anthropicLLMResponseChunk{usage: LLMUsage{OutputTokens: delta.Usage.OutputTokens}}, nil
case "content_block_delta":
var delta anthropicContentBlockDeltaData
if err := json.Unmarshal(data, &delta); err != nil {
return anthropicLLMResponseChunk{}, err
}
return anthropicLLMResponseChunk{hasTextToken: delta.Delta.Type == "text_delta" && delta.Delta.Text != ""}, nil
}
return anthropicLLMResponseChunk{}, nil
}
// anthropicUsageToLLM converts an Anthropic usage payload to the common LLMUsage shape.
func anthropicUsageToLLM(u anthropicUsage) LLMUsage {
return LLMUsage{
InputTokens: u.InputTokens,
OutputTokens: u.OutputTokens,
TotalTokens: u.InputTokens + u.OutputTokens,
}
}
var (
anthropicSSEEventPrefix = []byte("event: ")
anthropicSSEDataPrefix = []byte("data: ")
)
// anthropicSSEParser accumulates usage, text, tool calls, and cache-related
// fields from an Anthropic streaming SSE response.
type anthropicSSEParser struct {
buf []byte
done bool
inputTokens uint32
outputTokens uint32
cachedTokens uint32
currentEvent string
textByIndex map[int]string
toolByIndex map[int]*anthropicToolCall
seenTextToken bool
inputTokenDetails any
}
// newAnthropicSSEParser creates a parser for incremental Anthropic SSE accumulation.
func newAnthropicSSEParser() *anthropicSSEParser {
return &anthropicSSEParser{
textByIndex: map[int]string{},
toolByIndex: map[int]*anthropicToolCall{},
}
}
// Feed appends a new response body chunk and parses any complete SSE events.
func (a *anthropicSSEParser) Feed(data []byte) error {
if a.done {
return nil
}
a.buf = append(a.buf, data...)
return a.parseEvents()
}
// parseEvents processes complete SSE lines accumulated in the internal buffer.
func (a *anthropicSSEParser) parseEvents() error {
for {
idx := bytes.IndexByte(a.buf, '\n')
if idx < 0 {
return nil
}
line := bytes.TrimSpace(a.buf[:idx])
a.buf = a.buf[idx+1:]
if bytes.HasPrefix(line, anthropicSSEEventPrefix) {
a.currentEvent = string(bytes.TrimPrefix(line, anthropicSSEEventPrefix))
continue
}
if bytes.HasPrefix(line, anthropicSSEDataPrefix) {
payload := bytes.TrimPrefix(line, anthropicSSEDataPrefix)
if err := a.processEvent(a.currentEvent, payload); err != nil {
return err
}
a.currentEvent = ""
}
}
}
// processEvent handles a single parsed Anthropic SSE event.
func (a *anthropicSSEParser) processEvent(eventType string, data []byte) error {
if eventType == "message_stop" {
a.done = true
return nil
}
if eventType == "content_block_start" {
var block anthropicContentBlockStartData
if err := json.Unmarshal(data, &block); err != nil {
return fmt.Errorf("llm-proxy: failed to parse Anthropic SSE event %q: %w", eventType, err)
}
if block.ContentBlock.Type == "text" {
a.textByIndex[block.Index] = block.ContentBlock.Text
if block.ContentBlock.Text != "" {
a.seenTextToken = true
}
return nil
}
if block.ContentBlock.Type == "tool_use" {
a.toolByIndex[block.Index] = &anthropicToolCall{
ID: block.ContentBlock.ID,
Name: block.ContentBlock.Name,
Input: string(block.ContentBlock.Input),
}
}
return nil
}
if eventType == "content_block_delta" {
var delta anthropicContentBlockDeltaData
if err := json.Unmarshal(data, &delta); err != nil {
return fmt.Errorf("llm-proxy: failed to parse Anthropic SSE event %q: %w", eventType, err)
}
switch delta.Delta.Type {
case "text_delta":
a.seenTextToken = true
a.textByIndex[delta.Index] += delta.Delta.Text
case "input_json_delta":
if tc, ok := a.toolByIndex[delta.Index]; ok {
if tc.Input == "" || tc.Input == "{}" {
tc.Input = delta.Delta.PartialJSON
} else {
tc.Input += delta.Delta.PartialJSON
}
}
}
return nil
}
chunk, err := parseAnthropicChunk(eventType, data)
if err != nil {
return fmt.Errorf("llm-proxy: failed to parse Anthropic SSE event %q: %w", eventType, err)
}
if u := chunk.GetUsage(); u != (LLMUsage{}) {
if u.InputTokens > 0 {
a.inputTokens = u.InputTokens
}
if u.OutputTokens > 0 {
a.outputTokens = u.OutputTokens
}
}
if chunk.cachedTokens > 0 {
a.cachedTokens = chunk.cachedTokens
}
if chunk.inputTokenDetails != nil {
a.inputTokenDetails = chunk.inputTokenDetails
}
return nil
}
// Finish finalises the stream and returns the accumulated response fields.
func (a *anthropicSSEParser) Finish() (LLMResponse, error) {
answer := ""
if len(a.textByIndex) > 0 {
indexes := make([]int, 0, len(a.textByIndex))
for idx := range a.textByIndex {
indexes = append(indexes, idx)
}
sort.Ints(indexes)
for _, idx := range indexes {
answer += a.textByIndex[idx]
}
}
toolCalls := make([]anthropicToolCall, 0, len(a.toolByIndex))
if len(a.toolByIndex) > 0 {
indexes := make([]int, 0, len(a.toolByIndex))
for idx := range a.toolByIndex {
indexes = append(indexes, idx)
}
sort.Ints(indexes)
for _, idx := range indexes {
toolCalls = append(toolCalls, *a.toolByIndex[idx])
}
}
return &anthropicLLMResponse{usage: LLMUsage{
InputTokens: a.inputTokens,
OutputTokens: a.outputTokens,
TotalTokens: a.inputTokens + a.outputTokens,
}, answer: answer, toolCalls: toolCalls, cachedTokens: a.cachedTokens, inputTokenDetails: a.inputTokenDetails}, nil
}
type anthropicFactory struct{}
func (f *anthropicFactory) ParseRequest(body []byte) (LLMRequest, error) {
return parseAnthropicRequest(body)
}
func (f *anthropicFactory) ParseResponse(body []byte) (LLMResponse, error) {
return parseAnthropicResponse(body)
}
func (f *anthropicFactory) NewSSEParser() SSEParser { return newAnthropicSSEParser() }
// SeenTextToken reports whether the stream has emitted a real text token yet.
func (a *anthropicSSEParser) SeenTextToken() bool { return a.seenTextToken }
// extractAnthropicQuestion returns the last user message content from the request.
func extractAnthropicQuestion(messages []anthropicRequestMessage) string {
for i := len(messages) - 1; i >= 0; i-- {
if messages[i].Role != "user" {
continue
}
return extractAnthropicMessageContent(messages[i].Content)
}
return ""
}
// extractAnthropicSystem returns the system prompt from either string or block form.
func extractAnthropicSystem(raw json.RawMessage) string {
if len(raw) == 0 {
return ""
}
var s string
if err := json.Unmarshal(raw, &s); err == nil {
return s
}
var blocks []struct {
Type string `json:"type"`
Text string `json:"text"`
}
if err := json.Unmarshal(raw, &blocks); err == nil {
out := ""
for i, b := range blocks {
if b.Type == "text" && b.Text != "" {
if i > 0 && out != "" {
out += "\n"
}
out += b.Text
}
}
return out
}
return ""
}
// buildAnthropicInputTokenDetails returns cache-related input token detail fields when present.
func buildAnthropicInputTokenDetails(u anthropicUsage) any {
if u.CacheCreationInputTokens == 0 && u.CacheReadInputTokens == 0 {
return nil
}
return map[string]uint32{
"cache_creation_input_tokens": u.CacheCreationInputTokens,
"cache_read_input_tokens": u.CacheReadInputTokens,
}
}
// extractAnthropicMessageContent extracts text from either string or block-form content.
func extractAnthropicMessageContent(content any) string {
if s, ok := content.(string); ok {
return s
}
raw, err := json.Marshal(content)
if err != nil {
return ""
}
var blocks []struct {
Type string `json:"type"`
Text string `json:"text"`
}
if err := json.Unmarshal(raw, &blocks); err != nil {
return ""
}
texts := make([]string, 0, len(blocks))
for _, b := range blocks {
if b.Type == "text" && b.Text != "" {
texts = append(texts, b.Text)
}
}
return strings.Join(texts, "\n")
}