Skip to content

Commit 897e655

Browse files
committed
a2a support
1 parent 25303f6 commit 897e655

File tree

66 files changed

+470
-174
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+470
-174
lines changed

README.md

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,56 @@
1-
<div align="center">
2-
<a href="https://www.linkedin.com/posts/vishalrow_ai-appdevelopment-actions-activity-7171302152101900288-64qg?utm_source=share&utm_medium=member_desktop">
3-
<img src="tools4ai.png" width="300" height="300">
4-
</a>
5-
</div>
6-
<p align="center">
7-
<img src="https://api.visitorbadge.io/api/visitors?path=https%3A%2F%2Fgithub.com%2Fvishalmysore%2Ftools4ai&countColor=black&style=flat%22">
8-
<a target="_blank" href="https://github.com/vishalmyore/tools4ai"><img src="https://img.shields.io/github/stars/vishalmysore/tools4ai?color=black" /></a>
9-
<a target="_blank" href="https://github.com/vishalmysore/sam/actions/workflows/maven.yml"><img src="https://github.com/vishalmysore/sam/actions/workflows/maven.yml/badge.svg" /></a>
10-
</p>
11-
12-
# 🎬 Simple Action Model - SAM
13-
14-
SAM is a reference implementation of Tool4AI project https://github.com/vishalmysore/Tools4AI
15-
Basically showcasing how straight forward it is to build Agent AI applications in 100% Java. In addition to action model
16-
SAM can be used as an autonomous agent by utilizing Agentic AI scripts which are a specialized form of these intelligent systems, designed specifically for enterprise AI
17-
applications. While retaining the core capabilities of autonomy and adaptability, scripts can operates within
18-
a controlled framework, executing tasks and making decisions that align with predefined business rules and
19-
objectives
20-
21-
## Features and Articles
1+
# 🎬 Google A2A Protocol - Java Examples
2+
3+
If you're looking to build Java applications using the Google A2A (Agent-to-Agent) protocol, this repository provides numerous reference examples to help you get started.
4+
To get started you can look at A2AJavaAgent class, which shows how to define and trigger Google a2a tasks using natural language prompts.
5+
6+
This framework supports:
7+
8+
Complex Java types: Automatically convert between structured prompts and custom POJOs, including nested objects and lists.
9+
10+
Spring Boot integration: Seamlessly plug into your existing Spring application context.
11+
12+
Minimal setup: Use simple annotations like @Agent, and @Action to expose your service classes and methods as A2A-compatible agents.
13+
14+
```
15+
A2AJavaAgent
16+
```
17+
18+
if you run it with this prompt
19+
20+
```
21+
String prompt
22+
= "hey I am in Toronto do you think i can go out without jacket";
23+
24+
// Get the client from Spring context
25+
LocalA2ATaskClient client = context.getBean(LocalA2ATaskClient.class);
26+
27+
// Send task and log response
28+
Task t = client.sendTask(prompt);
29+
30+
log.info(client.getTask(t.getId(),2).toString());
31+
```
32+
33+
you will see in the result that ``` WeatherAction``` is triggerd
34+
35+
similarly
36+
37+
```
38+
prompt ="Sachin Tendulkar is very good cricket player, " +
39+
"he joined the sports on 24032022, he has played 300 matches " +
40+
"and his max score is 400. Can you send him a congratulations email
41+
```
42+
43+
will trigger
44+
45+
```
46+
@Agent(groupName = "player", groupDescription = "Player and sports related action")
47+
public class NotifyPlayerAction {
48+
```
49+
50+
51+
To know more about how to build agentic java applications please continue reading the below articles .
52+
53+
# Features and Articles on Agentic Java applications
2254
- **AI Agent Processor**: Execute actions based on prompt (OpenAI, Gemini, Anthropic) [here](https://www.linkedin.com/pulse/large-action-model-gemini-java-vishal-mysore-qki8c?trackingId=MuqKH2YZNwe74wisqhMSuw%3D%3D&lipi=urn%3Ali%3Apage%3Ad_flagship3_profile_view_base_recent_activity_content_view%3Basc8boqLRvqWpXTf9SUEpA%3D%3D)
2355
- **Image Processor**: Trigger actions based on images [here](https://www.linkedin.com/pulse/image-recognition-function-calling-gemini-java-vishal-mysore-sz5zc?trackingId=lpJITsmYD0XPgdaG676jmA%3D%3D&lipi=urn%3Ali%3Apage%3Ad_flagship3_profile_view_base_recent_activity_content_view%3Basc8boqLRvqWpXTf9SUEpA%3D%3D)
2456
- **Autonomous AI Agent**: Execute tasks based on scripts [here](https://www.linkedin.com/pulse/enterprise-ai-hub-llm-agent-built-openai-java-vishal-mysore-0p7oc?trackingId=qE91gQ%2Bngtn4vI45pxJEgg%3D%3D&lipi=urn%3Ali%3Apage%3Ad_flagship3_profile_view_base_recent_activity_content_view%3Basc8boqLRvqWpXTf9SUEpA%3D%3D)
@@ -65,7 +97,7 @@ and will be executed.
6597
```
6698
String cookPromptSingleText = "My friends name is Vishal ," +
6799
"I dont know what to cook for him today.";
68-
GeminiActionProcessor processor = new GeminiActionProcessor();
100+
GeminiV2ActionProcessor processor = new GeminiV2ActionProcessor();
69101
String result = (String)processor.processSingleAction(cookPromptSingleText);
70102
log.info(result);
71103
```
@@ -83,7 +115,7 @@ Create custom action by using @Predict annotation and @Action annotation. Parame
83115
anything and any number of parameters are allowed You need to make sure parameters have meaningful name.
84116

85117
```
86-
@Predict
118+
@Agent
87119
public class SimpleAction {
88120
89121
@Action(description = "Provide persons name and then find out what does that person like")
@@ -101,7 +133,7 @@ public class SimpleAction {
101133
or
102134
```
103135
@Log
104-
@Predict
136+
@Agent
105137
public class SearchAction {
106138
107139
@Action(description = "Search the web for information")

a2a.png

54.5 KB
Loading

article.md

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
# Google A2A Protocol : Integrating AI into existing Java Applications
2+
3+
> Transform your existing Java applications into AI-powered solutions without the need for a separate server infrastructure.
4+
5+
## Table of Contents
6+
- [Introduction](#introduction)
7+
- [Prerequisites](#prerequisites)
8+
- [Agentic Actions](#agentic-actions)
9+
- [Weather Service](#weather-service)
10+
- [Google Search](#google-search)
11+
- [Notification System](#notification-system)
12+
- [File Operations](#file-operations)
13+
- [Implementation Guide](#implementation-guide)
14+
- [Examples](#examples)
15+
- [Best Practices](#best-practices)
16+
17+
## Introduction
18+
19+
This guide demonstrates how to leverage the Google A2A (Agent-to-Agent) protocol to infuse AI capabilities into your existing Java applications. The A2A protocol enables seamless integration of AI functionalities without requiring a separate server infrastructure, making it an ideal solution for enhancing your business applications with AI features.
20+
21+
## Prerequisites
22+
23+
Before you begin, ensure you have:
24+
- Java 8 or higher
25+
- Maven or Gradle for dependency management
26+
- Basic understanding of Spring Framework
27+
- A2A library dependencies in your project
28+
29+
## Agentic Actions
30+
31+
The power of this solution lies in its ability to create combine A2A protocol with "agentic actions" - self-contained units of business logic that can be invoked through natural language processing. Let's explore some key actions that demonstrate this capability.
32+
33+
### Weather Service
34+
35+
The `WeatherAction` class demonstrates how to integrate weather data services into your application:
36+
37+
```java
38+
@Log
39+
@Agent(groupName = "Weather related actions")
40+
public class WeatherAction {
41+
@Action(description = "get weather for city")
42+
public double getTemperature(String cityName) {
43+
double temperature = 0;
44+
String urlStr = "https://geocoding-api.open-meteo.com/v1/search?name="+cityName+"&count=1&language=en&format=json";
45+
String weatherURlStr = "https://api.open-meteo.com/v1/forecast?latitude=";
46+
```
47+
48+
This action integrates with the Open-Meteo geocoding API to fetch real-time weather information for any given city. The `@Agent` and `@Action` annotations make this method accessible through natural language queries via Google A2A protocol.
49+
50+
### Google Search Integration
51+
```java
52+
@Agent(groupName = "GoogleSearch", groupDescription = "Google Search")
53+
public class GoogleSearchAction {
54+
55+
@Action(description = "search the web for information")
56+
public String googleSearch(String searchString, boolean isNews) {
57+
//Just return random string if you do not have serper key
58+
//to get serper key look here https://serper.dev/
59+
if(PredictionLoader.getInstance().getSerperKey() == null) {
60+
return "jamun , jalebi";
61+
}
62+
```
63+
This action demonstrates integration with web search capabilities. While this example uses a simplified approach, you can enhance it by integrating with the Serper API for production use.
64+
65+
### Notification System
66+
67+
The notification system demonstrates how to send automated communications based on natural language triggers. For example, sending congratulatory emails to players based on their achievements.
68+
69+
### File Operations
70+
71+
The `FileWriteAction` class handles file system operations, showcasing how traditional I/O operations can be triggered through natural language commands.
72+
73+
## Making Your Application AI-Ready
74+
75+
The A2A protocol can be applied to any business-centric application. For example, consider a banking application:
76+
77+
```
78+
public AccountDetails getAccountDetails(AccountInformation info) {
79+
80+
}
81+
```
82+
83+
By simply adding the `@Agent` and `@Action` annotations, you can transform regular business logic into AI-accessible endpoints. The A2A protocol handles the natural language processing and routing automatically.
84+
85+
## Implementation Examples
86+
87+
Let's explore some practical examples of how natural language prompts trigger different actions through the A2A protocol:
88+
89+
### 1. Weather Service Integration
90+
91+
```java
92+
// Initialize the A2A client
93+
LocalA2ATaskClient client = context.getBean(LocalA2ATaskClient.class);
94+
95+
// Natural language query for weather information
96+
String weatherQuery = "Hey, I am in Toronto. Do you think I need a jacket today?";
97+
Task weatherTask = client.sendTask(weatherQuery);
98+
99+
// The A2A protocol automatically:
100+
// 1. Identifies this as a weather-related query
101+
// 2. Extracts the city name (Toronto)
102+
// 3. Calls WeatherAction.getTemperature()
103+
// 4. Formats a natural language response
104+
log.info(client.getTask(weatherTask.getId(), 2).toString());
105+
```
106+
107+
### 2. Automated Notifications
108+
109+
```java
110+
// Natural language trigger for sending congratulations
111+
String notificationPrompt =
112+
"Sachin Tendulkar is a cricket player who joined on 24/03/2022. " +
113+
"He has played 300 matches with a highest score of 400. " +
114+
"Please send him a congratulatory email.";
115+
116+
Task notificationTask = client.sendTask(notificationPrompt);
117+
118+
// The system automatically:
119+
// 1. Extracts player information
120+
// 2. Generates appropriate congratulatory message
121+
// 3. Sends the email through NotifyPlayerAction
122+
log.info(client.getTask(notificationTask.getId(), 2).toString());
123+
```
124+
125+
### 3. Restaurant Booking Integration
126+
127+
```java
128+
// Natural language restaurant booking request
129+
String bookingPrompt = "Book a table at Maharaja restaurant in " +
130+
"Toronto for Sachin Tendulkar and 4 people on May 12th";
131+
Task bookingTask = client.sendTask(bookingPrompt);
132+
133+
// The system processes:
134+
// 1. Restaurant details
135+
// 2. Number of guests
136+
// 3. Date and time
137+
// 4. Customer information
138+
```
139+
140+
## Best Practices
141+
142+
1. **Action Design**
143+
- Keep actions focused and single-purpose
144+
- Use clear, descriptive names for actions
145+
- Include comprehensive documentation in `@Action` descriptions
146+
147+
2. **Error Handling**
148+
- Implement proper exception handling
149+
- Provide meaningful error messages
150+
- Include fallback mechanisms
151+
152+
3. **Testing**
153+
- Write unit tests for individual actions
154+
- Include integration tests for A2A flows
155+
- Test with various natural language inputs
156+
157+
## Conclusion
158+
159+
The Google A2A protocol provides a powerful way to add AI capabilities to existing Java applications. By following this guide, you can:
160+
161+
- Transform regular methods into AI-accessible endpoints
162+
- Process natural language commands effectively
163+
- Integrate multiple services seamlessly
164+
- Maintain clean and maintainable code
165+
166+
The examples demonstrated here are just the beginning. The protocol's flexibility allows for integration with any business logic, making it a valuable tool for modernizing existing applications with AI capabilities.
167+
168+
## Next Steps
169+
170+
- Explore more complex action combinations
171+
- Implement custom action handlers
172+
- Integrate with additional APIs and services
173+
- Contribute to the A2A community
174+
175+
For more information and updates, visit the [official documentation](https://github.com/google/a2a) and join the community discussions.
176+
177+
178+

pom.xml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,16 @@
1919
<artifactId>gson</artifactId>
2020
<version>2.8.9</version> <!-- or latest version -->
2121
</dependency>
22+
<dependency>
23+
<groupId>io.github.vishalmysore</groupId>
24+
<artifactId>a2ajava</artifactId>
25+
<version>0.0.7.3</version>
26+
</dependency>
27+
2228
<dependency>
2329
<groupId>io.github.vishalmysore</groupId>
2430
<artifactId>tools4ai</artifactId>
25-
<version>1.0.4</version>
31+
<version>1.0.7.5</version>
2632
</dependency>
2733
<dependency>
2834
<groupId>ch.qos.logback</groupId>
@@ -74,7 +80,7 @@
7480
</executions>
7581
<configuration>
7682
<!-- Specify the main class of your Java program -->
77-
<mainClass>org.example.GeminiActionExample</mainClass>
83+
<mainClass>io.github.vishalmysore.example.noprotocol.GeminiActionExample</mainClass>
7884
<!-- Pass arguments to your Java program -->
7985
</configuration>
8086
</plugin>
@@ -87,7 +93,7 @@
8793
</descriptorRefs>
8894
<archive>
8995
<manifest>
90-
<mainClass>org.example.GeminiActionExample</mainClass>
96+
<mainClass>io.github.vishalmysore.example.noprotocol.GeminiActionExample</mainClass>
9197
</manifest>
9298
</archive>
9399
</configuration>

src/main/java/org/example/actions/AddRestaurantAction.java renamed to src/main/java/io/github/vishalmysore/actions/AddRestaurantAction.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
package org.example.actions;
1+
package io.github.vishalmysore.actions;
22

33
import com.t4a.annotations.Action;
4-
import com.t4a.annotations.Predict;
5-
import com.t4a.api.JavaMethodAction;
4+
import com.t4a.annotations.Agent;
5+
66

77
import lombok.extern.java.Log;
8-
import org.example.pojo.RestaurantPojo;
8+
import io.github.vishalmysore.pojo.RestaurantPojo;
99

1010
@Log
11-
@Predict
11+
@Agent (groupName = "restaurant", groupDescription = "Restaurant Agent for getting details etc")
1212
public class AddRestaurantAction {
1313
@Action
1414
public String addRestaurantDetails(RestaurantPojo pojo){

src/main/java/org/example/actions/CreateJiraAction.java renamed to src/main/java/io/github/vishalmysore/actions/CreateJiraAction.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
package org.example.actions;
1+
package io.github.vishalmysore.actions;
22

33
import com.t4a.annotations.Action;
4-
import com.t4a.annotations.Predict;
4+
import com.t4a.annotations.Agent;
5+
56
import lombok.extern.java.Log;
67

78
@Log
8-
@Predict( groupName = "Jira actions", groupDescription = "all actions related to jira")
9+
@Agent( groupName = "Jira actions", groupDescription = "all actions related to jira")
910
public class CreateJiraAction {
1011
@Action
1112
public String createJira(String reasonForCreatingJira) {

src/main/java/org/example/actions/FileWriteAction.java renamed to src/main/java/io/github/vishalmysore/actions/FileWriteAction.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
package org.example.actions;
2-
import com.t4a.annotations.Action;
3-
import com.t4a.annotations.Predict;
4-
import com.t4a.api.JavaMethodAction;
1+
package io.github.vishalmysore.actions;
52

3+
import com.t4a.annotations.Action;
4+
import com.t4a.annotations.Agent;
65
import lombok.extern.java.Log;
76

87
import java.io.File;
98
import java.io.FileWriter;
109
import java.io.IOException;
10+
1111
@Log
12-
@Predict
12+
@Agent(groupName = "FileWriteAgent", groupDescription = "FileWriteAgent")
1313
public class FileWriteAction {
1414

1515
@Action(description = "saves the information in local file")

src/main/java/org/example/actions/GoogleSearchAction.java renamed to src/main/java/io/github/vishalmysore/actions/GoogleSearchAction.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
package org.example.actions;
1+
package io.github.vishalmysore.actions;
22

33
import com.t4a.annotations.Action;
4-
import com.t4a.annotations.Predict;
4+
import com.t4a.annotations.Agent;
5+
56
import com.t4a.api.JavaMethodAction;
67

78
import com.t4a.predict.PredictionLoader;
@@ -10,7 +11,7 @@
1011
import lombok.extern.java.Log;
1112

1213
@Log
13-
@Predict
14+
@Agent(groupName = "GoogleSearch", groupDescription = "Google Search")
1415
public class GoogleSearchAction {
1516

1617
@Action(description = "search the web for information")

0 commit comments

Comments
 (0)