mirror of
https://github.com/hwchase17/langchain.git
synced 2026-04-24 04:36:46 +00:00
75 lines
2.3 KiB
Python
75 lines
2.3 KiB
Python
"""Callback Handler that prints to std out."""
|
|
from typing import Any, Dict, List, Optional
|
|
|
|
from langchain.callbacks.base import BaseCallbackHandler
|
|
from langchain.input import print_text
|
|
from langchain.schema import AgentAction, LLMResult
|
|
|
|
|
|
class StdOutCallbackHandler(BaseCallbackHandler):
|
|
"""Callback Handler that prints to std out."""
|
|
|
|
def on_llm_start(
|
|
self, serialized: Dict[str, Any], prompts: List[str], **kwargs: Any
|
|
) -> None:
|
|
"""Print out the prompts."""
|
|
print("Prompts after formatting:")
|
|
for prompt in prompts:
|
|
print_text(prompt, color="green", end="\n")
|
|
|
|
def on_llm_end(self, response: LLMResult) -> None:
|
|
"""Do nothing."""
|
|
pass
|
|
|
|
def on_llm_error(self, error: Exception) -> None:
|
|
"""Do nothing."""
|
|
pass
|
|
|
|
def on_chain_start(
|
|
self, serialized: Dict[str, Any], inputs: Dict[str, Any], **kwargs: Any
|
|
) -> None:
|
|
"""Print out that we are entering a chain."""
|
|
class_name = serialized["name"]
|
|
print(f"\n\n\033[1m> Entering new {class_name} chain...\033[0m")
|
|
|
|
def on_chain_end(self, outputs: Dict[str, Any]) -> None:
|
|
"""Print out that we finished a chain."""
|
|
print("\n\033[1m> Finished chain.\033[0m")
|
|
|
|
def on_chain_error(self, error: Exception) -> None:
|
|
"""Do nothing."""
|
|
pass
|
|
|
|
def on_tool_start(
|
|
self,
|
|
serialized: Dict[str, Any],
|
|
action: AgentAction,
|
|
color: Optional[str] = None,
|
|
**kwargs: Any,
|
|
) -> None:
|
|
"""Print out the log in specified color."""
|
|
print_text(action.log, color=color)
|
|
|
|
def on_tool_end(
|
|
self,
|
|
output: str,
|
|
color: Optional[str] = None,
|
|
observation_prefix: Optional[str] = None,
|
|
llm_prefix: Optional[str] = None,
|
|
**kwargs: Any,
|
|
) -> None:
|
|
"""If not the final action, print out observation."""
|
|
print_text(f"\n{observation_prefix}")
|
|
print_text(output, color=color)
|
|
print_text(f"\n{llm_prefix}")
|
|
|
|
def on_tool_error(self, error: Exception) -> None:
|
|
"""Do nothing."""
|
|
pass
|
|
|
|
def on_agent_end(
|
|
self, log: str, color: Optional[str] = None, **kwargs: Any
|
|
) -> None:
|
|
"""Run when agent ends."""
|
|
print_text(log, color=color)
|