Skip to content
Snippets Groups Projects
Unverified Commit bbd5b8dd authored by Huu Le (Lee)'s avatar Huu Le (Lee) Committed by GitHub
Browse files

fix: Reuse PG vector store to avoid recreating sqlalchemy engine (#95)

parent 260d37a3
No related branches found
No related tags found
No related merge requests found
---
"create-llama": patch
---
Fix postgres connection leaking issue
import logging
import os import os
import logging
from datetime import timedelta
from cachetools import cached, TTLCache
from llama_index.core.storage import StorageContext from llama_index.core.storage import StorageContext
from llama_index.core.indices import load_index_from_storage from llama_index.core.indices import load_index_from_storage
logger = logging.getLogger("uvicorn") logger = logging.getLogger("uvicorn")
@cached(
TTLCache(maxsize=10, ttl=timedelta(minutes=5).total_seconds()),
key=lambda *args, **kwargs: "global_storage_context",
)
def get_storage_context(persist_dir: str) -> StorageContext:
return StorageContext.from_defaults(persist_dir=persist_dir)
def get_index(): def get_index():
storage_dir = os.getenv("STORAGE_DIR", "storage") storage_dir = os.getenv("STORAGE_DIR", "storage")
# check if storage already exists # check if storage already exists
...@@ -14,7 +24,7 @@ def get_index(): ...@@ -14,7 +24,7 @@ def get_index():
return None return None
# load the existing index # load the existing index
logger.info(f"Loading index from {storage_dir}...") logger.info(f"Loading index from {storage_dir}...")
storage_context = StorageContext.from_defaults(persist_dir=storage_dir) storage_context = get_storage_context(storage_dir)
index = load_index_from_storage(storage_context) index = load_index_from_storage(storage_context)
logger.info(f"Finished loading index from {storage_dir}") logger.info(f"Finished loading index from {storage_dir}")
return index return index
...@@ -5,26 +5,33 @@ from urllib.parse import urlparse ...@@ -5,26 +5,33 @@ from urllib.parse import urlparse
PGVECTOR_SCHEMA = "public" PGVECTOR_SCHEMA = "public"
PGVECTOR_TABLE = "llamaindex_embedding" PGVECTOR_TABLE = "llamaindex_embedding"
vector_store: PGVectorStore = None
def get_vector_store(): def get_vector_store():
original_conn_string = os.environ.get("PG_CONNECTION_STRING") global vector_store
if original_conn_string is None or original_conn_string == "":
raise ValueError("PG_CONNECTION_STRING environment variable is not set.") if vector_store is None:
original_conn_string = os.environ.get("PG_CONNECTION_STRING")
# The PGVectorStore requires both two connection strings, one for psycopg2 and one for asyncpg if original_conn_string is None or original_conn_string == "":
# Update the configured scheme with the psycopg2 and asyncpg schemes raise ValueError("PG_CONNECTION_STRING environment variable is not set.")
original_scheme = urlparse(original_conn_string).scheme + "://"
conn_string = original_conn_string.replace( # The PGVectorStore requires both two connection strings, one for psycopg2 and one for asyncpg
original_scheme, "postgresql+psycopg2://" # Update the configured scheme with the psycopg2 and asyncpg schemes
) original_scheme = urlparse(original_conn_string).scheme + "://"
async_conn_string = original_conn_string.replace( conn_string = original_conn_string.replace(
original_scheme, "postgresql+asyncpg://" original_scheme, "postgresql+psycopg2://"
) )
async_conn_string = original_conn_string.replace(
return PGVectorStore( original_scheme, "postgresql+asyncpg://"
connection_string=conn_string, )
async_connection_string=async_conn_string,
schema_name=PGVECTOR_SCHEMA, vector_store = PGVectorStore(
table_name=PGVECTOR_TABLE, connection_string=conn_string,
embed_dim=int(os.environ.get("EMBEDDING_DIM", 1024)), async_connection_string=async_conn_string,
) schema_name=PGVECTOR_SCHEMA,
table_name=PGVECTOR_TABLE,
embed_dim=int(os.environ.get("EMBEDDING_DIM", 1024)),
)
return vector_store
...@@ -16,6 +16,7 @@ python-dotenv = "^1.0.0" ...@@ -16,6 +16,7 @@ python-dotenv = "^1.0.0"
aiostream = "^0.5.2" aiostream = "^0.5.2"
llama-index = "0.10.28" llama-index = "0.10.28"
llama-index-core = "0.10.28" llama-index-core = "0.10.28"
cachetools = "^5.3.3"
[build-system] [build-system]
requires = ["poetry-core"] requires = ["poetry-core"]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment