docs: improve ch01 and ch02 quickstart documentation
- Add clear warnings for code snippets that cannot run directly - Replace absolute paths with GitHub URLs - Add natural transitions between Component and Agent concepts - Add ChatModel vs ChatModelAgent comparison table - Clarify the relationship between Component and Agent - Add analogies for better understanding - Improve language clarity and specificity - Add ch02 example code for ChatModelAgent and Runner Change-Id: I08e8944a7a09b65023b691e3c158a3b7f3fe5649drew/english
parent
b035dd29c5
commit
cdedfd0425
@ -0,0 +1,118 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/cloudwego/eino/adk"
|
||||
"github.com/cloudwego/eino/schema"
|
||||
|
||||
examplemodel "github.com/cloudwego/eino-examples/adk/common/model"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var instruction string
|
||||
flag.StringVar(&instruction, "instruction", "You are a helpful assistant.", "")
|
||||
flag.Parse()
|
||||
|
||||
ctx := context.Background()
|
||||
cm := examplemodel.NewChatModel()
|
||||
|
||||
agent, err := adk.NewChatModelAgent(ctx, &adk.ChatModelAgentConfig{
|
||||
Name: "Ch02ChatModelAgent",
|
||||
Description: "A minimal ChatModelAgent with in-memory multi-turn history.",
|
||||
Instruction: instruction,
|
||||
Model: cm,
|
||||
})
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
runner := adk.NewRunner(ctx, adk.RunnerConfig{
|
||||
Agent: agent,
|
||||
EnableStreaming: true,
|
||||
})
|
||||
|
||||
history := make([]*schema.Message, 0, 16)
|
||||
scanner := bufio.NewScanner(os.Stdin)
|
||||
for {
|
||||
_, _ = fmt.Fprint(os.Stdout, "you> ")
|
||||
if !scanner.Scan() {
|
||||
break
|
||||
}
|
||||
line := strings.TrimSpace(scanner.Text())
|
||||
if line == "" {
|
||||
break
|
||||
}
|
||||
history = append(history, schema.UserMessage(line))
|
||||
|
||||
events := runner.Run(ctx, history)
|
||||
content, err := printAndCollectAssistantFromEvents(events)
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
history = append(history, schema.AssistantMessage(content, nil))
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
_, _ = fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func printAndCollectAssistantFromEvents(events *adk.AsyncIterator[*adk.AgentEvent]) (string, error) {
|
||||
var sb strings.Builder
|
||||
|
||||
for {
|
||||
event, ok := events.Next()
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
if event.Err != nil {
|
||||
return "", event.Err
|
||||
}
|
||||
if event.Output == nil || event.Output.MessageOutput == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
mv := event.Output.MessageOutput
|
||||
if mv.Role != schema.Assistant {
|
||||
continue
|
||||
}
|
||||
|
||||
if mv.IsStreaming {
|
||||
mv.MessageStream.SetAutomaticClose()
|
||||
for {
|
||||
frame, err := mv.MessageStream.Recv()
|
||||
if errors.Is(err, io.EOF) {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if frame != nil && frame.Content != "" {
|
||||
sb.WriteString(frame.Content)
|
||||
_, _ = fmt.Fprint(os.Stdout, frame.Content)
|
||||
}
|
||||
}
|
||||
_, _ = fmt.Fprintln(os.Stdout)
|
||||
continue
|
||||
}
|
||||
|
||||
if mv.Message != nil {
|
||||
sb.WriteString(mv.Message.Content)
|
||||
_, _ = fmt.Fprintln(os.Stdout, mv.Message.Content)
|
||||
} else {
|
||||
_, _ = fmt.Fprintln(os.Stdout)
|
||||
}
|
||||
}
|
||||
|
||||
return sb.String(), nil
|
||||
}
|
||||
Loading…
Reference in New Issue