-
Notifications
You must be signed in to change notification settings - Fork 2
/
api_chat_completions_test.go
135 lines (111 loc) · 4.13 KB
/
api_chat_completions_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
package moonshot_test
import (
"context"
"errors"
"fmt"
"io"
"strings"
"testing"
"github.com/northes/go-moonshot"
"github.com/northes/go-moonshot/test"
"github.com/stretchr/testify/require"
)
func TestChat(t *testing.T) {
cli, err := NewTestClient()
if err != nil {
t.Fatal(err)
}
ctx := context.Background()
builder := moonshot.NewChatCompletionsBuilder()
builder.AddPrompt("你是 Kimi,由 Moonshot AI 提供的人工智能助手,你更擅长中文和英文的对话。你会为用户提供安全,有帮助,准确的回答。同时,你会拒绝一切涉及恐怖主义,种族歧视,黄色暴力等问题的回答。Moonshot AI 为专有名词,不可翻译成其他语言。").
AddUserContent("你好,我叫李雷,1+1等于多少?").
SetTemperature(0.3)
resp, err := cli.Chat().Completions(ctx, builder.ToRequest())
if err != nil {
t.Fatal(err)
}
t.Log(test.MarshalJsonToStringX(resp))
/*
{"id":"chatcmpl-dafad118ba6a4d1bb3e10be1734c6213","object":"chat.completion","created":15893254,"model":"moonshot-v1-8k","choices":[{"index":0,"message":{"Role":"assistant","Content":"你好,李雷!1+1等于2。如果你有更复杂的数学问题或者其他问题,也可以随时问我。"},"finish_reason":"stop"}],"usage":{"prompt_tokens":83,"completion_tokens":25,"total_tokens":108}}
*/
}
func TestChatStream(t *testing.T) {
cli, err := NewTestClient()
if err != nil {
t.Fatal(err)
}
builder := moonshot.NewChatCompletionsBuilder()
builder.SetModel(moonshot.ModelMoonshotV18K).
AddUserContent("你好,我叫李雷,1+1等于多少?").
SetStream(true)
resp, err := cli.Chat().CompletionsStream(context.Background(), builder.ToRequest())
if err != nil {
t.Fatal(err)
}
for receive := range resp.Receive() {
msg, err := receive.GetMessage()
if err != nil {
if errors.Is(err, io.EOF) {
break
}
t.Error(err)
continue
}
switch msg.Role {
case moonshot.RoleSystem:
case moonshot.RoleUser:
case moonshot.RoleAssistant:
default:
t.Logf("Role: %s,Content: %s", msg.Role, msg.Content)
}
}
}
func TestChatWithContext(t *testing.T) {
cli, err := NewTestClient()
if err != nil {
t.Fatal(err)
}
ctx := context.Background()
builder := moonshot.NewChatCompletionsBuilder()
builder.AddPrompt("你是 Kimi,由 Moonshot AI 提供的人工智能助手,你更擅长中文和英文的对话。你会为用户提供安全,有帮助,准确的回答。同时,你会拒绝一切涉及恐怖主义,种族歧视,黄色暴力等问题的回答。Moonshot AI 为专有名词,不可翻译成其他语言。").
AddUserContent("你好,我叫李雷,1+1等于多少?").
SetTemperature(0.3)
resp, err := cli.Chat().Completions(ctx, builder.ToRequest())
if err != nil {
t.Fatal(err)
}
t.Log(test.MarshalJsonToStringX(resp))
for _, choice := range resp.Choices {
builder.AddMessage(choice.Message)
}
builder.AddUserContent("在这个基础上再加3等于多少")
resp, err = cli.Chat().Completions(ctx, builder.ToRequest())
if err != nil {
t.Fatal(err)
}
t.Log(test.MarshalJsonToStringX(resp))
}
func TestPartialMode(t *testing.T) {
cli, err := NewTestClient()
if err != nil {
t.Fatal(err)
}
ctx := context.Background()
const leadingText = "{"
builder := moonshot.NewChatCompletionsBuilder()
builder.AddSystemContent("请从产品描述中提取名称、尺寸、价格和颜色,并在一个 JSON 对象中输出。")
builder.AddUserContent("大米 SmartHome Mini 是一款小巧的智能家居助手,有黑色和银色两种颜色,售价为 998 元,尺寸为 256 x 128 x 128mm。可让您通过语音或应用程序控制灯光、恒温器和其他联网设备,无论您将它放在家中的任何位置。")
builder.AddAssistantContent(leadingText, true)
builder.SetTemperature(0.3)
resp, err := cli.Chat().Completions(ctx, builder.ToRequest())
if err != nil {
t.Fatal(err)
}
message, err := resp.GetMessage()
if err != nil {
t.Fatal(err)
}
require.Equal(t, strings.HasPrefix(message.Content, leadingText), false, "message content should not start with '{'")
jsonStr := fmt.Sprintf("%s%s", leadingText, message.Content)
t.Log(jsonStr)
}