File size: 1,476 Bytes
fe95067
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import logging
from typing import Any

from langchain.callbacks import StdOutCallbackHandler

logging.addLevelName(15, "LangChain")


class LoggingCallbackHandler(StdOutCallbackHandler):
    """Callback Handler that prints to std out."""

    always_verbose = True

    def __init__(self, name: str) -> None:
        """Initialize callback handler."""
        super().__init__()
        self.logger = logging.getLogger(name)
        self.prompt = ""

    def on_chain_start(self, *args: Any, **kwargs: Any) -> None:
        pass

    def on_chain_end(self, *args: Any, **kwargs: Any) -> None:
        pass

    def on_agent_action(self, *args: Any, **kwargs: Any) -> Any:
        pass

    def on_tool_end(
        self,
        *args: Any,
        **kwargs: Any,
    ) -> None:
        pass

    def on_tool_error(
        self, error: BaseException | KeyboardInterrupt, **kwargs: Any
    ) -> None:
        """Do nothing."""
        pass

    def on_text(
        self,
        text: str,
        color: str | None = None,
        end: str = "",
        **kwargs: Any,
    ) -> None:
        """Run when agent ends."""
        # leave only prompt for environment
        text = text.replace("\x1b[32;1m\x1b[1;3mHuman: ", "")
        logging.log(15, f"LLM Call: {text}")
        self.prompt = text

    def retrive_prompt(self) -> str:
        return self.prompt

    def on_agent_finish(self, *args: Any, **kwargs: Any) -> None:
        """Run on agent end."""
        pass