Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ profile.cov
# Go workspace file
go.work
go.work.sum

.env
100 changes: 100 additions & 0 deletions examples/anthropic/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package provides an example ADK agent using Anthropic Claude.
//
// Run with:
//
// godotenv go run ./examples/anthropic
//
// Environment variables:
// - ANTHROPIC_API_KEY: API key for direct Anthropic API access
// - ANTHROPIC_USE_VERTEX: Set to "true" or "1" to use Vertex AI backend
// - GOOGLE_CLOUD_PROJECT: GCP project ID (required for Vertex AI)
// - GOOGLE_CLOUD_REGION: GCP region (required for Vertex AI)
package main

import (
"context"
"log"
"os"

anthropicsdk "github.com/anthropics/anthropic-sdk-go"

"google.golang.org/adk/agent"
"google.golang.org/adk/agent/llmagent"
"google.golang.org/adk/cmd/launcher"
"google.golang.org/adk/cmd/launcher/full"
"google.golang.org/adk/model/anthropic"
"google.golang.org/adk/tool"
"google.golang.org/adk/tool/functiontool"
)

// WeatherInput is the input schema for the weather tool.
type WeatherInput struct {
City string `json:"city" jsonschema:"the city name to get weather for"`
}

// WeatherOutput is the output schema for the weather tool.
type WeatherOutput struct {
Weather string `json:"weather"`
}

// getWeather is a simple tool function for the agent to call.
func getWeather(_ tool.Context, input WeatherInput) (WeatherOutput, error) {
// In a real application, this would call a weather API
weather := "The weather in " + input.City + " is sunny with a temperature of 22°C."
return WeatherOutput{Weather: weather}, nil
}

func main() {
ctx := context.Background()

// Create the Anthropic model.
// By default, uses the ANTHROPIC_API_KEY environment variable.
// Set ANTHROPIC_USE_VERTEX=true to use Vertex AI backend instead.
model, err := anthropic.NewModel(ctx, anthropicsdk.ModelClaudeHaiku4_5, nil)
if err != nil {
log.Fatalf("Failed to create model: %v", err)
}

// Create a function tool for the agent
weatherTool, err := functiontool.New(functiontool.Config{
Name: "get_weather",
Description: "Get the current weather for a city",
}, getWeather)
if err != nil {
log.Fatalf("Failed to create tool: %v", err)
}

a, err := llmagent.New(llmagent.Config{
Name: "anthropic_weather_agent",
Model: model,
Description: "Agent to answer questions about the weather in a city using Anthropic Claude.",
Instruction: "You are a helpful weather assistant. When asked about weather, use the get_weather tool to fetch current conditions. Be concise in your responses.",
Tools: []tool.Tool{weatherTool},
})
if err != nil {
log.Fatalf("Failed to create agent: %v", err)
}

config := &launcher.Config{
AgentLoader: agent.NewSingleLoader(a),
}

l := full.NewLauncher()
if err = l.Execute(ctx, config, os.Args[1:]); err != nil {
log.Fatalf("Run failed: %v\n\n%s", err, l.CommandLineSyntax())
}
}
113 changes: 113 additions & 0 deletions examples/workflowagents/loop-anthropic/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package demonstrates a workflow agent that runs an Anthropic-powered agent in a loop.
//
// Run with:
//
// godotenv go run ./examples/workflowagents/loop-anthropic
//
// Environment variables:
// - ANTHROPIC_API_KEY: API key for direct Anthropic API access
package main

import (
"context"
"log"
"os"

"google.golang.org/adk/agent"
"google.golang.org/adk/agent/llmagent"
"google.golang.org/adk/agent/workflowagents/loopagent"
"google.golang.org/adk/cmd/launcher"
"google.golang.org/adk/cmd/launcher/full"
"google.golang.org/adk/model/anthropic"
"google.golang.org/adk/tool"
"google.golang.org/adk/tool/functiontool"

anthropicsdk "github.com/anthropics/anthropic-sdk-go"
)

// StoryInput is the input for the continue_story tool.
type StoryInput struct {
Direction string `json:"direction" jsonschema:"the direction to take the story (e.g., 'add conflict', 'introduce character', 'resolve plot')"`
}

// StoryOutput is the output of the continue_story tool.
type StoryOutput struct {
Instruction string `json:"instruction"`
}

// continueStory provides story direction to the agent.
func continueStory(_ tool.Context, input StoryInput) (StoryOutput, error) {
return StoryOutput{
Instruction: "Continue the story by: " + input.Direction,
}, nil
}

