-
Notifications
You must be signed in to change notification settings - Fork 7
/
stream_call.go
66 lines (54 loc) · 1.52 KB
/
stream_call.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
package main
import (
"context"
"fmt"
"os"
"github.com/devinyf/dashscopego"
"github.com/devinyf/dashscopego/qwen"
)
func main() {
model := qwen.QwenVLPlus
token := os.Getenv("DASHSCOPE_API_KEY")
if token == "" {
panic("token is empty")
}
cli := dashscopego.NewTongyiClient(model, token).
SetUploadCache(qwen.NewMemoryFileCache()) // 可以通过 UploadCacher 接口 自定义缓存实现 避免重复上传, 默认使用内存缓存
homedir, _ := os.UserHomeDir()
sysContent := qwen.VLContentList{
{
Text: "You are a helpful assistant.",
},
}
userContent := qwen.VLContentList{
{
Text: "用唐诗体说明一下这张图片中的内容", //nolint:gosmopolitan
},
{
Image: "file://" + homedir + "/Downloads/pandas_img.jpg",
// Image: "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg",
},
}
input := dashscopego.VLInput{
Messages: []dashscopego.VLMessage{
{Role: qwen.RoleSystem, Content: &sysContent},
{Role: qwen.RoleUser, Content: &userContent},
},
}
// callback function: print stream result
streamCallbackFn := func(_ context.Context, chunk []byte) error {
fmt.Print(string(chunk)) //nolint:all
return nil
}
req := &dashscopego.VLRequest{
Input: input,
StreamingFn: streamCallbackFn,
}
ctx := context.TODO()
resp, err := cli.CreateVLCompletion(ctx, req)
if err != nil {
panic(err)
}
fmt.Println("\nnon-stream result: ") //nolint:all
fmt.Println(resp.Output.Choices[0].Message.Content.ToString()) //nolint:all
}