forked from agi-cn/llmplugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin_manager_test.go
169 lines (124 loc) · 3.73 KB
/
plugin_manager_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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package llmplugin
import (
"context"
"os"
"testing"
"github.com/agi-cn/llmplugin/llm"
"github.com/agi-cn/llmplugin/llm/openai"
"github.com/agi-cn/llmplugin/plugins/calculator"
"github.com/agi-cn/llmplugin/plugins/google"
"github.com/agi-cn/llmplugin/plugins/stablediffusion"
"github.com/joho/godotenv"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestManagerSelectPlugin(t *testing.T) {
manager := newChatGPTManager()
t.Run("Digital Computing", func(t *testing.T) {
pluginCtxs, err := manager.Select(context.Background(), "10 add 20 equals ?")
require.NoError(t, err)
require.Equal(t, 1, len(pluginCtxs))
require.True(t, includePlugin(pluginCtxs, "Calculator"))
choices := pluginCtxs[0]
answer, err := choices.Plugin.Do(context.Background(), choices.Input)
require.NoError(t, err)
assert.Equal(t, "30", answer)
})
t.Run("Query Weather", func(t *testing.T) {
choices, err := manager.Select(context.Background(), "How is the weather today?")
assert.NoError(t, err)
assert.NotEmpty(t, choices)
assert.True(t, includePlugin(choices, "Weather"))
})
t.Run("Stable Diffusion", func(t *testing.T) {
choices, err := manager.Select(context.Background(), "Draw a girl image")
assert.NoError(t, err)
assert.NotEmpty(t, choices)
assert.True(t, includePlugin(choices, "StableDiffusion"))
})
t.Run("Google", func(t *testing.T) {
choices, err := manager.Select(context.Background(), "NBA 总决赛现在如何?")
assert.NoError(t, err)
assert.NotEmpty(t, choices)
assert.True(t, includePlugin(choices, "Google"))
})
}
func TestManagerSelectPlugin_WithoutChoice(t *testing.T) {
manager := newChatGPTManager()
t.Run("Quick Sort Source Code", func(t *testing.T) {
choices, err := manager.Select(context.Background(), "quick sort source code in python")
assert.NoError(t, err)
assert.Empty(t, choices)
})
}
func includePlugin(pluginCtxs []PluginContext, target string) bool {
for _, p := range pluginCtxs {
if p.GetName() == target {
return true
}
}
return false
}
func TestChoicePlugins(t *testing.T) {
plugins := newPlugins()
manager := NewPluginManager(nil, WithPlugins(plugins))
t.Run("Choice Calculator", func(t *testing.T) {
answer := "Calculator: 1+4"
got := manager.choicePlugins(answer)
assert.True(t,
includePlugin(got, "Calculator"))
})
t.Run("Choice Weather", func(t *testing.T) {
answer := "Weather: "
got := manager.choicePlugins(answer)
assert.True(t,
includePlugin(got, "Weather"))
})
t.Run("Choice Google", func(t *testing.T) {
answer := `Google: 今天NBA比赛赛程表`
got := manager.choicePlugins(answer)
assert.True(t,
includePlugin(got, "Google"))
})
}
func newChatGPTManager() *PluginManager {
_ = godotenv.Load() // ignore if file not exists
var llmer llm.LLMer
{
token := os.Getenv("OPENAI_TOKEN")
if len(token) == 0 {
panic("empty openai token: set os env: OPENAI_TOKEN")
}
llmer = openai.NewChatGPT(token, openai.WithModel("gpt-4"))
}
plugins := newPlugins()
return NewPluginManager(llmer, WithPlugins(plugins))
}
func newPlugins() []Plugin {
var (
googleEngineID = os.Getenv("GOOGLE_ENGINE_ID")
googleToken = os.Getenv("GOOGLE_TOKEN")
)
plugins := []Plugin{
&SimplePlugin{
Name: "Weather",
InputExample: ``,
Desc: "Can check the weather forecast",
DoFunc: func(ctx context.Context, query string) (answer string, err error) {
answer = "Call Weather Plugin"
return
},
},
calculator.NewCalculator(),
google.NewGoogle(googleEngineID, googleToken),
}
{ // stable diffusion
var sdAddr = os.Getenv("SD_ADDR")
if len(sdAddr) != 0 {
plugins = append(plugins,
stablediffusion.NewStableDiffusion(sdAddr),
)
}
}
return plugins
}