|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log" |
| 6 | + |
| 7 | + "github.com/docker/cagent/pkg/agent" |
| 8 | + latest "github.com/docker/cagent/pkg/config/v2" |
| 9 | + "github.com/docker/cagent/pkg/environment" |
| 10 | + "github.com/docker/cagent/pkg/model/provider/openai" |
| 11 | + "github.com/docker/cagent/pkg/runtime" |
| 12 | + "github.com/docker/cagent/pkg/session" |
| 13 | + "github.com/docker/cagent/pkg/team" |
| 14 | +) |
| 15 | + |
| 16 | +func main() { |
| 17 | + ctx := context.Background() |
| 18 | + |
| 19 | + llm, err := openai.NewClient( |
| 20 | + ctx, |
| 21 | + &latest.ModelConfig{ |
| 22 | + Provider: "openai", |
| 23 | + Model: "gpt-4o", |
| 24 | + }, |
| 25 | + environment.NewDefaultProvider(ctx), |
| 26 | + ) |
| 27 | + if err != nil { |
| 28 | + log.Fatal(err) |
| 29 | + } |
| 30 | + |
| 31 | + human := agent.New( |
| 32 | + "root", |
| 33 | + "You are a human, with feelings and emotions.", |
| 34 | + agent.WithModel(llm), |
| 35 | + agent.WithDescription("A human."), |
| 36 | + ) |
| 37 | + |
| 38 | + humanTeam := team.New(team.WithAgents(human)) |
| 39 | + |
| 40 | + rt, err := runtime.New(humanTeam) |
| 41 | + if err != nil { |
| 42 | + log.Fatal(err) |
| 43 | + } |
| 44 | + |
| 45 | + sess := session.New(session.WithUserMessage("", "How are you doing?")) |
| 46 | + |
| 47 | + events := rt.RunStream(ctx, sess) |
| 48 | + for event := range events { |
| 49 | + switch e := event.(type) { |
| 50 | + case *runtime.AgentChoiceEvent: |
| 51 | + log.Printf("Agent %s: %s\n", e.AgentName, e.Content) |
| 52 | + case *runtime.StreamStartedEvent: |
| 53 | + log.Println("Stream started for session") |
| 54 | + case *runtime.StreamStoppedEvent: |
| 55 | + log.Println("Stream stopped for session") |
| 56 | + case *runtime.ToolCallConfirmationEvent: |
| 57 | + rt.Resume(ctx, "approve-session") |
| 58 | + case *runtime.ToolCallEvent: |
| 59 | + log.Printf("Tool call: %s\n", e.ToolCall.Function.Name) |
| 60 | + case *runtime.ToolCallResponseEvent: |
| 61 | + log.Printf("Tool call response: %s\n", e.Response) |
| 62 | + // etc... |
| 63 | + } |
| 64 | + } |
| 65 | +} |
0 commit comments