MCPcopy Create free account
hub / github.com/THUDM/AgentBench / TaskClient

Class TaskClient

src/client/task.py:18–153  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

16
17
18class TaskClient:
19 def __init__(
20 self, name: str, controller_address: str = "http://localhost:5000/api", *_, **__,
21 ) -> None:
22 self.name = name
23 self.controller_address = controller_address
24 print("TaskClient created: {} ({})".format(name, controller_address))
25
26 def get_indices(self) -> List[SampleIndex]:
27 result = requests.get(
28 self.controller_address + "/get_indices", params={"name": self.name}
29 )
30 if result.status_code != 200:
31 raise AgentBenchException(result.text, result.status_code, self.name)
32 return result.json()
33
34 def get_concurrency(self) -> int:
35 try:
36 result = requests.get(
37 self.controller_address + "/list_workers"
38 )
39 except Exception as e:
40 print(ColorMessage.yellow(f"Warning task {self.name} cannot connect to controller {e}"))
41 return 0
42 if result.status_code != 200:
43 raise AgentBenchException(result.text, result.status_code, self.name)
44 result = result.json()
45 if self.name not in result:
46 print(ColorMessage.yellow(f"task {self.name} not found in worker list"))
47 return 0
48 concurrency = 0
49 for worker in result[self.name]["workers"].values():
50 if worker["status"] == WorkerStatus.ALIVE:
51 concurrency += worker["capacity"] - worker["current"]
52 return concurrency
53
54 def run_sample(self, index: SampleIndex, agent: AgentClient) -> TaskClientOutput:
55 try:
56 result = requests.post(
57 self.controller_address + "/start_sample",
58 json=StartSampleRequest(name=self.name, index=index).dict(),
59 )
60 except Exception as e:
61 return TaskClientOutput(error=TaskError.NETWORK_ERROR.value, info=str(e))
62 if result.status_code == 406:
63 return TaskClientOutput(
64 error=TaskError.NOT_AVAILABLE.value, info=result.text
65 )
66 if result.status_code != 200:
67 return TaskClientOutput(
68 error=TaskError.START_FAILED.value, info=result.text
69 )
70 result = result.json()
71 sid = result["session_id"]
72 latest_result = result
73 while SampleStatus(result["output"]["status"]) == SampleStatus.RUNNING:
74 try:
75 content = agent.inference(result["output"]["history"])

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected