Skip to content

Get Vector Store

Create or retrieve a Vectorstore instance for PDF RAG.

get_vectorstore(embedding_model, config, force_new=False)

Factory function to get or create a Vectorstore instance. Ensures the same instance is reused across the application.

Parameters:

Name Type Description Default
embedding_model Embeddings

The embedding model to use

required
config Any

Configuration object

required
force_new bool

Force creation of a new instance

False

Returns:

Type Description
Vectorstore

Vectorstore instance

Source code in aiagents4pharma/talk2scholars/tools/pdf/utils/get_vectorstore.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
def get_vectorstore(
    embedding_model: Embeddings, config: Any, force_new: bool = False
) -> "Vectorstore":
    """
    Factory function to get or create a Vectorstore instance.
    Ensures the same instance is reused across the application.

    Args:
        embedding_model: The embedding model to use
        config: Configuration object
        force_new: Force creation of a new instance

    Returns:
        Vectorstore instance
    """
    collection_name = config.milvus.collection_name if config else "pdf_rag_documents"

    with _cache_lock:
        if force_new and collection_name in _vectorstore_cache:
            del _vectorstore_cache[collection_name]
            logger.info(
                "Forced new Vectorstore instance for collection: %s", collection_name
            )

        if collection_name not in _vectorstore_cache:
            logger.info(
                "Creating new Vectorstore instance for collection: %s", collection_name
            )
            _vectorstore_cache[collection_name] = Vectorstore(
                embedding_model=embedding_model, config=config
            )
        else:
            logger.info(
                "Reusing existing Vectorstore instance for collection: %s",
                collection_name,
            )
            # Update embedding model if different
            existing = _vectorstore_cache[collection_name]
            if existing.embedding_model != embedding_model:
                logger.warning("Embedding model changed, updating existing instance")
                existing.embedding_model = embedding_model
                existing.vector_store.embedding_function = embedding_model

        return _vectorstore_cache[collection_name]