-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataflow.py
50 lines (38 loc) · 1.5 KB
/
dataflow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import argparse
import asyncio
from coagent.agents import ChatAgent, RunContext, submit, tool
from coagent.core import AgentSpec, idle_loop, new, set_stderr_logger
from coagent.runtimes import NATSRuntime
import httpx
from pydantic import Field
class DataFlow(ChatAgent):
"""An agent that help users deal with tasks related to datasets."""
system = "You are an agent that help users deal with tasks related to datasets."
@tool
@submit()
async def search_dataset(
self, ctx: RunContext, name: str = Field(description="The name of the dataset")
) -> str:
"""Search datasets by the given name."""
async with httpx.AsyncClient() as client:
resp = await client.get(
"https://hub.opencsg.com/api/v1/datasets",
params=dict(search=name),
)
if resp.status_code != 200:
return f"Error: {resp.text}"
datasets = resp.json()["data"]
if not datasets:
return "Sorry, no datasets found."
return ", ".join([d["name"] for d in datasets])
async def main(name: str):
dataflow = AgentSpec(name, new(DataFlow))
async with NATSRuntime.from_servers() as runtime:
await runtime.register(dataflow)
await idle_loop()
if __name__ == "__main__":
set_stderr_logger("TRACE")
parser = argparse.ArgumentParser()
parser.add_argument("--name", type=str, default="dataflow")
args = parser.parse_args()
asyncio.run(main(args.name))