A `lookup` tool that writes to `timeline` when it starts, and returns `value of ` unless `lookup` says otherwise.
(
sync: bool, timeline: list[str], *, lookup: Callable[[str], str] | None = None, log_returns: bool = False
)
| 997 | |
| 998 | |
| 999 | def lookup_tool( |
| 1000 | sync: bool, timeline: list[str], *, lookup: Callable[[str], str] | None = None, log_returns: bool = False |
| 1001 | ) -> Any: |
| 1002 | """A `lookup` tool that writes to `timeline` when it starts, and returns `value of <key>` unless `lookup` says |
| 1003 | otherwise.""" |
| 1004 | |
| 1005 | def run(key: str) -> str: |
| 1006 | timeline.append(f"lookup({key}) STARTS") |
| 1007 | value = f"value of {key}" if lookup is None else lookup(key) |
| 1008 | if log_returns: |
| 1009 | timeline.append(f"lookup({key}) returns") |
| 1010 | return value |
| 1011 | |
| 1012 | if sync: |
| 1013 | |
| 1014 | @beta_tool(name="lookup") |
| 1015 | def sync_lookup(key: str) -> str: |
| 1016 | """Look up a key.""" |
| 1017 | return run(key) |
| 1018 | |
| 1019 | return sync_lookup |
| 1020 | |
| 1021 | @beta_async_tool(name="lookup") |
| 1022 | async def async_lookup(key: str) -> str: |
| 1023 | """Look up a key.""" |
| 1024 | return run(key) |
| 1025 | |
| 1026 | return async_lookup |
| 1027 | |
| 1028 | |
| 1029 | class TimelineDesktop(Desktop): |