2023-01-04 15:54:25 +00:00
|
|
|
"""A fake callback handler for testing purposes."""
|
2023-01-27 01:38:13 +00:00
|
|
|
from typing import Any, Dict, List, Union
|
|
|
|
|
|
|
|
from pydantic import BaseModel
|
2023-01-04 15:54:25 +00:00
|
|
|
|
|
|
|
from langchain.callbacks.base import BaseCallbackHandler
|
|
|
|
from langchain.schema import AgentAction, AgentFinish, LLMResult
|
|
|
|
|
|
|
|
|
2023-01-27 01:38:13 +00:00
|
|
|
class FakeCallbackHandler(BaseModel, BaseCallbackHandler):
|
2023-01-04 15:54:25 +00:00
|
|
|
"""Fake callback handler for testing."""
|
|
|
|
|
|
|
|
starts: int = 0
|
|
|
|
ends: int = 0
|
|
|
|
errors: int = 0
|
|
|
|
text: int = 0
|
2023-01-27 01:38:13 +00:00
|
|
|
ignore_llm_: bool = False
|
|
|
|
ignore_chain_: bool = False
|
|
|
|
ignore_agent_: bool = False
|
|
|
|
always_verbose_: bool = False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def always_verbose(self) -> bool:
|
|
|
|
"""Whether to call verbose callbacks even if verbose is False."""
|
|
|
|
return self.always_verbose_
|
|
|
|
|
|
|
|
@property
|
|
|
|
def ignore_llm(self) -> bool:
|
|
|
|
"""Whether to ignore LLM callbacks."""
|
|
|
|
return self.ignore_llm_
|
|
|
|
|
|
|
|
@property
|
|
|
|
def ignore_chain(self) -> bool:
|
|
|
|
"""Whether to ignore chain callbacks."""
|
|
|
|
return self.ignore_chain_
|
|
|
|
|
|
|
|
@property
|
|
|
|
def ignore_agent(self) -> bool:
|
|
|
|
"""Whether to ignore agent callbacks."""
|
|
|
|
return self.ignore_agent_
|
2023-01-04 15:54:25 +00:00
|
|
|
|
2023-01-28 16:05:20 +00:00
|
|
|
# add finer-grained counters for easier debugging of failing tests
|
|
|
|
chain_starts: int = 0
|
|
|
|
chain_ends: int = 0
|
|
|
|
llm_starts: int = 0
|
|
|
|
llm_ends: int = 0
|
|
|
|
tool_starts: int = 0
|
|
|
|
tool_ends: int = 0
|
|
|
|
agent_ends: int = 0
|
|
|
|
|
2023-01-04 15:54:25 +00:00
|
|
|
def on_llm_start(
|
|
|
|
self, serialized: Dict[str, Any], prompts: List[str], **kwargs: Any
|
|
|
|
) -> None:
|
|
|
|
"""Run when LLM starts running."""
|
2023-01-28 16:05:20 +00:00
|
|
|
self.llm_starts += 1
|
2023-01-04 15:54:25 +00:00
|
|
|
self.starts += 1
|
|
|
|
|
2023-01-22 20:44:14 +00:00
|
|
|
def on_llm_end(self, response: LLMResult, **kwargs: Any) -> None:
|
2023-01-04 15:54:25 +00:00
|
|
|
"""Run when LLM ends running."""
|
2023-01-28 16:05:20 +00:00
|
|
|
self.llm_ends += 1
|
2023-01-04 15:54:25 +00:00
|
|
|
self.ends += 1
|
|
|
|
|
2023-01-27 01:38:13 +00:00
|
|
|
def on_llm_error(
|
|
|
|
self, error: Union[Exception, KeyboardInterrupt], **kwargs: Any
|
|
|
|
) -> None:
|
2023-01-04 15:54:25 +00:00
|
|
|
"""Run when LLM errors."""
|
|
|
|
self.errors += 1
|
|
|
|
|
|
|
|
def on_chain_start(
|
|
|
|
self, serialized: Dict[str, Any], inputs: Dict[str, Any], **kwargs: Any
|
|
|
|
) -> None:
|
|
|
|
"""Run when chain starts running."""
|
2023-01-28 16:05:20 +00:00
|
|
|
self.chain_starts += 1
|
2023-01-04 15:54:25 +00:00
|
|
|
self.starts += 1
|
|
|
|
|
2023-01-22 20:44:14 +00:00
|
|
|
def on_chain_end(self, outputs: Dict[str, Any], **kwargs: Any) -> None:
|
2023-01-04 15:54:25 +00:00
|
|
|
"""Run when chain ends running."""
|
2023-01-28 16:05:20 +00:00
|
|
|
self.chain_ends += 1
|
2023-01-04 15:54:25 +00:00
|
|
|
self.ends += 1
|
|
|
|
|
2023-01-27 01:38:13 +00:00
|
|
|
def on_chain_error(
|
|
|
|
self, error: Union[Exception, KeyboardInterrupt], **kwargs: Any
|
|
|
|
) -> None:
|
2023-01-04 15:54:25 +00:00
|
|
|
"""Run when chain errors."""
|
|
|
|
self.errors += 1
|
|
|
|
|
|
|
|
def on_tool_start(
|
|
|
|
self, serialized: Dict[str, Any], action: AgentAction, **kwargs: Any
|
|
|
|
) -> None:
|
|
|
|
"""Run when tool starts running."""
|
2023-01-28 16:05:20 +00:00
|
|
|
self.tool_starts += 1
|
2023-01-04 15:54:25 +00:00
|
|
|
self.starts += 1
|
|
|
|
|
|
|
|
def on_tool_end(self, output: str, **kwargs: Any) -> None:
|
|
|
|
"""Run when tool ends running."""
|
2023-01-28 16:05:20 +00:00
|
|
|
self.tool_ends += 1
|
2023-01-04 15:54:25 +00:00
|
|
|
self.ends += 1
|
|
|
|
|
2023-01-27 01:38:13 +00:00
|
|
|
def on_tool_error(
|
|
|
|
self, error: Union[Exception, KeyboardInterrupt], **kwargs: Any
|
|
|
|
) -> None:
|
2023-01-04 15:54:25 +00:00
|
|
|
"""Run when tool errors."""
|
|
|
|
self.errors += 1
|
|
|
|
|
|
|
|
def on_text(self, text: str, **kwargs: Any) -> None:
|
|
|
|
"""Run when agent is ending."""
|
|
|
|
self.text += 1
|
|
|
|
|
|
|
|
def on_agent_finish(self, finish: AgentFinish, **kwargs: Any) -> None:
|
|
|
|
"""Run when agent ends running."""
|
2023-01-28 16:05:20 +00:00
|
|
|
self.agent_ends += 1
|
2023-01-04 15:54:25 +00:00
|
|
|
self.ends += 1
|