File size: 929 Bytes
2522851
18fc95f
2522851
 
18fc95f
 
 
2522851
 
 
18fc95f
2522851
 
e0cbbfe
2522851
 
 
 
 
 
 
 
 
 
 
 
 
 
e0cbbfe
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
30
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}")