ADK์ฉ Java ๋น ๋ฅธ ์์¶
์ด ๊ฐ์ด๋๋ ADK(Agent Development Kit) for Java๋ฅผ ์ฌ์ฉํ์ฌ ์์ํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ์ค๋๋ค. ์์ํ๊ธฐ ์ ์ ๋ค์์ด ์ค์น๋์ด ์๋์ง ํ์ธํ์ธ์.
- Java 17 ์ด์
- Maven 3.9 ์ด์
์์ด์ ํธ ํ๋ก์ ํธ ์์ฑ¶
๋ค์ ํ์ผ ๋ฐ ๋๋ ํ ๋ฆฌ ๊ตฌ์กฐ๋ก ์์ด์ ํธ ํ๋ก์ ํธ๋ฅผ ์์ฑํฉ๋๋ค.
my_agent/
src/main/java/com/example/agent/
HelloTimeAgent.java # ๋ฉ์ธ ์์ด์ ํธ ์ฝ๋
AgentCliRunner.java # ๋ช
๋ น์ค ์ธํฐํ์ด์ค
pom.xml # ํ๋ก์ ํธ ๊ตฌ์ฑ
.env # API ํค ๋๋ ํ๋ก์ ํธ ID
๋ช ๋ น์ค์ ์ฌ์ฉํ์ฌ ์ด ํ๋ก์ ํธ ๊ตฌ์กฐ ์์ฑ
์์ด์ ํธ ์ฝ๋ ์ ์¶
ADK ํจ์ ๋๊ตฌ์ ๊ฐ๋จํ ๊ตฌํ์ธ getCurrentTime()์ ํฌํจํ์ฌ ๊ธฐ๋ณธ ์์ด์ ํธ ์ฝ๋๋ฅผ ์์ฑํฉ๋๋ค. ํ๋ก์ ํธ ๋๋ ํ ๋ฆฌ์ 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("์ง์ ๋ ๋์์ ํ์ฌ ์๊ฐ์ ์๋ ค์ค๋๋ค.")
.instruction("""
๋น์ ์ ์ง์ ๋ ๋์์ ํ์ฌ ์๊ฐ์ ์๋ ค์ฃผ๋ ์ ์ฉํ ๋์ฐ๋ฏธ์
๋๋ค.
์ด๋ฅผ ์ํด 'getCurrentTime' ๋๊ตฌ๋ฅผ ์ฌ์ฉํ์ญ์์ค.
""")
.model("gemini-flash-latest")
.tools(FunctionTool.create(HelloTimeAgent.class, "getCurrentTime"))
.build();
}
/** Mock ๋๊ตฌ ๊ตฌํ */
@Schema(description = "์ฃผ์ด์ง ๋์์ ๋ํ ํ์ฌ ์๊ฐ์ ๊ฐ์ ธ์ต๋๋ค.")
public static Map<String, String> getCurrentTime(
@Schema(name = "city", description = "์๊ฐ์ ๊ฐ์ ธ์ฌ ๋์ ์ด๋ฆ") String city) {
return Map.of(
"city", city,
"forecast", "์๊ฐ์ ์ค์ 10์ 30๋ถ์
๋๋ค."
);
}
}
์ฃผ์: Gemini 3 ํธํ์ฑ
ADK Java v0.3.0 ์ดํ๋ ํจ์ ํธ์ถ์ ๋ํ ์ฌ๊ณ ์๋ช ๋ณ๊ฒฝ์ผ๋ก ์ธํด Gemini 3 Pro Preview์ ํธํ๋์ง ์์ต๋๋ค. ๋์ Gemini 2.5 ์ดํ ๋ชจ๋ธ์ ์ฌ์ฉํ์ธ์.
ํ๋ก์ ํธ ๋ฐ ์ข ์์ฑ ๊ตฌ์ฑ¶
ADK ์์ด์ ํธ ํ๋ก์ ํธ๋ pom.xml ํ๋ก์ ํธ ํ์ผ์ ๋ค์ ์ข
์์ฑ์ด ํ์ํฉ๋๋ค.
<dependencies>
<dependency>
<groupId>com.google.adk</groupId>
<artifactId>google-adk</artifactId>
<version>1.12.0</version>
</dependency>
</dependencies>
pom.xml ํ๋ก์ ํธ ํ์ผ์ ์
๋ฐ์ดํธํ์ฌ ์ด ์ข
์์ฑ๊ณผ ์ถ๊ฐ ์ค์ ์ ๋ค์ ๊ตฌ์ฑ ์ฝ๋๋ฅผ ํฌํจํ๋๋ก ํฉ๋๋ค.
ํ๋ก์ ํธ์ ๋ํ ์์ ํ pom.xml ๊ตฌ์ฑ
๋ค์ ์ฝ๋๋ ์ด ํ๋ก์ ํธ์ ๋ํ ์์ ํ 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>
<!-- ์ฌ์ฉํ Java ๋ฒ์ ์ง์ -->
<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>
<!-- ADK ์ฝ์ด ์ข
์์ฑ -->
<dependency>
<groupId>com.google.adk</groupId>
<artifactId>google-adk</artifactId>
<version>1.12.0</version>
</dependency>
<!-- ์์ด์ ํธ๋ฅผ ๋๋ฒ๊ทธํ๊ธฐ ์ํ ADK ๊ฐ๋ฐ ์น UI -->
<dependency>
<groupId>com.google.adk</groupId>
<artifactId>google-adk-dev</artifactId>
<version>1.12.0</version>
</dependency>
</dependencies>
</project>
API ํค ์ค์ ¶
์ด ํ๋ก์ ํธ๋ API ํค๊ฐ ํ์ํ Gemini API๋ฅผ ์ฌ์ฉํฉ๋๋ค. Gemini API ํค๊ฐ ์์ง ์๋ ๊ฒฝ์ฐ Google AI Studio์ API ํค ํ์ด์ง์์ ํค๋ฅผ ์์ฑํฉ๋๋ค.
ํฐ๋ฏธ๋ ์ฐฝ์์ ํ๋ก์ ํธ์ .env ํ์ผ์ API ํค๋ฅผ ์์ฑํ์ฌ ํ๊ฒฝ ๋ณ์๋ฅผ ์ค์ ํฉ๋๋ค.
ADK์์ ๋ค๋ฅธ AI ๋ชจ๋ธ ์ฌ์ฉ
ADK๋ ๋ค์ํ ์์ฑํ AI ๋ชจ๋ธ ์ฌ์ฉ์ ์ง์ํฉ๋๋ค. ADK ์์ด์ ํธ์์ ๋ค๋ฅธ ๋ชจ๋ธ์ ๊ตฌ์ฑํ๋ ๋ฐฉ๋ฒ์ ๋ํ ์์ธํ ๋ด์ฉ์ ๋ชจ๋ธ ๋ฐ ์ธ์ฆ์ ์ฐธ๊ณ ํ์ธ์.
์์ด์ ํธ ๋ช ๋ น์ค ์ธํฐํ์ด์ค ์์ฑ¶
AgentCliRunner.java ํด๋์ค๋ฅผ ์์ฑํ์ฌ ๋ช
๋ น์ค์์ HelloTimeAgent๋ฅผ ์คํํ๊ณ ์ํธ ์์ฉํ ์ ์๋๋ก ํฉ๋๋ค. ์ด ์ฝ๋๋ ์์ด์ ํธ๋ฅผ ์คํํ๊ธฐ ์ํ RunConfig ๊ฐ์ฒด์ ์คํ ์ค์ธ ์์ด์ ํธ์ ์ํธ ์์ฉํ๊ธฐ ์ํ Session ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ์ค๋๋ค.
package com.example.agent;
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;
import static java.nio.charset.StandardCharsets.UTF_8;
public class AgentCliRunner {
public static void main(String[] args) {
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());
}
});
}
}
}
}
์์ด์ ํธ ์คํ¶
์ ์ํ ๋ํํ ๋ช
๋ น์ค ์ธํฐํ์ด์ค AgentCliRunner ํด๋์ค ๋๋ AdkWebServer ํด๋์ค๋ฅผ ์ฌ์ฉํ์ฌ ADK์์ ์ ๊ณตํ๋ ADK ์น ์ฌ์ฉ์ ์ธํฐํ์ด์ค๋ฅผ ์ฌ์ฉํ์ฌ ADK ์์ด์ ํธ๋ฅผ ์คํํ ์ ์์ต๋๋ค. ์ด ๋ ๊ฐ์ง ์ต์
์ ํตํด ์์ด์ ํธ๋ฅผ ํ
์คํธํ๊ณ ์ํธ ์์ฉํ ์ ์์ต๋๋ค.
๋ช ๋ น์ค ์ธํฐํ์ด์ค๋ก ์คํ¶
๋ค์ Maven ๋ช
๋ น์ ์ฌ์ฉํ์ฌ ๋ช
๋ น์ค ์ธํฐํ์ด์ค AgentCliRunner ํด๋์ค๋ก ์์ด์ ํธ๋ฅผ ์คํํฉ๋๋ค.
# ํค ๋ฐ ์ค์ ๋ก๋: source .env ๋๋ env.bat
mvn compile exec:java -Dexec.mainClass="com.example.agent.AgentCliRunner"