func main() {
ctx := context.Background()

// Create the Anthropic model
model, err := anthropic.NewModel(ctx, anthropicsdk.ModelClaudeOpus4_5, nil)
if err != nil {
log.Fatalf("Failed to create model: %v", err)
}

// Create a tool for story continuation
storyTool, err := functiontool.New(functiontool.Config{
Name: "continue_story",
Description: "Get instructions for continuing the story in a specific direction",
}, continueStory)
if err != nil {
log.Fatalf("Failed to create tool: %v", err)
}

// Create an LLM agent that tells stories
storyteller, err := llmagent.New(llmagent.Config{
Name: "storyteller",
Model: model,
Description: "A creative storyteller that writes short story segments.",
Instruction: `You are a creative storyteller. Each time you are called, continue the ongoing story with 2-3 sentences.
Build upon what came before, adding new elements while maintaining narrative coherence.
Keep each segment brief but engaging. Do not repeat previous content.
If this is the first segment, begin a new story about an unexpected adventure.`,
Tools: []tool.Tool{storyTool},
})
if err != nil {
log.Fatalf("Failed to create storyteller agent: %v", err)
}

// Create a loop agent that runs the storyteller multiple times
loopAgent, err := loopagent.New(loopagent.Config{
MaxIterations: 3,
AgentConfig: agent.Config{
Name: "story_loop",
Description: "A loop agent that builds a story over multiple iterations using Anthropic Claude",
SubAgents: []agent.Agent{storyteller},
},
})
if err != nil {
log.Fatalf("Failed to create loop agent: %v", err)
}

config := &launcher.Config{
AgentLoader: agent.NewSingleLoader(loopAgent),
}

l := full.NewLauncher()
if err = l.Execute(ctx, config, os.Args[1:]); err != nil {
log.Fatalf("Run failed: %v\n\n%s", err, l.CommandLineSyntax())
}
}
98 changes: 98 additions & 0 deletions examples/workflowagents/parallel-anthropic/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package demonstrates a workflow agent that runs Anthropic-powered sub-agents in parallel.
//
// Run with:
//
// godotenv go run ./examples/workflowagents/parallel-anthropic
//
// Environment variables:
// - ANTHROPIC_API_KEY: API key for direct Anthropic API access
package main

import (
"context"
"log"
"os"

anthropicsdk "github.com/anthropics/anthropic-sdk-go"

"google.golang.org/adk/agent"
"google.golang.org/adk/agent/llmagent"
"google.golang.org/adk/agent/workflowagents/parallelagent"
"google.golang.org/adk/cmd/launcher"
"google.golang.org/adk/cmd/launcher/full"
"google.golang.org/adk/model/anthropic"
)

func main() {
ctx := context.Background()

// Create the Anthropic model
model, err := anthropic.NewModel(ctx, anthropicsdk.ModelClaudeSonnet4_20250514, nil)
if err != nil {
log.Fatalf("Failed to create model: %v", err)
}

// Create two LLM agents that will run in parallel
// Each agent has a different perspective on the same topic

optimistAgent, err := llmagent.New(llmagent.Config{
Name: "optimist_agent",
Model: model,
Description: "An optimistic analyst that focuses on opportunities.",
Instruction: `You are an optimistic business analyst. When given a topic or question,
provide a brief (2-3 sentences) analysis focusing on the positive aspects, opportunities,
and potential benefits. Be concise but insightful.`,
OutputKey: "optimist_view",
})
if err != nil {
log.Fatalf("Failed to create optimist agent: %v", err)
}

pessimistAgent, err := llmagent.New(llmagent.Config{
Name: "pessimist_agent",
Model: model,
Description: "A cautious analyst that focuses on risks.",
Instruction: `You are a cautious risk analyst. When given a topic or question,
provide a brief (2-3 sentences) analysis focusing on potential risks, challenges,
and things to watch out for. Be concise but thorough.`,
OutputKey: "pessimist_view",
})
if err != nil {
log.Fatalf("Failed to create pessimist agent: %v", err)
}

// Create a parallel agent that runs both analysts simultaneously
parallelAgent, err := parallelagent.New(parallelagent.Config{
AgentConfig: agent.Config{
Name: "dual_perspective_agent",
Description: "Analyses topics from both optimistic and pessimistic perspectives in parallel",
SubAgents: []agent.Agent{optimistAgent, pessimistAgent},
},
})
if err != nil {
log.Fatalf("Failed to create parallel agent: %v", err)
}

config := &launcher.Config{
AgentLoader: agent.NewSingleLoader(parallelAgent),
}

l := full.NewLauncher()
if err = l.Execute(ctx, config, os.Args[1:]); err != nil {
log.Fatalf("Run failed: %v\n\n%s", err, l.CommandLineSyntax())
}
}
Loading