Skip to content

The agent engineering platform.

mv base cache to schema (#9953)

Bagatur9828701de1e9
diff --git a/libs/langchain/langchain/__init__.py b/libs/langchain/langchain/__init__.py
index 32ad552ecc..721769d0e2 100644
--- a/libs/langchain/langchain/__init__.py
+++ b/libs/langchain/langchain/__init__.py
@@ -4,7 +4,6 @@ from importlib import metadata
from typing import Optional
from langchain.agents import MRKLChain, ReActChain, SelfAskWithSearchChain
-from langchain.cache import BaseCache
from langchain.chains import (
ConversationChain,
LLMBashChain,
@@ -40,6 +39,7 @@ from langchain.prompts import (
Prompt,
PromptTemplate,
)
+from langchain.schema.cache import BaseCache
from langchain.schema.prompt_template import BasePromptTemplate
from langchain.utilities.arxiv import ArxivAPIWrapper
from langchain.utilities.golden_query import GoldenQueryAPIWrapper
diff --git a/libs/langchain/langchain/cache.py b/libs/langchain/langchain/cache.py
index 3160fbae6a..4364c33e1a 100644
--- a/libs/langchain/langchain/cache.py
+++ b/libs/langchain/langchain/cache.py
@@ -26,7 +26,6 @@ import inspect
import json
import logging
import warnings
-from abc import ABC, abstractmethod
from datetime import timedelta
from 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, select
from sqlalchemy.engine.base import Engine
from sqlalchemy.orm import Session
-from langchain.utils import get_from_env
-
try:
from sqlalchemy.orm import declarative_base
except ImportError:
from sqlalchemy.ext.declarative import declarative_base
+
from langchain.embeddings.base import Embeddings
from langchain.load.dump import dumps
from langchain.load.load import loads
from langchain.schema import ChatGeneration, Generation
+from langchain.schema.cache import RETURN_VAL_TYPE, BaseCache
+from langchain.utils import get_from_env
from langchain.vectorstores.redis import Redis as RedisVectorstore
logger = 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__.py
index 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 BaseCache
from langchain.schema.chat_history import BaseChatMessageHistory
from langchain.schema.document import BaseDocumentTransformer, Document
from 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.py
new file mode 100644
index 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."""