From 10c10f2deadde0413e6b7b4df6e1e9f7fecc3998 Mon Sep 17 00:00:00 2001 From: Bagatur <22008038+baskaryan@users.noreply.github.com> Date: Fri, 9 Feb 2024 17:59:34 -0800 Subject: [PATCH] cli[patch]: integration template nits (#14691) Co-authored-by: Erick Friis --- .../integration_template/Makefile | 11 +--- .../integration_template/chat_models.py | 55 ++++++++++--------- .../integration_template/llms.py | 17 +++--- .../integration_template/vectorstores.py | 3 +- .../integration_template/pyproject.toml | 18 ++---- .../scripts/lint_imports.sh | 3 +- 6 files changed, 51 insertions(+), 56 deletions(-) diff --git a/libs/cli/langchain_cli/integration_template/Makefile b/libs/cli/langchain_cli/integration_template/Makefile index bed6f5bda53..2837e81a66a 100644 --- a/libs/cli/langchain_cli/integration_template/Makefile +++ b/libs/cli/langchain_cli/integration_template/Makefile @@ -5,16 +5,11 @@ all: help # Define a variable for the test file path. TEST_FILE ?= tests/unit_tests/ +integration_test integration_tests: TEST_FILE = tests/integration_tests/ -integration_tests: TEST_FILE = tests/integration_tests/ - -test integration_tests: +test tests integration_test integration_tests: poetry run pytest $(TEST_FILE) -tests: - poetry run pytest $(TEST_FILE) - - ###################### # LINTING AND FORMATTING ###################### @@ -32,7 +27,7 @@ lint lint_diff lint_package lint_tests: poetry run ruff . poetry run ruff format $(PYTHON_FILES) --diff poetry run ruff --select I $(PYTHON_FILES) - mkdir $(MYPY_CACHE); poetry run mypy $(PYTHON_FILES) --cache-dir $(MYPY_CACHE) + mkdir -p $(MYPY_CACHE); poetry run mypy $(PYTHON_FILES) --cache-dir $(MYPY_CACHE) format format_diff: poetry run ruff format $(PYTHON_FILES) diff --git a/libs/cli/langchain_cli/integration_template/integration_template/chat_models.py b/libs/cli/langchain_cli/integration_template/integration_template/chat_models.py index 3f60c9e6a72..df191524542 100644 --- a/libs/cli/langchain_cli/integration_template/integration_template/chat_models.py +++ b/libs/cli/langchain_cli/integration_template/integration_template/chat_models.py @@ -1,3 +1,4 @@ +"""__ModuleName__ chat models.""" from typing import Any, AsyncIterator, Iterator, List, Optional from langchain_core.callbacks import ( @@ -5,7 +6,7 @@ from langchain_core.callbacks import ( CallbackManagerForLLMRun, ) from langchain_core.language_models.chat_models import BaseChatModel -from langchain_core.messages import BaseMessage, BaseMessageChunk +from langchain_core.messages import BaseMessage from langchain_core.outputs import ChatGenerationChunk, ChatResult @@ -15,40 +16,19 @@ class Chat__ModuleName__(BaseChatModel): Example: .. code-block:: python + from langchain_core.messages import HumanMessage + from __module_name__ import Chat__ModuleName__ - model = Chat__ModuleName__() - """ + model.invoke([HumanMessage(content="Come up with 10 names for a song about parrots.")]) + """ # noqa: E501 @property def _llm_type(self) -> str: """Return type of chat model.""" return "chat-__package_name_short__" - def _stream( - self, - messages: List[BaseMessage], - stop: Optional[List[str]] = None, - run_manager: Optional[CallbackManagerForLLMRun] = None, - **kwargs: Any, - ) -> Iterator[ChatGenerationChunk]: - raise NotImplementedError - - async def _astream( - self, - messages: List[BaseMessage], - stop: Optional[List[str]] = None, - run_manager: Optional[AsyncCallbackManagerForLLMRun] = None, - **kwargs: Any, - ) -> AsyncIterator[ChatGenerationChunk]: - yield ChatGenerationChunk( - message=BaseMessageChunk(content="Yield chunks", type="ai"), - ) - yield ChatGenerationChunk( - message=BaseMessageChunk(content=" like this!", type="ai"), - ) - def _generate( self, messages: List[BaseMessage], @@ -58,6 +38,29 @@ class Chat__ModuleName__(BaseChatModel): ) -> ChatResult: raise NotImplementedError + # TODO: Implement if __model_name__ supports streaming. Otherwise delete method. + def _stream( + self, + messages: List[BaseMessage], + stop: Optional[List[str]] = None, + run_manager: Optional[CallbackManagerForLLMRun] = None, + **kwargs: Any, + ) -> Iterator[ChatGenerationChunk]: + raise NotImplementedError + + # TODO: Implement if __model_name__ supports async streaming. Otherwise delete + # method. + async def _astream( + self, + messages: List[BaseMessage], + stop: Optional[List[str]] = None, + run_manager: Optional[AsyncCallbackManagerForLLMRun] = None, + **kwargs: Any, + ) -> AsyncIterator[ChatGenerationChunk]: + raise NotImplementedError + + # TODO: Implement if __model_name__ supports async generation. Otherwise delete + # method. async def _agenerate( self, messages: List[BaseMessage], diff --git a/libs/cli/langchain_cli/integration_template/integration_template/llms.py b/libs/cli/langchain_cli/integration_template/integration_template/llms.py index bd8c2fc37fa..562fc0f4001 100644 --- a/libs/cli/langchain_cli/integration_template/integration_template/llms.py +++ b/libs/cli/langchain_cli/integration_template/integration_template/llms.py @@ -1,5 +1,4 @@ -import asyncio -from functools import partial +"""__ModuleName__ large language models.""" from typing import ( Any, AsyncIterator, @@ -25,6 +24,7 @@ class __ModuleName__LLM(BaseLLM): from __module_name__ import __ModuleName__LLM model = __ModuleName__LLM() + model.invoke("Come up with 10 names for a song about parrots") """ @property @@ -41,6 +41,8 @@ class __ModuleName__LLM(BaseLLM): ) -> LLMResult: raise NotImplementedError + # TODO: Implement if __model_name__ supports async generation. Otherwise + # delete method. async def _agenerate( self, prompts: List[str], @@ -48,11 +50,9 @@ class __ModuleName__LLM(BaseLLM): run_manager: Optional[AsyncCallbackManagerForLLMRun] = None, **kwargs: Any, ) -> LLMResult: - # Change implementation if integration natively supports async generation. - return await asyncio.get_running_loop().run_in_executor( - None, partial(self._generate, **kwargs), prompts, stop, run_manager - ) + raise NotImplementedError + # TODO: Implement if __model_name__ supports streaming. Otherwise delete method. def _stream( self, prompt: str, @@ -62,6 +62,8 @@ class __ModuleName__LLM(BaseLLM): ) -> Iterator[GenerationChunk]: raise NotImplementedError + # TODO: Implement if __model_name__ supports async streaming. Otherwise delete + # method. async def _astream( self, prompt: str, @@ -69,5 +71,4 @@ class __ModuleName__LLM(BaseLLM): run_manager: Optional[AsyncCallbackManagerForLLMRun] = None, **kwargs: Any, ) -> AsyncIterator[GenerationChunk]: - yield GenerationChunk(text="Yield chunks") - yield GenerationChunk(text=" like this!") + raise NotImplementedError diff --git a/libs/cli/langchain_cli/integration_template/integration_template/vectorstores.py b/libs/cli/langchain_cli/integration_template/integration_template/vectorstores.py index 10c22f22b2a..3683d53c4a9 100644 --- a/libs/cli/langchain_cli/integration_template/integration_template/vectorstores.py +++ b/libs/cli/langchain_cli/integration_template/integration_template/vectorstores.py @@ -1,3 +1,4 @@ +"""__ModuleName__ vector stores.""" from __future__ import annotations import asyncio @@ -24,7 +25,7 @@ VST = TypeVar("VST", bound=VectorStore) class __ModuleName__VectorStore(VectorStore): - """Interface for vector store. + """__ModuleName__ vector store. Example: .. code-block:: python diff --git a/libs/cli/langchain_cli/integration_template/pyproject.toml b/libs/cli/langchain_cli/integration_template/pyproject.toml index be76900001b..08a778b2fcd 100644 --- a/libs/cli/langchain_cli/integration_template/pyproject.toml +++ b/libs/cli/langchain_cli/integration_template/pyproject.toml @@ -12,25 +12,21 @@ license = "MIT" [tool.poetry.dependencies] python = ">=3.8.1,<4.0" -langchain-core = ">=0.0.12" +langchain-core = "^0.1" [tool.poetry.group.test] optional = true [tool.poetry.group.test.dependencies] -pytest = "^7.3.0" -freezegun = "^1.2.2" -pytest-mock = "^3.10.0" -syrupy = "^4.0.2" -pytest-watcher = "^0.3.4" -pytest-asyncio = "^0.21.1" +pytest = "^7.4.3" +pytest-asyncio = "^0.23.2" langchain-core = {path = "../../core", develop = true} [tool.poetry.group.codespell] optional = true [tool.poetry.group.codespell.dependencies] -codespell = "^2.2.0" +codespell = "^2.2.6" [tool.poetry.group.test_integration] optional = true @@ -41,10 +37,10 @@ optional = true optional = true [tool.poetry.group.lint.dependencies] -ruff = "^0.1.5" +ruff = "^0.1.8" [tool.poetry.group.typing.dependencies] -mypy = "^0.991" +mypy = "^1.7.1" langchain-core = {path = "../../core", develop = true} [tool.poetry.group.dev] @@ -87,8 +83,6 @@ addopts = "--snapshot-warn-unused --strict-markers --strict-config --durations=5 # Registering custom markers. # https://docs.pytest.org/en/7.1.x/example/markers.html#registering-markers markers = [ - "requires: mark tests as requiring a specific library", - "asyncio: mark tests as requiring asyncio", "compile: mark placeholder test used to compile integration tests without running them", ] asyncio_mode = "auto" diff --git a/libs/cli/langchain_cli/integration_template/scripts/lint_imports.sh b/libs/cli/langchain_cli/integration_template/scripts/lint_imports.sh index 695613c7ba8..19ccec1480c 100755 --- a/libs/cli/langchain_cli/integration_template/scripts/lint_imports.sh +++ b/libs/cli/langchain_cli/integration_template/scripts/lint_imports.sh @@ -5,9 +5,10 @@ set -eu # Initialize a variable to keep track of errors errors=0 -# make sure not importing from langchain or langchain_experimental +# make sure not importing from langchain, langchain_experimental, or langchain_community git --no-pager grep '^from langchain\.' . && errors=$((errors+1)) git --no-pager grep '^from langchain_experimental\.' . && errors=$((errors+1)) +git --no-pager grep '^from langchain_community\.' . && errors=$((errors+1)) # Decide on an exit status based on the errors if [ "$errors" -gt 0 ]; then