feat: initialize Eino examples and ADK applications
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>
This commit is contained in:
commit
81deca8c8e
75 files changed
+7048
No files matched your search
@@ -0,0 +1,160 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/cloudwego/eino/adk"
|
||||
"github.com/cloudwego/eino/callbacks"
|
||||
"github.com/cloudwego/eino/components"
|
||||
"github.com/cloudwego/eino/components/model"
|
||||
"github.com/cloudwego/eino/components/tool"
|
||||
"github.com/cloudwego/eino/schema"
|
||||
)
|
||||
|
||||
type traceKey struct{}
|
||||
|
||||
type traceSpan struct {
|
||||
id uint64
|
||||
parent uint64
|
||||
name string
|
||||
started time.Time
|
||||
}
|
||||
|
||||
type TraceHandler struct {
|
||||
callbacks.Handler
|
||||
next atomic.Uint64
|
||||
pending sync.WaitGroup
|
||||
}
|
||||
|
||||
func NewTraceHandler() *TraceHandler {
|
||||
h := &TraceHandler{}
|
||||
h.Handler = callbacks.NewHandlerBuilder().
|
||||
OnStartFn(h.start).
|
||||
OnEndFn(h.end).
|
||||
OnErrorFn(h.fail).
|
||||
OnEndWithStreamOutputFn(h.endStream).
|
||||
Build()
|
||||
return h
|
||||
}
|
||||
|
||||
// Wait 等待回调的流副本读完,避免上一轮 trace 混入下一轮。
|
||||
func (h *TraceHandler) Wait() {
|
||||
h.pending.Wait()
|
||||
}
|
||||
|
||||
func (h *TraceHandler) Needed(ctx context.Context, info *callbacks.RunInfo, timing callbacks.CallbackTiming) bool {
|
||||
return traced(info) && h.Handler.(callbacks.TimingChecker).Needed(ctx, info, timing)
|
||||
}
|
||||
|
||||
func traced(info *callbacks.RunInfo) bool {
|
||||
return info != nil && (info.Component == adk.ComponentOfAgent ||
|
||||
info.Component == components.ComponentOfChatModel || info.Component == components.ComponentOfTool)
|
||||
}
|
||||
|
||||
func (h *TraceHandler) start(ctx context.Context, info *callbacks.RunInfo, input callbacks.CallbackInput) context.Context {
|
||||
if !traced(info) {
|
||||
return ctx
|
||||
}
|
||||
span := &traceSpan{id: h.next.Add(1), name: fmt.Sprintf("%s/%s", info.Component, info.Name), started: time.Now()}
|
||||
if parent, ok := ctx.Value(traceKey{}).(*traceSpan); ok {
|
||||
span.parent = parent.id
|
||||
}
|
||||
log.Printf("[trace] start span=%d parent=%d %s", span.id, span.parent, span.name)
|
||||
switch info.Component {
|
||||
case components.ComponentOfChatModel:
|
||||
if in := model.ConvCallbackInput(input); in != nil {
|
||||
log.Printf("[trace] span=%d model input messages=%d", span.id, len(in.Messages))
|
||||
}
|
||||
case components.ComponentOfTool:
|
||||
if in := tool.ConvCallbackInput(input); in != nil {
|
||||
log.Printf("[trace] span=%d tool arguments=%s", span.id, in.ArgumentsInJSON)
|
||||
}
|
||||
}
|
||||
return context.WithValue(ctx, traceKey{}, span)
|
||||
}
|
||||
|
||||
func finishTrace(ctx context.Context, err error) {
|
||||
if span, ok := ctx.Value(traceKey{}).(*traceSpan); ok {
|
||||
log.Printf("[trace] end span=%d %s duration=%s error=%v", span.id, span.name, time.Since(span.started).Round(time.Millisecond), err)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *TraceHandler) end(ctx context.Context, info *callbacks.RunInfo, output callbacks.CallbackOutput) context.Context {
|
||||
if !traced(info) {
|
||||
return ctx
|
||||
}
|
||||
if out := adk.ConvAgentCallbackOutput(output); out != nil && out.Events != nil {
|
||||
// Agent 返回的是事件迭代器;读完副本后才是整轮执行结束。
|
||||
h.pending.Add(1)
|
||||
go func() {
|
||||
defer h.pending.Done()
|
||||
var runErr error
|
||||
for {
|
||||
event, ok := out.Events.Next()
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
if event.Err != nil {
|
||||
runErr = event.Err
|
||||
}
|
||||
if event.Output != nil && event.Output.MessageOutput != nil {
|
||||
if _, err := event.Output.MessageOutput.GetMessage(); err != nil {
|
||||
runErr = err
|
||||
}
|
||||
}
|
||||
}
|
||||
finishTrace(ctx, runErr)
|
||||
}()
|
||||
return ctx
|
||||
}
|
||||
if info.Component == components.ComponentOfTool {
|
||||
if out := tool.ConvCallbackOutput(output); out != nil {
|
||||
log.Printf("[trace] tool output=%s", out.Response)
|
||||
}
|
||||
}
|
||||
finishTrace(ctx, nil)
|
||||
return ctx
|
||||
}
|
||||
|
||||
func (h *TraceHandler) fail(ctx context.Context, info *callbacks.RunInfo, err error) context.Context {
|
||||
if traced(info) {
|
||||
finishTrace(ctx, err)
|
||||
}
|
||||
return ctx
|
||||
}
|
||||
|
||||
func (h *TraceHandler) endStream(ctx context.Context, info *callbacks.RunInfo, output *schema.StreamReader[callbacks.CallbackOutput]) context.Context {
|
||||
if !traced(info) {
|
||||
output.Close()
|
||||
return ctx
|
||||
}
|
||||
h.pending.Add(1)
|
||||
go func() {
|
||||
defer h.pending.Done()
|
||||
defer output.Close()
|
||||
chunks := 0
|
||||
for {
|
||||
chunk, err := output.Recv()
|
||||
if err == io.EOF {
|
||||
log.Printf("[trace] model output chunks=%d", chunks)
|
||||
finishTrace(ctx, nil)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
finishTrace(ctx, err)
|
||||
return
|
||||
}
|
||||
chunks++
|
||||
if out := model.ConvCallbackOutput(chunk); out != nil && out.TokenUsage != nil {
|
||||
log.Printf("[trace] model token usage=%+v", *out.TokenUsage)
|
||||
}
|
||||
}
|
||||
}()
|
||||
return ctx
|
||||
}
|
||||
Reference in new issue
Block a user