Skip to content

Commit d976d51

Browse files
authored
feat: Update frontend to use new RAG endpoint (#113)
1 parent ad6d1ef commit d976d51

3 files changed

Lines changed: 40 additions & 5 deletions

File tree

app/src/app_config.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111

1212
class AppConfig(PydanticBaseEnvConfig):
1313
environment: str = "local"
14+
# Use the similar syntax as the preview environment bucket names
15+
# Note: since there's a single ChromaDB instance for all environments,
16+
# there can be collision if multiple developers are running RAG simultaneously,
17+
# but this is unlikely at this time.
18+
bucket_name: str = "local-labs-referral-pilot-app-dev"
1419
# Set HOST to 127.0.0.1 by default to avoid other machines on the network
1520
# from accessing the application. This is especially important if you are
1621
# running the application locally on a public network. This needs to be
@@ -63,8 +68,9 @@ def chroma_client(self) -> ClientAPI:
6368
return chromadb.HttpClient(host=self.rag_db_host, port=self.rag_db_port)
6469

6570
def chroma_document_store(self) -> ChromaDocumentStore:
71+
# Use bucket name as part of collection name to avoid collision with `dev` environment for PRs
6672
return ChromaDocumentStore(
67-
collection_name=f"{self.collection_name_prefix}_{self.environment}",
73+
collection_name=f"{self.collection_name_prefix}_{self.bucket_name}",
6874
host=self.rag_db_host,
6975
port=self.rag_db_port,
7076
)

app/src/ingestion/rag_utils.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
from pathlib import Path
44

55
from botocore.exceptions import NoCredentialsError
6+
from chromadb.api import ClientAPI
67
from haystack import Pipeline
78
from haystack.components.converters import MultiFileConverter
89
from haystack.components.embedders import SentenceTransformersDocumentEmbedder
910
from haystack.components.preprocessors import DocumentPreprocessor
1011
from haystack.components.writers import DocumentWriter
12+
from haystack.document_stores.errors.errors import DocumentStoreError
1113
from haystack_integrations.document_stores.chroma import ChromaDocumentStore
1214

1315
from src.app_config import config
@@ -16,19 +18,40 @@
1618
logger = logging.getLogger(__name__)
1719

1820

21+
def delete_preview_collections(chroma_client: ClientAPI) -> None:
22+
collections = chroma_client.list_collections()
23+
for collection in collections:
24+
name = collection.name
25+
if name.startswith(f"{config.collection_name_prefix}_p-"):
26+
logger.info("Deleting preview collection: %s", name)
27+
chroma_client.delete_collection(name)
28+
29+
1930
def populate_vector_db() -> None:
2031
logging.basicConfig(format="%(levelname)s - %(name)s - %(message)s", level=logging.INFO)
2132

2233
chroma_client = config.chroma_client()
34+
delete_preview_collections(chroma_client)
35+
2336
logger.info("ChromaDB collections: %s", chroma_client.list_collections())
2437
doc_store = config.chroma_document_store()
2538
collection_name = doc_store._collection_name
2639

2740
# Clear existing collection if any
2841
if doc_store.count_documents() > 0:
29-
# Don't delete collection since it's referenced by existing pipelines upon their startup
30-
logger.info("Clearing out existing vector DB collection=%r", collection_name)
31-
doc_store.delete_all_documents()
42+
try:
43+
# Don't delete collection since it's referenced by existing pipelines upon their startup
44+
logger.info("Clearing out existing vector DB collection=%r", collection_name)
45+
# recreate_index=True results in a new id for the collection, which breaks existing pipelines
46+
doc_store.delete_all_documents(recreate_index=False)
47+
except DocumentStoreError as e:
48+
# Ignore this error from haystack.logging, which is okay since logging is the last step in delete_all_documents
49+
if "overwrite 'name' in LogRecord" in str(e):
50+
logger.info("Ignoring expected DocumentStoreError: %s", e)
51+
else:
52+
logger.warning("Unexpected DocumentStoreError: %s", e)
53+
raise
54+
assert doc_store.count_documents() == 0, "Documents should be deleted from collection"
3255

3356
# Download files from S3
3457
local_folder = download_s3_folder_to_local()

frontend/src/util/fetchResources.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ export async function fetchResources(
77
prompt_version_id: string | null,
88
) {
99
const apiDomain = await getApiDomain();
10-
const url = apiDomain + "generate_referrals/run";
10+
11+
const useNonRag = process.env.NEXT_PUBLIC_USE_NONRAG === "true";
12+
const url_path = useNonRag
13+
? "generate_referrals/run"
14+
: "generate_referrals_rag/run";
15+
16+
const url = apiDomain + url_path;
1117
const headers = {
1218
"Content-Type": "application/json",
1319
};

0 commit comments

Comments
 (0)