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>
150 lines
3.4 KiB
Go
150 lines
3.4 KiB
Go
package main
|
||
|
||
import (
|
||
"bufio"
|
||
"context"
|
||
"flag"
|
||
"fmt"
|
||
"io"
|
||
"log"
|
||
"os"
|
||
"os/signal"
|
||
"strings"
|
||
"syscall"
|
||
|
||
"example.com/ueino/pkg/llm"
|
||
"example.com/ueino/pkg/opt"
|
||
pkgTool "example.com/ueino/pkg/tool"
|
||
"example.com/ueino/steps/weather/internal"
|
||
"github.com/cloudwego/eino/adk"
|
||
"github.com/cloudwego/eino/components/tool"
|
||
"github.com/cloudwego/eino/compose"
|
||
"github.com/cloudwego/eino/schema"
|
||
)
|
||
|
||
var (
|
||
modelName string
|
||
)
|
||
|
||
func main() {
|
||
flag.StringVar(&modelName, "model", "mini-chat", "聊天模型名称")
|
||
flag.Parse()
|
||
|
||
log.Printf("[D] use model = %s", modelName)
|
||
ctx, cancel := context.WithCancel(context.Background())
|
||
defer cancel()
|
||
signals := make(chan os.Signal, 1)
|
||
signal.Notify(signals, os.Interrupt, syscall.SIGTERM)
|
||
defer signal.Stop(signals)
|
||
|
||
go func() {
|
||
select {
|
||
case <-signals:
|
||
cancel()
|
||
log.Println("用户退出...")
|
||
os.Exit(0)
|
||
case <-ctx.Done():
|
||
}
|
||
}()
|
||
|
||
assistant, err := adk.NewChatModelAgent(ctx, &adk.ChatModelAgentConfig{
|
||
Name: "terminal_chat",
|
||
Instruction: opt.SYSTEM_PROMPT,
|
||
Model: llm.MustNewChatModel(ctx, modelName),
|
||
MaxIterations: 10,
|
||
ToolsConfig: adk.ToolsConfig{
|
||
ToolsNodeConfig: compose.ToolsNodeConfig{
|
||
Tools: []tool.BaseTool{pkgTool.NewWeather()},
|
||
},
|
||
},
|
||
Handlers: []adk.ChatModelAgentMiddleware{&internal.DateHandler{}},
|
||
})
|
||
if err != nil {
|
||
log.Panicf("创建助手失败:%v", err)
|
||
}
|
||
|
||
runner := adk.NewRunner(ctx, adk.RunnerConfig{
|
||
Agent: assistant,
|
||
EnableStreaming: true,
|
||
})
|
||
|
||
scanner := bufio.NewScanner(os.Stdin)
|
||
scanner.Buffer(make([]byte, 4096), 1024*1024)
|
||
var history []*schema.Message
|
||
fmt.Println("输入问题后按回车,按 Ctrl+C 退出。")
|
||
for {
|
||
fmt.Print("你> ")
|
||
if !scanner.Scan() {
|
||
if err := scanner.Err(); err != nil {
|
||
log.Panicf("读取输入失败:%v", err)
|
||
}
|
||
return
|
||
}
|
||
input := strings.TrimSpace(scanner.Text())
|
||
if input == "" {
|
||
continue
|
||
}
|
||
|
||
history = append(history, schema.UserMessage(input))
|
||
trace := internal.NewTraceHandler()
|
||
iter := runner.Run(ctx, history, adk.WithCallbacks(trace))
|
||
for {
|
||
event, ok := iter.Next()
|
||
if !ok {
|
||
break
|
||
}
|
||
if event.Err != nil {
|
||
log.Panicf("请求失败:%v", event.Err)
|
||
}
|
||
if event.Output == nil || event.Output.MessageOutput == nil {
|
||
continue
|
||
}
|
||
if !event.Output.MessageOutput.IsStreaming {
|
||
m, err := event.Output.MessageOutput.GetMessage()
|
||
if err != nil {
|
||
log.Panicf("读取消息失败:%v", err)
|
||
}
|
||
fmt.Printf("\n%s:\n%s\n", m.Role, m.Content)
|
||
history = append(history, m)
|
||
continue
|
||
}
|
||
stream := event.Output.MessageOutput.MessageStream
|
||
var chunks []*schema.Message
|
||
section := ""
|
||
for {
|
||
m, err := stream.Recv()
|
||
if err == io.EOF {
|
||
break
|
||
}
|
||
if err != nil {
|
||
stream.Close()
|
||
log.Panicf("读取回复失败:%v", err)
|
||
}
|
||
chunks = append(chunks, m)
|
||
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()
|
||
m, err := schema.ConcatMessages(chunks)
|
||
if err != nil {
|
||
log.Panicf("合并回复失败:%v", err)
|
||
}
|
||
history = append(history, m)
|
||
}
|
||
trace.Wait()
|
||
}
|
||
}
|