-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclaude.go
547 lines (475 loc) · 15.5 KB
/
claude.go
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
package llm
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"strings"
"sync"
"github.com/liushuangls/go-anthropic/v2"
"golang.org/x/oauth2/google"
)
// ClaudeLLM implements the LLM interface for Anthropic's Claude
type ClaudeLLM struct {
client *anthropic.Client
}
type BetaVersion string
const (
BetaTools2024_04_04 BetaVersion = "tools-2024-04-04"
BetaTools2024_05_16 BetaVersion = "tools-2024-05-16"
BetaPromptCaching2024_07_31 BetaVersion = "prompt-caching-2024-07-31"
BetaMessageBatches2024_09_24 BetaVersion = "message-batches-2024-09-24"
BetaTokenCounting2024_11_01 BetaVersion = "token-counting-2024-11-01"
BetaMaxTokens35_2024_07_15 BetaVersion = "max-tokens-3-5-sonnet-2024-07-15"
)
type ClientOption func(*ClaudeLLM)
// NewAnthropicLLM creates a new Claude LLM client (via Anthropic API)
func NewAnthropicLLM(apiKey string) *ClaudeLLM {
// activate all beta versions by default
opts := []BetaVersion{BetaTools2024_04_04, BetaTools2024_05_16, BetaPromptCaching2024_07_31, BetaMessageBatches2024_09_24, BetaTokenCounting2024_11_01, BetaMaxTokens35_2024_07_15}
anthropicOpts := make([]anthropic.ClientOption, len(opts))
for i, opt := range opts {
anthropicOpts[i] = anthropic.WithBetaVersion(anthropic.BetaVersion(opt))
}
client := anthropic.NewClient(apiKey, anthropicOpts...)
return &ClaudeLLM{client: client}
}
// NewVertexLLM creates a new Claude LLM client (via Vertex AI custom integration)
func NewVertexLLM(credBytes []byte, projectID string, location string) *ClaudeLLM {
// activate all beta versions by default
opts := []BetaVersion{BetaTools2024_04_04, BetaTools2024_05_16, BetaPromptCaching2024_07_31, BetaMessageBatches2024_09_24, BetaTokenCounting2024_11_01, BetaMaxTokens35_2024_07_15}
ts, err := google.JWTAccessTokenSourceWithScope(
credBytes,
"https://www.googleapis.com/auth/cloud-platform",
"https://www.googleapis.com/auth/cloud-platform.read-only",
)
if err != nil {
fmt.Println("Error creating token source:", err)
return nil
}
token, err := ts.Token()
if err != nil {
fmt.Println("Error getting token:", err)
return nil
}
fmt.Println("Using Vertex AI with token prefix:", token.AccessToken[:10], "...")
// activate all beta versions by default
opts = append(opts, BetaTools2024_04_04, BetaTools2024_05_16, BetaPromptCaching2024_07_31, BetaMessageBatches2024_09_24, BetaTokenCounting2024_11_01, BetaMaxTokens35_2024_07_15)
betaOpts := make([]anthropic.ClientOption, len(opts))
for i, opt := range opts {
betaOpts[i] = anthropic.WithBetaVersion(anthropic.BetaVersion(opt))
}
anthropicOpts := append(betaOpts, anthropic.WithVertexAI(projectID, location))
client := anthropic.NewClient(token.AccessToken, anthropicOpts...)
return &ClaudeLLM{client: client}
}
// convertToClaudeMessages converts our generic InputMessage type to Anthropic's messages
func convertToClaudeMessages(messages []InputMessage) []anthropic.Message {
claudeMessages := make([]anthropic.Message, 0, len(messages))
for _, msg := range messages {
var role anthropic.ChatRole
switch msg.Role {
case RoleUser:
role = anthropic.RoleUser
case RoleAssistant:
role = anthropic.RoleAssistant
case RoleTool:
// For Anthropic, pass tool messages as role=User with a tool_result content block
role = anthropic.RoleUser
default:
// skip unknown roles
continue
}
claudeMessage := anthropic.Message{
Role: anthropic.ChatRole(role),
}
claudeMessage.Content = convertToClaudeMessageContent(msg.MultiContent)
if msg.Role == RoleTool && len(msg.ToolResults) > 0 {
toolResult := convertToClaudeMessageContentToolResult(msg.ToolResults[0])
claudeMessage.Content = []anthropic.MessageContent{
{
Type: anthropic.MessagesContentTypeToolResult,
MessageContentToolResult: &toolResult,
},
}
}
// If it's an assistant message calling a tool
if msg.Role == RoleAssistant && len(msg.ToolCalls) > 0 {
toolCall := msg.ToolCalls[0]
claudeMessage.Content = []anthropic.MessageContent{
{
Type: anthropic.MessagesContentTypeToolUse,
MessageContentToolUse: anthropic.NewMessageContentToolUse(
toolCall.ID,
toolCall.Function.Name,
json.RawMessage(toolCall.Function.Arguments),
),
},
}
}
claudeMessages = append(claudeMessages, claudeMessage)
}
return claudeMessages
}
// convertToClaudeMessageContent transforms our list of ContentPart into anthropic.MessageContent slices
func convertToClaudeMessageContent(content []ContentPart) []anthropic.MessageContent {
multiContent := make([]anthropic.MessageContent, 0, len(content))
for _, part := range content {
switch part.Type {
case ContentTypeText:
multiContent = append(multiContent, anthropic.NewTextMessageContent(part.Text))
case ContentTypeImage:
multiContent = append(multiContent, anthropic.NewImageMessageContent(
anthropic.NewMessageContentSource(
anthropic.MessagesContentSourceTypeBase64,
part.MediaType,
part.Data,
),
))
}
}
return multiContent
}
func convertToClaudeMessageContentToolResult(toolResult ToolResult) anthropic.MessageContentToolResult {
return anthropic.MessageContentToolResult{
ToolUseID: &toolResult.ToolCallID,
Content: []anthropic.MessageContent{anthropic.NewTextMessageContent(toolResult.Result)},
IsError: &toolResult.IsError,
}
}
// convertFromClaudeMessage converts an anthropic.MessagesResponse to our OutputMessage
func convertFromClaudeMessage(msg anthropic.MessagesResponse) OutputMessage {
var content string
var toolCalls []anthropic.MessageContentToolUse
// Gather text parts and possible tool calls
if len(msg.Content) > 0 {
var textParts []string
for _, part := range msg.Content {
switch part.Type {
case anthropic.MessagesContentTypeText:
if part.Text != nil {
textParts = append(textParts, *part.Text)
}
case anthropic.MessagesContentTypeToolUse:
if part.MessageContentToolUse != nil {
toolCalls = append(toolCalls, *part.MessageContentToolUse)
}
}
}
content = strings.Join(textParts, "")
}
return OutputMessage{
Role: Role(msg.Role),
Content: content,
ToolCalls: convertFromClaudeToolCalls(toolCalls),
}
}
// convertFromClaudeToolCalls converts anthropic tool calls to our ToolCall
func convertFromClaudeToolCalls(toolCalls []anthropic.MessageContentToolUse) []ToolCall {
if len(toolCalls) == 0 {
return nil
}
calls := make([]ToolCall, len(toolCalls))
for i, call := range toolCalls {
calls[i] = ToolCall{
ID: call.ID,
Type: "function",
Function: ToolCallFunction{
Name: call.Name,
Arguments: string(call.Input),
},
}
}
return calls
}
// convertToClaudeTools translates our Tool definitions into anthropic.ToolDefinition
func convertToClaudeTools(tools []Tool) []anthropic.ToolDefinition {
if len(tools) == 0 {
return nil
}
claudeTools := make([]anthropic.ToolDefinition, len(tools))
for i, tool := range tools {
claudeTools[i] = anthropic.ToolDefinition{
Name: tool.Function.Name,
Description: tool.Function.Description,
InputSchema: tool.Function.Parameters,
}
}
return claudeTools
}
// CreateChatCompletion implements the non-streaming LLM interface for Claude
func (c *ClaudeLLM) CreateChatCompletion(ctx context.Context, req ChatCompletionRequest) (ChatCompletionResponse, error) {
if !c.isSupported(req.Model) {
return ChatCompletionResponse{}, fmt.Errorf("model %s is not available", req.Model)
}
model := anthropic.Model(req.Model)
tools := convertToClaudeTools(req.Tools)
var toolChoice *anthropic.ToolChoice
if len(tools) > 0 {
toolChoice = &anthropic.ToolChoice{Type: "auto"}
}
topP := float32(1)
if req.TopP != nil {
topP = *req.TopP
}
var systemPrompt string
if req.SystemPrompt != nil {
systemPrompt = *req.SystemPrompt
}
claudeReq := anthropic.MessagesRequest{
Model: model,
Messages: convertToClaudeMessages(req.Messages),
System: systemPrompt,
Temperature: &req.Temperature,
TopP: &topP,
Tools: tools,
Stream: false,
MaxTokens: req.MaxTokens,
ToolChoice: toolChoice,
}
resp, err := c.client.CreateMessages(ctx, claudeReq)
if err != nil {
return ChatCompletionResponse{}, err
}
choices := make([]Choice, 1)
msg := convertFromClaudeMessage(resp)
choices[0] = Choice{
Index: 0,
Message: msg,
FinishReason: convertFromClaudeFinishReason(resp.StopReason),
}
return ChatCompletionResponse{
ID: resp.ID,
Choices: choices,
Usage: Usage{
PromptTokens: resp.Usage.InputTokens,
CompletionTokens: resp.Usage.OutputTokens,
TotalTokens: resp.Usage.InputTokens + resp.Usage.OutputTokens,
},
}, nil
}
func convertFromClaudeFinishReason(reason anthropic.MessagesStopReason) FinishReason {
switch reason {
case anthropic.MessagesStopReasonEndTurn:
return FinishReasonStop
case anthropic.MessagesStopReasonToolUse:
return FinishReasonToolCalls
case anthropic.MessagesStopReasonMaxTokens:
return FinishReasonMaxTokens
case anthropic.MessagesStopReasonStopSequence:
return FinishReasonStop
}
return FinishReason(reason)
}
// isSupported checks if the model is recognized as a Claude-friendly model
func (c *ClaudeLLM) isSupported(model Model) bool {
switch model {
case ModelClaude2Dot0,
ModelClaude2Dot1,
ModelClaude3Opus20240229,
ModelClaude3Sonnet20240229,
ModelClaude3Dot5Sonnet20240620,
ModelClaude3Dot5Sonnet20241022,
ModelClaude3Dot5SonnetLatest,
ModelClaude3Haiku20240307,
ModelClaude3Dot5HaikuLatest,
ModelClaude3Dot5Haiku20241022:
return true
default:
return false
}
}
// claudeStreamWrapper wraps Anthropic's streaming approach to implement ChatCompletionStream
type claudeStreamWrapper struct {
eventsChan chan ChatCompletionResponse
errChan chan error
cancelFunc context.CancelFunc
done bool
mu sync.Mutex
}
// Recv returns the next available partial or final ChatCompletionResponse.
// If streaming is complete or an error occurs, returns an error (possibly io.EOF).
func (w *claudeStreamWrapper) Recv() (ChatCompletionResponse, error) {
resp, ok := <-w.eventsChan
if !ok {
// channel closed; check if there was an error
select {
case err := <-w.errChan:
if err != nil {
return ChatCompletionResponse{}, err
}
return ChatCompletionResponse{}, io.EOF
default:
return ChatCompletionResponse{}, io.EOF
}
}
return resp, nil
}
// Close stops the stream and cleans up any resources
func (w *claudeStreamWrapper) Close() error {
w.mu.Lock()
defer w.mu.Unlock()
if w.done {
return nil
}
w.done = true
// Cancel the context if possible
if w.cancelFunc != nil {
w.cancelFunc()
}
// Drain channels
close(w.eventsChan)
close(w.errChan)
return nil
}
// CreateChatCompletionStream implements streaming for Claude with callbacks
func (c *ClaudeLLM) CreateChatCompletionStream(ctx context.Context, req ChatCompletionRequest) (ChatCompletionStream, error) {
if !c.isSupported(req.Model) {
return nil, fmt.Errorf("model %s is not available", req.Model)
}
model := anthropic.Model(req.Model)
// We'll create a child context to cancel if needed
ctxStream, cancel := context.WithCancel(ctx)
// We'll push partial updates to eventsChan, and push errors to errChan
eventsChan := make(chan ChatCompletionResponse, 10)
errChan := make(chan error, 1)
// We'll track partial text and partial tool calls
// We'll accumulate them as content comes in
var partialTextBuilder strings.Builder
var toolCalls []ToolCall
// var stopReason anthropic.MessagesStopReason
wrapper := &claudeStreamWrapper{
eventsChan: eventsChan,
errChan: errChan,
cancelFunc: cancel,
}
topP := float32(1)
if req.TopP != nil {
topP = *req.TopP
}
var systemPrompt string
if req.SystemPrompt != nil {
systemPrompt = *req.SystemPrompt
}
// Build request for streaming
streamReq := anthropic.MessagesStreamRequest{
MessagesRequest: anthropic.MessagesRequest{
Model: model,
Messages: convertToClaudeMessages(req.Messages),
System: systemPrompt,
Temperature: &req.Temperature,
TopP: &topP,
Tools: convertToClaudeTools(req.Tools),
Stream: true,
MaxTokens: req.MaxTokens,
},
OnError: func(e anthropic.ErrorResponse) {
select {
case errChan <- e.Error:
default:
}
},
OnPing: func(data anthropic.MessagesEventPingData) {
// Normally we ignore Ping events or log them
},
OnMessageStart: func(d anthropic.MessagesEventMessageStartData) {
// This indicates a new "assistant" message is starting
partialTextBuilder.Reset()
toolCalls = nil
},
OnContentBlockStart: func(d anthropic.MessagesEventContentBlockStartData) {
// We'll do nothing special here. We wait for deltas
},
OnContentBlockDelta: func(d anthropic.MessagesEventContentBlockDeltaData) {
// We only handle partial text or partial JSON for tool calls
if d.Delta.Type == anthropic.MessagesContentTypeTextDelta && d.Delta.Text != nil {
partialTextBuilder.WriteString(*d.Delta.Text)
// Send partial response
eventsChan <- ChatCompletionResponse{
Choices: []Choice{{
Index: 0,
Message: OutputMessage{
Role: RoleAssistant,
Content: *d.Delta.Text,
// tool calls remain the same
ToolCalls: toolCalls,
},
FinishReason: FinishReasonNull,
}},
}
} else if d.Delta.Type == anthropic.MessagesContentTypeInputJsonDelta && d.Delta.PartialJson != nil {
// For a tool call, accumulate partial JSON
// We'll finalize it at content_block_stop
// If we wanted partial function calls, we'd do something here
}
},
OnContentBlockStop: func(d anthropic.MessagesEventContentBlockStopData, block anthropic.MessageContent) {
// If the content block is a tool call, finalize its partial JSON
if block.Type == anthropic.MessagesContentTypeToolUse &&
block.MessageContentToolUse != nil {
tc := ToolCall{
ID: block.MessageContentToolUse.ID,
Type: "function",
Function: ToolCallFunction{
Name: block.MessageContentToolUse.Name,
Arguments: string(block.MessageContentToolUse.Input),
},
}
toolCalls = append(toolCalls, tc)
// Now send partial update with the new tool call
eventsChan <- ChatCompletionResponse{
Choices: []Choice{{
Index: 0,
Message: OutputMessage{
Role: RoleAssistant,
Content: partialTextBuilder.String(),
ToolCalls: toolCalls,
},
FinishReason: FinishReasonNull,
}},
}
}
},
OnMessageDelta: func(d anthropic.MessagesEventMessageDeltaData) {
// This might indicate a new stop reason or usage changes
// if d.Delta.StopReason != "" {
// stopReason = d.Delta.StopReason
// }
// We ignore usage here, or we could track usage tokens
},
OnMessageStop: func(d anthropic.MessagesEventMessageStopData) {
// This indicates the final chunk of the stream for the message
// We'll push a final chunk with the full content
// finalMsg := ChatCompletionResponse{
// Choices: []Choice{{
// Index: 0,
// Message: OutputMessage{
// Role: RoleAssistant,
// Content: partialTextBuilder.String(),
// ToolCalls: toolCalls,
// },
// FinishReason: convertFromClaudeFinishReason(stopReason),
// }},
// }
// eventsChan <- finalMsg
// streaming is complete, so close the channel
wrapper.Close()
},
}
// Run the streaming request in a goroutine
go func() {
defer func() {
// Ensure we close everything if an unexpected error occurs
_ = wrapper.Close()
}()
_, err := c.client.CreateMessagesStream(ctxStream, streamReq)
if err != nil && !errors.Is(err, io.EOF) {
select {
case errChan <- err:
default:
}
}
}()
return wrapper, nil
}