-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
68 lines (56 loc) · 1.69 KB
/
main.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
package main
import (
"context"
"log"
"os"
"github.com/devinyf/dashscopego"
"github.com/devinyf/dashscopego/qwen"
)
func main() {
model := qwen.QwenTurbo
token := os.Getenv("DASHSCOPE_API_KEY")
if token == "" {
panic("token is empty")
}
cli := dashscopego.NewTongyiClient(model, token)
sysContent := qwen.TextContent{
Text: "You are a helpful assistant.",
}
userContent := qwen.TextContent{
// Text: "有若干只鸡兔同在一个笼子里,从上面数,有100个头,从下面数,有334只脚。问笼中各有多少只鸡和兔?",
Text: "使用代码画一个y=x^2的函数图",
}
input := dashscopego.TextInput{
Messages: []dashscopego.TextMessage{
{Role: qwen.RoleSystem, Content: &sysContent},
{Role: qwen.RoleUser, Content: &userContent},
},
}
// TODO: 暂时不支持使用 streaming 模式, 报错: {"code":"InvalidParameter","message":"Plugins=[['code_interpreter']] don't support incremental_output"}
// callback function: print stream result
// streamCallbackFn := func(ctx context.Context, chunk []byte) error {
// log.Print(string(chunk))
// return nil
// }
req := &dashscopego.TextRequest{
Input: input,
// StreamingFn: streamCallbackFn,
Plugins: qwen.Plugins{qwen.PluginCodeInterpreter: {}},
}
ctx := context.TODO()
resp, err := cli.CreateCompletion(ctx, req)
if err != nil {
panic(err)
}
log.Println("\nnon-stream result: ")
// log.Printf("%+v\n", resp.ToJSONStr())
// 注意大部分plugin的返回结果是 Messages 不是 Message...
for _, v := range resp.Output.Choices[0].Messages {
if v.Content != nil {
log.Printf("%+v\n", v.Content.ToString())
}
if v.PluginCall != nil {
log.Printf("%+v\n", v.PluginCall.ToString())
}
}
}