Skip to content

Commit 11adcfd

Browse files
committed
add dify agent in examples
1 parent e696c8d commit 11adcfd

File tree

9 files changed

+2490
-0
lines changed

9 files changed

+2490
-0
lines changed

examples/dify/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Dify Integration Example
2+
3+
This example demonstrates an A2A agent that integrates with the Dify API.
4+
5+
## Features
6+
7+
- Makes HTTP POST requests to the Dify workflow API endpoint
8+
- Demonstrates how to wrap an external API in an A2A agent
9+
- Includes both regular and streaming response handling
10+
11+
## Getting started
12+
13+
1. Start the server
14+
15+
```bash
16+
python __main__.py
17+
```
18+
19+
2. Run the test client in another terminal
20+
21+
```bash
22+
python test_client.py
23+
```
24+
25+
## API Details
26+
27+
The agent makes requests to the following Dify endpoint:
28+
29+
- URL: `http://192.168.8.41:8080/v1/workflows/run`
30+
- Method: POST
31+
- Headers:
32+
- Authorization: Bearer app-wd1WTcAnHPLqRjTAm4QmswI9
33+
- Content-Type: application/json
34+
- Request Body:
35+
```json
36+
{
37+
"inputs": {},
38+
"response_mode": "blocking",
39+
40+
}
41+
```

examples/dify/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

examples/dify/__main__.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from a2a.server.apps import A2AStarletteApplication
2+
from a2a.server.request_handlers import DefaultRequestHandler
3+
from a2a.server.tasks import InMemoryTaskStore
4+
from a2a.types import (
5+
AgentAuthentication,
6+
AgentCapabilities,
7+
AgentCard,
8+
AgentSkill,
9+
)
10+
11+
from agent_executor import DifyAgentExecutor
12+
13+
if __name__ == "__main__":
14+
skill = AgentSkill(
15+
id="dify_api_call",
16+
name="Call Dify API",
17+
description="Makes a request to the Dify API endpoint",
18+
tags=["dify", "api", "workflow"],
19+
examples=["call dify", "run workflow"],
20+
)
21+
22+
agent_card = AgentCard(
23+
name="Dify Agent",
24+
description="Agent that wraps Dify API",
25+
url="http://localhost:8888/",
26+
version="1.0.0",
27+
defaultInputModes=["text/plain", "application/json"],
28+
defaultOutputModes=["text/plain", "application/json"],
29+
capabilities=AgentCapabilities(streaming=True),
30+
skills=[skill],
31+
authentication=AgentAuthentication(schemes=["public"]),
32+
)
33+
34+
request_handler = DefaultRequestHandler(
35+
agent_executor=DifyAgentExecutor(),
36+
task_store=InMemoryTaskStore(),
37+
)
38+
39+
server = A2AStarletteApplication(
40+
agent_card=agent_card, http_handler=request_handler
41+
)
42+
import uvicorn
43+
44+
uvicorn.run(server.build(), host="0.0.0.0", port=8888)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# import httpx
2+
3+
# from typing_extensions import override
4+
5+
# from a2a.server.agent_execution import AgentExecutor, RequestContext
6+
# from a2a.server.events import EventQueue
7+
# from a2a.utils import new_agent_text_message
8+
9+
10+
# class DifyAgent:
11+
# """Dify Agent wrapper."""
12+
13+
# async def invoke(self, city: str, response_mode: str = 'blocking') -> str:
14+
# """Make HTTP POST request to Dify API in blocking mode."""
15+
# url = 'https://api.dify.ai/v1/workflows/run'
16+
# headers = {
17+
# 'Authorization': 'Bearer app-SXqTiCm89mGr2IJhKvwWgR7s',
18+
# 'Content-Type': 'application/json',
19+
# }
20+
# payload = {
21+
# 'inputs': {'city': city},
22+
# 'response_mode': response_mode,
23+
# 'user': '[email protected]',
24+
# }
25+
26+
# async with httpx.AsyncClient(timeout=None) as client:
27+
# try:
28+
# response = await client.post(url, json=payload, headers=headers)
29+
# response.raise_for_status()
30+
# return f"Dify API Response: {response.json()}"
31+
# except httpx.HTTPError as e:
32+
# return f"Error calling Dify API: {str(e)}"
33+
34+
35+
# class DifyAgentExecutor(AgentExecutor):
36+
# """Dify Agent Executor Implementation."""
37+
38+
# def __init__(self):
39+
# self.agent = DifyAgent()
40+
41+
# @override
42+
# async def execute(
43+
# self,
44+
# context: RequestContext,
45+
# event_queue: EventQueue,
46+
# ) -> None:
47+
# city = None
48+
49+
# # Extract city from DataPart if available
50+
# for part in context.message.parts:
51+
# data = part.root.data
52+
# city = data['city']
53+
54+
# print(city)
55+
# result = await self.agent.invoke(city, 'blocking')
56+
# event_queue.enqueue_event(new_agent_text_message(result))
57+
58+
# @override
59+
# async def cancel(
60+
# self, context: RequestContext, event_queue: EventQueue
61+
# ) -> None:
62+
# raise Exception('cancel not supported')

0 commit comments

Comments
 (0)