-
Notifications
You must be signed in to change notification settings - Fork 952
Expand file tree
/
Copy pathanthropic.go
More file actions
366 lines (312 loc) · 10.5 KB
/
Copy pathanthropic.go
File metadata and controls
366 lines (312 loc) · 10.5 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
package providers
import (
"encoding/json"
"fmt"
"time"
"github.com/maximhq/bifrost/interfaces"
"github.com/valyala/fasthttp"
"github.com/maximhq/maxim-go"
)
type AnthropicToolChoice struct {
Type interfaces.ToolChoiceType `json:"type"`
Name *string `json:"name"`
DisableParallelToolUse *bool `json:"disable_parallel_tool_use"`
}
type AnthropicTextResponse struct {
ID string `json:"id"`
Type string `json:"type"`
Completion string `json:"completion"`
Model string `json:"model"`
Usage struct {
InputTokens int `json:"input_tokens"`
OutputTokens int `json:"output_tokens"`
} `json:"usage"`
}
type AnthropicChatResponse struct {
ID string `json:"id"`
Type string `json:"type"`
Role string `json:"role"`
Content []struct {
Type string `json:"type"`
Text string `json:"text,omitempty"`
Thinking string `json:"thinking,omitempty"`
ID string `json:"id"`
Name string `json:"name"`
Input map[string]interface{} `json:"input"`
} `json:"content"`
Model string `json:"model"`
StopReason string `json:"stop_reason,omitempty"`
StopSequence *string `json:"stop_sequence,omitempty"`
Usage struct {
InputTokens int `json:"input_tokens"`
OutputTokens int `json:"output_tokens"`
} `json:"usage"`
}
// AnthropicProvider implements the Provider interface for Anthropic's Claude API
type AnthropicProvider struct {
client *fasthttp.Client
}
// NewAnthropicProvider creates a new AnthropicProvider instance
func NewAnthropicProvider(config *interfaces.ProviderConfig) *AnthropicProvider {
return &AnthropicProvider{
client: &fasthttp.Client{
ReadTimeout: time.Second * time.Duration(config.NetworkConfig.DefaultRequestTimeoutInSeconds),
WriteTimeout: time.Second * time.Duration(config.NetworkConfig.DefaultRequestTimeoutInSeconds),
MaxConnsPerHost: config.ConcurrencyAndBufferSize.BufferSize,
},
}
}
func (provider *AnthropicProvider) GetProviderKey() interfaces.SupportedModelProvider {
return interfaces.Anthropic
}
func (provider *AnthropicProvider) PrepareTextCompletionParams(params map[string]interface{}) map[string]interface{} {
// Check if there is a key entry for max_tokens
if maxTokens, exists := params["max_tokens"]; exists {
// Check if max_tokens_to_sample is already present
if _, exists := params["max_tokens_to_sample"]; !exists {
// If max_tokens_to_sample is not present, rename max_tokens to max_tokens_to_sample
params["max_tokens_to_sample"] = maxTokens
}
delete(params, "max_tokens")
}
return params
}
func (provider *AnthropicProvider) PrepareToolChoices(params map[string]interface{}) map[string]interface{} {
toolChoice, exists := params["tool_choice"]
if !exists {
return params
}
switch tc := toolChoice.(type) {
case interfaces.ToolChoice:
anthropicToolChoice := AnthropicToolChoice{
Type: tc.Type,
Name: &tc.Function.Name,
}
parallelToolCalls, exists := params["parallel_tool_calls"]
if !exists {
return params
}
switch parallelTC := parallelToolCalls.(type) {
case bool:
disableParallel := !parallelTC
anthropicToolChoice.DisableParallelToolUse = &disableParallel
delete(params, "parallel_tool_calls")
}
params["tool_choice"] = anthropicToolChoice
}
return params
}
// TextCompletion implements text completion using Anthropic's API
func (provider *AnthropicProvider) TextCompletion(model, key, text string, params *interfaces.ModelParameters) (*interfaces.BifrostResponse, error) {
preparedParams := provider.PrepareTextCompletionParams(PrepareParams(params))
// Merge additional parameters
requestBody := MergeConfig(map[string]interface{}{
"model": model,
"prompt": fmt.Sprintf("\n\nHuman: %s\n\nAssistant:", text),
}, preparedParams)
// Marshal the request body
jsonData, err := json.Marshal(requestBody)
if err != nil {
return nil, fmt.Errorf("error marshaling request: %v", err)
}
// Create the request with the JSON body
req := fasthttp.AcquireRequest()
resp := fasthttp.AcquireResponse()
defer fasthttp.ReleaseRequest(req)
defer fasthttp.ReleaseResponse(resp)
req.SetRequestURI("https://api.anthropic.com/v1/complete")
req.Header.SetMethod("POST")
req.Header.SetContentType("application/json")
req.Header.Set("x-api-key", key)
req.Header.Set("anthropic-version", "2023-06-01")
req.SetBody(jsonData)
// Send the request
if err := provider.client.Do(req, resp); err != nil {
return nil, fmt.Errorf("error sending request: %v", err)
}
// Handle error response
if resp.StatusCode() != fasthttp.StatusOK {
return nil, fmt.Errorf("anthropic error: %s", resp.Body())
}
// Read the response body
body := resp.Body()
// Parse the response
var response AnthropicTextResponse
if err := json.Unmarshal(body, &response); err != nil {
return nil, fmt.Errorf("error parsing response: %v", err)
}
// Parse raw response
var rawResponse interface{}
if err := json.Unmarshal(body, &rawResponse); err != nil {
return nil, fmt.Errorf("error parsing raw response: %v", err)
}
// Create the completion result
completionResult := &interfaces.BifrostResponse{
ID: response.ID,
Choices: []interfaces.BifrostResponseChoice{
{
Index: 0,
Message: interfaces.BifrostResponseChoiceMessage{
Role: interfaces.RoleAssistant,
Content: &response.Completion,
},
},
},
Usage: interfaces.LLMUsage{
PromptTokens: response.Usage.InputTokens,
CompletionTokens: response.Usage.OutputTokens,
TotalTokens: response.Usage.InputTokens + response.Usage.OutputTokens,
},
Model: response.Model,
ExtraFields: interfaces.BifrostResponseExtraFields{
Provider: interfaces.Anthropic,
RawResponse: rawResponse,
},
}
return completionResult, nil
}
// ChatCompletion implements chat completion using Anthropic's API
func (provider *AnthropicProvider) ChatCompletion(model, key string, messages []interfaces.Message, params *interfaces.ModelParameters) (*interfaces.BifrostResponse, error) {
// Format messages for Anthropic API
var formattedMessages []map[string]interface{}
for _, msg := range messages {
if msg.ImageContent != nil {
var content []map[string]interface{}
imageContent := map[string]interface{}{
"type": "image",
"source": map[string]interface{}{
"type": msg.ImageContent.Type,
},
}
// Handle different image source types
if *msg.ImageContent.Type == "url" {
imageContent["source"].(map[string]interface{})["url"] = msg.ImageContent.URL
} else {
imageContent["source"].(map[string]interface{})["media_type"] = msg.ImageContent.MediaType
imageContent["source"].(map[string]interface{})["data"] = msg.ImageContent.URL
}
content = append(content, imageContent)
// Add text content if present
if msg.Content != nil {
content = append(content, map[string]interface{}{
"type": "text",
"text": msg.Content,
})
}
formattedMessages = append(formattedMessages, map[string]interface{}{
"role": msg.Role,
"content": content,
})
} else {
formattedMessages = append(formattedMessages, map[string]interface{}{
"role": msg.Role,
"content": msg.Content,
})
}
}
preparedParams := PrepareParams(params)
// Transform tools if present
if params != nil && params.Tools != nil && len(*params.Tools) > 0 {
var tools []map[string]interface{}
for _, tool := range *params.Tools {
tools = append(tools, map[string]interface{}{
"name": tool.Function.Name,
"description": tool.Function.Description,
"input_schema": tool.Function.Parameters,
})
}
preparedParams["tools"] = tools
}
// Merge additional parameters
requestBody := MergeConfig(map[string]interface{}{
"model": model,
"messages": formattedMessages,
}, preparedParams)
jsonBody, err := json.Marshal(requestBody)
if err != nil {
return nil, fmt.Errorf("error marshaling request: %v", err)
}
// Create request
req := fasthttp.AcquireRequest()
resp := fasthttp.AcquireResponse()
defer fasthttp.ReleaseRequest(req)
defer fasthttp.ReleaseResponse(resp)
req.SetRequestURI("https://api.anthropic.com/v1/messages")
req.Header.SetMethod("POST")
req.Header.SetContentType("application/json")
req.Header.Set("x-api-key", key)
req.Header.Set("anthropic-version", "2023-06-01")
req.SetBody(jsonBody)
// Make request
if err := provider.client.Do(req, resp); err != nil {
return nil, fmt.Errorf("error making request: %v", err)
}
// Handle error response
if resp.StatusCode() != fasthttp.StatusOK {
return nil, fmt.Errorf("anthropic error: %s", resp.Body())
}
// Decode structured response
var response AnthropicChatResponse
if err := json.Unmarshal(resp.Body(), &response); err != nil {
return nil, fmt.Errorf("error decoding structured response: %v", err)
}
// Decode raw response
var rawResponse interface{}
if err := json.Unmarshal(resp.Body(), &rawResponse); err != nil {
return nil, fmt.Errorf("error decoding raw response: %v", err)
}
// Process the response into our BifrostResponse format
var choices []interfaces.BifrostResponseChoice
// Process content and tool calls
for i, c := range response.Content {
var content string
var toolCalls []interfaces.ToolCall
switch c.Type {
case "thinking":
content = c.Thinking
case "text":
content = c.Text
case "tool_use":
function := interfaces.FunctionCall{
Name: &c.Name,
}
args, err := json.Marshal(c.Input)
if err != nil {
function.Arguments = fmt.Sprintf("%v", c.Input)
} else {
function.Arguments = string(args)
}
toolCalls = append(toolCalls, interfaces.ToolCall{
Type: maxim.StrPtr("function"),
ID: &c.ID,
Function: function,
})
}
choices = append(choices, interfaces.BifrostResponseChoice{
Index: i,
Message: interfaces.BifrostResponseChoiceMessage{
Role: interfaces.RoleAssistant,
Content: &content,
ToolCalls: &toolCalls,
},
FinishReason: &response.StopReason,
StopString: response.StopSequence,
})
}
// Create the completion result
result := &interfaces.BifrostResponse{
ID: response.ID,
Choices: choices,
Usage: interfaces.LLMUsage{
PromptTokens: response.Usage.InputTokens,
CompletionTokens: response.Usage.OutputTokens,
TotalTokens: response.Usage.InputTokens + response.Usage.OutputTokens,
},
Model: response.Model,
ExtraFields: interfaces.BifrostResponseExtraFields{
Provider: interfaces.Anthropic,
RawResponse: rawResponse,
},
}
return result, nil
}