From 418ae87eb6321d60e0edafb22a3b32e790545992 Mon Sep 17 00:00:00 2001 From: oskibosk0109 Date: Mon, 21 Apr 2025 12:42:29 +0100 Subject: [PATCH] =?UTF-8?q?Tilf=C3=B8jet=20fagspecifik-collections=20branc?= =?UTF-8?q?h?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/configuration.py | 10 +++++++++- backend/retrieval.py | 20 +++++++++++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) 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