generated from google/new-project
-
Notifications
You must be signed in to change notification settings - Fork 549
feat: add exchange rates currency sample #434
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cmtoan
wants to merge
3
commits into
a2aproject:main
Choose a base branch
from
cmtoan:feat/exchange_rates_currency
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| # Currency Exchange Rates | ||
|
|
||
| This sample demonstrates a currency conversion agent exposed through the A2A protocol. | ||
| It showcases conversational interactions with support for multi-turn dialogue and streaming responses. | ||
| The agent is written using Quarkus LangChain4j and makes use of the [A2A Java](https://github.com/a2aproject/a2a-java) SDK. | ||
|
|
||
| ## How It Works | ||
|
|
||
| This agent uses an Ollama LLM (for example, qwen2.5:7b) to provide currency exchange information. | ||
| The A2A protocol enables standardized interaction with the agent, allowing clients to send requests and receive real-time updates. | ||
|
|
||
| ```mermaid | ||
| sequenceDiagram | ||
| participant Client as A2A Client | ||
| participant Server as A2A Server | ||
| participant API as Frankfurter API | ||
|
|
||
| Client->>Server: Send task with currency query | ||
|
|
||
| alt Complete Information | ||
| Server->>API: Call get_exchange_rate tool | ||
| API->>Server: Return exchange rate data | ||
| Server->>Server: Process data & return result | ||
| Server->>Client: Respond with currency information | ||
| else Incomplete Information | ||
| Server->>Server: Request additional input | ||
| Server->>Client: Set state to "input-required" | ||
| Client->>Server: Send additional information | ||
| Server->>API: Call get_exchange_rate tool | ||
| API->>Server: Return exchange rate data | ||
| Server->>Server: Process data & return result | ||
| Server->>Client: Respond with currency information | ||
| end | ||
|
|
||
| alt With Streaming | ||
| Note over Client,Server: Real-time status updates | ||
| Server->>Client: "Looking up exchange rates..." | ||
| Server->>Client: "Processing exchange rates..." | ||
| Server->>Client: Final result | ||
| end | ||
| ``` | ||
|
|
||
| ## Key Features | ||
|
|
||
| - **Multi-turn Conversations**: Agent can request additional information when needed | ||
| - **Real-time Streaming**: Provides status updates during processing | ||
| - **Conversational Memory**: Maintains context across interactions | ||
| - **Currency Exchange Tool**: Integrates with Frankfurter API for real-time rates | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - Java 17 or higher | ||
| - Access to an LLM (Ollama) | ||
| - Ollama installed (see [Download Ollama](https://ollama.com/download)) | ||
|
|
||
| ## Running the Sample | ||
|
|
||
| This sample consists of an A2A server agent, which is in the `server` directory, and an A2A client, | ||
| which is in the `client` directory. | ||
|
|
||
| ### Running Ollama | ||
|
|
||
| 1. Launch the Ollama server by default on port 11434 ([Local Ollama server](http://localhost:11434)) | ||
|
|
||
| ```bash | ||
| ollama serve | ||
| ``` | ||
|
|
||
| 2. In this example, we use the model qwen2.5:7b. Download this model and run it | ||
|
|
||
| ```bash | ||
| ollama pull qwen2.5:7b | ||
| ollama run qwen2.5:7b | ||
| ``` | ||
|
|
||
| ### Running the A2A Server Agent | ||
|
|
||
| 1. Navigate to the `currency_exchange_rates` sample directory: | ||
|
|
||
| ```bash | ||
| cd samples/java/agents/currency_exchange_rates/server | ||
| ``` | ||
|
|
||
| 2. Start the A2A server agent | ||
|
|
||
| **NOTE:** | ||
| By default, the agent will start on port 10000. To override this, add the `-Dquarkus.http.port=YOUR_PORT` | ||
| option at the end of the command below. | ||
|
|
||
| ```bash | ||
| mvn quarkus:dev | ||
| ``` | ||
|
|
||
| ### Running the A2A Java Client | ||
|
|
||
| The Java `TestClient` communicates with the Currency Agent using the A2A Java SDK. | ||
|
|
||
| Since the A2A server agent's [transport](samples/java/agents/currency_exchange_rates/server/pom.xml) is JsonRpc and since our client | ||
| also [supports](client/src/main/java/com/samples/a2a/TestClient.java) gRPC and JsonRpc, the JsonRpc transport will be used. | ||
|
|
||
| 1. Make sure you have [JBang installed](https://www.jbang.dev/documentation/guide/latest/installation.html) | ||
|
|
||
| 2. Run the client using the JBang script: | ||
|
|
||
| ```bash | ||
| cd samples/java/agents/currency_exchange_rates/client/src/main/java/com/samples/a2a/client | ||
| jbang TestClientRunner.java | ||
| ``` | ||
| ## Expected Client Output | ||
|
|
||
| The Java A2A client will: | ||
| 1. Connect to the currency agent | ||
| 2. Fetch the agent card | ||
| 3. Automatically select JsonRpc as the transport to be used | ||
| 4. Send the message "how much is 10 USD in INR?" | ||
| 5. Display the exchange rate result from the agent | ||
| 6. Send the message "How much is the exchange rate for 1 USD?" | ||
| 7. Receive the request for additional input "Please specify the currency you want to convert USD to" | ||
| 8. Send additional information "CAD" | ||
| 9. Display the exchange rate result from the agent | ||
|
|
||
| ## Learn More | ||
|
|
||
| - [A2A Protocol Documentation](https://a2a-protocol.org/) | ||
| - [Frankfurter API](https://www.frankfurter.app/docs/) | ||
| - [Ollama](https://docs.ollama.com/) | ||
|
|
||
| ## Disclaimer | ||
| Important: The sample code provided is for demonstration purposes and illustrates the | ||
| mechanics of the Agent-to-Agent (A2A) protocol. When building production applications, | ||
| it is critical to treat any agent operating outside of your direct control as a | ||
| potentially untrusted entity. | ||
|
|
||
| All data received from an external agent—including but not limited to its AgentCard, | ||
| messages, artifacts, and task statuses—should be handled as untrusted input. For | ||
| example, a malicious agent could provide an AgentCard containing crafted data in its | ||
| fields (e.g., description, name, skills.description). If this data is used without | ||
| sanitization to construct prompts for a Large Language Model (LLM), it could expose | ||
| your application to prompt injection attacks. Failure to properly validate and | ||
| sanitize this data before use can introduce security vulnerabilities into your | ||
| application. | ||
|
|
||
| Developers are responsible for implementing appropriate security measures, such as | ||
| input validation and secure handling of credentials to protect their systems and users. |
54 changes: 54 additions & 0 deletions
54
samples/java/agents/currency_exchange_rates/client/pom.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| <?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> | ||
|
|
||
| <parent> | ||
| <groupId>com.samples.a2a</groupId> | ||
| <artifactId>currency_exchange_rates</artifactId> | ||
| <version>0.1.0</version> | ||
| </parent> | ||
|
|
||
| <artifactId>currency-agent-client</artifactId> | ||
| <name>Currency Agent Client</name> | ||
| <description>A2A Currency Agent Test Client</description> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>io.github.a2asdk</groupId> | ||
| <artifactId>a2a-java-sdk-client</artifactId> | ||
| <version>${io.a2a.sdk.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>io.github.a2asdk</groupId> | ||
| <artifactId>a2a-java-sdk-client-transport-grpc</artifactId> | ||
| <version>${io.a2a.sdk.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.fasterxml.jackson.core</groupId> | ||
| <artifactId>jackson-databind</artifactId> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>io.grpc</groupId> | ||
| <artifactId>grpc-netty-shaded</artifactId> | ||
| <scope>runtime</scope> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-compiler-plugin</artifactId> | ||
| </plugin> | ||
| <plugin> | ||
| <groupId>org.codehaus.mojo</groupId> | ||
| <artifactId>exec-maven-plugin</artifactId> | ||
| <configuration> | ||
| <mainClass>com.samples.a2a.TestClient</mainClass> | ||
| </configuration> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| </project> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The description is good, but it could be slightly more aligned with the
Expected Client Outputsection of the agent's own README, which showcases two distinct scenarios: a direct conversion and a multi-turn conversation. This would give users a clearer picture of what the sample demonstrates.