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
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<module>contrib/firestore-session-service</module>
<module>tutorials/city-time-weather</module>
<module>tutorials/live-audio-single-agent</module>
<module>tutorials/code-assistant</module>
<module>a2a</module>
</modules>

Expand Down
59 changes: 59 additions & 0 deletions tutorials/code-assistant/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Code Assistant Agent

A tutorial demonstrating how to build an AI code assistant agent using the Google ADK (Agent Development Kit). The agent can help with code review, debugging, generating code snippets, and explaining programming concepts.

## Setup API Key

```shell
export GOOGLE_API_KEY={YOUR-KEY}
```

## Go to example directory

```shell
cd /google_adk/tutorials/code-assistant
```

## Running the Agent

Start the server:

```shell
mvn exec:java -Dadk.agents.source-dir=$PWD
```

This starts the ADK web server with a code assistant agent (`code_assistant`) that can help with various programming tasks using the `gemini-2.0-flash` model.

## Usage

Once running, you can interact with the agent through:
- **Web interface:** `http://localhost:8080`
- **Agent name:** `code_assistant`
- **Try asking:**
- "Write a Java function to reverse a string"
- "Review this code and suggest improvements"
- "Explain what this code does"
- "Help me debug this error"

## Features

This code assistant agent includes several useful tools:

- **Code Generation**: Generate code snippets in various languages
- **Code Review**: Analyze code and suggest improvements
- **Code Explanation**: Explain what code does in simple terms
- **Debugging Help**: Assist with identifying and fixing bugs
- **Best Practices**: Provide guidance on coding best practices

## Agent Capabilities

The agent is designed to:
- Understand programming questions and requests
- Generate syntactically correct code
- Provide explanations in clear, accessible language
- Follow best practices and security guidelines
- Handle multiple programming languages (Java, Python, JavaScript, etc.)

## Learn More

See https://google.github.io/adk-docs/get-started/quickstart/#java for more information.
48 changes: 48 additions & 0 deletions tutorials/code-assistant/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- 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. -->
<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>

<parent>
<groupId>com.google.adk</groupId>
<artifactId>google-adk-parent</artifactId>
<version>1.7.1-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

<artifactId>google-adk-tutorials-code-assistant</artifactId>
<name>Agent Development Kit - Tutorial: Code Assistant</name>
<description>Code Assistant Agent Tutorial</description>

<dependencies>
<dependency>
<groupId>com.google.adk</groupId>
<artifactId>google-adk-dev</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.google.adk.tutorials.CodeAssistant</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* 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 com.google.adk.tutorials;

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 com.google.adk.web.AdkWebServer;
import java.util.Map;

public class CodeAssistant {

public static final BaseAgent ROOT_AGENT =
LlmAgent.builder()
.name("code_assistant")
.model("gemini-2.0-flash")
.description(
"An AI code assistant that helps with code generation, review, debugging, and explanation.")
.instruction(
"You are a helpful code assistant with expertise in multiple programming languages including Java, Python, JavaScript, and more. "
+ "You can help users with:\n"
+ "- Writing and generating code snippets\n"
+ "- Reviewing code and suggesting improvements\n"
+ "- Explaining code functionality\n"
+ "- Debugging and fixing errors\n"
+ "- Providing best practices and security guidance\n\n"
+ "When providing code, ensure it is syntactically correct, well-commented, and follows best practices. "
+ "Always explain your reasoning when suggesting changes or solutions.")
.tools(
FunctionTool.create(CodeAssistant.class, "generateCode"),
FunctionTool.create(CodeAssistant.class, "reviewCode"),
FunctionTool.create(CodeAssistant.class, "explainCode"),
FunctionTool.create(CodeAssistant.class, "debugCode"))
.build();

public static Map<String, String> generateCode(
@Schema(
name = "language",
description =
"The programming language for the code (e.g., Java, Python, JavaScript)")
String language,
@Schema(name = "task", description = "Description of what the code should do") String task) {
return Map.of(
"status",
"success",
"language",
language,
"task",
task,
"message",
"Code generation request received for " + language + " to: " + task);
}

public static Map<String, String> reviewCode(
@Schema(name = "code", description = "The code to review") String code,
@Schema(name = "language", description = "The programming language of the code (optional)")
String language) {
return Map.of(
"status",
"success",
"language",
language != null ? language : "detected",
"message",
"Code review request received. Analyzing code quality, best practices, and potential improvements.");
}

public static Map<String, String> explainCode(
@Schema(name = "code", description = "The code to explain") String code,
@Schema(
name = "detail_level",
description = "Level of detail for explanation (brief, detailed, comprehensive)")
String detailLevel) {
return Map.of(
"status",
"success",
"detail_level",
detailLevel != null ? detailLevel : "detailed",
"message",
"Code explanation request received. Will provide "
+ (detailLevel != null ? detailLevel : "detailed")
+ " explanation of the code.");
}

public static Map<String, String> debugCode(
@Schema(name = "code", description = "The code with the bug") String code,
@Schema(name = "error_message", description = "The error message or description of the issue")
String errorMessage) {
return Map.of(
"status",
"success",
"error_message",
errorMessage,
"message",
"Debugging request received. Analyzing code to identify and fix the issue: "
+ errorMessage);
}

public static void main(String[] args) {
AdkWebServer.start(ROOT_AGENT);
}
}