Skip to content

Commit 6b00e06

Browse files
github-actions[bot]akshaydeo
authored andcommitted
independent test cases
1 parent 2dcaaae commit 6b00e06

File tree

9 files changed

+2902
-0
lines changed

9 files changed

+2902
-0
lines changed

core/providers/anthropic_test.go

Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
package providers
2+
3+
import (
4+
"context"
5+
"os"
6+
"testing"
7+
8+
"github.com/maximhq/bifrost/core/schemas"
9+
)
10+
11+
func TestAnthropicChatCompletion(t *testing.T) {
12+
apiKey := os.Getenv("ANTHROPIC_API_KEY")
13+
if apiKey == "" {
14+
t.Skip("ANTHROPIC_API_KEY not set")
15+
}
16+
17+
provider := NewAnthropicProvider(&schemas.ProviderConfig{
18+
NetworkConfig: schemas.NetworkConfig{
19+
BaseURL: "https://api.anthropic.com",
20+
DefaultRequestTimeoutInSeconds: 30,
21+
},
22+
ConcurrencyAndBufferSize: schemas.ConcurrencyAndBufferSize{
23+
Concurrency: 10,
24+
},
25+
}, newTestLogger())
26+
27+
ctx := context.Background()
28+
key := schemas.Key{Value: apiKey}
29+
30+
request := &schemas.BifrostChatRequest{
31+
Provider: schemas.Anthropic,
32+
Model: "claude-3-haiku-20240307",
33+
Input: []schemas.ChatMessage{
34+
{
35+
Role: "user",
36+
Content: &schemas.ChatMessageContent{ContentStr: stringPtr("Say hello in one word")},
37+
},
38+
},
39+
Params: &schemas.ChatParameters{
40+
Temperature: float64Ptr(0.7),
41+
MaxCompletionTokens: intPtr(10),
42+
},
43+
}
44+
45+
resp, err := provider.ChatCompletion(ctx, key, request)
46+
if err != nil {
47+
t.Fatalf("ChatCompletion failed: %v", err)
48+
}
49+
50+
if resp == nil {
51+
t.Fatal("Expected non-nil response")
52+
}
53+
if len(resp.Choices) == 0 {
54+
t.Fatal("Expected at least one choice")
55+
}
56+
if resp.Choices[0].Message.Content == nil {
57+
t.Fatal("Expected message content")
58+
}
59+
t.Logf("Response: %v", resp.Choices[0].Message.Content)
60+
}
61+
62+
func TestAnthropicChatCompletionWithTools(t *testing.T) {
63+
apiKey := os.Getenv("ANTHROPIC_API_KEY")
64+
if apiKey == "" {
65+
t.Skip("ANTHROPIC_API_KEY not set")
66+
}
67+
68+
provider := NewAnthropicProvider(&schemas.ProviderConfig{
69+
NetworkConfig: schemas.NetworkConfig{
70+
BaseURL: "https://api.anthropic.com",
71+
DefaultRequestTimeoutInSeconds: 30,
72+
},
73+
ConcurrencyAndBufferSize: schemas.ConcurrencyAndBufferSize{
74+
Concurrency: 10,
75+
},
76+
}, newTestLogger())
77+
78+
ctx := context.Background()
79+
key := schemas.Key{Value: apiKey}
80+
81+
props := map[string]interface{}{
82+
"location": map[string]interface{}{
83+
"type": "string",
84+
"description": "The city name",
85+
},
86+
}
87+
88+
request := &schemas.BifrostChatRequest{
89+
Provider: schemas.Anthropic,
90+
Model: "claude-3-haiku-20240307",
91+
Input: []schemas.ChatMessage{
92+
{
93+
Role: "user",
94+
Content: &schemas.ChatMessageContent{ContentStr: stringPtr("What's the weather in San Francisco?")},
95+
},
96+
},
97+
Params: &schemas.ChatParameters{
98+
Temperature: float64Ptr(0.7),
99+
MaxCompletionTokens: intPtr(100),
100+
Tools: []schemas.ChatTool{
101+
{
102+
Type: schemas.ChatToolTypeFunction,
103+
Function: &schemas.ChatToolFunction{
104+
Name: "get_weather",
105+
Description: stringPtr("Get the current weather"),
106+
Parameters: &schemas.ToolFunctionParameters{
107+
Type: "object",
108+
Properties: &props,
109+
Required: []string{"location"},
110+
},
111+
},
112+
},
113+
},
114+
},
115+
}
116+
117+
resp, err := provider.ChatCompletion(ctx, key, request)
118+
if err != nil {
119+
t.Fatalf("ChatCompletion with tools failed: %v", err)
120+
}
121+
122+
if resp == nil {
123+
t.Fatal("Expected non-nil response")
124+
}
125+
if len(resp.Choices) == 0 {
126+
t.Fatal("Expected at least one choice")
127+
}
128+
t.Logf("Tool calls: %d", len(resp.Choices[0].Message.ToolCalls))
129+
}
130+
131+
func TestAnthropicChatCompletionStream(t *testing.T) {
132+
apiKey := os.Getenv("ANTHROPIC_API_KEY")
133+
if apiKey == "" {
134+
t.Skip("ANTHROPIC_API_KEY not set")
135+
}
136+
137+
provider := NewAnthropicProvider(&schemas.ProviderConfig{
138+
NetworkConfig: schemas.NetworkConfig{
139+
BaseURL: "https://api.anthropic.com",
140+
DefaultRequestTimeoutInSeconds: 30,
141+
},
142+
ConcurrencyAndBufferSize: schemas.ConcurrencyAndBufferSize{
143+
Concurrency: 10,
144+
},
145+
}, newTestLogger())
146+
147+
ctx := context.Background()
148+
key := schemas.Key{Value: apiKey}
149+
150+
request := &schemas.BifrostChatRequest{
151+
Provider: schemas.Anthropic,
152+
Model: "claude-3-haiku-20240307",
153+
Input: []schemas.ChatMessage{
154+
{
155+
Role: "user",
156+
Content: &schemas.ChatMessageContent{ContentStr: stringPtr("Count from 1 to 3")},
157+
},
158+
},
159+
Params: &schemas.ChatParameters{
160+
Temperature: float64Ptr(0.7),
161+
},
162+
}
163+
164+
streamChan, err := provider.ChatCompletionStream(ctx, mockPostHookRunner, key, request)
165+
if err != nil {
166+
t.Fatalf("ChatCompletionStream failed: %v", err)
167+
}
168+
169+
count := 0
170+
for chunk := range streamChan {
171+
if chunk == nil {
172+
continue
173+
}
174+
if chunk.BifrostError != nil {
175+
t.Fatalf("Stream error: %v", chunk.BifrostError)
176+
}
177+
count++
178+
}
179+
180+
if count == 0 {
181+
t.Fatal("Expected at least one chunk")
182+
}
183+
t.Logf("Received %d chunks", count)
184+
}
185+
186+
func TestAnthropicResponses(t *testing.T) {
187+
apiKey := os.Getenv("ANTHROPIC_API_KEY")
188+
if apiKey == "" {
189+
t.Skip("ANTHROPIC_API_KEY not set")
190+
}
191+
192+
provider := NewAnthropicProvider(&schemas.ProviderConfig{
193+
NetworkConfig: schemas.NetworkConfig{
194+
BaseURL: "https://api.anthropic.com",
195+
DefaultRequestTimeoutInSeconds: 30,
196+
},
197+
ConcurrencyAndBufferSize: schemas.ConcurrencyAndBufferSize{
198+
Concurrency: 10,
199+
},
200+
}, newTestLogger())
201+
202+
ctx := context.Background()
203+
key := schemas.Key{Value: apiKey}
204+
205+
userRole := schemas.ResponsesInputMessageRoleUser
206+
request := &schemas.BifrostResponsesRequest{
207+
Provider: schemas.Anthropic,
208+
Model: "claude-3-5-sonnet-20241022",
209+
Input: []schemas.ResponsesMessage{
210+
{
211+
Role: &userRole,
212+
Content: &schemas.ResponsesMessageContent{ContentStr: stringPtr("What is 2+2?")},
213+
},
214+
},
215+
Params: &schemas.ResponsesParameters{
216+
MaxOutputTokens: intPtr(100),
217+
},
218+
}
219+
220+
resp, err := provider.Responses(ctx, key, request)
221+
if err != nil {
222+
t.Fatalf("Responses failed: %v", err)
223+
}
224+
225+
if resp == nil {
226+
t.Fatal("Expected non-nil response")
227+
}
228+
if len(resp.Output) == 0 {
229+
t.Fatal("Expected at least one output message")
230+
}
231+
t.Logf("Response output messages: %d", len(resp.Output))
232+
}
233+
234+
func TestAnthropicGetProviderKey(t *testing.T) {
235+
provider := NewAnthropicProvider(&schemas.ProviderConfig{
236+
NetworkConfig: schemas.NetworkConfig{
237+
BaseURL: "https://api.anthropic.com",
238+
},
239+
ConcurrencyAndBufferSize: schemas.ConcurrencyAndBufferSize{
240+
Concurrency: 10,
241+
},
242+
}, newTestLogger())
243+
244+
key := provider.GetProviderKey()
245+
if key != schemas.Anthropic {
246+
t.Errorf("Expected provider key %s, got %s", schemas.Anthropic, key)
247+
}
248+
}

0 commit comments

Comments
 (0)