-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathchat_completions_builder_test.go
137 lines (124 loc) · 3.77 KB
/
chat_completions_builder_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
package moonshot_test
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/northes/go-moonshot"
)
func TestNewChatCompletionsBuilder(t *testing.T) {
tt := require.New(t)
builder := moonshot.NewChatCompletionsBuilder()
tt.NotNil(builder)
const (
promptContent = "你是 Kimi,由 Moonshot AI 提供的人工智能助手,你更擅长中文和英文的对话。你会为用户提供安全,有帮助,准确的回答。同时,你会拒绝一切涉及恐怖主义,种族歧视,黄色暴力等问题的回答。Moonshot AI 为专有名词,不可翻译成其他语言。"
userContent = "你好,我叫李雷,1+1等于多少?"
assistantContent = "我是小助手!"
functionName1 = "function1"
functionName2 = "function2"
)
var wantedReq = &moonshot.ChatCompletionsRequest{
Messages: []*moonshot.ChatCompletionsMessage{
{
Role: moonshot.RoleContextCache,
Content: "tag=tag1;reset_ttl=3600;dry_run=1",
},
{
Role: moonshot.RoleSystem,
Content: promptContent,
},
{
Role: moonshot.RoleUser,
Content: userContent,
},
{
Role: moonshot.RoleAssistant,
Content: assistantContent,
},
{
Role: moonshot.RoleUser,
Content: userContent,
},
},
Model: moonshot.ModelMoonshotV132K,
MaxTokens: 1024,
Temperature: 0.3,
TopP: 1.0,
N: 1,
PresencePenalty: 1.2,
FrequencyPenalty: 1.5,
ResponseFormat: &moonshot.ChatCompletionsRequestResponseFormat{
Type: moonshot.ChatCompletionsResponseFormatJSONObject,
},
Stop: []string{"结束"},
Stream: true,
Tools: []*moonshot.ChatCompletionsTool{{
Type: moonshot.ChatCompletionsToolTypeFunction,
Function: &moonshot.ChatCompletionsToolFunction{
Name: functionName1,
Description: "",
Parameters: nil,
},
}, {
Type: moonshot.ChatCompletionsToolTypeFunction,
Function: &moonshot.ChatCompletionsToolFunction{
Name: functionName2,
},
}, {
Type: moonshot.ChatCompletionsToolTypeFunction,
Function: &moonshot.ChatCompletionsToolFunction{
Name: functionName2,
},
}},
}
builder.SetContextCacheContent(
moonshot.NewContextCacheContentWithTag("tag1").
WithResetTTL(3600).
WithDryRun(true)).
AddPrompt(promptContent).
AddUserContent(userContent).
AddAssistantContent(assistantContent).
AddMessage(&moonshot.ChatCompletionsMessage{
Role: moonshot.RoleUser,
Content: userContent,
}).
SetModel(moonshot.ModelMoonshotV132K).
SetMaxTokens(1024).
SetTemperature(0.3).
SetTopP(1.0).
SetN(1).
SetPresencePenalty(1.2).
SetFrequencyPenalty(1.5).
SetResponseFormat(moonshot.ChatCompletionsResponseFormatJSONObject).
SetStop([]string{"结束"}).
SetStream(true).
SetTool(&moonshot.ChatCompletionsTool{
Type: moonshot.ChatCompletionsToolTypeFunction,
Function: &moonshot.ChatCompletionsToolFunction{
Name: functionName1,
Description: "",
Parameters: nil,
},
}).SetTools([]*moonshot.ChatCompletionsTool{
{
Type: moonshot.ChatCompletionsToolTypeFunction,
Function: &moonshot.ChatCompletionsToolFunction{
Name: functionName2,
},
},
{
Type: moonshot.ChatCompletionsToolTypeFunction,
Function: &moonshot.ChatCompletionsToolFunction{
Name: functionName2,
},
},
})
builder.SetTools(nil)
tt.Equal(wantedReq, builder.ToRequest())
tt.Equal(wantedReq, builder.ToRequest())
tt.NotEqual(wantedReq, builder.SetModel(moonshot.ModelMoonshotV1128K).ToRequest())
builder2 := moonshot.NewChatCompletionsBuilder(*wantedReq)
tt.Equal(wantedReq, builder2.ToRequest())
builder2.SetPresencePenalty(2)
tt.NotEqual(wantedReq, builder2.ToRequest())
builder2.SetResponseFormat(moonshot.ChatCompletionsResponseFormatText)
tt.NotEqual(wantedReq, builder2.ToRequest())
}