-
Notifications
You must be signed in to change notification settings - Fork 0
/
ai_test.go
139 lines (132 loc) · 2.87 KB
/
ai_test.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
package ai_test
import (
"context"
"fmt"
"io"
"os"
"testing"
"time"
"github.com/sunshineplan/ai"
"github.com/sunshineplan/ai/chatgpt"
"github.com/sunshineplan/ai/gemini"
)
func testChat(ai ai.AI, prompt string) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
fmt.Println(prompt)
resp, err := ai.Chat(ctx, prompt)
if err != nil {
return err
}
fmt.Println(resp.Results())
fmt.Println("---")
return nil
}
func testChatStream(ai ai.AI, prompt string) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
fmt.Println(prompt)
stream, err := ai.ChatStream(ctx, prompt)
if err != nil {
return err
}
defer stream.Close()
for {
resp, err := stream.Next()
if err != nil {
if err == io.EOF {
break
}
return err
}
fmt.Println(resp.Results())
}
fmt.Println("---")
return nil
}
func testChatSession(ai ai.AI) error {
s := ai.ChatSession()
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
fmt.Println("Hello, I have 2 dogs in my house.")
resp, err := s.Chat(ctx, "Hello, I have 2 dogs in my house.")
if err != nil {
return err
}
fmt.Println(resp.Results())
ctx, cancel = context.WithTimeout(context.Background(), time.Minute)
defer cancel()
fmt.Println("How many paws are in my house?")
stream, err := s.ChatStream(ctx, "How many paws are in my house?")
if err != nil {
return err
}
defer stream.Close()
for {
resp, err := stream.Next()
if err != nil {
if err == io.EOF {
break
}
return err
}
fmt.Println(resp.Results())
}
fmt.Println("---")
fmt.Println("History")
for _, i := range s.History() {
fmt.Println(i.Role, ":", i.Content)
}
fmt.Println("---")
return nil
}
func TestGemini(t *testing.T) {
apiKey := os.Getenv("GEMINI_API_KEY")
if apiKey == "" {
return
}
gemini, err := gemini.New(
ai.WithAPIKey(apiKey),
ai.WithEndpoint(os.Getenv("GEMINI_ENDPOINT")),
ai.WithProxy(os.Getenv("GEMINI_PROXY")),
ai.WithModel(os.Getenv("GEMINI_MODEL")),
)
if err != nil {
t.Fatal(err)
}
defer gemini.Close()
if err := testChat(gemini, "Who are you?"); err != nil {
t.Error(err)
}
if err := testChatStream(gemini, "Who am I?"); err != nil {
t.Error(err)
}
if err := testChatSession(gemini); err != nil {
t.Error(err)
}
}
func TestChatGPT(t *testing.T) {
apiKey := os.Getenv("CHATGPT_API_KEY")
if apiKey == "" {
return
}
chatgpt, err := chatgpt.New(
ai.WithAPIKey(apiKey),
ai.WithEndpoint(os.Getenv("CHATGPT_ENDPOINT")),
ai.WithProxy(os.Getenv("CHATGPT_PROXY")),
ai.WithModel(os.Getenv("CHATGPT_MODEL")),
)
if err != nil {
t.Fatal(err)
}
defer chatgpt.Close()
if err := testChat(chatgpt, "Who are you?"); err != nil {
t.Error(err)
}
if err := testChatStream(chatgpt, "Who am I?"); err != nil {
t.Error(err)
}
if err := testChatSession(chatgpt); err != nil {
t.Error(err)
}
}