-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.go
75 lines (59 loc) · 1.87 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
69
70
71
72
73
74
75
package main
import (
"context"
"fmt"
"os"
"github.com/tmc/langchaingo/agents"
"github.com/tmc/langchaingo/callbacks"
"github.com/tmc/langchaingo/chains"
"github.com/tmc/langchaingo/tools/dashscope/wanx"
"github.com/tmc/langchaingo/llms/tongyi"
"github.com/tmc/langchaingo/tools"
)
/*
* 使用通义千问 调用 通义万象 实现文生图功能
* 这个例子展示了如何使用 langchaingo Agent 来调用通义千问(llm) 和 通义万象(tool)
*
* 注意:
* *实验性功能: 没有并入 langchiango 主分支*
* *在 go.mod 中 replace 了 langchiango 到 fork 功能分支*
*/
func main() {
if err := agentExample(); err != nil {
panic(err)
}
}
func agentExample() error {
modelOpt := tongyi.WithModel("qwen-turbo")
keyOpt := tongyi.WithToken(os.Getenv("DASHSCOPE_API_KEY"))
llm, err := tongyi.New(modelOpt, keyOpt)
if err != nil {
return err
}
wanxDescOpt := wanx.WithDescription(wanx.WanxDescriptionCN) // 切换中文描述 默认是英文
wanxTool := wanx.NewTongyiWanx(wanxDescOpt)
agentTools := []tools.Tool{
tools.Calculator{},
wanxTool,
}
callbackHandler := callbacks.NewFinalStreamHandler()
// 打印全部输出信息, 包括中间 agent 调用工具的过程
callbackHandler.PrintOutput = true
agent := agents.NewOneShotAgent(llm, agentTools, agents.WithCallbacksHandler(callbackHandler))
executor := agents.NewExecutor(agent)
// 设置 streaming 结果的回调函数
fn := func(ctx context.Context, chunk []byte) {
fmt.Printf("%s", string(chunk))
}
// 获取 streaming 输出
callbackHandler.ReadFromEgress(context.Background(), fn)
fmt.Printf("\n")
question := "画一个武松打虎, 帮我生成提示词, 要写实的画风, 把老虎画的萌一点"
answer, err := chains.Run(context.Background(), executor, question)
if err != nil {
panic(err)
}
fmt.Println("\n<Final Answer>: ", answer)
_ = answer
return err
}