Shroominic commited on
Commit
2522851
·
1 Parent(s): 58a8d89

callback handler

Browse files
Files changed (1) hide show
  1. codeinterpreterapi/callbacks.py +27 -0
codeinterpreterapi/callbacks.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from uuid import UUID
2
+ from typing import Any, Optional, List
3
+ from langchain.schema import AgentAction
4
+ from langchain.callbacks import AsyncIteratorCallbackHandler
5
+ from codeinterpreterapi.session import CodeInterpreterSession
6
+
7
+
8
+ class CodeCallbackHandler(AsyncIteratorCallbackHandler):
9
+ def __init__(self, session: CodeInterpreterSession):
10
+ self.session = session
11
+ super().__init__()
12
+
13
+ async def on_agent_action(
14
+ self,
15
+ action: AgentAction,
16
+ *,
17
+ run_id: UUID,
18
+ parent_run_id: Optional[UUID] = None,
19
+ **kwargs: Any,
20
+ ) -> None:
21
+ """Run on agent action."""
22
+ if action.tool == "python":
23
+ await self.session.show_code(
24
+ f"⚙️ Running code: ```python\n{action.tool_input['code']}\n```" # type: ignore
25
+ )
26
+ else:
27
+ raise ValueError(f"Unknown action: {action.tool}")