forked from shroominic/codeinterpreter-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
callbacks.py
29 lines (25 loc) · 929 Bytes
/
callbacks.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
from uuid import UUID
from typing import Any, Optional, TYPE_CHECKING
from langchain.schema import AgentAction
from langchain.callbacks import AsyncIteratorCallbackHandler
if TYPE_CHECKING:
from codeinterpreterapi.session import CodeInterpreterSession
class CodeCallbackHandler(AsyncIteratorCallbackHandler):
def __init__(self, session: "CodeInterpreterSession"):
self.session = session
super().__init__()
async def on_agent_action(
self,
action: AgentAction,
*,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
**kwargs: Any,
) -> None:
"""Run on agent action."""
if action.tool == "python":
await self.session.show_code(
f"⚙️ Running code: ```python\n{action.tool_input['code']}\n```" # type: ignore
)
else:
raise ValueError(f"Unknown action: {action.tool}")