-
Notifications
You must be signed in to change notification settings - Fork 945
Expand file tree
/
Copy pathopenai_test.go
More file actions
75 lines (67 loc) · 1.73 KB
/
Copy pathopenai_test.go
File metadata and controls
75 lines (67 loc) · 1.73 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
package tests
import (
"bifrost"
"bifrost/interfaces"
"fmt"
"testing"
"time"
)
// setupOpenAIRequests sends multiple test requests to OpenAI
func setupOpenAIRequests(bifrost *bifrost.Bifrost) {
text := "Hello world!"
// Text completion request
go func() {
result, err := bifrost.TextCompletionRequest(interfaces.OpenAI, &interfaces.BifrostRequest{
Model: "gpt-4o-mini",
Input: interfaces.RequestInput{
StringInput: &text,
},
Params: nil,
})
if err != nil {
fmt.Println("Error:", err)
} else {
fmt.Println("🐒 Text Completion Result:", result.Choices[0].Message.Content)
}
}()
// Chat completion requests with different messages and delays
openAIMessages := []string{
"Hello! How are you today?",
"What's the weather like?",
"Tell me a joke!",
"What's your favorite programming language?",
}
for i, message := range openAIMessages {
delay := time.Duration(100*(i+1)) * time.Millisecond
go func(msg string, delay time.Duration, index int) {
time.Sleep(delay)
messages := []interfaces.Message{
{
Role: interfaces.UserRole,
Content: &msg,
},
}
result, err := bifrost.ChatCompletionRequest(interfaces.OpenAI, &interfaces.BifrostRequest{
Model: "gpt-4o-mini",
Input: interfaces.RequestInput{
MessageInput: &messages,
},
Params: nil,
})
if err != nil {
fmt.Printf("Error in OpenAI request %d: %v\n", index+1, err)
} else {
fmt.Printf("🐒 Chat Completion Result %d: %s\n", index+1, result.Choices[0].Message.Content)
}
}(message, delay, i)
}
}
func TestOpenAI(t *testing.T) {
bifrost, err := getBifrost()
if err != nil {
t.Fatalf("Error initializing bifrost: %v", err)
return
}
setupOpenAIRequests(bifrost)
bifrost.Cleanup()
}