Skip to content

Commit f8b1892

Browse files
committed
feat: add sample agent providing currency conversion to demonstrate multi-turn dialogue and streaming responses.
1 parent 4eee616 commit f8b1892

File tree

3 files changed

+34
-25
lines changed

3 files changed

+34
-25
lines changed

samples/java/agents/currency_exchange_rates/README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# Currency Exchange Rates
22

3-
This sample demonstrates a currency conversion agent exposed through the A2A protocol.
3+
This sample demonstrates a currency conversion agent exposed through the A2A protocol.
44
It showcases conversational interactions with support for multi-turn dialogue and streaming responses.
55
The agent is written using Quarkus LangChain4j and makes use of the [A2A Java](https://github.com/a2aproject/a2a-java) SDK.
66

77
## How It Works
88

9-
This agent uses an Ollama LLM (for example, qwen2.5:7b) to provide currency exchange information.
9+
This agent uses an Ollama LLM (for example, qwen2.5:7b) to provide currency exchange information.
1010
The A2A protocol enables standardized interaction with the agent, allowing clients to send requests and receive real-time updates.
1111

1212
```mermaid
@@ -51,7 +51,7 @@ sequenceDiagram
5151

5252
- Java 17 or higher
5353
- Access to an LLM (Ollama)
54-
- Ollama installed (see https://ollama.com/download)
54+
- Ollama installed (see [Download Ollama](https://ollama.com/download))
5555

5656
## Running the Sample
5757

@@ -60,7 +60,7 @@ which is in the `client` directory.
6060

6161
### Running Ollama
6262

63-
1. Launch the Ollama server by default on port 11434 (http://localhost:11434)
63+
1. Launch the Ollama server by default on port 11434 ([Local Ollama server](http://localhost:11434))
6464

6565
```bash
6666
ollama serve
@@ -72,15 +72,15 @@ which is in the `client` directory.
7272
ollama pull qwen2.5:7b
7373
ollama run qwen2.5:7b
7474
```
75-
75+
7676
### Running the A2A Server Agent
7777

7878
1. Navigate to the `currency_exchange_rates` sample directory:
7979

8080
```bash
8181
cd samples/java/agents/currency_exchange_rates/server
8282
```
83-
83+
8484
2. Start the A2A server agent
8585

8686
**NOTE:**
@@ -90,7 +90,7 @@ which is in the `client` directory.
9090
```bash
9191
mvn quarkus:dev
9292
```
93-
93+
9494
### Running the A2A Java Client
9595

9696
The Java `TestClient` communicates with the Currency Agent using the A2A Java SDK.

samples/java/agents/currency_exchange_rates/client/src/main/java/com/samples/a2a/client/TestClient.java

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -77,32 +77,40 @@ public static void main(final String[] args) {
7777
.build();
7878

7979
// Create and send the message without INPUT_REQUIRED
80-
StreamingClient streamingClient = getStreamingClient(publicAgentCard,
81-
clientConfig);
82-
sendMessage(streamingClient.client(), "how much is 10 USD in INR?",
83-
null, null, streamingClient.messageResponse());
80+
runSingleTurnDemo(publicAgentCard, clientConfig);
8481

8582
// Create and send the message with INPUT_REQUIRED
86-
StreamingClient streamingClient1 = getStreamingClient(publicAgentCard,
87-
clientConfig);
88-
StreamingClient streamingClient2 = getStreamingClient(publicAgentCard,
89-
clientConfig);
90-
91-
MessageResponse message1 = sendMessage(streamingClient1.client(),
92-
"How much is the exchange rate for 1 USD?", null, null,
93-
streamingClient1.messageResponse());
94-
95-
// Resubscribe to a task to send additional information for this task
96-
streamingClient2.client().resubscribe(new TaskIdParams(message1.taskId()),
97-
streamingClient2.consumers(), streamingClient2.streamingErrorHandler());
98-
sendMessage(streamingClient2.client(), "CAD", message1.contextId(),
99-
message1.taskId(), streamingClient2.messageResponse());
83+
runMultiTurnDemo(publicAgentCard, clientConfig);
10084
} catch (Exception e) {
10185
System.err.println("An error occurred: " + e.getMessage());
10286
e.printStackTrace();
10387
}
10488
}
10589

90+
private static void runMultiTurnDemo(AgentCard publicAgentCard, ClientConfig clientConfig) {
91+
StreamingClient streamingClient1 = getStreamingClient(publicAgentCard,
92+
clientConfig);
93+
StreamingClient streamingClient2 = getStreamingClient(publicAgentCard,
94+
clientConfig);
95+
96+
MessageResponse message1 = sendMessage(streamingClient1.client(),
97+
"How much is the exchange rate for 1 USD?", null, null,
98+
streamingClient1.messageResponse());
99+
100+
// Resubscribe to a task to send additional information for this task
101+
streamingClient2.client().resubscribe(new TaskIdParams(message1.taskId()),
102+
streamingClient2.consumers(), streamingClient2.streamingErrorHandler());
103+
sendMessage(streamingClient2.client(), "CAD", message1.contextId(),
104+
message1.taskId(), streamingClient2.messageResponse());
105+
}
106+
107+
private static void runSingleTurnDemo(AgentCard publicAgentCard, ClientConfig clientConfig) {
108+
StreamingClient streamingClient = getStreamingClient(publicAgentCard,
109+
clientConfig);
110+
sendMessage(streamingClient.client(), "how much is 10 USD in INR?",
111+
null, null, streamingClient.messageResponse());
112+
}
113+
106114
private static StreamingClient getStreamingClient(
107115
final AgentCard publicAgentCard, final ClientConfig clientConfig) {
108116
// Create a CompletableFuture to handle async response

samples/java/agents/currency_exchange_rates/server/src/main/java/com/samples/a2a/server/CurrencyAgent.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public interface CurrencyAgent {
5252
3. Never assume or guess the target currency.
5353
4. Never provide an answer until all required currencies are explicitly
5454
stated.
55+
5. You must replace all placeholders with real values.
5556
""")
5657
ResponseFormat handleRequest(@UserMessage String question);
5758
}

0 commit comments

Comments
 (0)