Skip to content

Commit 5d10ec2

Browse files
add docstrings for server.py
1 parent e749939 commit 5d10ec2

File tree

1 file changed

+87
-11
lines changed

1 file changed

+87
-11
lines changed

src/agentlab/analyze/server.py

Lines changed: 87 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import base64
33
import copy
44
import importlib
5+
from typing import Any
56

67
import dotenv
78
import numpy as np
@@ -17,11 +18,17 @@
1718
app = FastAPI()
1819

1920

20-
def import_from_path(path):
21+
def import_from_path(path: str) -> callable:
2122
"""
2223
Util function to import and instantiate a class, then return a specific method.
2324
For example, given `browsergym.core.action.highlevel.HighLevelActionSet.to_python_code`,
2425
this will instantiate `HighLevelActionSet` and return its `to_python_code` method.
26+
27+
:param path: Path to the method
28+
:type path: str
29+
:raises ModuleNotFoundError: If the module cannot be imported
30+
:return: The method
31+
:rtype: callable
2532
"""
2633

2734
parts = path.split(".")
@@ -51,10 +58,15 @@ def import_from_path(path):
5158
return obj
5259

5360

54-
def make_json_safe(obj):
61+
def make_json_safe(obj: Any) -> Any:
5562
"""
5663
Util function to convert numpy arrays and other non-JSON-serializable objects to JSON-serializable objects.
5764
Specifically, we convert numpy arrays to base64 encoded strings so that payloads are of reasonable size.
65+
66+
:param obj: Object to convert
67+
:type obj: Any
68+
:return: JSON-serializable object
69+
:rtype: Any
5870
"""
5971
if isinstance(obj, np.ndarray):
6072
# convert to base64
@@ -418,6 +430,14 @@ def close(self) -> dict:
418430
# --- FastAPI endpoints ---
419431
@app.post("/set_info")
420432
def set_info(req: SetInfoRequest):
433+
"""
434+
Set the environment info.
435+
436+
:param req: Request containing environment info
437+
:type req: SetInfoRequest
438+
:return: Dictionary with status
439+
:rtype: dict
440+
"""
421441
return env.set_info(
422442
benchmark_name=req.benchmark_name,
423443
task_name=req.task_name,
@@ -428,47 +448,103 @@ def set_info(req: SetInfoRequest):
428448

429449

430450
@app.get("/get_info")
431-
def get_info():
451+
def get_info() -> dict:
452+
"""
453+
Get the environment info.
454+
455+
:return: Dictionary with info
456+
:rtype: dict
457+
"""
432458
return env.get_info()
433459

434460

435461
@app.post("/unset_info")
436-
def unset_info():
462+
def unset_info() -> dict:
463+
"""
464+
Unset the environment info.
465+
466+
:return: Dictionary with status
467+
:rtype: dict
468+
"""
437469
return env.unset_info()
438470

439471

440472
@app.get("/status")
441-
def status():
473+
def status() -> dict:
474+
"""
475+
Get the status of the environment.
476+
477+
:return: Dictionary with status
478+
:rtype: dict
479+
"""
442480
return env.status()
443481

444482

445483
@app.post("/prepare_benchmark")
446-
def prepare_benchmark():
484+
def prepare_benchmark() -> dict:
485+
"""
486+
Prepare the benchmark.
487+
488+
:return: Dictionary with status
489+
:rtype: dict
490+
"""
447491
return env.prepare_benchmark()
448492

449493

450494
@app.post("/reload_task")
451-
def reload_task():
495+
def reload_task() -> dict:
496+
"""
497+
Reload the task.
498+
499+
:return: Dictionary with status
500+
:rtype: dict
501+
"""
452502
return env.reload_task()
453503

454504

455505
@app.post("/reset")
456-
def reset():
506+
def reset() -> dict:
507+
"""
508+
Reset the environment.
509+
510+
:return: Dictionary with status
511+
:rtype: dict
512+
"""
457513
return env.reset()
458514

459515

460516
@app.post("/step")
461-
def step(req: StepRequest):
517+
def step(req: StepRequest) -> dict:
518+
"""
519+
Step the environment.
520+
521+
:param req: Request containing action
522+
:type req: StepRequest
523+
:return: Dictionary with obs, reward, terminated, truncated and info
524+
:rtype: dict
525+
"""
462526
return env.step(action=req.action)
463527

464528

465529
@app.get("/get_obs")
466-
def get_obs():
530+
def get_obs() -> dict:
531+
"""
532+
Get the last observation.
533+
534+
:return: Dictionary with obs and info
535+
:rtype: dict
536+
"""
467537
return env.get_obs()
468538

469539

470540
@app.post("/close")
471-
def close():
541+
def close() -> dict:
542+
"""
543+
Close the environment.
544+
545+
:return: Dictionary with status
546+
:rtype: dict
547+
"""
472548
return env.close()
473549

474550

0 commit comments

Comments
 (0)