์น ์ธํฐํ์ด์ค๋ก ์คํ¶
๋ค์ Maven ๋ช ๋ น์ ์ฌ์ฉํ์ฌ ADK ์น ์ธํฐํ์ด์ค๋ก ์์ด์ ํธ๋ฅผ ์คํํฉ๋๋ค.
# ํค ๋ฐ ์ค์ ๋ก๋: source .env ๋๋ env.bat
mvn compile exec:java \
-Dexec.mainClass="com.google.adk.web.AdkWebServer" \
-Dexec.args="--adk.agents.source-dir=target --server.port=8000"
์ด ๋ช ๋ น์ ์์ด์ ํธ์ฉ ์ฑํ ์ธํฐํ์ด์ค๊ฐ ์๋ ์น ์๋ฒ๋ฅผ ์์ํฉ๋๋ค. ์น ์ธํฐํ์ด์ค๋ (http://localhost:8000)์์ ์ก์ธ์คํ ์ ์์ต๋๋ค. ์ผ์ชฝ ์๋จ ๋ชจ์๋ฆฌ์์ ์์ด์ ํธ๋ฅผ ์ ํํ๊ณ ์์ฒญ์ ์ ๋ ฅํฉ๋๋ค.

์ฃผ์: ADK Web์ ๊ฐ๋ฐ์ฉ์ผ๋ก๋ง ์ฌ์ฉํ์ธ์
ADK Web์ ํ๋ก๋์ ๋ฐฐํฌ์์ ์ฌ์ฉํ๋๋ก ์ค๊ณ๋์ง ์์์ต๋๋ค. ADK Web์ ๊ฐ๋ฐ ๋ฐ ๋๋ฒ๊น ์ฉ๋๋ก๋ง ์ฌ์ฉํ์ธ์.
๋ค์: ์์ด์ ํธ ๋น๋¶
์ด์ ADK๊ฐ ์ค์น๋์๊ณ ์ฒซ ๋ฒ์งธ ์์ด์ ํธ๊ฐ ์คํ ์ค์ด๋ฏ๋ก ์ด์ ๋น๋ ๊ฐ์ด๋๋ฅผ ์ฌ์ฉํ์ฌ ์์ ๋ง์ ์์ด์ ํธ๋ฅผ ๋น๋ํด ๋ณด์ธ์.
```