The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
mv base cache to schema (#9953)
Bagatur9828701de1e9
diff --git a/libs/langchain/langchain/__init__.py b/libs/langchain/langchain/__init__.pyindex 32ad552ecc..721769d0e2 100644--- a/libs/langchain/langchain/__init__.py+++ b/libs/langchain/langchain/__init__.py@@ -4,7 +4,6 @@ from importlib import metadatafrom typing import Optionalfrom langchain.agents import MRKLChain, ReActChain, SelfAskWithSearchChain-from langchain.cache import BaseCachefrom langchain.chains import (ConversationChain,LLMBashChain,@@ -40,6 +39,7 @@ from langchain.prompts import (Prompt,PromptTemplate,)+from langchain.schema.cache import BaseCachefrom langchain.schema.prompt_template import BasePromptTemplatefrom langchain.utilities.arxiv import ArxivAPIWrapperfrom langchain.utilities.golden_query import GoldenQueryAPIWrapperdiff --git a/libs/langchain/langchain/cache.py b/libs/langchain/langchain/cache.pyindex 3160fbae6a..4364c33e1a 100644--- a/libs/langchain/langchain/cache.py+++ b/libs/langchain/langchain/cache.py@@ -26,7 +26,6 @@ import inspectimport jsonimport loggingimport warnings-from abc import ABC, abstractmethodfrom datetime import timedeltafrom typing import (TYPE_CHECKING,@@ -35,7 +34,6 @@ from typing import (Dict,List,Optional,- Sequence,Tuple,Type,Union,@@ -46,17 +44,18 @@ from sqlalchemy import Column, Integer, String, create_engine, selectfrom sqlalchemy.engine.base import Enginefrom sqlalchemy.orm import Session-from langchain.utils import get_from_env-try:from sqlalchemy.orm import declarative_baseexcept ImportError:from sqlalchemy.ext.declarative import declarative_base+from langchain.embeddings.base import Embeddingsfrom langchain.load.dump import dumpsfrom langchain.load.load import loadsfrom langchain.schema import ChatGeneration, Generation+from langchain.schema.cache import RETURN_VAL_TYPE, BaseCache+from langchain.utils import get_from_envfrom langchain.vectorstores.redis import Redis as RedisVectorstorelogger = logging.getLogger(__file__)@@ -64,8 +63,6 @@ logger = logging.getLogger(__file__)if TYPE_CHECKING:import momento-RETURN_VAL_TYPE = Sequence[Generation]-def _hash(_input: str) -> str:"""Use a deterministic hashing approach."""@@ -105,22 +102,6 @@ def _load_generations_from_json(generations_json: str) -> RETURN_VAL_TYPE:)-class BaseCache(ABC):- """Base interface for cache."""-- @abstractmethod- def lookup(self, prompt: str, llm_string: str) -> Optional[RETURN_VAL_TYPE]:- """Look up based on prompt and llm_string."""-- @abstractmethod- def update(self, prompt: str, llm_string: str, return_val: RETURN_VAL_TYPE) -> None:- """Update cache based on prompt and llm_string."""-- @abstractmethod- def clear(self, **kwargs: Any) -> None:- """Clear cache that can take additional keyword arguments."""--class InMemoryCache(BaseCache):"""Cache that stores things in memory."""diff --git a/libs/langchain/langchain/schema/__init__.py b/libs/langchain/langchain/schema/__init__.pyindex aec8a1e391..be830b10aa 100644--- a/libs/langchain/langchain/schema/__init__.py+++ b/libs/langchain/langchain/schema/__init__.py@@ -1,5 +1,6 @@"""**Schemas** are the LangChain Base Classes and Interfaces."""from langchain.schema.agent import AgentAction, AgentFinish+from langchain.schema.cache import BaseCachefrom langchain.schema.chat_history import BaseChatMessageHistoryfrom langchain.schema.document import BaseDocumentTransformer, Documentfrom langchain.schema.exceptions import LangChainException@@ -39,6 +40,7 @@ RUN_KEY = "__run"Memory = BaseMemory__all__ = [+ "BaseCache","BaseMemory","BaseStore","AgentFinish",diff --git a/libs/langchain/langchain/schema/cache.py b/libs/langchain/langchain/schema/cache.pynew file mode 100644index 0000000000..7adb07fd1d--- /dev/null+++ b/libs/langchain/langchain/schema/cache.py@@ -0,0 +1,24 @@+from __future__ import annotations++from abc import ABC, abstractmethod+from typing import Any, Optional, Sequence++from langchain.schema.output import Generation++RETURN_VAL_TYPE = Sequence[Generation]+++class BaseCache(ABC):+ """Base interface for cache."""++ @abstractmethod+ def lookup(self, prompt: str, llm_string: str) -> Optional[RETURN_VAL_TYPE]:+ """Look up based on prompt and llm_string."""++ @abstractmethod+ def update(self, prompt: str, llm_string: str, return_val: RETURN_VAL_TYPE) -> None:+ """Update cache based on prompt and llm_string."""++ @abstractmethod+ def clear(self, **kwargs: Any) -> None:+ """Clear cache that can take additional keyword arguments."""