filterExample 演示过滤事件
(ctx context.Context, ag *agent.Agent)
| 107 | |
| 108 | // filterExample 演示过滤事件 |
| 109 | func filterExample(ctx context.Context, ag *agent.Agent) { |
| 110 | // 只处理来自 assistant 的事件 |
| 111 | reader := agent.StreamFilter( |
| 112 | ag.Stream(ctx, "Tell me a joke"), |
| 113 | func(event *session.Event) bool { |
| 114 | return event.Author == "assistant" |
| 115 | }, |
| 116 | ) |
| 117 | |
| 118 | for { |
| 119 | event, err := reader.Recv() |
| 120 | if err != nil { |
| 121 | if errors.Is(err, io.EOF) { |
| 122 | break |
| 123 | } |
| 124 | log.Printf("Error: %v", err) |
| 125 | break |
| 126 | } |
| 127 | fmt.Printf("Assistant says: %s\n", event.Content.Content) |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // lastEventExample 演示获取最后一个事件 |
| 132 | func lastEventExample(ctx context.Context, ag *agent.Agent) { |
no test coverage detected