Include chat, weather, image, travel, streaming ASR and text examples, shared model initialization, documentation, and existing tests. Co-authored-by: TRAE CLI <traecli@bytedance.com>
89 lines
1.8 KiB
Go
89 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
|
|
"example.com/ueino/pkg/llm"
|
|
"example.com/ueino/pkg/opt"
|
|
"github.com/cloudwego/eino/adk"
|
|
"github.com/cloudwego/eino/components/tool"
|
|
"github.com/cloudwego/eino/compose"
|
|
)
|
|
|
|
func main() {
|
|
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
|
defer cancel()
|
|
|
|
assistant, err := adk.NewChatModelAgent(ctx, &adk.ChatModelAgentConfig{
|
|
Name: "terminal_chat",
|
|
Instruction: opt.SYSTEM_PROMPT,
|
|
Model: llm.MustNewChatModel(ctx, "mini-chat"),
|
|
MaxIterations: 10,
|
|
ToolsConfig: adk.ToolsConfig{
|
|
ToolsNodeConfig: compose.ToolsNodeConfig{
|
|
Tools: []tool.BaseTool{},
|
|
},
|
|
},
|
|
Handlers: []adk.ChatModelAgentMiddleware{},
|
|
})
|
|
if err != nil {
|
|
// 按你的应用方式处理错误。
|
|
panic(err)
|
|
}
|
|
|
|
runner := adk.NewRunner(ctx, adk.RunnerConfig{
|
|
Agent: assistant,
|
|
EnableStreaming: true,
|
|
})
|
|
|
|
iter := runner.Query(ctx, "你好,很高兴认识你")
|
|
for {
|
|
event, contiune := iter.Next()
|
|
if !contiune {
|
|
break
|
|
}
|
|
|
|
if event.Err != nil {
|
|
log.Panicf("query failed: %v", event.Err)
|
|
}
|
|
|
|
if event.Output == nil || event.Output.MessageOutput == nil {
|
|
continue
|
|
}
|
|
stream := event.Output.MessageOutput.MessageStream
|
|
section := ""
|
|
for {
|
|
m, err := stream.Recv()
|
|
if err == io.EOF {
|
|
break
|
|
}
|
|
if err != nil {
|
|
stream.Close()
|
|
log.Panicf("读取回复失败:%v", err)
|
|
}
|
|
if m.ReasoningContent != "" {
|
|
if section != "思考过程" {
|
|
fmt.Print("\n思考过程:\n")
|
|
section = "思考过程"
|
|
}
|
|
fmt.Print(m.ReasoningContent)
|
|
}
|
|
if m.Content != "" {
|
|
if section != "最终回答" {
|
|
fmt.Print("\n最终回答:\n")
|
|
section = "最终回答"
|
|
}
|
|
fmt.Print(m.Content)
|
|
}
|
|
}
|
|
stream.Close()
|
|
fmt.Println()
|
|
}
|
|
}
|