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

Class DBBenchTask

src/server/tasks/dbbench/task.py:37–274  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

35
36
37class DBBenchTask(Task):
38
39 def __init__(self,
40 data_file: str,
41 db_file: Optional[str] = None,
42 db_password: str = 'password',
43 max_round: int = 20,
44 env_driver: str = 'docker',
45 env_options: Optional[dict] = None,
46 **configs):
47 super().__init__(**configs)
48 self.full_async = True
49 self.logger = logging.getLogger(__name__)
50
51 self.max_round = max_round
52 self.data_file = data_file
53 self.db_root_dir = db_file
54
55 self.dataset = []
56 # Load dataset
57 with open(self.data_file) as f:
58 raw_data = f.read()
59 if self.data_file.endswith("json"):
60 data = json.loads(raw_data)
61 else: # Assuming jsonl
62 data = [json.loads(line) for line in raw_data.strip().split('\n')]
63
64 for entry in data:
65 ans_key = "answer_md5" if entry["type"][0] in ("INSERT", "DELETE", "UPDATE") else "label"
66 ans = entry.pop(ans_key, None) # Use pop with default
67 inp = entry
68 self.dataset.append((inp, ans))
69
70 self.env_delegation = DBBenchEnvironmentDelegation(db_password)
71 self.env_controller = create_controller(env_driver, self.env_delegation, **env_options)
72 self.env_controller_background_task = None
73
74 self.logger.info(f"DBBench initialized with {len(self.dataset)} samples. Root dir: {self.db_root_dir}")
75
76 def get_indices(self) -> List[SampleIndex]:
77 return list(range(len(self.dataset)))
78
79 async def start_sample(self, index: int, session: Session) -> TaskSampleExecutionResult:
80 self.env_controller.loop = asyncio.get_running_loop()
81 if not self.env_controller_background_task:
82 self.env_controller_background_task = asyncio.create_task(self.env_controller.background_task())
83 weakref.finalize(self, self.env_controller_background_task.cancel)
84
85 database: Optional[Database] = None
86 try:
87 entry = self.dataset[index][0]
88 ground_truth = self.dataset[index][1]
89
90 use_sqlite = entry.get("user_sqlite", False)
91 if use_sqlite:
92 db_dir = entry['create']['database']
93 init_file = entry['create']['init']
94 sqlite_path = os.path.join(self.db_root_dir, db_dir, init_file)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected