Skip to content

Commit 7e0362c

Browse files
committed
Add Agent2Agent demo code
1 parent b1c0a73 commit 7e0362c

File tree

4 files changed

+244
-0
lines changed

4 files changed

+244
-0
lines changed

agent2agent-demo/agent1.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from python_a2a import A2AServer, skill, agent, run_server, TaskStatus, TaskState
2+
import math
3+
import re
4+
5+
@agent(
6+
name="Sine Agent",
7+
description="Provides the sine of a number",
8+
version="1.0.0"
9+
)
10+
class SineAgent(A2AServer):
11+
12+
@skill(
13+
name="Get Sine",
14+
description="Get the sine of a number",
15+
tags=["sine", "sin"]
16+
)
17+
def get_sine(self, number):
18+
"""Get the sine of a number."""
19+
# Mock implementation
20+
return f"The sine of {number} is {math.sin(number)}"
21+
22+
def handle_task(self, task):
23+
# Extract location from message
24+
25+
input_message = task.message["content"]["text"]
26+
27+
# regex to extract the number from the text
28+
match = re.search(r"([-+]?[0-9]*\.?[0-9]+)", input_message)
29+
30+
number = float(match.group(1))
31+
print("number", number)
32+
# Get weather and create response
33+
sine_output = self.get_sine(number)
34+
task.artifacts = [{
35+
"parts": [{"type": "text", "text": sine_output}]
36+
}]
37+
task.status = TaskStatus(state=TaskState.COMPLETED)
38+
39+
return task
40+
41+
# Run the server
42+
if __name__ == "__main__":
43+
agent = SineAgent()
44+
run_server(agent, port=4737)

agent2agent-demo/agent2.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from python_a2a import A2AServer, skill, agent, run_server, TaskStatus, TaskState
2+
import math
3+
import re
4+
5+
@agent(
6+
name="Cosine Agent",
7+
description="Provides the cosine of a number",
8+
version="1.0.0"
9+
)
10+
class CosineAgent(A2AServer):
11+
12+
@skill(
13+
name="Get Cos",
14+
description="Get the cosine of a number",
15+
tags=["cos", "cosine"]
16+
)
17+
def get_cosine(self, number):
18+
"""Get the cosine of a number."""
19+
# Mock implementation
20+
return f"The cosine of {number} is {math.cos(number)}"
21+
22+
def handle_task(self, task):
23+
# Extract location from message
24+
25+
input_message = task.message["content"]["text"]
26+
27+
# regex to extract the number from the text
28+
match = re.search(r"([-+]?[0-9]*\.?[0-9]+)", input_message)
29+
30+
number = float(match.group(1))
31+
print("number", number)
32+
# Get weather and create response
33+
cosine_output = self.get_cosine(number)
34+
task.artifacts = [{
35+
"parts": [{"type": "text", "text": cosine_output}]
36+
}]
37+
task.status = TaskStatus(state=TaskState.COMPLETED)
38+
39+
return task
40+
41+
# Run the server
42+
if __name__ == "__main__":
43+
agent = CosineAgent()
44+
run_server(agent, port=4738)

agent2agent-demo/agent3.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from python_a2a import A2AServer, skill, agent, run_server, TaskStatus, TaskState
2+
import math
3+
import re
4+
5+
@agent(
6+
name="Tangent Agent",
7+
description="Provides the tangent of a number",
8+
version="1.0.0"
9+
)
10+
class TangentAgent(A2AServer):
11+
12+
@skill(
13+
name="Get Tangent",
14+
description="Get the tangent of a number",
15+
tags=["tangent", "tan"]
16+
)
17+
def get_tangent(self, number):
18+
"""Get the tangent of a number."""
19+
# Mock implementation
20+
return f"The tangent of {number} is {math.tan(number)}"
21+
22+
def handle_task(self, task):
23+
# Extract location from message
24+
25+
input_message = task.message["content"]["text"]
26+
27+
# regex to extract the number from the text
28+
match = re.search(r"([-+]?[0-9]*\.?[0-9]+)", input_message)
29+
30+
number = float(match.group(1))
31+
print("number", number)
32+
# Get weather and create response
33+
tangent_output = self.get_tangent(number)
34+
task.artifacts = [{
35+
"parts": [{"type": "text", "text": tangent_output}]
36+
}]
37+
task.status = TaskStatus(state=TaskState.COMPLETED)
38+
39+
return task
40+
41+
# Run the server
42+
if __name__ == "__main__":
43+
agent = TangentAgent()
44+
run_server(agent, port=4739)

