feat: add weather tools to legacy and Eino chat
This commit is contained in:
1 parent
6e6171f481
commit
86e86bc9fa
18 files changed
+926
-77
No files matched your search
+14
-20
@@ -12,9 +12,11 @@ import (
|
||||
"syscall"
|
||||
|
||||
"example.com/ueino/internal/opt"
|
||||
"example.com/ueino/legacy/round"
|
||||
"example.com/ueino/pkg/memory"
|
||||
"example.com/ueino/pkg/model"
|
||||
"example.com/ueino/pkg/provider"
|
||||
"example.com/ueino/pkg/tool"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -29,11 +31,10 @@ func init() {
|
||||
flag.StringVar(&modelName, "model", "local-mini", "")
|
||||
flag.StringVar(&apiKeyEnv, "api-key-env", "AI_KEY", "")
|
||||
flag.IntVar(&maxToken, "max-token", 4096, "")
|
||||
|
||||
flag.Parse()
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer cancel()
|
||||
|
||||
@@ -57,11 +58,12 @@ func run(ctx context.Context) {
|
||||
)
|
||||
|
||||
mem := memory.NewMemory(maxToken)
|
||||
tools := []tool.Tool{tool.NewWeather()}
|
||||
scanner := bufio.NewScanner(os.Stdin)
|
||||
fmt.Println("输入消息开始聊天,按 Ctrl+C 退出。")
|
||||
|
||||
for ctx.Err() == nil {
|
||||
fmt.Print("你> ")
|
||||
fmt.Print("📝 > ")
|
||||
if !scanner.Scan() {
|
||||
break
|
||||
}
|
||||
@@ -75,30 +77,22 @@ func run(ctx context.Context) {
|
||||
messages = append(messages, mem.LoadHistory()...)
|
||||
messages = append(messages, model.ChatMessage{Role: "user", Content: input})
|
||||
|
||||
stream, err := chat.ChatCompletion(ctx, messages)
|
||||
if err != nil {
|
||||
if ctx.Err() != nil {
|
||||
return
|
||||
}
|
||||
log.Printf("请求失败: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Print("助手> ")
|
||||
var reply strings.Builder
|
||||
|
||||
for chunk := range stream {
|
||||
fmt.Println("🤖 thinking...")
|
||||
reply, err := round.Complete(ctx, chat, messages, tools, func(chunk string) {
|
||||
fmt.Print(chunk)
|
||||
reply.WriteString(chunk)
|
||||
}
|
||||
})
|
||||
fmt.Println()
|
||||
|
||||
if ctx.Err() != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if reply.Len() > 0 {
|
||||
mem.Save(input, reply.String())
|
||||
if err != nil {
|
||||
log.Printf("请求失败: %v", err)
|
||||
continue
|
||||
}
|
||||
if reply != "" {
|
||||
mem.Save(input, reply)
|
||||
}
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
|
||||
Reference in new issue
Block a user