Skip to content

Commit 5ad3c7f

Browse files
codebase/google-adk [BAEL-9565] (#19043)
* build baelgent * rename api path * remove java version property * upgrade ADK version * refactor: use generic class names
1 parent 60a8e1e commit 5ad3c7f

13 files changed

Lines changed: 233 additions & 0 deletions

google-adk/pom.xml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<name>google-adk</name>
8+
<artifactId>google-adk</artifactId>
9+
<version>0.0.1</version>
10+
<packaging>jar</packaging>
11+
12+
<parent>
13+
<groupId>com.baeldung</groupId>
14+
<artifactId>parent-boot-4</artifactId>
15+
<version>0.0.1-SNAPSHOT</version>
16+
<relativePath>../parent-boot-4</relativePath>
17+
</parent>
18+
19+
<dependencyManagement>
20+
<dependencies>
21+
<dependency>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-dependencies</artifactId>
24+
<version>${spring-boot.version}</version>
25+
<type>pom</type>
26+
<scope>import</scope>
27+
</dependency>
28+
</dependencies>
29+
</dependencyManagement>
30+
31+
<dependencies>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-webmvc</artifactId>
35+
</dependency>
36+
<dependency>
37+
<groupId>com.google.adk</groupId>
38+
<artifactId>google-adk</artifactId>
39+
<version>${google-adk.version}</version>
40+
</dependency>
41+
</dependencies>
42+
43+
<build>
44+
<plugins>
45+
<plugin>
46+
<groupId>org.springframework.boot</groupId>
47+
<artifactId>spring-boot-maven-plugin</artifactId>
48+
</plugin>
49+
</plugins>
50+
</build>
51+
52+
<properties>
53+
<google-adk.version>0.5.0</google-adk.version>
54+
</properties>
55+
56+
</project>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.google.adk;
2+
3+
import com.google.adk.agents.BaseAgent;
4+
import com.google.adk.agents.LlmAgent;
5+
import com.google.adk.tools.FunctionTool;
6+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.Configuration;
9+
10+
import java.io.IOException;
11+
import java.nio.charset.Charset;
12+
13+
@Configuration
14+
@EnableConfigurationProperties(AgentProperties.class)
15+
class AgentConfiguration {
16+
17+
@Bean
18+
BaseAgent baseAgent(AgentProperties agentProperties) throws IOException {
19+
return LlmAgent
20+
.builder()
21+
.name(agentProperties.name())
22+
.description(agentProperties.description())
23+
.model(agentProperties.aiModel())
24+
.instruction(agentProperties.systemPrompt().getContentAsString(Charset.defaultCharset()))
25+
.tools(
26+
FunctionTool.create(AuthorFetcher.class, "fetch")
27+
)
28+
.build();
29+
}
30+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.google.adk;
2+
3+
import org.springframework.web.bind.annotation.PostMapping;
4+
import org.springframework.web.bind.annotation.RequestBody;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
@RestController
9+
@RequestMapping("/api/v1/agent/interact")
10+
class AgentController {
11+
12+
private final AgentService agentService;
13+
14+
AgentController(AgentService agentService) {
15+
this.agentService = agentService;
16+
}
17+
18+
@PostMapping
19+
UserResponse interact(@RequestBody UserRequest request) {
20+
return agentService.interact(request);
21+
}
22+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.google.adk;
2+
3+
import org.springframework.boot.context.properties.ConfigurationProperties;
4+
import org.springframework.core.io.Resource;
5+
6+
@ConfigurationProperties(prefix = "com.baeldung.agent")
7+
record AgentProperties(
8+
String name,
9+
String description,
10+
String aiModel,
11+
Resource systemPrompt
12+
) {}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.baeldung.google.adk;
2+
3+
import com.google.adk.agents.BaseAgent;
4+
import com.google.adk.runner.InMemoryRunner;
5+
import com.google.adk.sessions.Session;
6+
import com.google.genai.types.Content;
7+
import com.google.genai.types.Part;
8+
import org.springframework.stereotype.Service;
9+
10+
import java.util.UUID;
11+
import java.util.concurrent.ConcurrentHashMap;
12+
import java.util.concurrent.ConcurrentMap;
13+
14+
@Service
15+
class AgentService {
16+
17+
private final InMemoryRunner runner;
18+
private final ConcurrentMap<String, Session> inMemorySessionCache = new ConcurrentHashMap<>();
19+
20+
AgentService(BaseAgent baseAgent) {
21+
this.runner = new InMemoryRunner(baseAgent);
22+
}
23+
24+
UserResponse interact(UserRequest request) {
25+
UUID userId = request.userId() != null ? request.userId() : UUID.randomUUID();
26+
UUID sessionId = request.sessionId() != null ? request.sessionId() : UUID.randomUUID();
27+
28+
String cacheKey = userId + ":" + sessionId;
29+
Session session = inMemorySessionCache.computeIfAbsent(cacheKey, key ->
30+
runner.sessionService()
31+
.createSession(runner.appName(), userId.toString(), null, sessionId.toString())
32+
.blockingGet()
33+
);
34+
35+
Content userMessage = Content.fromParts(Part.fromText(request.question()));
36+
StringBuilder answerBuilder = new StringBuilder();
37+
runner.runAsync(userId.toString(), session.id(), userMessage)
38+
.blockingForEach(event -> {
39+
String content = event.stringifyContent();
40+
if (content != null && !content.isBlank()) {
41+
answerBuilder.append(content);
42+
}
43+
});
44+
45+
return new UserResponse(userId, sessionId, answerBuilder.toString());
46+
}
47+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.google.adk;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
class Application {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(Application.class, args);
11+
}
12+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.google.adk;
2+
3+
import static com.google.adk.tools.Annotations.Schema;
4+
5+
public class AuthorFetcher {
6+
7+
@Schema(description = "Get author details using an article title")
8+
public static Author fetch(String articleTitle) {
9+
return new Author("John Doe", "john.doe@baeldung.com");
10+
}
11+
12+
record Author(String name, String emailId) {}
13+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.google.adk;
2+
3+
import jakarta.annotation.Nullable;
4+
5+
import java.util.UUID;
6+
7+
record UserRequest(@Nullable UUID userId, @Nullable UUID sessionId, String question) {
8+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.baeldung.google.adk;
2+
3+
import java.util.UUID;
4+
5+
record UserResponse(UUID userId, UUID sessionId, String answer) {
6+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
com:
2+
baeldung:
3+
agent:
4+
name: baelgent
5+
description: Baeldung's AI agent
6+
ai-model: gemini-2.5-flash
7+
system-prompt: classpath:prompts/agent-system-prompt.txt

0 commit comments

Comments
 (0)