agent2agent-demo/notebook.ipynb

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"!curl -LsSf https://astral.sh/uv/install.sh | sh\n",
10+
"!uv pip install python-a2a"
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": 1,
16+
"metadata": {},
17+
"outputs": [],
18+
"source": [
19+
"# open a terminal and run: python agent1.py\n",
20+
"# open another terminal and run: python agent2.py\n",
21+
"# open another terminal and run: python agent3.py\n",
22+
"\n",
23+
"# after that, run the following cells"
24+
]
25+
},
26+
{
27+
"cell_type": "code",
28+
"execution_count": null,
29+
"metadata": {},
30+
"outputs": [],
31+
"source": [
32+
"from python_a2a import AgentNetwork, A2AClient, AIAgentRouter\n",
33+
"\n",
34+
"# Create an agent network\n",
35+
"network = AgentNetwork(name=\"Math Assistant Network\")\n",
36+
"\n",
37+
"# Add agents to the network\n",
38+
"network.add(\"Sine\", \"http://localhost:4737\")\n",
39+
"network.add(\"Cosine\", \"http://localhost:4738\")\n",
40+
"network.add(\"Tangent\", \"http://localhost:4739\")\n",
41+
"\n",
42+
"# Create a router to intelligently direct queries to the best agent\n",
43+
"router = AIAgentRouter(\n",
44+
" llm_client=A2AClient(\"http://localhost:5000/openai\"), # LLM for making routing decisions\n",
45+
" agent_network=network\n",
46+
")"
47+
]
48+
},
49+
{
50+
"cell_type": "code",
51+
"execution_count": null,
52+
"metadata": {},
53+
"outputs": [],
54+
"source": [
55+
"# Route a query to the appropriate agent\n",
56+
"query = \"Tan of 0.78545\"\n",
57+
"agent_name, confidence = router.route_query(query)\n",
58+
"print(f\"Routing to {agent_name} with {confidence:.2f} confidence\")\n",
59+
"\n",
60+
"# Get the selected agent and ask the question\n",
61+
"agent = network.get_agent(agent_name)\n",
62+
"response = agent.ask(query)\n",
63+
"print(f\"Response: {response}\")"
64+
]
65+
},
66+
{
67+
"cell_type": "code",
68+
"execution_count": null,
69+
"metadata": {},
70+
"outputs": [],
71+
"source": [
72+
"# Route a query to the appropriate agent\n",
73+
"query = \"Sine of 3.14159\"\n",
74+
"agent_name, confidence = router.route_query(query)\n",
75+
"print(f\"Routing to {agent_name} with {confidence:.2f} confidence\")\n",
76+
"\n",
77+
"# Get the selected agent and ask the question\n",
78+
"agent = network.get_agent(agent_name)\n",
79+
"response = agent.ask(query)\n",
80+
"print(f\"Response: {response}\")"
81+
]
82+
},
83+
{
84+
"cell_type": "code",
85+
"execution_count": null,
86+
"metadata": {},
87+
"outputs": [],
88+
"source": []
89+
}
90+
],
91+
"metadata": {
92+
"kernelspec": {
93+
"display_name": "base",
94+
"language": "python",
95+
"name": "python3"
96+
},
97+
"language_info": {
98+
"codemirror_mode": {
99+
"name": "ipython",
100+
"version": 3
101+
},
102+
"file_extension": ".py",
103+
"mimetype": "text/x-python",
104+
"name": "python",
105+
"nbconvert_exporter": "python",
106+
"pygments_lexer": "ipython3",
107+
"version": "3.12.2"
108+
}
109+
},
110+
"nbformat": 4,
111+
"nbformat_minor": 2
112+
}

0 commit comments

Comments
 (0)