-
Notifications
You must be signed in to change notification settings - Fork 343
Phase 1: ADK Onboarding journey revision #739
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -1,131 +1,258 @@ | ||||||||
# Java Quickstart for ADK | ||||||||
|
||||||||
This guide shows you how to get up and running with the Agent Development Kit | ||||||||
This guide shows you how to get up and running with Agent Development Kit | ||||||||
for Java. Before you start, make sure you have the following installed: | ||||||||
|
||||||||
* Java 17 or later | ||||||||
* Maven 3.9 or later | ||||||||
|
||||||||
## Installation | ||||||||
## Create an agent project | ||||||||
|
||||||||
Create an agent project with the following files and directory structure: | ||||||||
|
||||||||
```console | ||||||||
my_agent/ | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Assuming that the |
||||||||
src/main/java/com/example/agent/ | ||||||||
HelloTimeAgent.java # main agent code | ||||||||
Comment on lines
+15
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Suggest unwrapping this line to remove ambiguity. |
||||||||
pom.xml # project configuration | ||||||||
.env # API keys or project IDs | ||||||||
``` | ||||||||
|
||||||||
??? tip "Create this project structure using the command line" | ||||||||
|
||||||||
=== "Windows" | ||||||||
|
||||||||
```console | ||||||||
mkdir my_agent\src\main\java\com\example\agent | ||||||||
type nul > my_agent\src\main\java\com\example\agent\HelloTimeAgent.java | ||||||||
type nul > my_agent\pom.xml | ||||||||
type nul > my_agent\.env | ||||||||
``` | ||||||||
|
||||||||
=== "MacOS / Linux" | ||||||||
|
||||||||
```bash | ||||||||
mkdir -p my_agent/src/main/java/com/example/agent && \ | ||||||||
touch my_agent/src/main/java/com/example/agent/HelloTimeAgent.java \ | ||||||||
touch my_agent/pom.xml my_agent/.env | ||||||||
``` | ||||||||
|
||||||||
### Define the agent code | ||||||||
|
||||||||
Create the code for a basic agent, including a simple implementation of an ADK | ||||||||
[Function Tool](/adk-docs/tools/function-tools/), called `getCurrentTime()`. | ||||||||
Add the following code to the `HelloTimeAgent.java` file in your project | ||||||||
directory: | ||||||||
|
||||||||
```java title="my_agent/src/main/java/com/example/agent/HelloTimeAgent.java" | ||||||||
package com.example.agent; | ||||||||
|
||||||||
import com.google.adk.agents.BaseAgent; | ||||||||
import com.google.adk.agents.LlmAgent; | ||||||||
import com.google.adk.tools.Annotations.Schema; | ||||||||
import com.google.adk.tools.FunctionTool; | ||||||||
|
||||||||
import java.util.Map; | ||||||||
|
||||||||
public class HelloTimeAgent { | ||||||||
|
||||||||
public static BaseAgent ROOT_AGENT = initAgent(); | ||||||||
|
||||||||
private static BaseAgent initAgent() { | ||||||||
return LlmAgent.builder() | ||||||||
.name("hello-time-agent") | ||||||||
.description("Tells the current time in a specified city") | ||||||||
.instruction(""" | ||||||||
You are a helpful assistant that tells the current time in a city. | ||||||||
Use the 'getCurrentTime' tool for this purpose. | ||||||||
""") | ||||||||
.model("gemini-2.5-flash") | ||||||||
.tools(FunctionTool.create(HelloTimeAgent.class, "getCurrentTime")) | ||||||||
.build(); | ||||||||
} | ||||||||
|
||||||||
/** Mock tool implementation */ | ||||||||
@Schema(description = "Get the current time for a given city") | ||||||||
public static Map<String, String> getCurrentTime( | ||||||||
@Schema(name = "city", description = "Name of the city to get the time for") String city) { | ||||||||
return Map.of( | ||||||||
"city", city, | ||||||||
"forecast", "The time is 10:30am." | ||||||||
); | ||||||||
} | ||||||||
} | ||||||||
``` | ||||||||
|
||||||||
### Configure project and dependencies | ||||||||
|
||||||||
Add the following dependency to your `pom.xml` file: | ||||||||
An ADK agent project requires this dependency in your | ||||||||
`pom.xml` project file: | ||||||||
|
||||||||
```xml | ||||||||
```xml title="my_agent/pom.xml (partial)" | ||||||||
<dependencies> | ||||||||
<dependency> | ||||||||
<groupId>com.google.adk</groupId> | ||||||||
<artifactId>adk-core</artifactId> | ||||||||
<version>0.0.1-SNAPSHOT</version> | ||||||||
<version>0.3.0</version> | ||||||||
</dependency> | ||||||||
</dependencies> | ||||||||
``` | ||||||||
|
||||||||
## Set up your API key | ||||||||
Update the `pom.xml` project file to include this dependency and | ||||||||
addtional settings with the following configuration code: | ||||||||
|
||||||||
??? info "Complete `pom.xml` configuration for project" | ||||||||
The following code shows a complete `pom.xml` configuration for | ||||||||
this project: | ||||||||
|
||||||||
```xml title="my_agent/pom.xml" | ||||||||
<?xml version="1.0" encoding="UTF-8"?> | ||||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||||||||
<modelVersion>4.0.0</modelVersion> | ||||||||
|
||||||||
<groupId>com.example.agent</groupId> | ||||||||
<artifactId>adk-agents</artifactId> | ||||||||
<version>1.0-SNAPSHOT</version> | ||||||||
|
||||||||
<!-- Specify the version of Java you'll be using --> | ||||||||
<properties> | ||||||||
<maven.compiler.source>17</maven.compiler.source> | ||||||||
<maven.compiler.target>17</maven.compiler.target> | ||||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||||||||
</properties> | ||||||||
|
||||||||
<dependencies> | ||||||||
<!-- The ADK core dependency --> | ||||||||
<dependency> | ||||||||
<groupId>com.google.adk</groupId> | ||||||||
<artifactId>google-adk</artifactId> | ||||||||
<version>0.3.0</version> | ||||||||
</dependency> | ||||||||
<!-- The ADK dev web UI to debug your agent --> | ||||||||
<dependency> | ||||||||
<groupId>com.google.adk</groupId> | ||||||||
<artifactId>google-adk-dev</artifactId> | ||||||||
<version>0.3.0</version> | ||||||||
</dependency> | ||||||||
</dependencies> | ||||||||
|
||||||||
</project> | ||||||||
``` | ||||||||
|
||||||||
### Set your API key | ||||||||
|
||||||||
This starter project uses the Gemini API, which requires an API key. If you | ||||||||
This project uses the Gemini API, which requires an API key. If you | ||||||||
don't already have Gemini API key, create a key in Google AI Studio on the | ||||||||
[API Keys](https://aistudio.google.com/app/apikey) page. | ||||||||
|
||||||||
In a terminal window, write your API key into an `.env` file as an environment variable: | ||||||||
In a terminal window, write your API key into your `.env` file of your project | ||||||||
to set environment variables: | ||||||||
|
||||||||
=== "Windows" | ||||||||
|
||||||||
```shell | ||||||||
```console title="Update: my_agent/.env" | ||||||||
echo 'GOOGLE_API_KEY="YOUR_API_KEY"' > .env | ||||||||
``` | ||||||||
|
||||||||
=== "MacOS / Linux" | ||||||||
|
||||||||
```shell | ||||||||
```bash title="Update: my_agent/.env" | ||||||||
echo 'export GOOGLE_API_KEY="YOUR_API_KEY"' > .env | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
These are read in as key-value pairs, so no need to export to the current environment. |
||||||||
``` | ||||||||
|
||||||||
## Create an agent project | ||||||||
|
||||||||
Run the `adk create` command to start a new agent project. | ||||||||
### Create an agent command-line interface | ||||||||
|
||||||||
```shell | ||||||||
adk create my_agent --language java | ||||||||
``` | ||||||||
|
||||||||
|
||||||||
### Explore the agent project | ||||||||
For convenience, create a `AgentCliRunner.java` class to allow you to run | ||||||||
and interact with `HelloTimeAgent` from the command line. This code shows | ||||||||
how to create a `RunConfig` object to run the agent and a `Session` object | ||||||||
to interact with the running agent. | ||||||||
|
||||||||
The created agent project has the following structure, with the `MyAgent.java` | ||||||||
file containing the main control code for the agent. | ||||||||
```java title="my_agent/src/main/java/com/example/agent/AgentCliRunner.java" | ||||||||
package com.example.agent; | ||||||||
|
||||||||
```shell | ||||||||
my_agent/ | ||||||||
src/main/java/com/example/MyAgent.java # main agent code. | ||||||||
pom.xml # project configuration. | ||||||||
.env # API keys or project IDs | ||||||||
``` | ||||||||
import com.google.adk.agents.RunConfig; | ||||||||
import com.google.adk.events.Event; | ||||||||
import com.google.adk.runner.InMemoryRunner; | ||||||||
import com.google.adk.sessions.Session; | ||||||||
import com.google.genai.types.Content; | ||||||||
import com.google.genai.types.Part; | ||||||||
import io.reactivex.rxjava3.core.Flowable; | ||||||||
import java.util.Scanner; | ||||||||
|
||||||||
The `MyAgent.java` file contains a `main` method which is the entry point for | ||||||||
the agent. You can also define tools for the agent to use. The following | ||||||||
example includes an additional `getCurrentTime` tool for use by the agent: | ||||||||
import static java.nio.charset.StandardCharsets.UTF_8; | ||||||||
|
||||||||
```java | ||||||||
import com.google.adk.agents.Agent; | ||||||||
import com.google.adk.tools.FunctionTool; | ||||||||
|
||||||||
import java.util.Map; | ||||||||
|
||||||||
public class MyAgent { | ||||||||
public class AgentCliRunner { | ||||||||
|
||||||||
public static void main(String[] args) { | ||||||||
var timeTool = new FunctionTool( | ||||||||
"get_current_time", | ||||||||
"Returns the current time in a specified city.", | ||||||||
MyAgent.class, | ||||||||
"getCurrentTime" | ||||||||
); | ||||||||
|
||||||||
var agent = Agent.builder() | ||||||||
.setName("time_teller_agent") | ||||||||
.setModel("gemini-1.5-flash") | ||||||||
.setInstruction("You are a helpful assistant that tells the current time in cities. Use the 'get_current_time' tool for this purpose.") | ||||||||
.setDescription("Tells the current time in a specified city.") | ||||||||
.setTools(java.util.List.of(timeTool)) | ||||||||
.build(); | ||||||||
|
||||||||
// TODO: Run the agent | ||||||||
} | ||||||||
|
||||||||
public static Map<String, Object> getCurrentTime(String city) { | ||||||||
if (city.equalsIgnoreCase("new york")) { | ||||||||
return Map.of("status", "success", "time", "10:30 AM EST"); | ||||||||
RunConfig runConfig = RunConfig.builder().build(); | ||||||||
InMemoryRunner runner = new InMemoryRunner(HelloTimeAgent.ROOT_AGENT); | ||||||||
|
||||||||
Session session = runner | ||||||||
.sessionService() | ||||||||
.createSession(runner.appName(), "user1234") | ||||||||
.blockingGet(); | ||||||||
|
||||||||
try (Scanner scanner = new Scanner(System.in, UTF_8)) { | ||||||||
while (true) { | ||||||||
System.out.print("\nYou > "); | ||||||||
String userInput = scanner.nextLine(); | ||||||||
if ("quit".equalsIgnoreCase(userInput)) { | ||||||||
break; | ||||||||
} | ||||||||
|
||||||||
Content userMsg = Content.fromParts(Part.fromText(userInput)); | ||||||||
Flowable<Event> events = runner.runAsync(session.userId(), session.id(), userMsg, runConfig); | ||||||||
|
||||||||
System.out.print("\nAgent > "); | ||||||||
events.blockingForEach(event -> { | ||||||||
if (event.finalResponse()) { | ||||||||
System.out.println(event.stringifyContent()); | ||||||||
} | ||||||||
}); | ||||||||
} | ||||||||
} | ||||||||
return Map.of("status", "error", "message", "Time for " + city + " not available."); | ||||||||
} | ||||||||
} | ||||||||
``` | ||||||||
|
||||||||
## Run your agent | ||||||||
|
||||||||
Run your agent using the `adk run` command-line tool. | ||||||||
You can run your ADK agent using the interactive command-line interface | ||||||||
`AgentCliRunner` class you defined or the ADK web user interface provided by | ||||||||
the ADK using the `AdkWebServer` class. Both these options allow you to test and | ||||||||
interact with your agent. | ||||||||
|
||||||||
### Run with command-line interface | ||||||||
|
||||||||
```shell | ||||||||
adk run my_agent | ||||||||
Run your agent with the command-line interface `AgentCliRunner` class | ||||||||
using the following Maven command: | ||||||||
|
||||||||
```console | ||||||||
mvn compile exec:java -Dexec.mainClass="com.example.agent.AgentCliRunner" | ||||||||
``` | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tested this with ADK Java, and it's working as expected. Thanks! |
||||||||
|
||||||||
 | ||||||||
 | ||||||||
|
||||||||
### Run agent with web interface | ||||||||
### Run with web interface | ||||||||
|
||||||||
The ADK framework provides web interface you can use to test and interact with | ||||||||
your agent. You can start the web interface using the following terminal | ||||||||
Run your agent with the ADK web interface using the following maven command: | ||||||||
|
||||||||
```shell | ||||||||
adk web my_agent | ||||||||
```console | ||||||||
mvn compile exec:java -Dexec.mainClass="com.google.adk.web.AdkWebServer" \ | ||||||||
-Dexec.args="--adk.agents.source-dir=src/main/java/com/example/agent --server.port=8080" | ||||||||
Comment on lines
+242
to
+244
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||
``` | ||||||||
This command starts a web server with a chat interface for your agent: | ||||||||
|
||||||||
 | ||||||||
This command starts a web server with a chat interface for your agent. You can | ||||||||
access the web interface at (http://localhost:8080). Select your agent at the | ||||||||
upper right corner and type a request. | ||||||||
|
||||||||
 | ||||||||
|
||||||||
## Next: build your agent | ||||||||
## Next: Build your agent | ||||||||
|
||||||||
Now that you have ADK installed and your first agent running, try building | ||||||||
your own agent with our intermediate build guides: | ||||||||
your own agent with our build guides: | ||||||||
|
||||||||
* [Build your agent](/adk-docs/tutorials/) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This image matches the output of the Java-based agent and tool, but the Python-based agent will return:
Should we use different images to match the output of each agent? Or refactor so the agent's tool implementation is the same and thus the output is the same?