diff --git a/backend/configuration.py b/backend/configuration.py index e8a5be6c..7c1dee7f 100644 --- a/backend/configuration.py +++ b/backend/configuration.py @@ -71,6 +71,14 @@ class BaseConfiguration: }, ) + # Tilføj denne nye parameter for fagspecifik søgning + fag: Optional[str] = field( + default=None, + metadata={ + "description": "Fagområde for søgning (fx 'jura', 'matematik'). Bestemmer hvilken kollektion der søges i." + }, + ) + @classmethod def from_runnable_config( cls: Type[T], config: Optional[RunnableConfig] = None @@ -91,4 +99,4 @@ def from_runnable_config( return cls(**{k: v for k, v in configurable.items() if k in _fields}) -T = TypeVar("T", bound=BaseConfiguration) +T = TypeVar("T", bound=BaseConfiguration) \ No newline at end of file diff --git a/backend/retrieval.py b/backend/retrieval.py index 585a29b5..9488dd8a 100644 --- a/backend/retrieval.py +++ b/backend/retrieval.py @@ -1,6 +1,6 @@ import os from contextlib import contextmanager -from typing import Iterator +from typing import Iterator, Optional import weaviate from langchain_core.embeddings import Embeddings @@ -35,9 +35,23 @@ def make_weaviate_retriever( ), skip_init_checks=True, ) as weaviate_client: + # Få fag fra konfigurationen, hvis det findes + fag = getattr(configuration, 'fag', None) + + # Bestem collection navnet baseret på fag eller brug fallback + if fag: + # Brug fagspecifik collection + collection_name = f"{fag}_Pensum" + else: + # Fallback: Brug miljøvariabel eller konstant + collection_name = os.environ.get("WEAVIATE_INDEX_NAME", WEAVIATE_DOCS_INDEX_NAME) + + # Brug den bestemte collection + print(f"Søger i collection: {collection_name}") + store = WeaviateVectorStore( client=weaviate_client, - index_name=WEAVIATE_DOCS_INDEX_NAME, + index_name=collection_name, # Nu bruger vi det dynamiske collection navn text_key="text", embedding=embedding_model, attributes=["source", "title"], @@ -63,4 +77,4 @@ def make_retriever( "Unrecognized retriever_provider in configuration. " f"Expected one of: {', '.join(BaseConfiguration.__annotations__['retriever_provider'].__args__)}\n" f"Got: {configuration.retriever_provider}" - ) + ) \ No newline at end of file