mirror of
https://github.com/hwchase17/langchain
synced 2024-11-11 19:11:02 +00:00
a0c2281540
```python """python scripts/update_mypy_ruff.py""" import glob import tomllib from pathlib import Path import toml import subprocess import re ROOT_DIR = Path(__file__).parents[1] def main(): for path in glob.glob(str(ROOT_DIR / "libs/**/pyproject.toml"), recursive=True): print(path) with open(path, "rb") as f: pyproject = tomllib.load(f) try: pyproject["tool"]["poetry"]["group"]["typing"]["dependencies"]["mypy"] = ( "^1.10" ) pyproject["tool"]["poetry"]["group"]["lint"]["dependencies"]["ruff"] = ( "^0.5" ) except KeyError: continue with open(path, "w") as f: toml.dump(pyproject, f) cwd = "/".join(path.split("/")[:-1]) completed = subprocess.run( "poetry lock --no-update; poetry install --with typing; poetry run mypy . --no-color", cwd=cwd, shell=True, capture_output=True, text=True, ) logs = completed.stdout.split("\n") to_ignore = {} for l in logs: if re.match("^(.*)\:(\d+)\: error:.*\[(.*)\]", l): path, line_no, error_type = re.match( "^(.*)\:(\d+)\: error:.*\[(.*)\]", l ).groups() if (path, line_no) in to_ignore: to_ignore[(path, line_no)].append(error_type) else: to_ignore[(path, line_no)] = [error_type] print(len(to_ignore)) for (error_path, line_no), error_types in to_ignore.items(): all_errors = ", ".join(error_types) full_path = f"{cwd}/{error_path}" try: with open(full_path, "r") as f: file_lines = f.readlines() except FileNotFoundError: continue file_lines[int(line_no) - 1] = ( file_lines[int(line_no) - 1][:-1] + f" # type: ignore[{all_errors}]\n" ) with open(full_path, "w") as f: f.write("".join(file_lines)) subprocess.run( "poetry run ruff format .; poetry run ruff --select I --fix .", cwd=cwd, shell=True, capture_output=True, text=True, ) if __name__ == "__main__": main() ```
395 lines
8.9 KiB
Python
395 lines
8.9 KiB
Python
"""A fake callback handler for testing purposes."""
|
|
|
|
from itertools import chain
|
|
from typing import Any, Dict, List, Optional, Union
|
|
from uuid import UUID
|
|
|
|
from langchain_core.callbacks.base import AsyncCallbackHandler, BaseCallbackHandler
|
|
from langchain_core.messages import BaseMessage
|
|
from langchain_core.pydantic_v1 import BaseModel
|
|
|
|
|
|
class BaseFakeCallbackHandler(BaseModel):
|
|
"""Base fake callback handler for testing."""
|
|
|
|
starts: int = 0
|
|
ends: int = 0
|
|
errors: int = 0
|
|
errors_args: List[Any] = []
|
|
text: int = 0
|
|
ignore_llm_: bool = False
|
|
ignore_chain_: bool = False
|
|
ignore_agent_: bool = False
|
|
ignore_retriever_: bool = False
|
|
ignore_chat_model_: bool = False
|
|
|
|
# to allow for similar callback handlers that are not technically equal
|
|
fake_id: Union[str, None] = None
|
|
|
|
# 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
|
|
llm_streams: int = 0
|
|
tool_starts: int = 0
|
|
tool_ends: int = 0
|
|
agent_actions: int = 0
|
|
agent_ends: int = 0
|
|
chat_model_starts: int = 0
|
|
retriever_starts: int = 0
|
|
retriever_ends: int = 0
|
|
retriever_errors: int = 0
|
|
retries: int = 0
|
|
|
|
|
|
class BaseFakeCallbackHandlerMixin(BaseFakeCallbackHandler):
|
|
"""Base fake callback handler mixin for testing."""
|
|
|
|
def on_llm_start_common(self) -> None:
|
|
self.llm_starts += 1
|
|
self.starts += 1
|
|
|
|
def on_llm_end_common(self) -> None:
|
|
self.llm_ends += 1
|
|
self.ends += 1
|
|
|
|
def on_llm_error_common(self, *args: Any, **kwargs: Any) -> None:
|
|
self.errors += 1
|
|
self.errors_args.append({"args": args, "kwargs": kwargs})
|
|
|
|
def on_llm_new_token_common(self) -> None:
|
|
self.llm_streams += 1
|
|
|
|
def on_retry_common(self) -> None:
|
|
self.retries += 1
|
|
|
|
def on_chain_start_common(self) -> None:
|
|
self.chain_starts += 1
|
|
self.starts += 1
|
|
|
|
def on_chain_end_common(self) -> None:
|
|
self.chain_ends += 1
|
|
self.ends += 1
|
|
|
|
def on_chain_error_common(self) -> None:
|
|
self.errors += 1
|
|
|
|
def on_tool_start_common(self) -> None:
|
|
self.tool_starts += 1
|
|
self.starts += 1
|
|
|
|
def on_tool_end_common(self) -> None:
|
|
self.tool_ends += 1
|
|
self.ends += 1
|
|
|
|
def on_tool_error_common(self) -> None:
|
|
self.errors += 1
|
|
|
|
def on_agent_action_common(self) -> None:
|
|
self.agent_actions += 1
|
|
self.starts += 1
|
|
|
|
def on_agent_finish_common(self) -> None:
|
|
self.agent_ends += 1
|
|
self.ends += 1
|
|
|
|
def on_chat_model_start_common(self) -> None:
|
|
self.chat_model_starts += 1
|
|
self.starts += 1
|
|
|
|
def on_text_common(self) -> None:
|
|
self.text += 1
|
|
|
|
def on_retriever_start_common(self) -> None:
|
|
self.starts += 1
|
|
self.retriever_starts += 1
|
|
|
|
def on_retriever_end_common(self) -> None:
|
|
self.ends += 1
|
|
self.retriever_ends += 1
|
|
|
|
def on_retriever_error_common(self) -> None:
|
|
self.errors += 1
|
|
self.retriever_errors += 1
|
|
|
|
|
|
class FakeCallbackHandler(BaseCallbackHandler, BaseFakeCallbackHandlerMixin):
|
|
"""Fake callback handler for testing."""
|
|
|
|
@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_
|
|
|
|
@property
|
|
def ignore_retriever(self) -> bool:
|
|
"""Whether to ignore retriever callbacks."""
|
|
return self.ignore_retriever_
|
|
|
|
def on_llm_start(
|
|
self,
|
|
*args: Any,
|
|
**kwargs: Any,
|
|
) -> Any:
|
|
self.on_llm_start_common()
|
|
|
|
def on_llm_new_token(
|
|
self,
|
|
*args: Any,
|
|
**kwargs: Any,
|
|
) -> Any:
|
|
self.on_llm_new_token_common()
|
|
|
|
def on_llm_end(
|
|
self,
|
|
*args: Any,
|
|
**kwargs: Any,
|
|
) -> Any:
|
|
self.on_llm_end_common()
|
|
|
|
def on_llm_error(
|
|
self,
|
|
*args: Any,
|
|
**kwargs: Any,
|
|
) -> Any:
|
|
self.on_llm_error_common(*args, **kwargs)
|
|
|
|
def on_retry(
|
|
self,
|
|
*args: Any,
|
|
**kwargs: Any,
|
|
) -> Any:
|
|
self.on_retry_common()
|
|
|
|
def on_chain_start(
|
|
self,
|
|
*args: Any,
|
|
**kwargs: Any,
|
|
) -> Any:
|
|
self.on_chain_start_common()
|
|
|
|
def on_chain_end(
|
|
self,
|
|
*args: Any,
|
|
**kwargs: Any,
|
|
) -> Any:
|
|
self.on_chain_end_common()
|
|
|
|
def on_chain_error(
|
|
self,
|
|
*args: Any,
|
|
**kwargs: Any,
|
|
) -> Any:
|
|
self.on_chain_error_common()
|
|
|
|
def on_tool_start(
|
|
self,
|
|
*args: Any,
|
|
**kwargs: Any,
|
|
) -> Any:
|
|
self.on_tool_start_common()
|
|
|
|
def on_tool_end(
|
|
self,
|
|
*args: Any,
|
|
**kwargs: Any,
|
|
) -> Any:
|
|
self.on_tool_end_common()
|
|
|
|
def on_tool_error(
|
|
self,
|
|
*args: Any,
|
|
**kwargs: Any,
|
|
) -> Any:
|
|
self.on_tool_error_common()
|
|
|
|
def on_agent_action(
|
|
self,
|
|
*args: Any,
|
|
**kwargs: Any,
|
|
) -> Any:
|
|
self.on_agent_action_common()
|
|
|
|
def on_agent_finish(
|
|
self,
|
|
*args: Any,
|
|
**kwargs: Any,
|
|
) -> Any:
|
|
self.on_agent_finish_common()
|
|
|
|
def on_text(
|
|
self,
|
|
*args: Any,
|
|
**kwargs: Any,
|
|
) -> Any:
|
|
self.on_text_common()
|
|
|
|
def on_retriever_start(
|
|
self,
|
|
*args: Any,
|
|
**kwargs: Any,
|
|
) -> Any:
|
|
self.on_retriever_start_common()
|
|
|
|
def on_retriever_end(
|
|
self,
|
|
*args: Any,
|
|
**kwargs: Any,
|
|
) -> Any:
|
|
self.on_retriever_end_common()
|
|
|
|
def on_retriever_error(
|
|
self,
|
|
*args: Any,
|
|
**kwargs: Any,
|
|
) -> Any:
|
|
self.on_retriever_error_common()
|
|
|
|
def __deepcopy__(self, memo: dict) -> "FakeCallbackHandler":
|
|
return self
|
|
|
|
|
|
class FakeCallbackHandlerWithChatStart(FakeCallbackHandler):
|
|
def on_chat_model_start(
|
|
self,
|
|
serialized: Dict[str, Any],
|
|
messages: List[List[BaseMessage]],
|
|
*,
|
|
run_id: UUID,
|
|
parent_run_id: Optional[UUID] = None,
|
|
**kwargs: Any,
|
|
) -> Any:
|
|
assert all(isinstance(m, BaseMessage) for m in chain(*messages))
|
|
self.on_chat_model_start_common()
|
|
|
|
|
|
class FakeAsyncCallbackHandler(AsyncCallbackHandler, BaseFakeCallbackHandlerMixin):
|
|
"""Fake async callback handler for testing."""
|
|
|
|
@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_
|
|
|
|
async def on_retry(
|
|
self,
|
|
*args: Any,
|
|
**kwargs: Any,
|
|
) -> Any:
|
|
self.on_retry_common()
|
|
|
|
async def on_llm_start(
|
|
self,
|
|
*args: Any,
|
|
**kwargs: Any,
|
|
) -> None:
|
|
self.on_llm_start_common()
|
|
|
|
async def on_llm_new_token(
|
|
self,
|
|
*args: Any,
|
|
**kwargs: Any,
|
|
) -> None:
|
|
self.on_llm_new_token_common()
|
|
|
|
async def on_llm_end(
|
|
self,
|
|
*args: Any,
|
|
**kwargs: Any,
|
|
) -> None:
|
|
self.on_llm_end_common()
|
|
|
|
async def on_llm_error(
|
|
self,
|
|
*args: Any,
|
|
**kwargs: Any,
|
|
) -> None:
|
|
self.on_llm_error_common(*args, **kwargs)
|
|
|
|
async def on_chain_start(
|
|
self,
|
|
*args: Any,
|
|
**kwargs: Any,
|
|
) -> None:
|
|
self.on_chain_start_common()
|
|
|
|
async def on_chain_end(
|
|
self,
|
|
*args: Any,
|
|
**kwargs: Any,
|
|
) -> None:
|
|
self.on_chain_end_common()
|
|
|
|
async def on_chain_error(
|
|
self,
|
|
*args: Any,
|
|
**kwargs: Any,
|
|
) -> None:
|
|
self.on_chain_error_common()
|
|
|
|
async def on_tool_start(
|
|
self,
|
|
*args: Any,
|
|
**kwargs: Any,
|
|
) -> None:
|
|
self.on_tool_start_common()
|
|
|
|
async def on_tool_end(
|
|
self,
|
|
*args: Any,
|
|
**kwargs: Any,
|
|
) -> None:
|
|
self.on_tool_end_common()
|
|
|
|
async def on_tool_error(
|
|
self,
|
|
*args: Any,
|
|
**kwargs: Any,
|
|
) -> None:
|
|
self.on_tool_error_common()
|
|
|
|
async def on_agent_action(
|
|
self,
|
|
*args: Any,
|
|
**kwargs: Any,
|
|
) -> None:
|
|
self.on_agent_action_common()
|
|
|
|
async def on_agent_finish(
|
|
self,
|
|
*args: Any,
|
|
**kwargs: Any,
|
|
) -> None:
|
|
self.on_agent_finish_common()
|
|
|
|
async def on_text(
|
|
self,
|
|
*args: Any,
|
|
**kwargs: Any,
|
|
) -> None:
|
|
self.on_text_common()
|
|
|
|
def __deepcopy__(self, memo: dict) -> "FakeAsyncCallbackHandler":
|
|
return self
|