forked from Oyum2814/health-copilot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
socketcallback.py
63 lines (45 loc) · 2.25 KB
/
socketcallback.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from typing import Any, Dict, List, Union
from langchain.callbacks.base import BaseCallbackHandler
from langchain.schema import AgentAction, AgentFinish, LLMResult
class SocketIOCallback(BaseCallbackHandler):
"""Callback handler for streaming. Only works with LLMs that support streaming."""
# constructor to pass socketio object
def __init__(self, fn):
self.fn = fn
def on_llm_start(self, serialized: Dict[str, Any], prompts: List[str],
**kwargs: Any) -> None:
self.current_text = ""
"""Run when LLM starts running."""
def on_llm_new_token(self, token: str, **kwargs: Any) -> None:
"""Run on new LLM token. Only available when streaming is enabled."""
self.current_text += token
self.fn(self.current_text)
def on_llm_end(self, response: LLMResult, **kwargs: Any) -> None:
"""Run when LLM ends running."""
self.current_text = ""
def on_llm_error(self, error: Union[Exception, KeyboardInterrupt],
**kwargs: Any) -> None:
"""Run when LLM errors."""
def on_chain_start(self, serialized: Dict[str, Any],
inputs: Dict[str, Any], **kwargs: Any) -> None:
"""Run when chain starts running."""
def on_chain_end(self, outputs: Dict[str, Any], **kwargs: Any) -> None:
"""Run when chain ends running."""
def on_chain_error(self, error: Union[Exception, KeyboardInterrupt],
**kwargs: Any) -> None:
"""Run when chain errors."""
def on_tool_start(self, serialized: Dict[str, Any], input_str: str,
**kwargs: Any) -> None:
"""Run when tool starts running."""
def on_agent_action(self, action: AgentAction, **kwargs: Any) -> Any:
"""Run on agent action."""
pass
def on_tool_end(self, output: str, **kwargs: Any) -> None:
"""Run when tool ends running."""
def on_tool_error(self, error: Union[Exception, KeyboardInterrupt],
**kwargs: Any) -> None:
"""Run when tool errors."""
def on_text(self, text: str, **kwargs: Any) -> None:
"""Run on arbitrary text."""
def on_agent_finish(self, finish: AgentFinish, **kwargs: Any) -> None:
"""Run on agent end."""