mirror of
https://github.com/hwchase17/langchain.git
synced 2025-08-11 05:45:01 +00:00
add sqlalchemy generic cache (#361)
Created a generic SQLAlchemyCache class to plug any database supported by SQAlchemy. (I am using Postgres). I also based the class SQLiteCache class on this class SQLAlchemyCache. As a side note, I'm questioning the need for two distinct class LLMCache, FullLLMCache. Shouldn't we merge both ?
This commit is contained in:
parent
809a9f485f
commit
85c1bd2cd0
@ -373,12 +373,19 @@
|
|||||||
"id": "6053408b",
|
"id": "6053408b",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": []
|
"source": [
|
||||||
|
"# You can use SQLAlchemyCache to cache with any SQL database supported by SQLAlchemy.\n",
|
||||||
|
"from langchain.cache import SQLAlchemyCache\n",
|
||||||
|
"from sqlalchemy import create_engine\n",
|
||||||
|
"\n",
|
||||||
|
"engine = create_engine(\"postgresql://postgres:postgres@localhost:5432/postgres\")\n",
|
||||||
|
"langchain.llm_cache = SQLAlchemyCache(engine)"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"kernelspec": {
|
"kernelspec": {
|
||||||
"display_name": "Python 3 (ipykernel)",
|
"display_name": "base",
|
||||||
"language": "python",
|
"language": "python",
|
||||||
"name": "python3"
|
"name": "python3"
|
||||||
},
|
},
|
||||||
@ -392,7 +399,12 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.10.8"
|
"version": "3.9.12 (main, Jun 1 2022, 06:34:44) \n[Clang 12.0.0 ]"
|
||||||
|
},
|
||||||
|
"vscode": {
|
||||||
|
"interpreter": {
|
||||||
|
"hash": "1235b9b19e8e9828b5c1fdb2cd89fe8d3de0fcde5ef5f3db36e4b671adb8660f"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
@ -3,6 +3,7 @@ from abc import ABC, abstractmethod
|
|||||||
from typing import Dict, List, Optional, Tuple, Union
|
from typing import Dict, List, Optional, Tuple, Union
|
||||||
|
|
||||||
from sqlalchemy import Column, Integer, String, create_engine, select
|
from sqlalchemy import Column, Integer, String, create_engine, select
|
||||||
|
from sqlalchemy.engine.base import Engine
|
||||||
from sqlalchemy.ext.declarative import declarative_base
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
@ -61,12 +62,12 @@ class FullLLMCache(Base): # type: ignore
|
|||||||
response = Column(String)
|
response = Column(String)
|
||||||
|
|
||||||
|
|
||||||
class SQLiteCache(BaseCache):
|
class SQLAlchemyCache(BaseCache):
|
||||||
"""Cache that uses SQLite as a backend."""
|
"""Cache that uses SQAlchemy as a backend."""
|
||||||
|
|
||||||
def __init__(self, database_path: str = ".langchain.db"):
|
def __init__(self, engine: Engine):
|
||||||
"""Initialize by creating the engine and all tables."""
|
"""Initialize by creating all tables."""
|
||||||
self.engine = create_engine(f"sqlite:///{database_path}")
|
self.engine = engine
|
||||||
Base.metadata.create_all(self.engine)
|
Base.metadata.create_all(self.engine)
|
||||||
|
|
||||||
def lookup(self, prompt: str, llm_string: str) -> Optional[RETURN_VAL_TYPE]:
|
def lookup(self, prompt: str, llm_string: str) -> Optional[RETURN_VAL_TYPE]:
|
||||||
@ -106,3 +107,12 @@ class SQLiteCache(BaseCache):
|
|||||||
)
|
)
|
||||||
with Session(self.engine) as session, session.begin():
|
with Session(self.engine) as session, session.begin():
|
||||||
session.add(item)
|
session.add(item)
|
||||||
|
|
||||||
|
|
||||||
|
class SQLiteCache(SQLAlchemyCache):
|
||||||
|
"""Cache that uses SQLite as a backend."""
|
||||||
|
|
||||||
|
def __init__(self, database_path: str = ".langchain.db"):
|
||||||
|
"""Initialize by creating the engine and all tables."""
|
||||||
|
engine = create_engine(f"sqlite:///{database_path}")
|
||||||
|
super().__init__(engine)
|
||||||
|
Loading…
Reference in New Issue
Block a user