diff --git a/0.7/objects.inv b/0.7/objects.inv index e651aef..6b0e614 100644 Binary files a/0.7/objects.inv and b/0.7/objects.inv differ diff --git a/0.7/reference/gerd/frontends/generate/index.html b/0.7/reference/gerd/frontends/generate/index.html index d89a661..ff5d30c 100644 --- a/0.7/reference/gerd/frontends/generate/index.html +++ b/0.7/reference/gerd/frontends/generate/index.html @@ -893,6 +893,30 @@ + + +
  • + + +  upload_lora + + + + +
  • @@ -1737,6 +1761,14 @@

    + + upload_lora + +
    +

    Upload a LoRA archive.

    +
    + + @@ -1967,10 +1999,7 @@

    Source code in gerd/frontends/generate.py -
    57
    -58
    -59
    -60
    +              
    60
     61
     62
     63
    @@ -1985,25 +2014,28 @@ 

    72 73 74 -75

    def generate(textbox: str, temp: float, top_p: float, max_tokens: int) -> str:
    -    """Generate text from the model.
    -
    -    Parameters:
    -        textbox: The text to generate from
    -        temp: The temperature for the generation
    -        top_p: The top p value for the generation
    -        max_tokens: The maximum number of tokens to generate
    -
    -    Returns:
    -        The generated text
    -    """
    -    if Global.service is None:
    -        msg = "Model not loaded"
    -        raise gr.Error(msg)
    -    Global.service.config.model.top_p = top_p
    -    Global.service.config.model.temperature = temp
    -    Global.service.config.model.max_new_tokens = max_tokens
    -    return Global.service.generate({"message": textbox}).text
    +75
    +76
    +77
    +78
    def generate(textbox: str, temp: float, top_p: float, max_tokens: int) -> str:
    +    """Generate text from the model.
    +
    +    Parameters:
    +        textbox: The text to generate from
    +        temp: The temperature for the generation
    +        top_p: The top p value for the generation
    +        max_tokens: The maximum number of tokens to generate
    +
    +    Returns:
    +        The generated text
    +    """
    +    if Global.service is None:
    +        msg = "Model not loaded"
    +        raise gr.Error(msg)
    +    Global.service.config.model.top_p = top_p
    +    Global.service.config.model.temperature = temp
    +    Global.service.config.model.max_new_tokens = max_tokens
    +    return Global.service.generate({"message": textbox}).text
     
    @@ -2097,10 +2129,7 @@

    Source code in gerd/frontends/generate.py -
    37
    -38
    -39
    -40
    +              
    40
     41
     42
     43
    @@ -2114,24 +2143,137 @@ 

    51 52 53 -54

    def load_model(model_name: str, origin: str) -> dict[str, Any]:
    -    """Load a global large language model.
    -
    -    Parameters:
    -        model_name: The name of the model
    -        origin: Whether to use an extra LoRA
    -
    -    Returns:
    -        The updated interactive state, returns interactive=True when the model is loaded
    -    """
    -    if Global.service is not None:
    -        del Global.service
    -    model_config = config.model_copy(deep=True)
    -    model_config.model.name = model_name
    -    if origin != "None":
    -        model_config.model.loras.add(lora_dir / origin)
    -    Global.service = ChatService(model_config)
    -    return gr.update(interactive=True)
    +54
    +55
    +56
    +57
    def load_model(model_name: str, origin: str) -> dict[str, Any]:
    +    """Load a global large language model.
    +
    +    Parameters:
    +        model_name: The name of the model
    +        origin: Whether to use an extra LoRA
    +
    +    Returns:
    +        The updated interactive state, returns interactive=True when the model is loaded
    +    """
    +    if Global.service is not None:
    +        del Global.service
    +    model_config = config.model_copy(deep=True)
    +    model_config.model.name = model_name
    +    if origin != "None":
    +        model_config.model.loras.add(lora_dir / origin)
    +    Global.service = ChatService(model_config)
    +    return gr.update(interactive=True)
    +
    + +
    + + + +
    + + +

    + upload_lora + + +

    +
    upload_lora(file_upload: str) -> str
    +
    + +
    + +

    Upload a LoRA archive.

    + + +

    Parameters:

    + + + + + + + + + + + + + + + + + +
    NameTypeDescriptionDefault
    +

    file_upload +

    + str + +
    +

    The path to the uploaded archive

    +
    +
    + required +
    + + +

    Returns:

    + + + + + + + + + + + + + +
    TypeDescription
    + str + +
    +

    an empty string to clear the input

    +
    +
    + +
    + Source code in gerd/frontends/generate.py +
    81
    +82
    +83
    +84
    +85
    +86
    +87
    +88
    +89
    +90
    +91
    +92
    +93
    +94
    +95
    +96
    +97
    def upload_lora(file_upload: str) -> str:
    +    """Upload a LoRA archive.
    +
    +    Parameters:
    +        file_upload: The path to the uploaded archive
    +
    +    Returns:
    +        an empty string to clear the input
    +    """
    +    if file_upload is None:
    +        gr.Warning("No file uploaded")
    +    p = Path(file_upload)
    +    if p.exists() is False:
    +        gr.Warning("File does not exist")
    +    shutil.unpack_archive(p.as_posix(), lora_dir / p.stem)
    +    gr.Info(f"Uploaded '{p.stem}'")
    +    return ""
     
    diff --git a/0.7/reference/gerd/training/trainer/index.html b/0.7/reference/gerd/training/trainer/index.html index ecefea0..b05d717 100644 --- a/0.7/reference/gerd/training/trainer/index.html +++ b/0.7/reference/gerd/training/trainer/index.html @@ -3064,7 +3064,8 @@

    239 240 241 -242

    def __init__(
    +242
    +243
    def __init__(
         self,
         config: LoraTrainingConfig,
         callback_cls: Optional[List[Type[transformers.TrainerCallback]]] = None,
    @@ -3142,10 +3143,11 @@ 

    ddp_find_unused_parameters=None, use_ipex=config.flags.use_ipex, save_steps=config.save_steps, - # any of these two will set `torch_compile=True` - # torch_compile_backend="inductor", - # torch_compile_mode="reduce-overhead", - ) + use_cpu=config.flags.use_cpu, + # any of these two will set `torch_compile=True` + # torch_compile_backend="inductor", + # torch_compile_mode="reduce-overhead", + )

    @@ -3178,11 +3180,11 @@

    Source code in gerd/training/trainer.py -
    def interrupt(self) -> None:
    -    """Interrupt the training process."""
    -    self.tracked.interrupted = True
    +              
    def interrupt(self) -> None:
    +    """Interrupt the training process."""
    +    self.tracked.interrupted = True
     
    @@ -3207,9 +3209,7 @@

    Source code in gerd/training/trainer.py -
    352
    -353
    -354
    +              
    354
     355
     356
     357
    @@ -3222,22 +3222,24 @@ 

    364 365 366 -367

    def save(self) -> None:
    -    """Save the model and log files to the path set in the trainer configuration.
    -
    -    When the `zip_output` flag is set, the output directory is zipped as well.
    -    """
    -    if self.trainer is not None:
    -        self.trainer.save_model(self.config.output_dir)
    -        if self.config.zip_output:
    -            shutil.make_archive(
    -                base_name=self.config.output_dir.as_posix(),
    -                root_dir=self.config.output_dir,
    -                # base_dir=self.config.output_dir,
    -                format="zip",
    -            )
    -    else:
    -        _LOGGER.warning("Trainer not initialized")
    +367
    +368
    +369
    def save(self) -> None:
    +    """Save the model and log files to the path set in the trainer configuration.
    +
    +    When the `zip_output` flag is set, the output directory is zipped as well.
    +    """
    +    if self.trainer is not None:
    +        self.trainer.save_model(self.config.output_dir)
    +        if self.config.zip_output:
    +            shutil.make_archive(
    +                base_name=self.config.output_dir.as_posix(),
    +                root_dir=self.config.output_dir,
    +                # base_dir=self.config.output_dir,
    +                format="zip",
    +            )
    +    else:
    +        _LOGGER.warning("Trainer not initialized")
     
    @@ -3324,8 +3326,7 @@

    Source code in gerd/training/trainer.py -
    309
    -310
    +              
    310
     311
     312
     313
    @@ -3365,48 +3366,51 @@ 

    347 348 349 -350

    def setup_training(
    -    self,
    -    train_data: Dataset,
    -    train_template: Dict,
    -    torch_compile: bool = False,
    -) -> None:
    -    """Setup the training process and initialize the transformer trainer.
    -
    -    Parameters:
    -        train_data: The training data
    -        train_template: The training template
    -        torch_compile: Whether to use torch compile
    -    """
    -    self.trainer = transformers.Trainer(
    -        model=self.lora_model,
    -        train_dataset=train_data,
    -        eval_dataset=None,
    -        args=self.args,
    -        data_collator=transformers.DataCollatorForLanguageModeling(
    -            self.config.tokenizer, mlm=False
    -        ),
    -        callbacks=self.callbacks,
    -    )
    -
    -    # This must be done after Trainer init because otherwise
    -    # the trainer cannot identify the relevant (as in 'trainable') parameters
    -    # and will remove required information from the training data set.
    -    # Whether it is useful to compile after reassignment.
    -    if torch_compile and torch.__version__ >= "2" and sys.platform != "win32":
    -        self.lora_model = torch.compile(self.lora_model)  # type: ignore[assignment]
    -
    -    # == Save parameters for reuse ==
    -    with open(
    -        f"{self.config.output_dir}/training_parameters.json", "w", encoding="utf-8"
    -    ) as file:
    -        file.write(self.config.model_dump_json(indent=2))
    -
    -    # == Save training prompt ==
    -    with open(
    -        f"{self.config.output_dir}/training_prompt.json", "w", encoding="utf-8"
    -    ) as file:
    -        json.dump(train_template, file, indent=2)
    +350
    +351
    +352
    def setup_training(
    +    self,
    +    train_data: Dataset,
    +    train_template: Dict,
    +    torch_compile: bool = False,
    +) -> None:
    +    """Setup the training process and initialize the transformer trainer.
    +
    +    Parameters:
    +        train_data: The training data
    +        train_template: The training template
    +        torch_compile: Whether to use torch compile
    +    """
    +    _LOGGER.debug("Training parameter\n============\n %s", self.args)
    +    self.trainer = transformers.Trainer(
    +        model=self.lora_model,
    +        train_dataset=train_data,
    +        eval_dataset=None,
    +        args=self.args,
    +        data_collator=transformers.DataCollatorForLanguageModeling(
    +            self.config.tokenizer, mlm=False
    +        ),
    +        callbacks=self.callbacks,
    +    )
    +
    +    # This must be done after Trainer init because otherwise
    +    # the trainer cannot identify the relevant (as in 'trainable') parameters
    +    # and will remove required information from the training data set.
    +    # Whether it is useful to compile after reassignment.
    +    if torch_compile and torch.__version__ >= "2" and sys.platform != "win32":
    +        self.lora_model = torch.compile(self.lora_model)  # type: ignore[assignment]
    +
    +    # == Save parameters for reuse ==
    +    with open(
    +        f"{self.config.output_dir}/training_parameters.json", "w", encoding="utf-8"
    +    ) as file:
    +        file.write(self.config.model_dump_json(indent=2))
    +
    +    # == Save training prompt ==
    +    with open(
    +        f"{self.config.output_dir}/training_prompt.json", "w", encoding="utf-8"
    +    ) as file:
    +        json.dump(train_template, file, indent=2)
     
    @@ -3453,8 +3457,7 @@

    Source code in gerd/training/trainer.py -
    244
    -245
    +              
    245
     246
     247
     248
    @@ -3516,70 +3519,71 @@ 

    304 305 306 -307

    def train(self) -> threading.Thread:
    -    """Start the training process.
    -
    -    Returns:
    -        The training thread
    -    """
    -    model_type = type(self.base_model).__name__
    -    model_id = MODEL_CLASSES[model_type]
    -
    -    projections_string = ", ".join(
    -        [
    -            projection.replace("_proj", "")
    -            for projection in self.config.modules.target_modules(self.base_model)
    -        ]
    -    )
    -
    -    _LOGGER.info(
    -        "Training '%s' model using (%s) projections", model_id, projections_string
    -    )
    -    # == Main run and monitor loop ==
    -
    -    log = self.tracked.train_log
    -    log.update({"base_model_config": self.config.model.name})
    -    log.update({"base_model_class": self.base_model.__class__.__name__})
    -    log.update(
    -        {
    -            "base_loaded_in_4bit": getattr(
    -                self.lora_model, "is_loaded_in_4bit", False
    -            )
    -        }
    -    )
    -    log.update(
    -        {
    -            "base_loaded_in_8bit": getattr(
    -                self.lora_model, "is_loaded_in_8bit", False
    -            )
    -        }
    -    )
    -    log.update({"projections": projections_string})
    -
    -    if self.config.stop_at_loss > 0:
    -        _LOGGER.info(
    -            "Monitoring loss \033[1;31;1m(Auto-Stop at: %f)\033[0;37;0m",
    -            self.config.stop_at_loss,
    -        )
    -
    -    def threaded_run() -> None:
    -        if self.trainer is None:
    -            msg = "Trainer not initialized"
    -            raise RuntimeError(msg)
    -        self.trainer.train()
    -        # Note: save in the thread in case the gradio thread breaks
    -        # (eg browser closed)
    -        self.save()
    -        self.tracked.did_save = True
    -        # Save log
    -        with open(
    -            f"{self.config.output_dir}/training_log.json", "w", encoding="utf-8"
    -        ) as file:
    -            json.dump(self.tracked.train_log, file, indent=2)
    -
    -    self.thread = threading.Thread(target=threaded_run)
    -    self.thread.start()
    -    return self.thread
    +307
    +308
    def train(self) -> threading.Thread:
    +    """Start the training process.
    +
    +    Returns:
    +        The training thread
    +    """
    +    model_type = type(self.base_model).__name__
    +    model_id = MODEL_CLASSES[model_type]
    +
    +    projections_string = ", ".join(
    +        [
    +            projection.replace("_proj", "")
    +            for projection in self.config.modules.target_modules(self.base_model)
    +        ]
    +    )
    +
    +    _LOGGER.info(
    +        "Training '%s' model using (%s) projections", model_id, projections_string
    +    )
    +    # == Main run and monitor loop ==
    +
    +    log = self.tracked.train_log
    +    log.update({"base_model_config": self.config.model.name})
    +    log.update({"base_model_class": self.base_model.__class__.__name__})
    +    log.update(
    +        {
    +            "base_loaded_in_4bit": getattr(
    +                self.lora_model, "is_loaded_in_4bit", False
    +            )
    +        }
    +    )
    +    log.update(
    +        {
    +            "base_loaded_in_8bit": getattr(
    +                self.lora_model, "is_loaded_in_8bit", False
    +            )
    +        }
    +    )
    +    log.update({"projections": projections_string})
    +
    +    if self.config.stop_at_loss > 0:
    +        _LOGGER.info(
    +            "Monitoring loss \033[1;31;1m(Auto-Stop at: %f)\033[0;37;0m",
    +            self.config.stop_at_loss,
    +        )
    +
    +    def threaded_run() -> None:
    +        if self.trainer is None:
    +            msg = "Trainer not initialized"
    +            raise RuntimeError(msg)
    +        self.trainer.train()
    +        # Note: save in the thread in case the gradio thread breaks
    +        # (eg browser closed)
    +        self.save()
    +        self.tracked.did_save = True
    +        # Save log
    +        with open(
    +            f"{self.config.output_dir}/training_log.json", "w", encoding="utf-8"
    +        ) as file:
    +            json.dump(self.tracked.train_log, file, indent=2)
    +
    +    self.thread = threading.Thread(target=threaded_run)
    +    self.thread.start()
    +    return self.thread
     
    diff --git a/0.7/search/search_index.json b/0.7/search/search_index.json index becfda6..546f3a9 100644 --- a/0.7/search/search_index.json +++ b/0.7/search/search_index.json @@ -1 +1 @@ -{"config":{"lang":["en"],"separator":"[\\s\\-]+","pipeline":["stopWordFilter"]},"docs":[{"location":"","title":"Generating and evaluating relevant documentation","text":"

    GERD is developed as an experimental library to investigate how large language models (LLMs) can be used to generate and analyze (sets of) documents.

    This project was initially forked from Llama-2-Open-Source-LLM-CPU-Inference by Kenneth Leung.

    "},{"location":"#quickstart","title":"Quickstart","text":"

    If you just want to it try out, you can clone the project and install dependencies with pip:

    git clone https://github.com/caretech-owl/gerd.git\ncd gerd\npip install -e \".[full]\"\npython examples/hello.py\n
    Source: examples/hello.py
    \"\"\"A 'hello world' example for GERD.\"\"\"\n\nimport logging\n\nfrom gerd.config import load_gen_config\nfrom gerd.gen.chat_service import ChatService\nfrom gerd.models.model import PromptConfig\n\nlogging.basicConfig(level=logging.WARNING)\nlogging.getLogger(\"gerd\").setLevel(logging.DEBUG)\n\nlogging.info(\n    \"Loading chat service...\"\n    \" When this is the first time you run this script, it will download the model.\"\n    \" This may take a few minutes.\"\n)\n\nchat = ChatService(load_gen_config(\"hello\"))\nres = chat.submit_user_message({\"word\": \"teleportation\"})\nlogging.info(res)\n\nchat.set_prompt_config(PromptConfig.model_validate({\"text\": \"{message}\"}))\nres = chat.submit_user_message({\"message\": \"Hello! What is one plus one?\"})\nlogging.info(res)\n

    If you want to try this out in your browser, head over to binder \ud83d\udc49 . Note that running LLMs on the CPU (and especially on limited virtual machines like binder) takes some time.

    "},{"location":"#question-and-answer-example","title":"Question and Answer Example","text":"

    Follow quickstart but execute gradio with the qa_frontend instead of the example file. When the server is done loading, open http://127.0.0.1:7860 in your browser.

    gradio gerd/frontends/qa_frontend.py\n# Some Llama.cpp outut\n# ...\n# * Running on local URL:  http://127.0.0.1:7860\n

    Click the 'Click to Upload' button and search for a GRASCCO document named Caja.txt which is located in the tests/data/grascoo folder and upload it into the vector store. Next, you can query information from the document. For instance Wie hei\u00dft der Patient? (What is the patient called?).

    "},{"location":"#prompt-chaining","title":"Prompt Chaining","text":"

    Prompt chaining is a prompt engineering approach to increase the 'reflection' of a large language model onto its given answer. Check examples/chaining.py for an illustration.

    python examples/chaining.py\n# ...\n====== Resolved prompt =====\n\nsystem: You are a helpful assistant. Please answer the following question in a truthful and brief manner.\nuser: What type of mammal lays the biggest eggs?\n\n# ...\nResult: Based on the given information, the largest egg-laying mammal is the blue whale, which can lay up to 100 million eggs per year. However, the other assertions provided do not align with this information.\n
    Source: examples/chaining.py
    \"\"\"A chaining example for the chat service.\"\"\"\n\nimport logging\n\nfrom gerd.config import load_gen_config\nfrom gerd.gen.chat_service import ChatService\n\nlogging.basicConfig(level=logging.DEBUG)\n\ngen = ChatService(load_gen_config(\"gen_chaining\"))\nres = gen.generate({\"question\": \"What type of mammal lays the biggest eggs?\"})\nprint(f\"Result: {res.text}\")  # noqa: T201\n
    Config: config/gen_chaining.yml
    model:\n  name: \"Qwen/Qwen2.5-0.5B-Instruct\"\n  temperature: 0.05\n  max_new_tokens: 512\n  context_length: 2048\n  prompt_setup:\n    - [\"system\", text: \"You are a helpful assistant. Please answer the following question in a truthful and brief manner.\"]\nfeatures:\n  prompt_chaining:\n    prompts:\n      - text: \"{question}\"\n      - text: \"Here is a statement:\\n{response_1}\\n\\nMake a bullet point list of the assumptions you made when producing the above statement.\\n\\n\"\n      - text: \"Here is a bullet point list of assertions:\\n{response_2}\\n\\nFor each assertion, determine whether it is true or false. If it is false, explain why.\\n\\n\"\n      - text: \"{response_3}\\n\\nIn light of the above facts, how would you answer the question '{question}'\"\n

    As you see, the answer does not make much sense with the default model which is rather small. Give it a try with meta-llama/Llama-3.2-3B. To use this model, you need to login with the huggingface cli and accept the Meta Community License Agreement.

    "},{"location":"#full-documentation","title":"Full Documentation","text":"

    A more detailled documentation can be found here \ud83d\udc49 .

    "},{"location":"#used-tools","title":"Used Tools","text":"
    • LangChain: Framework for developing applications powered by language models
    • C Transformers: Python bindings for the Transformer models implemented in C/C++ using GGML library
    • FAISS: Open-source library for efficient similarity search and clustering of dense vectors.
    • Sentence-Transformers (all-MiniLM-L6-v2): Open-source pre-trained transformer model for embedding text to a 384-dimensional dense vector space for tasks like
    • Poetry: Tool for dependency management and Python packaging
    "},{"location":"#files-and-content","title":"Files and Content","text":"
    • /assets: Images relevant to the project
    • /config: Configuration files for LLM applications
    • /examples: Examples that demonstrate the different usage scenarios
    • /gerd: Code related to GERD
    • /images: Images for the documentation
    • /models: Binary file of GGML quantized LLM model (i.e., Llama-2-7B-Chat)
    • /prompts: Plain text prompt files
    • /templates: Prompt files as jinja2 templates
    • /tests: Unit tests for GERD
    • /vectorstore: FAISS vector store for documents
    • pyproject.toml: TOML file to specify which versions of the dependencies used (Poetry)
    "},{"location":"#references","title":"References","text":"
    • https://github.com/kennethleungty/Llama-2-Open-Source-LLM-CPU-Inference
    • https://pubmed.ncbi.nlm.nih.gov/36073490
    • https://huggingface.co
    "},{"location":"concepts/","title":"Concepts","text":"

    GERD is primarly a tool for prototyping workflows for working with Large Language Models. It is meant to act as 'glue' between different tools and services and should ease the access to these tools.

    In general, there should be only be two components involved in a GERD workflow: A configuration and a service. The configuration can be assembled from different sources and should be able to be used in different services. The foundation of such a configration is a YAML file. GERD provides a set of those which can be found in the config directory. Configurations in this directly can be accessed by name as shown in the following example. A simple configutation file may look like this:

    model:\n  name: \"Qwen/Qwen2.5-0.5B-Instruct\"\n  temperature: 0.1\n  top_p: 0.90\n  max_new_tokens: 256\n  repetition_penalty: 1.1\n  prompt_setup:\n    - [system, text: \"You are a helpful assistant. Start your answers with 'Sure thing, buddy!'.\"]\n  prompt_config:\n    text: \"Please say the word '{word}' three times.\"\n

    And can be used with a ChatService via load_gen_config shown in the following example:

    \"\"\"A 'hello world' example for GERD.\"\"\"\n\nimport logging\n\nfrom gerd.config import load_gen_config\nfrom gerd.gen.chat_service import ChatService\nfrom gerd.models.model import PromptConfig\n\nlogging.basicConfig(level=logging.WARNING)\nlogging.getLogger(\"gerd\").setLevel(logging.DEBUG)\n\nlogging.info(\n    \"Loading chat service...\"\n    \" When this is the first time you run this script, it will download the model.\"\n    \" This may take a few minutes.\"\n)\n\nchat = ChatService(load_gen_config(\"hello\"))\nres = chat.submit_user_message({\"word\": \"teleportation\"})\nlogging.info(res)\n\nchat.set_prompt_config(PromptConfig.model_validate({\"text\": \"{message}\"}))\nres = chat.submit_user_message({\"message\": \"Hello! What is one plus one?\"})\nlogging.info(res)\n
    "},{"location":"develop/","title":"Development Guide","text":""},{"location":"develop/#basics","title":"Basics","text":"

    To get started on development you need to install uv. You can use pip, pipx or conda to do so:

    pip install uv\n

    Next install the package and all dependencies with uv sync.

    # cd <gerd_project_root>\nuv sync\n

    After that, it should be possible to run scripts without further issues:

    uv run examples/hello.py\n

    To add a new runtime dependency, just run uv add:

    uv add langchain\n

    To add a new development dependency, run uv add with the --dev flag:

    uv add mypy --dev\n
    "},{"location":"develop/#pre-commit-hooks-recommended","title":"Pre-commit hooks (recommended)","text":"

    Pre-commit hooks are used to check linting and run tests before commit changes to prevent faulty commits. Thus, it is recommended to use these hooks! Hooks should not include long running actions (such as tests) since committing should be fast. To install pre-commit hooks, execute this once:

    uv run pre-commit install\n
    "},{"location":"develop/#further-tools","title":"Further tools","text":""},{"location":"develop/#poe-task-runner","title":"Poe Task Runner","text":"

    Task runner configuration are stored in the pyproject.toml file. You can run most of the tools mentioned above with a (shorter) call to poe.

    uv run poe lint  # do some linting (with mypy)\n
    "},{"location":"develop/#pytest","title":"PyTest","text":"

    Test case are run via pytest. Tests can be found in the /tests folder. Tests will not be run via pre-commit since they might be too complex to be done before commits. To run the standard set of tests use the poe task test:

    uv run poe test\n

    More excessive testing can be trigger with test_manual which will NOT mock calls to the used models:

    uv run poe test_manual\n
    "},{"location":"develop/#ruff","title":"Ruff","text":"

    Ruff is used for linting and code formatting. Ruff follows black styling ref. Ruff will be run automatically before commits when pre-commit hooks are installed. To run ruff manually, use uv:

    uv run ruff check gerd\n

    There is a VSCode extension that handles formatting and linting.

    "},{"location":"develop/#mypy","title":"MyPy","text":"

    MyPy does static type checking. It will not be run automatically. To run MyPy manually use uv with the folder to be checked:

    uv run mypy gerd\n
    "},{"location":"develop/#implemented-guis","title":"Implemented GUIs","text":""},{"location":"develop/#run-frontend","title":"Run Frontend","text":"

    Either run Generate Frontend:

    uv run poe gen_dev\n

    or QA Frontend:

    uv run poe qa_dev\n

    or the GERD Router:

    # add _dev for the gradio live reload version\n# omit in 'prod'\nuv run router[_dev]\n
    "},{"location":"develop/#cicd-and-distribution","title":"CI/CD and Distribution","text":""},{"location":"develop/#github-actions","title":"GitHub Actions","text":"

    GitHub Actions can be found under .github/workflows. There is currently one main CI workflow called python-ci.yml:

    # This workflow will install Python dependencies, run tests and lint with a variety of Python versions\n# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions\n\nname: Linting/Testing Python\n\non:\n  push:\n    branches: [main, dev-gha]\n  pull_request:\n    branches: [main]\n\njobs:\n  linting:\n    runs-on: ubuntu-latest\n    steps:\n      - uses: actions/checkout@v4\n      - name: Set up Python 3.12\n        uses: actions/setup-python@v5\n        with:\n          python-version: \"3.12\"\n      - name: Install uv and development dependencies with extras\n        run: |\n          pipx install uv \n          uv sync\n      - name: Run linting\n        run: uv run poe lint\n  testing:\n    needs: linting\n    strategy:\n      matrix:\n        os: [ubuntu-latest, windows-latest]\n        python-version: [\"3.12\", \"3.10\"]\n    runs-on: ${{ matrix.os }}\n    steps:\n      - uses: actions/checkout@v4\n      - name: Cache HuggingFace models and data\n        uses: actions/cache@v4\n        with:\n          path: ~/.cache/huggingface\n          key: ${{ runner.os }}-hf\n      - name: Install uv\n        run: pipx install uv\n      - name: Set up Python ${{ matrix.python-version }}\n        uses: actions/setup-python@v5\n        with:\n          python-version: ${{ matrix.python-version }}\n      - name: Install development dependencies with extras\n        run: uv sync\n      - name: Run Tests\n        run: uv run poe cov\n      - name: Report Coverage\n        run: uv run coverage report --skip-covered --omit=\"tests/*\" --format=markdown >> $GITHUB_STEP_SUMMARY\n

    In its current config it will only be executed when a PR for main is created or when a special dev-gha branch is created. It will also trigger actions when commits are pushed to main directly but this should be avoided.

    "},{"location":"develop/#github-issue-templates","title":"GitHub Issue Templates","text":"

    This project uses GitHub issue templates. Currently, there are three templates available.

    "},{"location":"develop/#bug-report","title":"Bug Report","text":"
    name: Bug\ndescription: File a bug report\ntitle: \"[Bug]: \"\nlabels: [\"bug\"]\n# assignees:\n#   - aleneum\nbody:\n  - type: markdown\n    attributes:\n      value: |\n        Thanks for taking the time to fill out this bug report!\n  - type: textarea\n    id: expected-behavior\n    attributes:\n      label: Expected Behavior\n      description: What should happen\n    validations:\n      required: true\n  - type: textarea\n    id: actual-behavior\n    attributes:\n      label: Actual Behavior\n      description: What happened instead\n    validations:\n      required: true\n  - type: textarea\n    id: steps-reproduce\n    attributes:\n      label: Steps to Reproduce the Problem\n      value: |\n        1.\n        2.\n        3.\n\n    validations:\n      required: true\n  - type: input\n    id: affected-version\n    attributes:\n      label: Affected version\n      description: Version number or commit hash\n      placeholder: \"0.1.0\"\n    validations:\n      required: true\n  - type: dropdown\n    id: affected-components\n    attributes:\n      label: Affected components\n      description: Which components are affected\n      multiple: true\n      options:\n        - Gen Service\n        - Qa Service\n        - Frontend\n        - Backend\n    validations:\n      required: false\n  - type: dropdown\n    id: affected-plattforms\n    attributes:\n      label: Affected plattforms\n      description: Which plattforms are affected\n      multiple: true\n      options:\n        - Windows\n        - Linux\n        - MacOS\n    validations:\n      required: false\n  - type: textarea\n    id: further-information\n    attributes:\n      label: Further contextual information and suggestions\n      description: More detailed descriptions or possible solutions\n    validations:\n      required: false\n
    "},{"location":"develop/#feature-request","title":"Feature Request","text":"
    name: Feature Request\ndescription: Suggest an idea for this project\ntitle: \"[FR]: \"\nlabels: [\"enhancement\"]\n# assignees:\n#   - aleneum\nbody:\n  - type: markdown\n    attributes:\n      value: Do you have an idea for a feature that would make this project more helpful or easier to use?\n  - type: textarea\n    id: problem-description\n    attributes:\n      label: Problem description\n      description: Is your feature request related to a problem? Please describe.\n    validations:\n      required: true\n  - type: textarea\n    id: solution\n    attributes:\n      label: Solution\n      description: A clear and concise description of what you want to happen.\n    validations:\n      required: true\n  - type: textarea\n    id: additional-context\n    attributes:\n      label: Additional context\n      description: Add any other context or screenshots about the feature request here.\n    validations:\n      required: false\n  - type: dropdown\n    id: affected-components\n    attributes:\n      label: Affected components\n      description: Which components are affected\n      multiple: true\n      options:\n        - Gen Service\n        - Qa Service\n        - Frontend\n        - Backend\n    validations:\n      required: false\n
    "},{"location":"develop/#use-case","title":"Use Case","text":"
    name: Use Case\ndescription: A description of a user-centered system requirement\ntitle: \"[UC]: \"\nlabels: [\"use case\"]\n# assignees:\n#   - aleneum\nbody:\n  - type: textarea\n    id: summary\n    attributes:\n      label: Summary\n      description: A concise description of the use case\n    validations:\n      required: true\n  - type: textarea\n    id: rationale\n    attributes:\n      label: Rationale\n      description: The motivation and value added by the described use case\n    validations:\n      required: true\n  - type: dropdown\n    id: level\n    attributes:\n      label: Level\n      multiple: false\n      options:\n        - user goal\n        - subfunction\n  - type: input\n    id: actors\n    attributes:\n      label: Actors\n      description: Someone or something with behavior, e.g. a person (identified by role), a computer system, or an organization\n    validations:\n      required: true\n  - type: textarea\n    id: preconditions\n    attributes:\n      label: Preconditions\n      description: What needs to be true on start\n      placeholder: |\n        - Use Cases: #1, #2\n        - User is authenticated\n    validations:\n      required: true\n  - type: textarea\n    id: postconditions\n    attributes:\n      label: Postconditions\n      description: What must be true on successful completion\n      placeholder: \"The form \"\n    validations:\n      required: true\n  - type: textarea\n    id: basic-flow\n    attributes:\n      label: Basic Flow\n      description: A typical, unconditional happy path\n      value: |\n        1.\n        2.\n        3.\n\n    validations:\n      required: true\n  - type: textarea\n    id: alternative-paths\n    attributes:\n      label: Alternative Paths\n      description: Alternate scenarios of success or failure\n      placeholder: |\n        2.1\n        2.2\n        3.1\n\n    validations:\n      required: false\n  - type: textarea\n    id: visualisation\n    attributes:\n      label: Visualisation\n      description: A mermaid diagram or image depiciting the action flow\n      value: |\n        ```mermaid\n        flowchart LR;\n          1-->2\n          2-->3\n          3-->4\n          3-->3.1\n          3.1-->3.2\n          3.2-->2\n          4-->4.1\n          4.1-->4\n        ```\n    validations:\n      required: false\n  - type: textarea\n    id: related-issues\n    attributes:\n      label: Other related issues, use cases, features\n      description: What must be true on successful completion\n      placeholder: \"#1 #2 #3\"\n    validations:\n      required: false\n
    "},{"location":"reference/gerd/","title":"gerd","text":""},{"location":"reference/gerd/#gerd","title":"gerd","text":"

    Generating and evaluating relevant documentation (GERD).

    This package provides the GERD system for working with large language models (LLMs). This includes means to generate texts using different backends and frontends. The system is designed to be flexible and extensible to support different use cases. It can also be used for Retrieval Augmented Generation (RAG) tasks or as a chatbot.

    Modules:

    Name Description backends

    This module contains backend implementations that manage services.

    config

    Configuration for the application.

    features

    Special features to extend the functionality of GERD services.

    frontends

    A collection of several gradio frontends.

    gen

    Services and utilities for text generation with LLMs.

    loader

    Module for loading language models.

    models

    Pydantic model definitions and data classes that are share accross modules.

    qa

    Services and utilities for retrieval augmented generation (RAG).

    rag

    Retrieval-Augmented Generation (RAG) backend.

    training

    Collections of training routines for GERD.

    transport

    Module to define the transport protocol.

    "},{"location":"reference/gerd/backends/","title":"gerd.backends","text":""},{"location":"reference/gerd/backends/#gerd.backends","title":"gerd.backends","text":"

    This module contains backend implementations that manage services.

    These backends can be used by frontends such as gradio. Furthermore, the backend module contains service implementations for loading LLMs or vector stores for Retrieval Augmented Generation.

    Modules:

    Name Description bridge

    The Bridge connects backend and frontend services directly for local use.

    rest_client

    REST client for the GERD server.

    rest_server

    REST server as a GERD backend.

    Attributes:

    Name Type Description TRANSPORTER Transport

    The default transporter that connects the backend services to the frontend.

    "},{"location":"reference/gerd/backends/#gerd.backends.TRANSPORTER","title":"TRANSPORTER module-attribute","text":"
    TRANSPORTER: Transport = Bridge()\n

    The default transporter that connects the backend services to the frontend.

    "},{"location":"reference/gerd/backends/bridge/","title":"gerd.backends.bridge","text":""},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge","title":"gerd.backends.bridge","text":"

    The Bridge connects backend and frontend services directly for local use.

    Classes:

    Name Description Bridge

    Direct connection between backend services and frontend.

    "},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge","title":"Bridge","text":"
    Bridge()\n

    Bases: Transport

    Direct connection between backend services and frontend.

    Frontends that make use of the Transport abstraction can use Bridge to get accessto generation and QA services directly. This is useful for local use cases where the frontend and backend are running in the same process.

    The services associated with the bridge are initialized lazily.

    Methods:

    Name Description add_file

    Add a file to the vector store.

    analyze_mult_prompts_query

    Queries the vector store with a set of predefined queries.

    analyze_query

    Queries the vector store with a predefined query.

    db_embedding

    Converts a question to an embedding.

    db_query

    Queries the vector store with a question.

    generate

    Generates text with the generation service.

    get_gen_prompt

    Gets the prompt configuration for the generation service.

    get_qa_prompt

    Gets the prompt configuration for a mode of the QA service.

    qa_query

    Query the QA service with a question.

    remove_file

    Remove a file from the vector store.

    set_gen_prompt

    Sets the prompt configuration for the generation service.

    set_qa_prompt

    Sets the prompt configuration for the QA service.

    Attributes:

    Name Type Description gen GenerationService

    Get the generation service instance.

    qa QAService

    Get the QA service instance. It will be created if it does not exist.

    Source code in gerd/backends/bridge.py
    def __init__(self) -> None:\n    \"\"\"The services associated with the bridge are initialized lazily.\"\"\"\n    super().__init__()\n    self._qa: Optional[QAService] = None\n    self._gen: Optional[GenerationService] = None\n
    "},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge.gen","title":"gen property","text":"
    gen: GenerationService\n

    Get the generation service instance.

    It will be created if it does not exist.

    "},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge.qa","title":"qa property","text":"
    qa: QAService\n

    Get the QA service instance. It will be created if it does not exist.

    "},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge.add_file","title":"add_file","text":"
    add_file(file: QAFileUpload) -> QAAnswer\n

    Add a file to the vector store.

    The returned answer has a status code of 200 if the file was added successfully. Parameters: file: The file to add to the vector store.

    Returns:

    Type Description QAAnswer

    The answer from the QA service

    Source code in gerd/backends/bridge.py
    @override\ndef add_file(self, file: QAFileUpload) -> QAAnswer:\n    return self.qa.add_file(file)\n
    "},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge.analyze_mult_prompts_query","title":"analyze_mult_prompts_query","text":"
    analyze_mult_prompts_query() -> QAAnalyzeAnswer\n

    Queries the vector store with a set of predefined queries.

    In contrast to analyze_query, this method queries the vector store with multiple prompts.

    Returns:

    Type Description QAAnalyzeAnswer

    The answer from the QA service.

    Source code in gerd/backends/bridge.py
    @override\ndef analyze_mult_prompts_query(self) -> QAAnalyzeAnswer:\n    return self.qa.analyze_mult_prompts_query()\n
    "},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge.analyze_query","title":"analyze_query","text":"
    analyze_query() -> QAAnalyzeAnswer\n

    Queries the vector store with a predefined query.

    The query should return vital information gathered from letters of discharge.

    Returns:

    Type Description QAAnalyzeAnswer

    The answer from the QA service.

    Source code in gerd/backends/bridge.py
    @override\ndef analyze_query(self) -> QAAnalyzeAnswer:\n    return self.qa.analyze_query()\n
    "},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge.db_embedding","title":"db_embedding","text":"
    db_embedding(question: QAQuestion) -> List[float]\n

    Converts a question to an embedding.

    The embedding is defined by the vector store.

    Parameters:

    Name Type Description Default QAQuestion

    The question to convert to an embedding.

    required

    Returns:

    Type Description List[float]

    The embedding of the question

    Source code in gerd/backends/bridge.py
    @override\ndef db_embedding(self, question: QAQuestion) -> List[float]:\n    return self.qa.db_embedding(question)\n
    "},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge.db_embedding(question)","title":"question","text":""},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge.db_query","title":"db_query","text":"
    db_query(question: QAQuestion) -> List[DocumentSource]\n

    Queries the vector store with a question.

    Parameters:

    Name Type Description Default QAQuestion

    The question to query the vector store with.

    required

    Returns:

    Type Description List[DocumentSource]

    A list of document sources

    Source code in gerd/backends/bridge.py
    @override\ndef db_query(self, question: QAQuestion) -> List[DocumentSource]:\n    return self.qa.db_query(question)\n
    "},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge.db_query(question)","title":"question","text":""},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge.generate","title":"generate","text":"
    generate(parameters: Dict[str, str]) -> GenResponse\n

    Generates text with the generation service.

    Parameters:

    Name Type Description Default Dict[str, str]

    The parameters to generate text with

    required

    Returns:

    Type Description GenResponse

    The generation result

    Source code in gerd/backends/bridge.py
    @override\ndef generate(self, parameters: Dict[str, str]) -> GenResponse:\n    return self.gen.generate(parameters)\n
    "},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge.generate(parameters)","title":"parameters","text":""},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge.get_gen_prompt","title":"get_gen_prompt","text":"
    get_gen_prompt() -> PromptConfig\n

    Gets the prompt configuration for the generation service.

    Returns:

    Type Description PromptConfig

    The current prompt configuration

    Source code in gerd/backends/bridge.py
    @override\ndef get_gen_prompt(self) -> PromptConfig:\n    return self.gen.get_prompt_config()\n
    "},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge.get_qa_prompt","title":"get_qa_prompt","text":"
    get_qa_prompt(qa_mode: QAModesEnum) -> PromptConfig\n

    Gets the prompt configuration for a mode of the QA service.

    Parameters:

    Name Type Description Default QAModesEnum

    The mode to get the prompt configuration for

    required

    Returns:

    Type Description PromptConfig

    The prompt configuration for the QA service

    Source code in gerd/backends/bridge.py
    @override\ndef get_qa_prompt(self, qa_mode: QAModesEnum) -> PromptConfig:\n    return self.qa.get_prompt_config(qa_mode)\n
    "},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge.get_qa_prompt(qa_mode)","title":"qa_mode","text":""},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge.qa_query","title":"qa_query","text":"
    qa_query(question: QAQuestion) -> QAAnswer\n

    Query the QA service with a question.

    Parameters:

    Name Type Description Default QAQuestion

    The question to query the QA service with.

    required

    Returns:

    Type Description QAAnswer

    The answer from the QA service.

    Source code in gerd/backends/bridge.py
    @override\ndef qa_query(self, question: QAQuestion) -> QAAnswer:\n    return self.qa.query(question)\n
    "},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge.qa_query(query)","title":"query","text":""},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge.remove_file","title":"remove_file","text":"
    remove_file(file_name: str) -> QAAnswer\n

    Remove a file from the vector store.

    The returned answer has a status code of 200 if the file was removed successfully. Parameters: file_name: The name of the file to remove from the vector store.

    Returns:

    Type Description QAAnswer

    The answer from the QA service

    Source code in gerd/backends/bridge.py
    @override\ndef remove_file(self, file_name: str) -> QAAnswer:\n    return self.qa.remove_file(file_name)\n
    "},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge.set_gen_prompt","title":"set_gen_prompt","text":"
    set_gen_prompt(config: PromptConfig) -> PromptConfig\n

    Sets the prompt configuration for the generation service.

    The prompt configuration that is returned should in most cases be the same as the one that was set. Parameters: config: The prompt configuration to set

    Returns:

    Type Description PromptConfig

    The prompt configuration that was set

    Source code in gerd/backends/bridge.py
    @override\ndef set_gen_prompt(self, config: PromptConfig) -> PromptConfig:\n    return self.gen.set_prompt_config(config)\n
    "},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge.set_qa_prompt","title":"set_qa_prompt","text":"
    set_qa_prompt(config: PromptConfig, qa_mode: QAModesEnum) -> QAAnswer\n

    Sets the prompt configuration for the QA service.

    Since the QA service uses multiple prompt configurations, the mode should be specified. For more details, see the documentation of QAService.set_prompt_config.

    Parameters:

    Name Type Description Default PromptConfig

    The prompt configuration to set

    required QAModesEnum

    The mode to set the prompt configuration for

    required

    Returns:

    Type Description QAAnswer

    The answer from the QA service

    Source code in gerd/backends/bridge.py
    @override\ndef set_qa_prompt(self, config: PromptConfig, qa_mode: QAModesEnum) -> QAAnswer:\n    return self.qa.set_prompt_config(config, qa_mode)\n
    "},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge.set_qa_prompt(config)","title":"config","text":""},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge.set_qa_prompt(qa_mode)","title":"qa_mode","text":""},{"location":"reference/gerd/backends/rest_client/","title":"gerd.backends.rest_client","text":""},{"location":"reference/gerd/backends/rest_client/#gerd.backends.rest_client","title":"gerd.backends.rest_client","text":"

    REST client for the GERD server.

    Classes:

    Name Description RestClient

    REST client for the GERD server.

    "},{"location":"reference/gerd/backends/rest_client/#gerd.backends.rest_client.RestClient","title":"RestClient","text":"
    RestClient()\n

    Bases: Transport

    REST client for the GERD server.

    The client initalizes the server URL.

    It is retrieved from the global CONFIG. Other (timeout) settings are also set here but not configurable as of now.

    Methods:

    Name Description add_file

    Add a file to the vector store.

    analyze_mult_prompts_query

    Queries the vector store with a set of predefined queries.

    analyze_query

    Queries the vector store with a predefined query.

    db_embedding

    Converts a question to an embedding.

    db_query

    Queries the vector store with a question.

    generate

    Generates text with the generation service.

    get_gen_prompt

    Gets the prompt configuration for the generation service.

    get_qa_prompt

    Gets the prompt configuration for a mode of the QA service.

    qa_query

    Query the QA service with a question.

    remove_file

    Remove a file from the vector store.

    set_gen_prompt

    Sets the prompt configuration for the generation service.

    set_qa_prompt

    Sets the prompt configuration for the QA service.

    Source code in gerd/backends/rest_client.py
    def __init__(self) -> None:\n    \"\"\"The client initalizes the server URL.\n\n    It is retrieved from the global [CONFIG][gerd.config.CONFIG].\n    Other (timeout) settings are also set here but not configurable as of now.\n    \"\"\"\n    super().__init__()\n    self._url = f\"http://{CONFIG.server.host}:{CONFIG.server.port}{CONFIG.server.api_prefix}\"\n    self.timeout = 10\n    self.longtimeout = 10000\n
    "},{"location":"reference/gerd/backends/rest_client/#gerd.backends.rest_client.RestClient.add_file","title":"add_file","text":"
    add_file(file: QAFileUpload) -> QAAnswer\n

    Add a file to the vector store.

    The returned answer has a status code of 200 if the file was added successfully. Parameters: file: The file to add to the vector store.

    Returns:

    Type Description QAAnswer

    The answer from the QA service

    Source code in gerd/backends/rest_client.py
    @override\ndef add_file(self, file: QAFileUpload) -> QAAnswer:\n    t = file.model_dump_json().encode(\"utf-8\")\n    return QAAnswer.model_validate(\n        requests.post(\n            f\"{self._url}/qa/file\",\n            data=file.model_dump_json().encode(\"utf-8\"),\n            timeout=self.timeout,\n        ).json()\n    )\n
    "},{"location":"reference/gerd/backends/rest_client/#gerd.backends.rest_client.RestClient.analyze_mult_prompts_query","title":"analyze_mult_prompts_query","text":"
    analyze_mult_prompts_query() -> QAAnalyzeAnswer\n

    Queries the vector store with a set of predefined queries.

    In contrast to analyze_query, this method queries the vector store with multiple prompts.

    Returns:

    Type Description QAAnalyzeAnswer

    The answer from the QA service.

    Source code in gerd/backends/rest_client.py
    @override\ndef analyze_mult_prompts_query(self) -> QAAnalyzeAnswer:\n    return QAAnalyzeAnswer.model_validate(\n        requests.post(\n            f\"{self._url}/qa/query_analyze_mult_prompt\",\n            timeout=self.longtimeout,\n        ).json()\n    )\n
    "},{"location":"reference/gerd/backends/rest_client/#gerd.backends.rest_client.RestClient.analyze_query","title":"analyze_query","text":"
    analyze_query() -> QAAnalyzeAnswer\n

    Queries the vector store with a predefined query.

    The query should return vital information gathered from letters of discharge.

    Returns:

    Type Description QAAnalyzeAnswer

    The answer from the QA service.

    Source code in gerd/backends/rest_client.py
    @override\ndef analyze_query(self) -> QAAnalyzeAnswer:\n    return QAAnalyzeAnswer.model_validate(\n        requests.post(\n            f\"{self._url}/qa/query_analyze\",\n            timeout=self.longtimeout,\n        ).json()\n    )\n
    "},{"location":"reference/gerd/backends/rest_client/#gerd.backends.rest_client.RestClient.db_embedding","title":"db_embedding","text":"
    db_embedding(question: QAQuestion) -> List[float]\n

    Converts a question to an embedding.

    The embedding is defined by the vector store.

    Parameters:

    Name Type Description Default QAQuestion

    The question to convert to an embedding.

    required

    Returns:

    Type Description List[float]

    The embedding of the question

    Source code in gerd/backends/rest_client.py
    @override\ndef db_embedding(self, question: QAQuestion) -> List[float]:\n    request = question.model_dump_json()\n    _LOGGER.debug(\"db_embedding - request: %s\", request)\n    response = requests.post(\n        f\"{self._url}/qa/db_embedding\",\n        data=question.model_dump_json().encode(\"utf-8\"),\n        timeout=self.timeout,\n    )\n    _LOGGER.debug(\"db_embedding - response: %s\", response.json())\n    return TypeAdapter(List[float]).validate_python(response.json())\n
    "},{"location":"reference/gerd/backends/rest_client/#gerd.backends.rest_client.RestClient.db_embedding(question)","title":"question","text":""},{"location":"reference/gerd/backends/rest_client/#gerd.backends.rest_client.RestClient.db_query","title":"db_query","text":"
    db_query(question: QAQuestion) -> List[DocumentSource]\n

    Queries the vector store with a question.

    Parameters:

    Name Type Description Default QAQuestion

    The question to query the vector store with.

    required

    Returns:

    Type Description List[DocumentSource]

    A list of document sources

    Source code in gerd/backends/rest_client.py
    @override\ndef db_query(self, question: QAQuestion) -> List[DocumentSource]:\n    request = question.model_dump_json()\n    _LOGGER.debug(\"db_query - request: %s\", request)\n    response = requests.post(\n        f\"{self._url}/qa/db_query\",\n        data=question.model_dump_json(),\n        timeout=self.timeout,\n    )\n    _LOGGER.debug(\"db_query - response: %s\", response.json())\n    return TypeAdapter(List[DocumentSource]).validate_python(response.json())\n
    "},{"location":"reference/gerd/backends/rest_client/#gerd.backends.rest_client.RestClient.db_query(question)","title":"question","text":""},{"location":"reference/gerd/backends/rest_client/#gerd.backends.rest_client.RestClient.generate","title":"generate","text":"
    generate(parameters: Dict[str, str]) -> GenResponse\n

    Generates text with the generation service.

    Parameters:

    Name Type Description Default Dict[str, str]

    The parameters to generate text with

    required

    Returns:

    Type Description GenResponse

    The generation result

    Source code in gerd/backends/rest_client.py
    @override\ndef generate(self, parameters: Dict[str, str]) -> GenResponse:\n    return GenResponse.model_validate(\n        requests.post(\n            f\"{self._url}/gen/generate\",\n            json=parameters,\n            timeout=self.timeout,\n        ).json()\n    )\n
    "},{"location":"reference/gerd/backends/rest_client/#gerd.backends.rest_client.RestClient.generate(parameters)","title":"parameters","text":""},{"location":"reference/gerd/backends/rest_client/#gerd.backends.rest_client.RestClient.get_gen_prompt","title":"get_gen_prompt","text":"
    get_gen_prompt() -> PromptConfig\n

    Gets the prompt configuration for the generation service.

    Returns:

    Type Description PromptConfig

    The current prompt configuration

    Source code in gerd/backends/rest_client.py
    @override\ndef get_gen_prompt(self) -> PromptConfig:\n    return PromptConfig.model_validate(\n        requests.get(f\"{self._url}/gen/prompt\", timeout=self.timeout).json()\n    )\n
    "},{"location":"reference/gerd/backends/rest_client/#gerd.backends.rest_client.RestClient.get_qa_prompt","title":"get_qa_prompt","text":"
    get_qa_prompt(qa_mode: QAModesEnum) -> PromptConfig\n

    Gets the prompt configuration for a mode of the QA service.

    Parameters:

    Name Type Description Default QAModesEnum

    The mode to get the prompt configuration for

    required

    Returns:

    Type Description PromptConfig

    The prompt configuration for the QA service

    Source code in gerd/backends/rest_client.py
    @override\ndef get_qa_prompt(self, qa_mode: QAModesEnum) -> PromptConfig:\n    return PromptConfig.model_validate(\n        requests.get(\n            f\"{self._url}/qa/prompt\",\n            timeout=self.timeout,\n            params={\"qa_mode\": qa_mode.value},\n        ).json()\n    )\n
    "},{"location":"reference/gerd/backends/rest_client/#gerd.backends.rest_client.RestClient.get_qa_prompt(qa_mode)","title":"qa_mode","text":""},{"location":"reference/gerd/backends/rest_client/#gerd.backends.rest_client.RestClient.qa_query","title":"qa_query","text":"
    qa_query(question: QAQuestion) -> QAAnswer\n

    Query the QA service with a question.

    Parameters:

    Name Type Description Default QAQuestion

    The question to query the QA service with.

    required

    Returns:

    Type Description QAAnswer

    The answer from the QA service.

    Source code in gerd/backends/rest_client.py
    @override\ndef qa_query(self, question: QAQuestion) -> QAAnswer:\n    return QAAnswer.model_validate(\n        requests.post(\n            f\"{self._url}/qa/query\",\n            data=question.model_dump_json().encode(\"utf-8\"),\n            timeout=self.longtimeout,\n        ).json()\n    )\n
    "},{"location":"reference/gerd/backends/rest_client/#gerd.backends.rest_client.RestClient.qa_query(query)","title":"query","text":""},{"location":"reference/gerd/backends/rest_client/#gerd.backends.rest_client.RestClient.remove_file","title":"remove_file","text":"
    remove_file(file_name: str) -> QAAnswer\n

    Remove a file from the vector store.

    The returned answer has a status code of 200 if the file was removed successfully. Parameters: file_name: The name of the file to remove from the vector store.

    Returns:

    Type Description QAAnswer

    The answer from the QA service

    Source code in gerd/backends/rest_client.py
    @override\ndef remove_file(self, file_name: str) -> QAAnswer:\n    return QAAnswer.model_validate(\n        requests.delete(\n            f\"{self._url}/qa/file\",\n            data=file_name.encode(\"utf-8\"),\n            timeout=self.timeout,\n        ).json()\n    )\n
    "},{"location":"reference/gerd/backends/rest_client/#gerd.backends.rest_client.RestClient.set_gen_prompt","title":"set_gen_prompt","text":"
    set_gen_prompt(config: PromptConfig) -> PromptConfig\n

    Sets the prompt configuration for the generation service.

    The prompt configuration that is returned should in most cases be the same as the one that was set. Parameters: config: The prompt configuration to set

    Returns:

    Type Description PromptConfig

    The prompt configuration that was set

    Source code in gerd/backends/rest_client.py
    @override\ndef set_gen_prompt(self, config: PromptConfig) -> PromptConfig:\n    return PromptConfig.model_validate(\n        requests.post(\n            f\"{self._url}/gen/prompt\",\n            data=config.model_dump_json(),\n            timeout=self.timeout,\n        ).json()\n    )\n
    "},{"location":"reference/gerd/backends/rest_client/#gerd.backends.rest_client.RestClient.set_qa_prompt","title":"set_qa_prompt","text":"
    set_qa_prompt(config: PromptConfig, qa_mode: QAModesEnum) -> QAAnswer\n

    Sets the prompt configuration for the QA service.

    Since the QA service uses multiple prompt configurations, the mode should be specified. For more details, see the documentation of QAService.set_prompt_config.

    Parameters:

    Name Type Description Default PromptConfig

    The prompt configuration to set

    required QAModesEnum

    The mode to set the prompt configuration for

    required

    Returns:

    Type Description QAAnswer

    The answer from the QA service

    Source code in gerd/backends/rest_client.py
    @override\ndef set_qa_prompt(self, config: PromptConfig, qa_mode: QAModesEnum) -> QAAnswer:\n    return QAAnswer.model_validate(\n        requests.post(\n            f\"{self._url}/qa/prompt\",\n            data=QAPromptConfig(config=config, mode=qa_mode).model_dump_json(),\n            timeout=self.timeout,\n        ).json()\n    )\n
    "},{"location":"reference/gerd/backends/rest_client/#gerd.backends.rest_client.RestClient.set_qa_prompt(config)","title":"config","text":""},{"location":"reference/gerd/backends/rest_client/#gerd.backends.rest_client.RestClient.set_qa_prompt(qa_mode)","title":"qa_mode","text":""},{"location":"reference/gerd/backends/rest_server/","title":"gerd.backends.rest_server","text":""},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server","title":"gerd.backends.rest_server","text":"

    REST server as a GERD backend.

    Classes:

    Name Description RestServer

    REST server as a GERD backend.

    "},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer","title":"RestServer","text":"
    RestServer()\n

    Bases: Transport

    REST server as a GERD backend.

    The REST server initializes a private bridge and an API router.

    The API router is used to define the endpoints for the REST server.

    Methods:

    Name Description add_file

    Add a file to the vector store.

    analyze_mult_prompts_query

    Queries the vector store with a set of predefined queries.

    analyze_query

    Queries the vector store with a predefined query.

    db_embedding

    Converts a question to an embedding.

    db_query

    Queries the vector store with a question.

    generate

    Generates text with the generation service.

    get_gen_prompt

    Gets the prompt configuration for the generation service.

    get_qa_prompt

    Gets the prompt configuration for a mode of the QA service.

    get_qa_prompt_rest

    Get the QA prompt configuration.

    qa_query

    Query the QA service with a question.

    remove_file

    Remove a file from the vector store.

    set_gen_prompt

    Sets the prompt configuration for the generation service.

    set_qa_prompt

    Sets the prompt configuration for the QA service.

    set_qa_prompt_rest

    Set the QA prompt configuration.

    Source code in gerd/backends/rest_server.py
    def __init__(self) -> None:\n    \"\"\"The REST server initializes a private bridge and an API router.\n\n    The API router is used to define the endpoints for the REST server.\n    \"\"\"\n    super().__init__()\n\n    prefix = CONFIG.server.api_prefix\n    self._bridge = Bridge()\n    self.router = APIRouter()\n    self.router.add_api_route(f\"{prefix}/qa/query\", self.qa_query, methods=[\"POST\"])\n    self.router.add_api_route(\n        f\"{prefix}/qa/query_analyze\", self.analyze_query, methods=[\"POST\"]\n    )\n    self.router.add_api_route(\n        f\"{prefix}/qa/query_analyze_mult_prompt\",\n        self.analyze_mult_prompts_query,\n        methods=[\"POST\"],\n    )\n    self.router.add_api_route(\n        f\"{prefix}/qa/db_query\", self.db_query, methods=[\"POST\"]\n    )\n    self.router.add_api_route(\n        f\"{prefix}/qa/db_embedding\", self.db_embedding, methods=[\"POST\"]\n    )\n    self.router.add_api_route(f\"{prefix}/qa/file\", self.add_file, methods=[\"POST\"])\n    self.router.add_api_route(\n        f\"{prefix}/qa/file\", self.remove_file, methods=[\"DELETE\"]\n    )\n    self.router.add_api_route(\n        f\"{prefix}/gen/prompt\", self.set_gen_prompt, methods=[\"POST\"]\n    )\n    self.router.add_api_route(\n        f\"{prefix}/gen/prompt\", self.get_gen_prompt, methods=[\"GET\"]\n    )\n    self.router.add_api_route(\n        f\"{prefix}/qa/prompt\", self.set_qa_prompt_rest, methods=[\"POST\"]\n    )\n    self.router.add_api_route(\n        f\"{prefix}/qa/prompt\", self.get_qa_prompt_rest, methods=[\"GET\"]\n    )\n    self.router.add_api_route(\n        f\"{prefix}/gen/generate\", self.generate, methods=[\"POST\"]\n    )\n
    "},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer.add_file","title":"add_file","text":"
    add_file(file: QAFileUpload) -> QAAnswer\n

    Add a file to the vector store.

    The returned answer has a status code of 200 if the file was added successfully. Parameters: file: The file to add to the vector store.

    Returns:

    Type Description QAAnswer

    The answer from the QA service

    Source code in gerd/backends/rest_server.py
    @override\ndef add_file(self, file: QAFileUpload) -> QAAnswer:\n    return self._bridge.add_file(file)\n
    "},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer.analyze_mult_prompts_query","title":"analyze_mult_prompts_query","text":"
    analyze_mult_prompts_query() -> QAAnalyzeAnswer\n

    Queries the vector store with a set of predefined queries.

    In contrast to analyze_query, this method queries the vector store with multiple prompts.

    Returns:

    Type Description QAAnalyzeAnswer

    The answer from the QA service.

    Source code in gerd/backends/rest_server.py
    @override\ndef analyze_mult_prompts_query(self) -> QAAnalyzeAnswer:\n    return self._bridge.qa.analyze_mult_prompts_query()\n
    "},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer.analyze_query","title":"analyze_query","text":"
    analyze_query() -> QAAnalyzeAnswer\n

    Queries the vector store with a predefined query.

    The query should return vital information gathered from letters of discharge.

    Returns:

    Type Description QAAnalyzeAnswer

    The answer from the QA service.

    Source code in gerd/backends/rest_server.py
    @override\ndef analyze_query(self) -> QAAnalyzeAnswer:\n    return self._bridge.qa.analyze_query()\n
    "},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer.db_embedding","title":"db_embedding","text":"
    db_embedding(question: QAQuestion) -> List[float]\n

    Converts a question to an embedding.

    The embedding is defined by the vector store.

    Parameters:

    Name Type Description Default QAQuestion

    The question to convert to an embedding.

    required

    Returns:

    Type Description List[float]

    The embedding of the question

    Source code in gerd/backends/rest_server.py
    @override\ndef db_embedding(self, question: QAQuestion) -> List[float]:\n    return self._bridge.db_embedding(question)\n
    "},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer.db_embedding(question)","title":"question","text":""},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer.db_query","title":"db_query","text":"
    db_query(question: QAQuestion) -> List[DocumentSource]\n

    Queries the vector store with a question.

    Parameters:

    Name Type Description Default QAQuestion

    The question to query the vector store with.

    required

    Returns:

    Type Description List[DocumentSource]

    A list of document sources

    Source code in gerd/backends/rest_server.py
    @override\ndef db_query(self, question: QAQuestion) -> List[DocumentSource]:\n    # _LOGGER.debug(\"dq_query - request: %s\", question)\n    response = self._bridge.db_query(question)\n    # _LOGGER.debug(\"dq_query - response: %s\", response)\n    return response\n
    "},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer.db_query(question)","title":"question","text":""},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer.generate","title":"generate","text":"
    generate(parameters: Dict[str, str]) -> GenResponse\n

    Generates text with the generation service.

    Parameters:

    Name Type Description Default Dict[str, str]

    The parameters to generate text with

    required

    Returns:

    Type Description GenResponse

    The generation result

    Source code in gerd/backends/rest_server.py
    @override\ndef generate(self, parameters: Dict[str, str]) -> GenResponse:\n    return self._bridge.generate(parameters)\n
    "},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer.generate(parameters)","title":"parameters","text":""},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer.get_gen_prompt","title":"get_gen_prompt","text":"
    get_gen_prompt() -> PromptConfig\n

    Gets the prompt configuration for the generation service.

    Returns:

    Type Description PromptConfig

    The current prompt configuration

    Source code in gerd/backends/rest_server.py
    @override\ndef get_gen_prompt(self) -> PromptConfig:\n    return self._bridge.get_gen_prompt()\n
    "},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer.get_qa_prompt","title":"get_qa_prompt","text":"
    get_qa_prompt(qa_mode: QAModesEnum) -> PromptConfig\n

    Gets the prompt configuration for a mode of the QA service.

    Parameters:

    Name Type Description Default QAModesEnum

    The mode to get the prompt configuration for

    required

    Returns:

    Type Description PromptConfig

    The prompt configuration for the QA service

    Source code in gerd/backends/rest_server.py
    @override\ndef get_qa_prompt(self, qa_mode: QAModesEnum) -> PromptConfig:\n    return self._bridge.get_qa_prompt(qa_mode)\n
    "},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer.get_qa_prompt(qa_mode)","title":"qa_mode","text":""},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer.get_qa_prompt_rest","title":"get_qa_prompt_rest","text":"
    get_qa_prompt_rest(qa_mode: int) -> PromptConfig\n

    Get the QA prompt configuration.

    The call is forwarded to the bridge. Parameters: qa_mode: The QA mode Returns: The QA prompt configuration

    Source code in gerd/backends/rest_server.py
    def get_qa_prompt_rest(self, qa_mode: int) -> PromptConfig:\n    \"\"\"Get the QA prompt configuration.\n\n    The call is forwarded to the bridge.\n    Parameters:\n        qa_mode: The QA mode\n    Returns:\n        The QA prompt configuration\n    \"\"\"\n    return self._bridge.get_qa_prompt(QAModesEnum(qa_mode))\n
    "},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer.qa_query","title":"qa_query","text":"
    qa_query(question: QAQuestion) -> QAAnswer\n

    Query the QA service with a question.

    Parameters:

    Name Type Description Default QAQuestion

    The question to query the QA service with.

    required

    Returns:

    Type Description QAAnswer

    The answer from the QA service.

    Source code in gerd/backends/rest_server.py
    @override\ndef qa_query(self, question: QAQuestion) -> QAAnswer:\n    return self._bridge.qa_query(question)\n
    "},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer.qa_query(query)","title":"query","text":""},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer.remove_file","title":"remove_file","text":"
    remove_file(file_name: str) -> QAAnswer\n

    Remove a file from the vector store.

    The returned answer has a status code of 200 if the file was removed successfully. Parameters: file_name: The name of the file to remove from the vector store.

    Returns:

    Type Description QAAnswer

    The answer from the QA service

    Source code in gerd/backends/rest_server.py
    @override\ndef remove_file(self, file_name: str) -> QAAnswer:\n    return self._bridge.remove_file(file_name)\n
    "},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer.set_gen_prompt","title":"set_gen_prompt","text":"
    set_gen_prompt(config: PromptConfig) -> PromptConfig\n

    Sets the prompt configuration for the generation service.

    The prompt configuration that is returned should in most cases be the same as the one that was set. Parameters: config: The prompt configuration to set

    Returns:

    Type Description PromptConfig

    The prompt configuration that was set

    Source code in gerd/backends/rest_server.py
    @override\ndef set_gen_prompt(self, config: PromptConfig) -> PromptConfig:\n    return self._bridge.set_gen_prompt(config)\n
    "},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer.set_qa_prompt","title":"set_qa_prompt","text":"
    set_qa_prompt(config: PromptConfig, qa_mode: QAModesEnum) -> QAAnswer\n

    Sets the prompt configuration for the QA service.

    Since the QA service uses multiple prompt configurations, the mode should be specified. For more details, see the documentation of QAService.set_prompt_config.

    Parameters:

    Name Type Description Default PromptConfig

    The prompt configuration to set

    required QAModesEnum

    The mode to set the prompt configuration for

    required

    Returns:

    Type Description QAAnswer

    The answer from the QA service

    Source code in gerd/backends/rest_server.py
    @override\ndef set_qa_prompt(self, config: PromptConfig, qa_mode: QAModesEnum) -> QAAnswer:\n    return self._bridge.set_qa_prompt(config, qa_mode)\n
    "},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer.set_qa_prompt(config)","title":"config","text":""},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer.set_qa_prompt(qa_mode)","title":"qa_mode","text":""},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer.set_qa_prompt_rest","title":"set_qa_prompt_rest","text":"
    set_qa_prompt_rest(config: QAPromptConfig) -> QAAnswer\n

    Set the QA prompt configuration.

    The call is forwarded to the bridge. Parameters: config: The QA prompt configuration Returns: The QA prompt configuration; Should be the same as the input in most cases

    Source code in gerd/backends/rest_server.py
    def set_qa_prompt_rest(self, config: QAPromptConfig) -> QAAnswer:\n    \"\"\"Set the QA prompt configuration.\n\n    The call is forwarded to the bridge.\n    Parameters:\n        config: The QA prompt configuration\n    Returns:\n        The QA prompt configuration; Should be the same as the input in most cases\n    \"\"\"\n    return self._bridge.set_qa_prompt(config.config, config.mode)\n
    "},{"location":"reference/gerd/config/","title":"gerd.config","text":""},{"location":"reference/gerd/config/#gerd.config","title":"gerd.config","text":"

    Configuration for the application.

    Classes:

    Name Description EnvVariables

    Environment variables.

    Settings

    Settings for the application.

    YamlConfig

    YAML configuration source.

    Functions:

    Name Description load_gen_config

    Load the LLM model configuration.

    load_qa_config

    Load the LLM model configuration.

    Attributes:

    Name Type Description CONFIG

    The global configuration object.

    "},{"location":"reference/gerd/config/#gerd.config.CONFIG","title":"CONFIG module-attribute","text":"
    CONFIG = Settings()\n

    The global configuration object.

    "},{"location":"reference/gerd/config/#gerd.config.EnvVariables","title":"EnvVariables","text":"

    Bases: BaseModel

    Environment variables.

    "},{"location":"reference/gerd/config/#gerd.config.Settings","title":"Settings","text":"

    Bases: BaseSettings

    Settings for the application.

    Methods:

    Name Description settings_customise_sources

    Customize the settings sources used by pydantic-settings.

    "},{"location":"reference/gerd/config/#gerd.config.Settings.settings_customise_sources","title":"settings_customise_sources classmethod","text":"
    settings_customise_sources(settings_cls: Type[BaseSettings], init_settings: PydanticBaseSettingsSource, env_settings: PydanticBaseSettingsSource, dotenv_settings: PydanticBaseSettingsSource, file_secret_settings: PydanticBaseSettingsSource) -> Tuple[PydanticBaseSettingsSource, ...]\n

    Customize the settings sources used by pydantic-settings.

    The order of the sources is important. The first source has the highest priority.

    Parameters:

    Name Type Description Default

    The class of the settings.

    required PydanticBaseSettingsSource

    The settings from the initialization.

    required PydanticBaseSettingsSource

    The settings from the environment.

    required PydanticBaseSettingsSource

    The settings from the dotenv file.

    required PydanticBaseSettingsSource

    The settings from the secret file.

    required

    Returns:

    Type Description Tuple[PydanticBaseSettingsSource, ...]

    The customized settings sources.

    Source code in gerd/config.py
    @classmethod\ndef settings_customise_sources(\n    cls,\n    settings_cls: Type[BaseSettings],\n    init_settings: PydanticBaseSettingsSource,\n    env_settings: PydanticBaseSettingsSource,\n    dotenv_settings: PydanticBaseSettingsSource,\n    file_secret_settings: PydanticBaseSettingsSource,\n) -> Tuple[PydanticBaseSettingsSource, ...]:\n    \"\"\"Customize the settings sources used by pydantic-settings.\n\n    The order of the sources is important.\n    The first source has the highest priority.\n\n    Parameters:\n        cls: The class of the settings.\n        init_settings: The settings from the initialization.\n        env_settings: The settings from the environment.\n        dotenv_settings: The settings from the dotenv file.\n        file_secret_settings: The settings from the secret file.\n\n    Returns:\n        The customized settings sources.\n    \"\"\"\n    return (\n        file_secret_settings,\n        env_settings,\n        dotenv_settings,\n        init_settings,\n        YamlConfig(settings_cls),\n    )\n
    "},{"location":"reference/gerd/config/#gerd.config.Settings.settings_customise_sources(cls)","title":"cls","text":""},{"location":"reference/gerd/config/#gerd.config.Settings.settings_customise_sources(init_settings)","title":"init_settings","text":""},{"location":"reference/gerd/config/#gerd.config.Settings.settings_customise_sources(env_settings)","title":"env_settings","text":""},{"location":"reference/gerd/config/#gerd.config.Settings.settings_customise_sources(dotenv_settings)","title":"dotenv_settings","text":""},{"location":"reference/gerd/config/#gerd.config.Settings.settings_customise_sources(file_secret_settings)","title":"file_secret_settings","text":""},{"location":"reference/gerd/config/#gerd.config.YamlConfig","title":"YamlConfig","text":"

    Bases: PydanticBaseSettingsSource

    YAML configuration source.

    Methods:

    Name Description get_field_value

    Overrides a method from PydanticBaseSettingsSource.

    "},{"location":"reference/gerd/config/#gerd.config.YamlConfig.get_field_value","title":"get_field_value","text":"
    get_field_value(field: FieldInfo, field_name: str) -> Tuple[Any, str, bool]\n

    Overrides a method from PydanticBaseSettingsSource.

    Fails if it should ever be called. Parameters: field: The field to get the value for. field_name: The name of the field.

    Raises:

    Type Description NotImplementedError

    Always.

    Source code in gerd/config.py
    def get_field_value(\n    self, field: FieldInfo, field_name: str\n) -> Tuple[Any, str, bool]:\n    \"\"\"Overrides a method from `PydanticBaseSettingsSource`.\n\n    Fails if it should ever be called.\n    Parameters:\n        field: The field to get the value for.\n        field_name: The name of the field.\n\n    Raises:\n        NotImplementedError: Always.\n    \"\"\"\n    raise NotImplementedError()\n
    "},{"location":"reference/gerd/config/#gerd.config.load_gen_config","title":"load_gen_config","text":"
    load_gen_config(config: str = 'gen_default') -> GenerationConfig\n

    Load the LLM model configuration.

    Parameters:

    Name Type Description Default str

    The name of the configuration.

    'gen_default'

    Returns:

    Type Description GenerationConfig

    The model configuration.

    Source code in gerd/config.py
    def load_gen_config(config: str = \"gen_default\") -> GenerationConfig:\n    \"\"\"Load the LLM model configuration.\n\n    Parameters:\n        config: The name of the configuration.\n\n    Returns:\n        The model configuration.\n    \"\"\"\n    config_path = (\n        Path(config)\n        if config.endswith(\"yml\")\n        else Path(PROJECT_DIR, \"config\", f\"{config}.yml\")\n    )\n    with config_path.open(\"r\", encoding=\"utf-8\") as f:\n        conf = GenerationConfig.model_validate(safe_load(f))\n    if CONFIG.env and CONFIG.env.api_token and conf.model.endpoint:\n        conf.model.endpoint.key = CONFIG.env.api_token\n    return conf\n
    "},{"location":"reference/gerd/config/#gerd.config.load_gen_config(config)","title":"config","text":""},{"location":"reference/gerd/config/#gerd.config.load_qa_config","title":"load_qa_config","text":"
    load_qa_config(config: str = 'qa_default') -> QAConfig\n

    Load the LLM model configuration.

    Parameters:

    Name Type Description Default str

    The name of the configuration.

    'qa_default'

    Returns:

    Type Description QAConfig

    The model configuration.

    Source code in gerd/config.py
    def load_qa_config(config: str = \"qa_default\") -> QAConfig:\n    \"\"\"Load the LLM model configuration.\n\n    Parameters:\n        config: The name of the configuration.\n\n    Returns:\n        The model configuration.\n    \"\"\"\n    config_path = (\n        Path(config)\n        if config.endswith(\"yml\")\n        else Path(PROJECT_DIR, \"config\", f\"{config}.yml\")\n    )\n    with config_path.open(\"r\", encoding=\"utf-8\") as f:\n        conf = QAConfig.model_validate(safe_load(f))\n\n    if CONFIG.env and CONFIG.env.api_token and conf.model.endpoint:\n        conf.model.endpoint.key = CONFIG.env.api_token\n    return conf\n
    "},{"location":"reference/gerd/config/#gerd.config.load_qa_config(config)","title":"config","text":""},{"location":"reference/gerd/features/","title":"gerd.features","text":""},{"location":"reference/gerd/features/#gerd.features","title":"gerd.features","text":"

    Special features to extend the functionality of GERD services.

    Modules:

    Name Description prompt_chaining

    The prompt chaining extension.

    "},{"location":"reference/gerd/features/prompt_chaining/","title":"gerd.features.prompt_chaining","text":""},{"location":"reference/gerd/features/prompt_chaining/#gerd.features.prompt_chaining","title":"gerd.features.prompt_chaining","text":"

    The prompt chaining extension.

    Prompt chaining is a method to improve the factual accuracy of the model's output. To do this, the model generates a series of prompts and uses the output of each prompt as the input for the next prompt. This allows the model to reflect on its own output and generate a more coherent response.

    Classes:

    Name Description PromptChaining

    The prompt chaining extension.

    PromptChainingConfig

    Configuration for prompt chaining.

    "},{"location":"reference/gerd/features/prompt_chaining/#gerd.features.prompt_chaining.PromptChaining","title":"PromptChaining","text":"
    PromptChaining(config: PromptChainingConfig, llm: LLM, prompt: PromptConfig)\n

    The prompt chaining extension.

    The service is initialized with a chaining configuration and an LLM.

    Parameters:

    Name Type Description Default PromptChainingConfig

    The configuration for the prompt chaining

    required LLM

    The language model to use for the generation

    required PromptConfig

    The prompt that is used to wrap the questions

    required

    Methods:

    Name Description generate

    Generate text based on the prompt configuration and use chaining.

    Source code in gerd/features/prompt_chaining.py
    def __init__(\n    self, config: PromptChainingConfig, llm: LLM, prompt: PromptConfig\n) -> None:\n    \"\"\"The service is initialized with a chaining configuration and an LLM.\n\n    Parameters:\n        config: The configuration for the prompt chaining\n        llm: The language model to use for the generation\n        prompt: The prompt that is used to wrap the questions\n    \"\"\"\n    self.llm = llm\n    self.config = config\n    self.prompt = prompt\n
    "},{"location":"reference/gerd/features/prompt_chaining/#gerd.features.prompt_chaining.PromptChaining(config)","title":"config","text":""},{"location":"reference/gerd/features/prompt_chaining/#gerd.features.prompt_chaining.PromptChaining(llm)","title":"llm","text":""},{"location":"reference/gerd/features/prompt_chaining/#gerd.features.prompt_chaining.PromptChaining(prompt)","title":"prompt","text":""},{"location":"reference/gerd/features/prompt_chaining/#gerd.features.prompt_chaining.PromptChaining.generate","title":"generate","text":"
    generate(parameters: dict[str, str]) -> str\n

    Generate text based on the prompt configuration and use chaining.

    Parameters:

    Name Type Description Default dict[str, str]

    The parameters to format the prompt with

    required

    Returns:

    Type Description str

    The result of the last prompt that was chained

    Source code in gerd/features/prompt_chaining.py
    def generate(self, parameters: dict[str, str]) -> str:\n    \"\"\"Generate text based on the prompt configuration and use chaining.\n\n    Parameters:\n        parameters: The parameters to format the prompt with\n\n    Returns:\n        The result of the last prompt that was chained\n    \"\"\"\n    res = \"\"\n    for i, prompt in enumerate(self.config.prompts, 1):\n        resolved = self.prompt.format({\"prompt\": prompt.format(parameters)})\n        _LOGGER.debug(\"\\n===== Input =====\\n\\n%s\\n\\n====================\", resolved)\n        res = self.llm.generate(resolved).strip()\n        _LOGGER.debug(\"\\n===== Response =====\\n\\n%s\\n\\n====================\", res)\n        parameters[f\"response_{i}\"] = res\n    return res\n
    "},{"location":"reference/gerd/features/prompt_chaining/#gerd.features.prompt_chaining.PromptChaining.generate(parameters)","title":"parameters","text":""},{"location":"reference/gerd/features/prompt_chaining/#gerd.features.prompt_chaining.PromptChainingConfig","title":"PromptChainingConfig","text":"

    Bases: BaseModel

    Configuration for prompt chaining.

    Note that prompts should contain placeholders for the responses to be inserted. The initial question can be used with {question} if it is passed as a parameter with this key. The responses will be indexed as response_1, response_2, etc. This way any prompt can refer to all previous responses and the initial question if needed.

    Attributes:

    Name Type Description prompts list[PromptConfig]

    The list of prompts to chain.

    "},{"location":"reference/gerd/features/prompt_chaining/#gerd.features.prompt_chaining.PromptChainingConfig.prompts","title":"prompts instance-attribute","text":"
    prompts: list[PromptConfig]\n

    The list of prompts to chain.

    "},{"location":"reference/gerd/frontends/","title":"gerd.frontends","text":""},{"location":"reference/gerd/frontends/#gerd.frontends","title":"gerd.frontends","text":"

    A collection of several gradio frontends.

    A variety of frontends to interact with GERD services and backends.

    Modules:

    Name Description gen_frontend

    A gradio frontend to interact with the generation service.

    generate

    A simple gradio frontend to interact with the GERD chat and generate service.

    instruct

    A gradio frontend to interact with the GERD instruct service.

    qa_frontend

    A gradio frontend to query the QA service and upload files to the vectorstore.

    router

    A gradio frontend to start and stop the GERD services.

    training

    A gradio frontend to train LoRAs with.

    "},{"location":"reference/gerd/frontends/gen_frontend/","title":"gerd.frontends.gen_frontend","text":""},{"location":"reference/gerd/frontends/gen_frontend/#gerd.frontends.gen_frontend","title":"gerd.frontends.gen_frontend","text":"

    A gradio frontend to interact with the generation service.

    This frontend is tailored to the letter of discharge generation task. For a more general frontend see gerd.frontend.generate.

    Functions:

    Name Description compare_paragraphs

    Compare paragraphs of two documents and return the modified parts.

    generate

    Generate a letter of discharge based on the provided fields.

    insert_paragraphs

    Insert modified paragraphs into the source document.

    response_parser

    Parse the response from the generation service.

    "},{"location":"reference/gerd/frontends/gen_frontend/#gerd.frontends.gen_frontend.compare_paragraphs","title":"compare_paragraphs","text":"
    compare_paragraphs(src_doc: str, mod_doc: str) -> Dict[str, str]\n

    Compare paragraphs of two documents and return the modified parts.

    Parameters:

    Name Type Description Default str

    The source document

    required str

    The modified document

    required

    Returns:

    Type Description Dict[str, str]

    The modified parts of the document

    Source code in gerd/frontends/gen_frontend.py
    def compare_paragraphs(src_doc: str, mod_doc: str) -> Dict[str, str]:\n    \"\"\"Compare paragraphs of two documents and return the modified parts.\n\n    Parameters:\n        src_doc: The source document\n        mod_doc: The modified document\n\n    Returns:\n        The modified parts of the document\n    \"\"\"\n    mod_parts = {}\n    src_doc_split = src_doc.split(\"\\n\\n\")\n    mod_doc_split = mod_doc.split(\"\\n\\n\")\n    for section_order, src_para in zip(sections, src_doc_split, strict=True):\n        mod_para = mod_doc_split[sections.index(section_order)]\n        if src_para != mod_para:\n            mod_parts[section_order] = mod_para\n    return mod_parts\n
    "},{"location":"reference/gerd/frontends/gen_frontend/#gerd.frontends.gen_frontend.compare_paragraphs(src_doc)","title":"src_doc","text":""},{"location":"reference/gerd/frontends/gen_frontend/#gerd.frontends.gen_frontend.compare_paragraphs(mod_doc)","title":"mod_doc","text":""},{"location":"reference/gerd/frontends/gen_frontend/#gerd.frontends.gen_frontend.generate","title":"generate","text":"
    generate(*fields: str) -> Tuple[str, str, gr.TextArea, gr.Button]\n

    Generate a letter of discharge based on the provided fields.

    Parameters:

    Name Type Description Default str

    The fields to generate the letter of discharge from.

    ()

    Returns:

    Type Description str

    The generated letter of discharge, a text area to display it,

    str

    and a button state to continue the generation

    Source code in gerd/frontends/gen_frontend.py
    def generate(*fields: str) -> Tuple[str, str, gr.TextArea, gr.Button]:\n    \"\"\"Generate a letter of discharge based on the provided fields.\n\n    Parameters:\n        *fields: The fields to generate the letter of discharge from.\n\n    Returns:\n        The generated letter of discharge, a text area to display it,\n        and a button state to continue the generation\n    \"\"\"\n    params = {}\n    for key, name, value in _pairwise(fields):\n        if not value:\n            msg = f\"Feld '{name}' darf nicht leer sein!\"\n            raise gr.Error(msg)\n        params[key] = value\n    response = TRANSPORTER.generate(params)\n    return (\n        response.text,\n        response.text,\n        gr.TextArea(label=\"Dokument\", interactive=True),\n        gr.Button(\"Kontinuiere Dokument\", visible=True),\n    )\n
    "},{"location":"reference/gerd/frontends/gen_frontend/#gerd.frontends.gen_frontend.generate(*fields)","title":"*fields","text":""},{"location":"reference/gerd/frontends/gen_frontend/#gerd.frontends.gen_frontend.insert_paragraphs","title":"insert_paragraphs","text":"
    insert_paragraphs(src_doc: str, new_para: Dict[str, str]) -> str\n

    Insert modified paragraphs into the source document.

    Parameters:

    Name Type Description Default str

    The source document

    required Dict[str, str]

    The modified paragraphs

    required

    Returns:

    Type Description str

    The updated document

    Source code in gerd/frontends/gen_frontend.py
    def insert_paragraphs(src_doc: str, new_para: Dict[str, str]) -> str:\n    \"\"\"Insert modified paragraphs into the source document.\n\n    Parameters:\n        src_doc: The source document\n        new_para: The modified paragraphs\n\n    Returns:\n        The updated document\n    \"\"\"\n    for section_order, mod_para in new_para.items():\n        split_doc = src_doc.split(\"\\n\\n\")[sections.index(section_order)]\n        src_doc = src_doc.replace(split_doc, mod_para)\n    return src_doc\n
    "},{"location":"reference/gerd/frontends/gen_frontend/#gerd.frontends.gen_frontend.insert_paragraphs(src_doc)","title":"src_doc","text":""},{"location":"reference/gerd/frontends/gen_frontend/#gerd.frontends.gen_frontend.insert_paragraphs(new_para)","title":"new_para","text":""},{"location":"reference/gerd/frontends/gen_frontend/#gerd.frontends.gen_frontend.response_parser","title":"response_parser","text":"
    response_parser(response: str) -> Dict[str, str]\n

    Parse the response from the generation service.

    Parameters:

    Name Type Description Default str

    The response from the generation service

    required

    Returns:

    Type Description Dict[str, str]

    The parsed response

    Source code in gerd/frontends/gen_frontend.py
    def response_parser(response: str) -> Dict[str, str]:\n    \"\"\"Parse the response from the generation service.\n\n    Parameters:\n        response: The response from the generation service\n\n    Returns:\n        The parsed response\n    \"\"\"\n    parsed_response = {}\n    split_response = response.split(\"\\n\\n\")\n    for paragraph in split_response:\n        for section in sections:\n            if section in paragraph:\n                parsed_response[section] = paragraph\n                break\n    return parsed_response\n
    "},{"location":"reference/gerd/frontends/gen_frontend/#gerd.frontends.gen_frontend.response_parser(response)","title":"response","text":""},{"location":"reference/gerd/frontends/generate/","title":"gerd.frontends.generate","text":""},{"location":"reference/gerd/frontends/generate/#gerd.frontends.generate","title":"gerd.frontends.generate","text":"

    A simple gradio frontend to interact with the GERD chat and generate service.

    Classes:

    Name Description Global

    Singleton to store the service.

    Functions:

    Name Description generate

    Generate text from the model.

    load_model

    Load a global large language model.

    Attributes:

    Name Type Description KIOSK_MODE

    Whether the frontend is running in kiosk mode.

    "},{"location":"reference/gerd/frontends/generate/#gerd.frontends.generate.KIOSK_MODE","title":"KIOSK_MODE module-attribute","text":"
    KIOSK_MODE = kiosk_mode\n

    Whether the frontend is running in kiosk mode.

    Kiosk mode reduces the number of options to a minimum and automatically loads the model.

    "},{"location":"reference/gerd/frontends/generate/#gerd.frontends.generate.Global","title":"Global","text":"

    Singleton to store the service.

    "},{"location":"reference/gerd/frontends/generate/#gerd.frontends.generate.generate","title":"generate","text":"
    generate(textbox: str, temp: float, top_p: float, max_tokens: int) -> str\n

    Generate text from the model.

    Parameters:

    Name Type Description Default str

    The text to generate from

    required float

    The temperature for the generation

    required float

    The top p value for the generation

    required int

    The maximum number of tokens to generate

    required

    Returns:

    Type Description str

    The generated text

    Source code in gerd/frontends/generate.py
    def generate(textbox: str, temp: float, top_p: float, max_tokens: int) -> str:\n    \"\"\"Generate text from the model.\n\n    Parameters:\n        textbox: The text to generate from\n        temp: The temperature for the generation\n        top_p: The top p value for the generation\n        max_tokens: The maximum number of tokens to generate\n\n    Returns:\n        The generated text\n    \"\"\"\n    if Global.service is None:\n        msg = \"Model not loaded\"\n        raise gr.Error(msg)\n    Global.service.config.model.top_p = top_p\n    Global.service.config.model.temperature = temp\n    Global.service.config.model.max_new_tokens = max_tokens\n    return Global.service.generate({\"message\": textbox}).text\n
    "},{"location":"reference/gerd/frontends/generate/#gerd.frontends.generate.generate(textbox)","title":"textbox","text":""},{"location":"reference/gerd/frontends/generate/#gerd.frontends.generate.generate(temp)","title":"temp","text":""},{"location":"reference/gerd/frontends/generate/#gerd.frontends.generate.generate(top_p)","title":"top_p","text":""},{"location":"reference/gerd/frontends/generate/#gerd.frontends.generate.generate(max_tokens)","title":"max_tokens","text":""},{"location":"reference/gerd/frontends/generate/#gerd.frontends.generate.load_model","title":"load_model","text":"
    load_model(model_name: str, origin: str) -> dict[str, Any]\n

    Load a global large language model.

    Parameters:

    Name Type Description Default str

    The name of the model

    required str

    Whether to use an extra LoRA

    required

    Returns:

    Type Description dict[str, Any]

    The updated interactive state, returns interactive=True when the model is loaded

    Source code in gerd/frontends/generate.py
    def load_model(model_name: str, origin: str) -> dict[str, Any]:\n    \"\"\"Load a global large language model.\n\n    Parameters:\n        model_name: The name of the model\n        origin: Whether to use an extra LoRA\n\n    Returns:\n        The updated interactive state, returns interactive=True when the model is loaded\n    \"\"\"\n    if Global.service is not None:\n        del Global.service\n    model_config = config.model_copy(deep=True)\n    model_config.model.name = model_name\n    if origin != \"None\":\n        model_config.model.loras.add(lora_dir / origin)\n    Global.service = ChatService(model_config)\n    return gr.update(interactive=True)\n
    "},{"location":"reference/gerd/frontends/generate/#gerd.frontends.generate.load_model(model_name)","title":"model_name","text":""},{"location":"reference/gerd/frontends/generate/#gerd.frontends.generate.load_model(origin)","title":"origin","text":""},{"location":"reference/gerd/frontends/instruct/","title":"gerd.frontends.instruct","text":""},{"location":"reference/gerd/frontends/instruct/#gerd.frontends.instruct","title":"gerd.frontends.instruct","text":"

    A gradio frontend to interact with the GERD instruct service.

    Classes:

    Name Description Global

    Singleton to store the service.

    Functions:

    Name Description generate

    Generate text from the model.

    load_model

    Load a global large language model.

    Attributes:

    Name Type Description KIOSK_MODE

    Whether the frontend is running in kiosk mode.

    "},{"location":"reference/gerd/frontends/instruct/#gerd.frontends.instruct.KIOSK_MODE","title":"KIOSK_MODE module-attribute","text":"
    KIOSK_MODE = kiosk_mode\n

    Whether the frontend is running in kiosk mode.

    Kiosk mode reduces the number of options to a minimum and automatically loads the model.

    "},{"location":"reference/gerd/frontends/instruct/#gerd.frontends.instruct.Global","title":"Global","text":"

    Singleton to store the service.

    "},{"location":"reference/gerd/frontends/instruct/#gerd.frontends.instruct.generate","title":"generate","text":"
    generate(temperature: float, top_p: float, max_tokens: int, system_text: str, *args: str) -> str\n

    Generate text from the model.

    Parameters:

    Name Type Description Default float

    The temperature for the generation

    required float

    The top-p value for the generation

    required int

    The maximum number of tokens to generate

    required str

    The system text to set up the context

    required str

    The user input

    () Source code in gerd/frontends/instruct.py
    def generate(\n    temperature: float,\n    top_p: float,\n    max_tokens: int,\n    system_text: str,\n    *args: str,\n) -> str:\n    \"\"\"Generate text from the model.\n\n    Parameters:\n        temperature: The temperature for the generation\n        top_p: The top-p value for the generation\n        max_tokens: The maximum number of tokens to generate\n        system_text: The system text to set up the context\n        args: The user input\n    \"\"\"\n    fields = dict(zip(config.model.prompt_config.parameters, args, strict=True))\n    if Global.service is None:\n        msg = \"Model not loaded\"\n        raise gr.Error(msg)\n    if system_text:\n        Global.service.config.model.prompt_setup = [\n            (\"system\", PromptConfig(text=system_text))\n        ]\n    Global.service.config.model.top_p = top_p\n    Global.service.config.model.temperature = temperature\n    Global.service.config.model.max_new_tokens = max_tokens\n    Global.service.reset()\n    return Global.service.submit_user_message(fields).text\n
    "},{"location":"reference/gerd/frontends/instruct/#gerd.frontends.instruct.generate(temperature)","title":"temperature","text":""},{"location":"reference/gerd/frontends/instruct/#gerd.frontends.instruct.generate(top_p)","title":"top_p","text":""},{"location":"reference/gerd/frontends/instruct/#gerd.frontends.instruct.generate(max_tokens)","title":"max_tokens","text":""},{"location":"reference/gerd/frontends/instruct/#gerd.frontends.instruct.generate(system_text)","title":"system_text","text":""},{"location":"reference/gerd/frontends/instruct/#gerd.frontends.instruct.generate(args)","title":"args","text":""},{"location":"reference/gerd/frontends/instruct/#gerd.frontends.instruct.load_model","title":"load_model","text":"
    load_model(model_name: str, origin: str = 'None') -> dict[str, Any]\n

    Load a global large language model.

    Parameters:

    Name Type Description Default str

    The name of the model

    required str

    Whether to use an extra LoRA

    'None' Source code in gerd/frontends/instruct.py
    def load_model(model_name: str, origin: str = \"None\") -> dict[str, Any]:\n    \"\"\"Load a global large language model.\n\n    Parameters:\n        model_name: The name of the model\n        origin: Whether to use an extra LoRA\n    \"\"\"\n    if Global.service is not None:\n        _LOGGER.debug(\"Unloading model\")\n        del Global.service\n    model_config = config.model_copy()\n    model_config.model.name = model_name\n    if origin != \"None\" and (lora_dir / origin).is_dir():\n        model_config.model.loras.add(lora_dir / origin)\n    _LOGGER.info(\"Loading model %s\", model_config.model.name)\n    Global.service = ChatService(model_config)\n    _LOGGER.info(\"Model loaded\")\n    return gr.update(interactive=True)\n
    "},{"location":"reference/gerd/frontends/instruct/#gerd.frontends.instruct.load_model(model_name)","title":"model_name","text":""},{"location":"reference/gerd/frontends/instruct/#gerd.frontends.instruct.load_model(origin)","title":"origin","text":""},{"location":"reference/gerd/frontends/qa_frontend/","title":"gerd.frontends.qa_frontend","text":""},{"location":"reference/gerd/frontends/qa_frontend/#gerd.frontends.qa_frontend","title":"gerd.frontends.qa_frontend","text":"

    A gradio frontend to query the QA service and upload files to the vectorstore.

    Functions:

    Name Description files_changed

    Check if the file upload element has changed.

    get_qa_mode

    Get QAMode from string.

    handle_developer_mode_checkbox_change

    Enable/disable developermode.

    handle_type_radio_selection_change

    Enable/disable gui elements depend on which mode is selected.

    query

    Starts the selected QA Mode.

    set_prompt

    Updates the prompt of the selected QA Mode.

    "},{"location":"reference/gerd/frontends/qa_frontend/#gerd.frontends.qa_frontend.files_changed","title":"files_changed","text":"
    files_changed(file_paths: Optional[list[str]]) -> None\n

    Check if the file upload element has changed.

    If so, upload the new files to the vectorstore and delete the one that have been removed.

    Parameters:

    Name Type Description Default Optional[list[str]]

    The file paths to upload

    required Source code in gerd/frontends/qa_frontend.py
    def files_changed(file_paths: Optional[list[str]]) -> None:\n    \"\"\"Check if the file upload element has changed.\n\n    If so, upload the new files to the vectorstore and delete the one that\n    have been removed.\n\n    Parameters:\n        file_paths: The file paths to upload\n    \"\"\"\n    file_paths = file_paths or []\n    progress = gr.Progress()\n    new_set = set(file_paths)\n    new_files = new_set - store_set\n    delete_files = store_set - new_set\n    for new_file in new_files:\n        store_set.add(new_file)\n        with pathlib.Path(new_file).open(\"rb\") as file:\n            data = QAFileUpload(\n                data=file.read(),\n                name=pathlib.Path(new_file).name,\n            )\n        res = TRANSPORTER.add_file(data)\n        if res.status != 200:\n            _LOGGER.warning(\n                \"Data upload failed with error code: %d\\nReason: %s\",\n                res.status,\n                res.error_msg,\n            )\n            msg = (\n                f\"Datei konnte nicht hochgeladen werden: {res.error_msg}\"\n                \"(Error Code {res.status})\"\n            )\n            raise gr.Error(msg)\n    for delete_file in delete_files:\n        store_set.remove(delete_file)\n        res = TRANSPORTER.remove_file(pathlib.Path(delete_file).name)\n    progress(100, desc=\"Fertig!\")\n
    "},{"location":"reference/gerd/frontends/qa_frontend/#gerd.frontends.qa_frontend.files_changed(file_paths)","title":"file_paths","text":""},{"location":"reference/gerd/frontends/qa_frontend/#gerd.frontends.qa_frontend.get_qa_mode","title":"get_qa_mode","text":"
    get_qa_mode(search_type: str) -> QAModesEnum\n

    Get QAMode from string.

    Parameters:

    Name Type Description Default str

    The search type

    required

    Returns:

    Type Description QAModesEnum

    The QAMode

    Source code in gerd/frontends/qa_frontend.py
    def get_qa_mode(search_type: str) -> QAModesEnum:\n    \"\"\"Get QAMode from string.\n\n    Parameters:\n        search_type: The search type\n\n    Returns:\n        The QAMode\n    \"\"\"\n    if search_type in qa_modes_dict:\n        return qa_modes_dict[search_type]\n    else:\n        return QAModesEnum.NONE\n
    "},{"location":"reference/gerd/frontends/qa_frontend/#gerd.frontends.qa_frontend.get_qa_mode(search_type)","title":"search_type","text":""},{"location":"reference/gerd/frontends/qa_frontend/#gerd.frontends.qa_frontend.handle_developer_mode_checkbox_change","title":"handle_developer_mode_checkbox_change","text":"
    handle_developer_mode_checkbox_change(check: bool) -> List[Any]\n

    Enable/disable developermode.

    Enables or disables the developer mode and the corresponding GUI elements. Parameters: check: The current state of the developer mode checkbox Returns: The list of GUI element property changes to update

    Source code in gerd/frontends/qa_frontend.py
    def handle_developer_mode_checkbox_change(check: bool) -> List[Any]:\n    \"\"\"Enable/disable developermode.\n\n    Enables or disables the developer mode and the corresponding GUI elements.\n    Parameters:\n        check: The current state of the developer mode checkbox\n    Returns:\n        The list of GUI element property changes to update\n    \"\"\"\n    return [\n        gr.update(visible=check),\n        gr.update(visible=check),\n        gr.update(visible=check),\n        gr.update(visible=check),\n        gr.update(\n            choices=(\n                [\"LLM\", \"Analyze\", \"Analyze mult.\", \"VectorDB\"]\n                if check\n                else [\"LLM\", \"Analyze mult.\"]\n            )\n        ),\n    ]\n
    "},{"location":"reference/gerd/frontends/qa_frontend/#gerd.frontends.qa_frontend.handle_type_radio_selection_change","title":"handle_type_radio_selection_change","text":"
    handle_type_radio_selection_change(search_type: str) -> List[Any]\n

    Enable/disable gui elements depend on which mode is selected.

    This order of the updates elements must be considered
    • input text
    • prompt
    • k_slider
    • search strategy_dropdown

    Parameters:

    Name Type Description Default str

    The current search type

    required

    Returns:

    Type Description List[Any]

    The list of GUI element property changes to update

    Source code in gerd/frontends/qa_frontend.py
    def handle_type_radio_selection_change(search_type: str) -> List[Any]:\n    \"\"\"Enable/disable gui elements depend on which mode is selected.\n\n    This order of the updates elements must be considered:\n        - input text\n        - prompt\n        - k_slider\n        - search strategy_dropdown\n\n    Parameters:\n        search_type: The current search type\n\n    Returns:\n        The list of GUI element property changes to update\n    \"\"\"\n    if search_type == \"LLM\":\n        return [\n            gr.update(interactive=True, placeholder=\"Wie hei\u00dft der Patient?\"),\n            gr.update(value=TRANSPORTER.get_qa_prompt(get_qa_mode(search_type)).text),\n            gr.update(interactive=False),\n            gr.update(interactive=False),\n        ]\n    elif search_type == \"VectorDB\":\n        return [\n            gr.update(interactive=True, placeholder=\"Wie hei\u00dft der Patient?\"),\n            gr.update(value=TRANSPORTER.get_qa_prompt(get_qa_mode(search_type)).text),\n            gr.update(interactive=True),\n            gr.update(interactive=True),\n        ]\n\n    return [\n        gr.update(interactive=False, placeholder=\"\"),\n        gr.update(value=TRANSPORTER.get_qa_prompt(get_qa_mode(search_type)).text),\n        gr.update(interactive=False),\n        gr.update(interactive=False),\n    ]\n
    "},{"location":"reference/gerd/frontends/qa_frontend/#gerd.frontends.qa_frontend.handle_type_radio_selection_change(search_type)","title":"search_type","text":""},{"location":"reference/gerd/frontends/qa_frontend/#gerd.frontends.qa_frontend.query","title":"query","text":"
    query(question: str, search_type: str, k_source: int, search_strategy: str) -> str\n

    Starts the selected QA Mode.

    Parameters:

    Name Type Description Default str

    The question to ask

    required str

    The search type

    required int

    The number of sources

    required str

    The search strategy

    required

    Returns:

    Type Description str

    The response from the QA service

    Source code in gerd/frontends/qa_frontend.py
    def query(question: str, search_type: str, k_source: int, search_strategy: str) -> str:\n    \"\"\"Starts the selected QA Mode.\n\n    Parameters:\n        question: The question to ask\n        search_type: The search type\n        k_source: The number of sources\n        search_strategy: The search strategy\n\n    Returns:\n        The response from the QA service\n    \"\"\"\n    q = QAQuestion(\n        question=question, search_strategy=search_strategy, max_sources=k_source\n    )\n    # start search mode\n    if search_type == \"LLM\":\n        qa_res = TRANSPORTER.qa_query(q)\n        if qa_res.status != 200:\n            msg = (\n                f\"Query was unsuccessful: {qa_res.error_msg}\"\n                f\" (Error Code {qa_res.status})\"\n            )\n            raise gr.Error(msg)\n        return qa_res.response\n    # start analyze mode\n    elif search_type == \"Analyze\":\n        qa_analyze_res = TRANSPORTER.analyze_query()\n        if qa_analyze_res.status != 200:\n            msg = (\n                f\"Query was unsuccessful: {qa_analyze_res.error_msg}\"\n                f\" (Error Code {qa_analyze_res.status})\"\n            )\n            raise gr.Error(msg)\n        # remove unwanted fields from answer\n        qa_res_dic = {\n            key: value\n            for key, value in vars(qa_analyze_res).items()\n            if value is not None\n            and value != \"\"\n            and key not in qa_analyze_res.__class__.__dict__\n            and key != \"sources\"\n            and key != \"status\"\n            and key != \"response\"\n            and key != \"prompt\"\n        }\n        qa_res_str = \", \".join(f\"{key}={value}\" for key, value in qa_res_dic.items())\n        return qa_res_str\n    # start analyze mult prompts mode\n    elif search_type == \"Analyze mult.\":\n        qa_analyze_mult_res = TRANSPORTER.analyze_mult_prompts_query()\n        if qa_analyze_mult_res.status != 200:\n            msg = (\n                f\"Query was unsuccessful: {qa_analyze_mult_res.error_msg}\"\n                f\" (Error Code {qa_analyze_mult_res.status})\"\n            )\n            raise gr.Error(msg)\n        # remove unwanted fields from answer\n        qa_res_dic = {\n            key: value\n            for key, value in vars(qa_analyze_mult_res).items()\n            if value is not None\n            and value != \"\"\n            and key not in qa_analyze_mult_res.__class__.__dict__\n            and key != \"sources\"\n            and key != \"status\"\n            and key != \"response\"\n            and key != \"prompt\"\n        }\n        qa_res_str = \", \".join(f\"{key}={value}\" for key, value in qa_res_dic.items())\n        return qa_res_str\n    # start db search mode\n    db_res = TRANSPORTER.db_query(q)\n    if not db_res:\n        msg = f\"Database query returned empty!\"\n        raise gr.Error(msg)\n    output = \"\"\n    for doc in db_res:\n        output += f\"{doc.content}\\n\"\n        output += f\"({doc.name} / {doc.page})\\n----------\\n\\n\"\n    return output\n
    "},{"location":"reference/gerd/frontends/qa_frontend/#gerd.frontends.qa_frontend.query(question)","title":"question","text":""},{"location":"reference/gerd/frontends/qa_frontend/#gerd.frontends.qa_frontend.query(search_type)","title":"search_type","text":""},{"location":"reference/gerd/frontends/qa_frontend/#gerd.frontends.qa_frontend.query(k_source)","title":"k_source","text":""},{"location":"reference/gerd/frontends/qa_frontend/#gerd.frontends.qa_frontend.query(search_strategy)","title":"search_strategy","text":""},{"location":"reference/gerd/frontends/qa_frontend/#gerd.frontends.qa_frontend.set_prompt","title":"set_prompt","text":"
    set_prompt(prompt: str, search_type: str, progress: Optional[gr.Progress] = None) -> None\n

    Updates the prompt of the selected QA Mode.

    Parameters:

    Name Type Description Default str

    The new prompt

    required str

    The search type

    required Optional[Progress]

    The progress bar to update

    None Source code in gerd/frontends/qa_frontend.py
    def set_prompt(\n    prompt: str, search_type: str, progress: Optional[gr.Progress] = None\n) -> None:\n    \"\"\"Updates the prompt of the selected QA Mode.\n\n    Parameters:\n        prompt: The new prompt\n        search_type: The search type\n        progress: The progress bar to update\n    \"\"\"\n    if progress is None:\n        progress = gr.Progress()\n    progress(0, \"Aktualisiere Prompt...\")\n\n    answer = TRANSPORTER.set_qa_prompt(\n        PromptConfig(text=prompt), get_qa_mode(search_type)\n    )\n    if answer.error_msg:\n        if answer.status != 200:\n            msg = (\n                f\"Prompt konnte nicht aktualisiert werden: {answer.error_msg}\"\n                f\" (Error Code {answer.status})\"\n            )\n            raise gr.Error(msg)\n        else:\n            msg = f\"{answer.error_msg}\"\n            gr.Warning(msg)\n    else:\n        gr.Info(\"Prompt wurde aktualisiert!\")\n    progress(100, \"Fertig!\")\n
    "},{"location":"reference/gerd/frontends/qa_frontend/#gerd.frontends.qa_frontend.set_prompt(prompt)","title":"prompt","text":""},{"location":"reference/gerd/frontends/qa_frontend/#gerd.frontends.qa_frontend.set_prompt(search_type)","title":"search_type","text":""},{"location":"reference/gerd/frontends/qa_frontend/#gerd.frontends.qa_frontend.set_prompt(progress)","title":"progress","text":""},{"location":"reference/gerd/frontends/router/","title":"gerd.frontends.router","text":""},{"location":"reference/gerd/frontends/router/#gerd.frontends.router","title":"gerd.frontends.router","text":"

    A gradio frontend to start and stop the GERD services.

    Since most hosts that use a frontend will not have enough memory to run multiple services at the same time this router is used to start and stop the services as needed.

    Classes:

    Name Description AppController

    The controller for the app.

    AppState

    The state of the service.

    Functions:

    Name Description check_state

    Checks the app state and waits for the service to start.

    Attributes:

    Name Type Description GRADIO_ROUTER_PORT

    The port the router is running on.

    GRADIO_SERVER_PORT

    The port the gradio server is running on.

    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.GRADIO_ROUTER_PORT","title":"GRADIO_ROUTER_PORT module-attribute","text":"
    GRADIO_ROUTER_PORT = get('GRADIO_ROUTER_PORT', '7860')\n

    The port the router is running on.

    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.GRADIO_SERVER_PORT","title":"GRADIO_SERVER_PORT module-attribute","text":"
    GRADIO_SERVER_PORT = get('GRADIO_SERVER_PORT', '12121')\n

    The port the gradio server is running on.

    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppController","title":"AppController","text":"
    AppController()\n

    The controller for the app.

    The controlller is initialized in the stopped state.

    Methods:

    Name Description check_port

    Check if the service port is open.

    instance

    Get the instance of the controller.

    start

    Start the service with the given frontend.

    start_gen

    Start the generation service.

    start_instruct

    Start the instruct service.

    start_qa

    Start the QA service.

    start_simple

    Start the simple generation service.

    start_training

    Start the training service.

    stop

    Stop the service when it is running.

    Source code in gerd/frontends/router.py
    def __init__(self) -> None:\n    \"\"\"The controlller is initialized in the stopped state.\"\"\"\n    self.process: subprocess.Popen | None = None\n    self.state = AppState.STOPPED\n
    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppController.check_port","title":"check_port staticmethod","text":"
    check_port(port: int) -> bool\n

    Check if the service port is open.

    Parameters:

    Name Type Description Default int

    The port to check

    required

    Returns:

    Type Description bool

    True if the port us open, False otherwise.

    Source code in gerd/frontends/router.py
    @staticmethod\ndef check_port(port: int) -> bool:\n    \"\"\"Check if the service port is open.\n\n    Parameters:\n        port: The port to check\n\n    Returns:\n        True if the port us open, False otherwise.\n    \"\"\"\n    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n    result = sock.connect_ex((\"127.0.0.1\", port)) == 0\n    sock.close()\n    return result\n
    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppController.check_port(port)","title":"port","text":""},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppController.instance","title":"instance classmethod","text":"
    instance() -> AppController\n

    Get the instance of the controller.

    Source code in gerd/frontends/router.py
    @classmethod\ndef instance(cls) -> \"AppController\":\n    \"\"\"Get the instance of the controller.\"\"\"\n    if cls._instance is None:\n        cls._instance = AppController()\n    return cls._instance\n
    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppController.start","title":"start","text":"
    start(frontend: str) -> None\n

    Start the service with the given frontend.

    Parameters:

    Name Type Description Default str

    The frontend service name to start.

    required Source code in gerd/frontends/router.py
    def start(self, frontend: str) -> None:\n    \"\"\"Start the service with the given frontend.\n\n    Parameters:\n        frontend: The frontend service name to start.\n    \"\"\"\n    if not re.match(\"^[a-zA-Z0-9_]+$\", frontend):\n        msg = \"Invalid frontend name\"\n        raise gr.Error(msg)\n    self.stop()\n    cmd = [sys.executable, \"-m\", f\"gerd.frontends.{frontend}\"]\n    self.process = subprocess.Popen(  # noqa: S603\n        cmd,\n        env=os.environ | {\"GRADIO_SERVER_PORT\": GRADIO_SERVER_PORT},\n    )\n
    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppController.start(frontend)","title":"frontend","text":""},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppController.start_gen","title":"start_gen","text":"
    start_gen() -> str\n

    Start the generation service.

    Returns:

    Type Description str

    The name of the current app state.

    Source code in gerd/frontends/router.py
    def start_gen(self) -> str:\n    \"\"\"Start the generation service.\n\n    Returns:\n        The name of the current app state.\n    \"\"\"\n    self.start(\"gen_frontend\")\n    self.state = AppState.GENERATE_STARTING\n    return self.state.name\n
    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppController.start_instruct","title":"start_instruct","text":"
    start_instruct() -> str\n

    Start the instruct service.

    Returns:

    Type Description str

    The name of the current app state

    Source code in gerd/frontends/router.py
    def start_instruct(self) -> str:\n    \"\"\"Start the instruct service.\n\n    Returns:\n        The name of the current app state\n    \"\"\"\n    self.start(\"instruct\")\n    self.state = AppState.INSTRUCT_STARTING\n    return self.state.name\n
    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppController.start_qa","title":"start_qa","text":"
    start_qa() -> str\n

    Start the QA service.

    Returns:

    Type Description str

    The name of the current app state

    Source code in gerd/frontends/router.py
    def start_qa(self) -> str:\n    \"\"\"Start the QA service.\n\n    Returns:\n        The name of the current app state\n    \"\"\"\n    self.start(\"qa_frontend\")\n    self.state = AppState.QA_STARTING\n    return self.state.name\n
    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppController.start_simple","title":"start_simple","text":"
    start_simple() -> str\n

    Start the simple generation service.

    Returns:

    Type Description str

    The name of the current app state

    Source code in gerd/frontends/router.py
    def start_simple(self) -> str:\n    \"\"\"Start the simple generation service.\n\n    Returns:\n        The name of the current app state\n    \"\"\"\n    self.start(\"generate\")\n    self.state = AppState.SIMPLE_STARTING\n    return self.state.name\n
    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppController.start_training","title":"start_training","text":"
    start_training() -> str\n

    Start the training service.

    Returns:

    Type Description str

    The name of the current app state

    Source code in gerd/frontends/router.py
    def start_training(self) -> str:\n    \"\"\"Start the training service.\n\n    Returns:\n        The name of the current app state\n    \"\"\"\n    self.start(\"training\")\n    self.state = AppState.TRAINING_STARTING\n    return self.state.name\n
    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppController.stop","title":"stop","text":"
    stop() -> str\n

    Stop the service when it is running.

    Returns:

    Type Description str

    The name of the current app state.

    Source code in gerd/frontends/router.py
    def stop(self) -> str:\n    \"\"\"Stop the service when it is running.\n\n    Returns:\n        The name of the current app state.\n    \"\"\"\n    if self.state != AppState.STOPPED and self.process:\n        gr.Info(f\"Trying to stop process... {self.process.pid}\")\n        self.process.terminate()\n        ret = self.process.wait(5)\n        if ret is None:\n            gr.Info(\"Process did not stop in time, killing it\")\n            self.process.kill()\n        gr.Info(\"Stopped\")\n    if self.check_port(int(GRADIO_SERVER_PORT)):\n        gr.Info(\"Stopping service\")\n        prt = int(GRADIO_SERVER_PORT)\n        res = subprocess.check_output([\"lsof\", \"-i\", f\":{prt}\"])  # noqa: S603, S607\n        m = re.search(r\"[Py]ython\\s+(\\d+)\", res.decode(encoding=\"utf-8\"))\n        if m:\n            subprocess.check_call([\"kill\", m.group(1)])  # noqa: S603, S607\n            gr.Warning(f\"Killed service on port {prt}\")\n        else:\n            msg = \"Service could not be stopped\"\n            raise gr.Error(msg)\n    self.state = AppState.STOPPED\n    return self.state.name\n
    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppState","title":"AppState","text":"

    Bases: Enum

    The state of the service.

    Attributes:

    Name Type Description GENERATE_STARTED

    The generation service is started.

    GENERATE_STARTING

    The generation service is starting.

    INSTRUCT_STARTED

    The instruct service is started.

    INSTRUCT_STARTING

    The instruct service is starting.

    QA_STARTED

    The QA service is started.

    QA_STARTING

    The QA service is starting.

    SIMPLE_STARTED

    The simple generation service is started.

    SIMPLE_STARTING

    The simple generation service is starting.

    STOPPED

    All services is stopped.

    TRAINING_STARTED

    The training service is started.

    TRAINING_STARTING

    The training service is starting.

    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppState.GENERATE_STARTED","title":"GENERATE_STARTED class-attribute instance-attribute","text":"
    GENERATE_STARTED = auto()\n

    The generation service is started.

    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppState.GENERATE_STARTING","title":"GENERATE_STARTING class-attribute instance-attribute","text":"
    GENERATE_STARTING = auto()\n

    The generation service is starting.

    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppState.INSTRUCT_STARTED","title":"INSTRUCT_STARTED class-attribute instance-attribute","text":"
    INSTRUCT_STARTED = auto()\n

    The instruct service is started.

    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppState.INSTRUCT_STARTING","title":"INSTRUCT_STARTING class-attribute instance-attribute","text":"
    INSTRUCT_STARTING = auto()\n

    The instruct service is starting.

    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppState.QA_STARTED","title":"QA_STARTED class-attribute instance-attribute","text":"
    QA_STARTED = auto()\n

    The QA service is started.

    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppState.QA_STARTING","title":"QA_STARTING class-attribute instance-attribute","text":"
    QA_STARTING = auto()\n

    The QA service is starting.

    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppState.SIMPLE_STARTED","title":"SIMPLE_STARTED class-attribute instance-attribute","text":"
    SIMPLE_STARTED = auto()\n

    The simple generation service is started.

    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppState.SIMPLE_STARTING","title":"SIMPLE_STARTING class-attribute instance-attribute","text":"
    SIMPLE_STARTING = auto()\n

    The simple generation service is starting.

    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppState.STOPPED","title":"STOPPED class-attribute instance-attribute","text":"
    STOPPED = auto()\n

    All services is stopped.

    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppState.TRAINING_STARTED","title":"TRAINING_STARTED class-attribute instance-attribute","text":"
    TRAINING_STARTED = auto()\n

    The training service is started.

    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppState.TRAINING_STARTING","title":"TRAINING_STARTING class-attribute instance-attribute","text":"
    TRAINING_STARTING = auto()\n

    The training service is starting.

    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.check_state","title":"check_state","text":"
    check_state() -> str\n

    Checks the app state and waits for the service to start.

    Returns:

    Type Description str

    The name of the current app state.

    Source code in gerd/frontends/router.py
    def check_state() -> str:\n    \"\"\"Checks the app state and waits for the service to start.\n\n    Returns:\n        The name of the current app state.\n    \"\"\"\n    app = AppController.instance()\n    cnt = 0\n    while not app.check_port(int(GRADIO_SERVER_PORT)):\n        _LOGGER.info(\"Waiting for service to start\")\n        sleep(1)\n        cnt += 1\n        if cnt > 30:\n            app.state = AppState.STOPPED\n            msg = \"Service did not start in time\"\n            raise Exception(msg)\n    app.state = AppState(app.state.value + 1)\n    gr.Success(f\"Service started on port {GRADIO_SERVER_PORT}\")\n    return app.state.name\n
    "},{"location":"reference/gerd/frontends/training/","title":"gerd.frontends.training","text":""},{"location":"reference/gerd/frontends/training/#gerd.frontends.training","title":"gerd.frontends.training","text":"

    A gradio frontend to train LoRAs with.

    Classes:

    Name Description Global

    A singleton class handle to store the current trainer instance.

    Functions:

    Name Description check_trainer

    Check if the trainer is (still) running.

    get_file_list

    Get a list of files matching the glob pattern.

    get_loras

    Get a list of available LoRAs.

    start_training

    Start the training process.

    validate_files

    Validate the uploaded files.

    "},{"location":"reference/gerd/frontends/training/#gerd.frontends.training.Global","title":"Global","text":"

    A singleton class handle to store the current trainer instance.

    "},{"location":"reference/gerd/frontends/training/#gerd.frontends.training.check_trainer","title":"check_trainer","text":"
    check_trainer() -> dict[str, Any]\n

    Check if the trainer is (still) running.

    When the trainer is running, a progress bar is shown. The method returns a gradio property update of 'visible' which can be used to activate and deactivate elements based on the current training status.

    Returns:

    Type Description dict[str, Any]

    A dictionary with the status of gradio 'visible' property

    Source code in gerd/frontends/training.py
    def check_trainer() -> dict[str, Any]:\n    \"\"\"Check if the trainer is (still) running.\n\n    When the trainer is running, a progress bar is shown.\n    The method returns a gradio property update of 'visible'\n    which can be used to activate and deactivate elements based\n    on the current training status.\n\n    Returns:\n        A dictionary with the status of gradio 'visible' property\n    \"\"\"\n    if Global.trainer is not None:\n        progress = gr.Progress()\n        progress(0)\n        while Global.trainer is not None and Global.trainer.thread.is_alive():\n            max_steps = max(Global.trainer.tracked.max_steps, 1)\n            progress(Global.trainer.tracked.current_steps / max_steps)\n            time.sleep(0.5)\n        return gr.update(visible=True, value=\"Training complete\")\n    return gr.update(visible=False)\n
    "},{"location":"reference/gerd/frontends/training/#gerd.frontends.training.get_file_list","title":"get_file_list","text":"
    get_file_list(glob_pattern: str) -> str\n

    Get a list of files matching the glob pattern.

    Parameters:

    Name Type Description Default str

    The glob pattern to search for files

    required

    Returns:

    Type Description str

    A string with the list of files

    Source code in gerd/frontends/training.py
    def get_file_list(glob_pattern: str) -> str:\n    \"\"\"Get a list of files matching the glob pattern.\n\n    Parameters:\n        glob_pattern: The glob pattern to search for files\n\n    Returns:\n        A string with the list of files\n    \"\"\"\n    if not glob_pattern:\n        return \"\"\n    res = [str(f) for f in Path().glob(glob_pattern)]\n    return \"\\n\".join(res) if res else \"<No files found>\"\n
    "},{"location":"reference/gerd/frontends/training/#gerd.frontends.training.get_file_list(glob_pattern)","title":"glob_pattern","text":""},{"location":"reference/gerd/frontends/training/#gerd.frontends.training.get_loras","title":"get_loras","text":"
    get_loras() -> dict[str, Path]\n

    Get a list of available LoRAs.

    LORAs are loaded from the path defined in the default LoraTrainingConfig.

    Returns:

    Type Description dict[str, Path]

    A dictionary with the LoRA names as keys and the paths as values

    Source code in gerd/frontends/training.py
    def get_loras() -> dict[str, Path]:\n    \"\"\"Get a list of available LoRAs.\n\n    LORAs are loaded from the path defined in the default\n    [LoraTrainingConfig][gerd.training.lora.LoraTrainingConfig].\n\n    Returns:\n        A dictionary with the LoRA names as keys and the paths as values\n    \"\"\"\n    return {\n        path.stem: path\n        for path in default_config.output_dir.parent.iterdir()\n        if path.is_file() and path.suffix == \".zip\"\n    }\n
    "},{"location":"reference/gerd/frontends/training/#gerd.frontends.training.start_training","title":"start_training","text":"
    start_training(files: list[str] | None, model_name: str, lora_name: str, mode: str, data_source: str, input_glob: str, override: bool, modules: list[str], flags: list[str], epochs: int, batch_size: int, micro_batch_size: int, cutoff_len: int, overlap_len: int) -> str\n

    Start the training process.

    While training, the method will update the progress bar.

    Parameters:

    Name Type Description Default list[str] | None

    The list of files to train on

    required str

    The name of the model to train

    required str

    The name of the LoRA to train

    required str

    The training mode

    required str

    The source of the data

    required str

    The glob pattern to search for files

    required bool

    Whether to override existing models

    required list[str]

    The modules to train

    required list[str]

    The flags to set

    required int

    The number of epochs to train

    required int

    The batch size

    required int

    The micro batch size

    required int

    The cutoff length

    required int

    The overlap length

    required

    Returns:

    Type Description str

    A string with the status of the training

    Source code in gerd/frontends/training.py
    def start_training(\n    files: list[str] | None,\n    model_name: str,\n    lora_name: str,\n    mode: str,\n    data_source: str,\n    input_glob: str,\n    override: bool,\n    modules: list[str],\n    flags: list[str],\n    epochs: int,\n    batch_size: int,\n    micro_batch_size: int,\n    cutoff_len: int,\n    overlap_len: int,\n) -> str:\n    \"\"\"Start the training process.\n\n    While training, the method will update the progress bar.\n\n    Parameters:\n        files: The list of files to train on\n        model_name: The name of the model to train\n        lora_name: The name of the LoRA to train\n        mode: The training mode\n        data_source: The source of the data\n        input_glob: The glob pattern to search for files\n        override: Whether to override existing models\n        modules: The modules to train\n        flags: The flags to set\n        epochs: The number of epochs to train\n        batch_size: The batch size\n        micro_batch_size: The micro batch size\n        cutoff_len: The cutoff length\n        overlap_len: The overlap length\n\n    Returns:\n        A string with the status of the training\n    \"\"\"\n    if \"..\" in lora_name:\n        msg = \"Invalid LoRA name\"\n        raise gr.Error(msg)\n    progress = gr.Progress()\n    # a bit verbose conversion to appease mypy\n    # since training_mode is a Literal type\n    if mode.lower() not in [\"unstructured\", \"instructions\"]:\n        msg = f\"Invalid training mode '{mode}'\"\n        raise AssertionError(msg)\n    training_mode: Literal[\"unstructured\", \"instructions\"] = (\n        \"unstructured\" if mode.lower() == \"unstructured\" else \"instructions\"\n    )\n    train_config = LoraTrainingConfig(\n        model=ModelConfig(name=model_name),\n        mode=training_mode,\n        output_dir=default_config.output_dir.parent / lora_name,\n        override_existing=override,\n        modules=LoraModules(default=False, **{mod: True for mod in modules}),\n        flags=TrainingFlags(**{flag: True for flag in flags}),\n        epochs=epochs,\n        batch_size=batch_size,\n        micro_batch_size=micro_batch_size,\n        cutoff_len=cutoff_len,\n        input_glob=input_glob,\n        overlap_len=overlap_len,\n        zip_output=True,\n    )\n    # init settings will be overriden by env settings\n    # we need to set the output_dir explicitly\n    train_config.output_dir = default_config.output_dir.parent / lora_name\n\n    if files is None or not files and data_source == \"Upload\":\n        msg = \"No files uploaded\"\n        raise gr.Error(msg)\n    progress(0)\n    try:\n        if train_config.mode == \"unstructured\":\n            Global.trainer = train_unstructured(\n                train_config,\n                (\n                    [Path(f).read_text() for f in files]\n                    if data_source == \"Upload\"\n                    else None\n                ),\n            )\n        elif train_config.mode == \"instructions\":\n\n            def load_data() -> InstructTrainingData:\n                data = InstructTrainingData()\n                for f in files:\n                    with Path(f).open(\"r\") as file:\n                        data.samples.extend(\n                            InstructTrainingData.model_validate(json.load(file)).samples\n                        )\n                return data\n\n            Global.trainer = train_instruct(\n                train_config, load_data() if data_source == \"Upload\" else None\n            )\n        else:\n            msg = \"Invalid training mode\"\n            raise AssertionError(msg)\n    except AssertionError as e:\n        msg = str(e)\n        raise gr.Error(msg) from e\n    return \"Training started\"\n
    "},{"location":"reference/gerd/frontends/training/#gerd.frontends.training.start_training(files)","title":"files","text":""},{"location":"reference/gerd/frontends/training/#gerd.frontends.training.start_training(model_name)","title":"model_name","text":""},{"location":"reference/gerd/frontends/training/#gerd.frontends.training.start_training(lora_name)","title":"lora_name","text":""},{"location":"reference/gerd/frontends/training/#gerd.frontends.training.start_training(mode)","title":"mode","text":""},{"location":"reference/gerd/frontends/training/#gerd.frontends.training.start_training(data_source)","title":"data_source","text":""},{"location":"reference/gerd/frontends/training/#gerd.frontends.training.start_training(input_glob)","title":"input_glob","text":""},{"location":"reference/gerd/frontends/training/#gerd.frontends.training.start_training(override)","title":"override","text":""},{"location":"reference/gerd/frontends/training/#gerd.frontends.training.start_training(modules)","title":"modules","text":""},{"location":"reference/gerd/frontends/training/#gerd.frontends.training.start_training(flags)","title":"flags","text":""},{"location":"reference/gerd/frontends/training/#gerd.frontends.training.start_training(epochs)","title":"epochs","text":""},{"location":"reference/gerd/frontends/training/#gerd.frontends.training.start_training(batch_size)","title":"batch_size","text":""},{"location":"reference/gerd/frontends/training/#gerd.frontends.training.start_training(micro_batch_size)","title":"micro_batch_size","text":""},{"location":"reference/gerd/frontends/training/#gerd.frontends.training.start_training(cutoff_len)","title":"cutoff_len","text":""},{"location":"reference/gerd/frontends/training/#gerd.frontends.training.start_training(overlap_len)","title":"overlap_len","text":""},{"location":"reference/gerd/frontends/training/#gerd.frontends.training.validate_files","title":"validate_files","text":"
    validate_files(file_paths: list[str] | None, mode: str) -> tuple[list[str], dict[str, bool]]\n

    Validate the uploaded files.

    Whether the property 'interactive' is True depends on whether any files were valid. Parameters: file_paths: The list of file paths mode: The training mode

    Returns:

    Type Description tuple[list[str], dict[str, bool]]

    A tuple with the validated file paths and gradio property 'interactive'

    Source code in gerd/frontends/training.py
    def validate_files(\n    file_paths: list[str] | None, mode: str\n) -> tuple[list[str], dict[str, bool]]:\n    \"\"\"Validate the uploaded files.\n\n    Whether the property 'interactive' is True depends on whether\n    any files were valid.\n    Parameters:\n        file_paths: The list of file paths\n        mode: The training mode\n\n    Returns:\n        A tuple with the validated file paths and gradio property 'interactive'\n    \"\"\"\n    file_paths = file_paths or []\n    if mode.lower() == \"instructions\":\n\n        def validate(path: Path) -> bool:\n            if path.suffix != \".json\":\n                gr.Warning(f\"{path.name} has an invalid file type\")\n                return False\n            try:\n                with path.open(\"r\") as f:\n                    InstructTrainingData.model_validate(json.load(f))\n            except Exception as e:\n                gr.Warning(f\"{path.name} could not be validated: {e}\")\n                return False\n            return True\n\n    elif mode.lower() == \"unstructured\":\n\n        def validate(path: Path) -> bool:\n            if path.suffix != \".txt\":\n                gr.Warning(f\"{path.name} has an invalid file type\")\n                return False\n            return True\n\n    else:\n        gr.Error(\"Invalid training mode\")\n        return (file_paths, gr.update(interactive=False))\n\n    res = [f_path for f_path in file_paths if validate(Path(f_path).resolve())]\n    return (res, gr.update(interactive=len(res) > 0))\n
    "},{"location":"reference/gerd/gen/","title":"gerd.gen","text":""},{"location":"reference/gerd/gen/#gerd.gen","title":"gerd.gen","text":"

    Services and utilities for text generation with LLMs.

    Modules:

    Name Description chat_service

    Implementation of the ChatService class.

    generation_service

    Implements the Generation class.

    "},{"location":"reference/gerd/gen/chat_service/","title":"gerd.gen.chat_service","text":""},{"location":"reference/gerd/gen/chat_service/#gerd.gen.chat_service","title":"gerd.gen.chat_service","text":"

    Implementation of the ChatService class.

    This features the currently favoured approach of instruction-based work with large language models. Thus, models fined tuned for chat or instructions work best with this service. The service can be used to generate text as well as long as the model features a chat template. In this case this service should be prefered over the GenerationService since it is easier to setup a prompt according to the model's requirements.

    Classes:

    Name Description ChatService

    Service to generate text based on a chat history.

    "},{"location":"reference/gerd/gen/chat_service/#gerd.gen.chat_service.ChatService","title":"ChatService","text":"
    ChatService(config: GenerationConfig, parameters: Dict[str, str] | None = None)\n

    Service to generate text based on a chat history.

    The service is initialized with a config and parameters.

    The parameters are used to initialize the message history. However, future reset will not consider them. loads a model according to this config. The used LLM is loaded according to the model configuration right on initialization.

    Methods:

    Name Description add_message

    Add a message to the chat history.

    generate

    Generate a response based on the chat history.

    get_prompt_config

    Get the prompt configuration.

    reset

    Reset the chat history.

    set_prompt_config

    Set the prompt configuration.

    submit_user_message

    Submit a message with the user role and generates a response.

    Source code in gerd/gen/chat_service.py
    def __init__(\n    self, config: GenerationConfig, parameters: Dict[str, str] | None = None\n) -> None:\n    \"\"\"The service is initialized with a config and parameters.\n\n    The parameters are used to initialize the message history.\n    However, future reset will not consider them.\n    loads a model according to this config.\n    The used LLM is loaded according to the model configuration\n    right on initialization.\n    \"\"\"\n    self.config = config\n    self._model = gerd_loader.load_model_from_config(self.config.model)\n    self.messages: list[ChatMessage] = []\n    self.reset(parameters)\n
    "},{"location":"reference/gerd/gen/chat_service/#gerd.gen.chat_service.ChatService.add_message","title":"add_message","text":"
    add_message(parameters: Dict[str, str] | None = None, role: Literal['user', 'system', 'assistant'] = 'user', prompt_config: Optional[PromptConfig] = None) -> None\n

    Add a message to the chat history.

    Source code in gerd/gen/chat_service.py
    def add_message(\n    self,\n    parameters: Dict[str, str] | None = None,\n    role: Literal[\"user\", \"system\", \"assistant\"] = \"user\",\n    prompt_config: Optional[PromptConfig] = None,\n) -> None:\n    \"\"\"Add a message to the chat history.\"\"\"\n    parameters = parameters or {}\n    user_prompt: PromptConfig = prompt_config or self.config.model.prompt_config\n    self.messages.append(\n        {\n            \"role\": role,\n            \"content\": user_prompt.format(parameters),\n        }\n    )\n
    "},{"location":"reference/gerd/gen/chat_service/#gerd.gen.chat_service.ChatService.generate","title":"generate","text":"
    generate(parameters: Dict[str, str]) -> GenResponse\n

    Generate a response based on the chat history.

    This method can be used as a replacement for GenerationService.generate in cases where the used model provides a chat template. When this is the case, using this method is more reliable as it requires less manual configuration to set up the prompt according to the model's requirements.

    Parameters:

    Name Type Description Default Dict[str, str]

    The parameters to format the prompt with

    required

    Returns:

    Type Description GenResponse

    The generation result

    Source code in gerd/gen/chat_service.py
    def generate(self, parameters: Dict[str, str]) -> GenResponse:\n    \"\"\"Generate a response based on the chat history.\n\n    This method can be used as a replacement for\n    [GenerationService.generate][gerd.gen.generation_service.GenerationService.generate]\n    in cases where the used model provides a chat template.\n    When this is the case, using this method is more reliable as it requires less\n    manual configuration to set up the prompt according to the model's requirements.\n\n    Parameters:\n        parameters: The parameters to format the prompt with\n\n    Returns:\n        The generation result\n    \"\"\"\n    self.reset(parameters)\n    self.add_message(parameters, role=\"user\")\n\n    if self.config.features.prompt_chaining:\n        for i, prompt_config in enumerate(\n            self.config.features.prompt_chaining.prompts, 1\n        ):\n            self.reset()\n            res = self.submit_user_message(parameters, prompt_config=prompt_config)\n            parameters[f\"response_{i}\"] = res.text\n        response = res.text\n        resolved = \"\\n\".join(parameters.values())\n    else:\n        resolved = \"\\n\".join(m[\"content\"] for m in self.messages)\n        _LOGGER.debug(\n            \"\\n\"\n            \"===== Resolved prompt ======\\n\\n\"\n            \"%s\\n\\n\"\n            \"============================\",\n            resolved,\n        )\n        response = self._model.generate(resolved)\n        _LOGGER.debug(\n            \"\\n\"\n            \"========= Response =========\\n\\n\"\n            \"%s\\n\\n\"\n            \"============================\",\n            response,\n        )\n    return GenResponse(text=response, prompt=resolved)\n
    "},{"location":"reference/gerd/gen/chat_service/#gerd.gen.chat_service.ChatService.generate(parameters)","title":"parameters","text":""},{"location":"reference/gerd/gen/chat_service/#gerd.gen.chat_service.ChatService.get_prompt_config","title":"get_prompt_config","text":"
    get_prompt_config() -> PromptConfig\n

    Get the prompt configuration.

    Source code in gerd/gen/chat_service.py
    def get_prompt_config(self) -> PromptConfig:\n    \"\"\"Get the prompt configuration.\"\"\"\n    return self.config.model.prompt_config\n
    "},{"location":"reference/gerd/gen/chat_service/#gerd.gen.chat_service.ChatService.reset","title":"reset","text":"
    reset(parameters: Dict[str, str] | None = None) -> None\n

    Reset the chat history.

    Source code in gerd/gen/chat_service.py
    def reset(self, parameters: Dict[str, str] | None = None) -> None:\n    \"\"\"Reset the chat history.\"\"\"\n    parameters = parameters or {}\n    self.messages.clear()\n    for role, message in self.config.model.prompt_setup:\n        self.messages.append(\n            {\n                \"role\": role,\n                \"content\": message.format(parameters),\n            }\n        )\n
    "},{"location":"reference/gerd/gen/chat_service/#gerd.gen.chat_service.ChatService.set_prompt_config","title":"set_prompt_config","text":"
    set_prompt_config(config: PromptConfig) -> PromptConfig\n

    Set the prompt configuration.

    Source code in gerd/gen/chat_service.py
    def set_prompt_config(\n    self,\n    config: PromptConfig,\n) -> PromptConfig:\n    \"\"\"Set the prompt configuration.\"\"\"\n    self.config.model.prompt_config = config\n    return self.config.model.prompt_config\n
    "},{"location":"reference/gerd/gen/chat_service/#gerd.gen.chat_service.ChatService.submit_user_message","title":"submit_user_message","text":"
    submit_user_message(parameters: Dict[str, str] | None = None, prompt_config: Optional[PromptConfig] = None) -> GenResponse\n

    Submit a message with the user role and generates a response.

    The service's prompt configuration is used to format the prompt unless a different prompt configuration is provided. Parameters: parameters: The parameters to format the prompt with prompt_config: The optional prompt configuration to be used

    Returns:

    Type Description GenResponse

    The generation result

    Source code in gerd/gen/chat_service.py
    def submit_user_message(\n    self,\n    parameters: Dict[str, str] | None = None,\n    prompt_config: Optional[PromptConfig] = None,\n) -> GenResponse:\n    \"\"\"Submit a message with the user role and generates a response.\n\n    The service's prompt configuration is used to format the prompt unless\n    a different prompt configuration is provided.\n    Parameters:\n        parameters: The parameters to format the prompt with\n        prompt_config: The optional prompt configuration to be used\n\n    Returns:\n        The generation result\n    \"\"\"\n    self.add_message(parameters, role=\"user\", prompt_config=prompt_config)\n    _LOGGER.debug(\n        \"\\n\"\n        \"===== Resolved prompt ======\\n\\n\"\n        \"%s\\n\\n\"\n        \"============================\",\n        \"\\n\".join(m[\"role\"] + \": \" + str(m[\"content\"]) for m in self.messages),\n    )\n    role, response = self._model.create_chat_completion(self.messages)\n    _LOGGER.debug(\n        \"\\n\"\n        \"========= Response =========\\n\\n\"\n        \"%s: %s\\n\\n\"\n        \"============================\",\n        role,\n        response,\n    )\n    self.messages.append({\"role\": role, \"content\": response})\n    return GenResponse(text=response, prompt=self.messages[-2][\"content\"])\n
    "},{"location":"reference/gerd/gen/generation_service/","title":"gerd.gen.generation_service","text":""},{"location":"reference/gerd/gen/generation_service/#gerd.gen.generation_service","title":"gerd.gen.generation_service","text":"

    Implements the Generation class.

    The generation services is meant to generate text based on a prompt and/or the continuation of a provided text.

    Classes:

    Name Description GenerationService

    Service to generate text based on a prompt.

    "},{"location":"reference/gerd/gen/generation_service/#gerd.gen.generation_service.GenerationService","title":"GenerationService","text":"
    GenerationService(config: GenerationConfig)\n

    Service to generate text based on a prompt.

    Initialize the generation service and loads the model.

    Parameters:

    Name Type Description Default GenerationConfig

    The configuration for the generation service

    required

    Methods:

    Name Description generate

    Generate text based on the prompt configuration.

    get_prompt_config

    Get the prompt configuration.

    set_prompt_config

    Sets the prompt configuration.

    Source code in gerd/gen/generation_service.py
    def __init__(self, config: GenerationConfig) -> None:\n    \"\"\"Initialize the generation service and loads the model.\n\n    Parameters:\n        config: The configuration for the generation service\n    \"\"\"\n    self.config = config\n    self._model = gerd_loader.load_model_from_config(self.config.model)\n
    "},{"location":"reference/gerd/gen/generation_service/#gerd.gen.generation_service.GenerationService(config)","title":"config","text":""},{"location":"reference/gerd/gen/generation_service/#gerd.gen.generation_service.GenerationService.generate","title":"generate","text":"
    generate(parameters: Dict[str, str], add_prompt: bool = False) -> GenResponse\n

    Generate text based on the prompt configuration.

    The actual prompt is provided by the prompt configuration. The list of parameters is used to format the prompt and replace the placeholders. The list can be empty if the prompt does not contain any placeholders.

    Parameters:

    Name Type Description Default Dict[str, str]

    The parameters to format the prompt with

    required bool

    Whether to add the prompt to the response

    False

    Returns:

    Type Description GenResponse

    The generation result

    Source code in gerd/gen/generation_service.py
    def generate(\n    self, parameters: Dict[str, str], add_prompt: bool = False\n) -> GenResponse:\n    \"\"\"Generate text based on the prompt configuration.\n\n    The actual prompt is provided by the prompt configuration.\n    The list of parameters is used to format the prompt\n    and replace the placeholders. The list can be empty if\n    the prompt does not contain any placeholders.\n\n    Parameters:\n        parameters: The parameters to format the prompt with\n        add_prompt: Whether to add the prompt to the response\n\n    Returns:\n        The generation result\n    \"\"\"\n    if self.config.features.prompt_chaining:\n        from gerd.features.prompt_chaining import PromptChaining\n\n        response = PromptChaining(\n            self.config.features.prompt_chaining,\n            self._model,\n            self.config.model.prompt_config,\n        ).generate(parameters)\n    else:\n        template = self.config.model.prompt_config.template\n        resolved = (\n            template.render(**parameters)\n            if template\n            else self.config.model.prompt_config.text.format(**parameters)\n        )\n        _LOGGER.debug(\n            \"\\n====== Resolved prompt =====\\n\\n%s\\n\\n=============================\",\n            resolved,\n        )\n        response = self._model.generate(resolved)\n        _LOGGER.debug(\n            \"\\n====== Response =====\\n\\n%s\\n\\n=============================\",\n            response,\n        )\n    return GenResponse(text=response, prompt=resolved if add_prompt else None)\n
    "},{"location":"reference/gerd/gen/generation_service/#gerd.gen.generation_service.GenerationService.generate(parameters)","title":"parameters","text":""},{"location":"reference/gerd/gen/generation_service/#gerd.gen.generation_service.GenerationService.generate(add_prompt)","title":"add_prompt","text":""},{"location":"reference/gerd/gen/generation_service/#gerd.gen.generation_service.GenerationService.get_prompt_config","title":"get_prompt_config","text":"
    get_prompt_config() -> PromptConfig\n

    Get the prompt configuration.

    Returns:

    Type Description PromptConfig

    The prompt configuration

    Source code in gerd/gen/generation_service.py
    def get_prompt_config(self) -> PromptConfig:\n    \"\"\"Get the prompt configuration.\n\n    Returns:\n        The prompt configuration\n    \"\"\"\n    return self.config.model.prompt_config\n
    "},{"location":"reference/gerd/gen/generation_service/#gerd.gen.generation_service.GenerationService.set_prompt_config","title":"set_prompt_config","text":"
    set_prompt_config(config: PromptConfig) -> PromptConfig\n

    Sets the prompt configuration.

    Parameters:

    Name Type Description Default PromptConfig

    The prompt configuration

    required

    Returns: The prompt configuration; Should be the same as the input in most cases

    Source code in gerd/gen/generation_service.py
    def set_prompt_config(\n    self,\n    config: PromptConfig,\n) -> PromptConfig:\n    \"\"\"Sets the prompt configuration.\n\n    Parameters:\n        config: The prompt configuration\n    Returns:\n        The prompt configuration; Should be the same as the input in most cases\n    \"\"\"\n    self.config.model.prompt_config = config\n    return self.config.model.prompt_config\n
    "},{"location":"reference/gerd/gen/generation_service/#gerd.gen.generation_service.GenerationService.set_prompt_config(config)","title":"config","text":""},{"location":"reference/gerd/loader/","title":"gerd.loader","text":""},{"location":"reference/gerd/loader/#gerd.loader","title":"gerd.loader","text":"

    Module for loading language models.

    Depending on the configuration, different language models are loaded and different libraries are used. The main goal is to provide a unified interface to the different models and libraries.

    Classes:

    Name Description LLM

    The abstract base class for large language models.

    LlamaCppLLM

    A language model using the Llama.cpp library.

    MockLLM

    A mock language model for testing purposes.

    RemoteLLM

    A language model using a remote endpoint.

    TransformerLLM

    A language model using the transformers library.

    Functions:

    Name Description load_model_from_config

    Loads a language model based on the configuration.

    "},{"location":"reference/gerd/loader/#gerd.loader.LLM","title":"LLM","text":"
    LLM(config: ModelConfig)\n

    The abstract base class for large language models.

    Should be implemented by all language model backends.

    A language model is initialized with a configuration.

    Parameters:

    Name Type Description Default ModelConfig

    The configuration for the language model

    required

    Methods:

    Name Description create_chat_completion

    Create a chat completion based on a list of messages.

    generate

    Generate text based on a prompt.

    Source code in gerd/loader.py
    @abc.abstractmethod\ndef __init__(self, config: ModelConfig) -> None:\n    \"\"\"A language model is initialized with a configuration.\n\n    Parameters:\n        config: The configuration for the language model\n    \"\"\"\n    pass\n
    "},{"location":"reference/gerd/loader/#gerd.loader.LLM(config)","title":"config","text":""},{"location":"reference/gerd/loader/#gerd.loader.LLM.create_chat_completion","title":"create_chat_completion abstractmethod","text":"
    create_chat_completion(messages: list[ChatMessage]) -> tuple[ChatRole, str]\n

    Create a chat completion based on a list of messages.

    Parameters:

    Name Type Description Default list[ChatMessage]

    The list of messages in the chat history

    required

    Returns:

    Type Description tuple[ChatRole, str]

    The role of the generated message and the content

    Source code in gerd/loader.py
    @abc.abstractmethod\ndef create_chat_completion(\n    self, messages: list[ChatMessage]\n) -> tuple[ChatRole, str]:\n    \"\"\"Create a chat completion based on a list of messages.\n\n    Parameters:\n        messages: The list of messages in the chat history\n\n    Returns:\n        The role of the generated message and the content\n    \"\"\"\n    pass\n
    "},{"location":"reference/gerd/loader/#gerd.loader.LLM.create_chat_completion(messages)","title":"messages","text":""},{"location":"reference/gerd/loader/#gerd.loader.LLM.generate","title":"generate abstractmethod","text":"
    generate(prompt: str) -> str\n

    Generate text based on a prompt.

    Parameters:

    Name Type Description Default str

    The prompt to generate text from

    required

    Returns:

    Type Description str

    The generated text

    Source code in gerd/loader.py
    @abc.abstractmethod\ndef generate(self, prompt: str) -> str:\n    \"\"\"Generate text based on a prompt.\n\n    Parameters:\n        prompt: The prompt to generate text from\n\n    Returns:\n        The generated text\n    \"\"\"\n    pass\n
    "},{"location":"reference/gerd/loader/#gerd.loader.LLM.generate(prompt)","title":"prompt","text":""},{"location":"reference/gerd/loader/#gerd.loader.LlamaCppLLM","title":"LlamaCppLLM","text":"
    LlamaCppLLM(config: ModelConfig)\n

    Bases: LLM

    A language model using the Llama.cpp library.

    A language model is initialized with a configuration.

    Parameters:

    Name Type Description Default ModelConfig

    The configuration for the language model

    required

    Methods:

    Name Description create_chat_completion

    Create a chat completion based on a list of messages.

    generate

    Generate text based on a prompt.

    Source code in gerd/loader.py
    @override\ndef __init__(self, config: ModelConfig) -> None:\n    from llama_cpp import Llama\n\n    self.config = config\n    self._model = Llama.from_pretrained(\n        repo_id=config.name,\n        filename=config.file,\n        n_ctx=config.context_length,\n        n_gpu_layers=config.gpu_layers,\n        n_threads=config.threads,\n        **config.extra_kwargs or {},\n    )\n
    "},{"location":"reference/gerd/loader/#gerd.loader.LlamaCppLLM(config)","title":"config","text":""},{"location":"reference/gerd/loader/#gerd.loader.LlamaCppLLM.create_chat_completion","title":"create_chat_completion","text":"
    create_chat_completion(messages: list[ChatMessage]) -> tuple[ChatRole, str]\n

    Create a chat completion based on a list of messages.

    Parameters:

    Name Type Description Default list[ChatMessage]

    The list of messages in the chat history

    required

    Returns:

    Type Description tuple[ChatRole, str]

    The role of the generated message and the content

    Source code in gerd/loader.py
    @override\ndef create_chat_completion(\n    self, messages: list[ChatMessage]\n) -> tuple[ChatRole, str]:\n    res = self._model.create_chat_completion(\n        # mypy cannot resolve the role parameter even though\n        # is is defined on compatible literals\n        [{\"role\": m[\"role\"], \"content\": m[\"content\"]} for m in messages],  # type: ignore[misc]\n        stop=self.config.stop,\n        max_tokens=self.config.max_new_tokens,\n        top_p=self.config.top_p,\n        top_k=self.config.top_k,\n        temperature=self.config.temperature,\n        repeat_penalty=self.config.repetition_penalty,\n    )\n    if not isinstance(res, Iterator):\n        msg = res[\"choices\"][0][\"message\"]\n        if msg[\"role\"] == \"function\":\n            error_msg = \"function role not expected\"\n            raise NotImplementedError(error_msg)\n        return (msg[\"role\"], msg[\"content\"].strip() if msg[\"content\"] else \"\")\n\n    error_msg = \"Cannot process stream responses for now\"\n    raise NotImplementedError(error_msg)\n
    "},{"location":"reference/gerd/loader/#gerd.loader.LlamaCppLLM.create_chat_completion(messages)","title":"messages","text":""},{"location":"reference/gerd/loader/#gerd.loader.LlamaCppLLM.generate","title":"generate","text":"
    generate(prompt: str) -> str\n

    Generate text based on a prompt.

    Parameters:

    Name Type Description Default str

    The prompt to generate text from

    required

    Returns:

    Type Description str

    The generated text

    Source code in gerd/loader.py
    @override\ndef generate(self, prompt: str) -> str:\n    res = self._model(\n        prompt,\n        stop=self.config.stop,\n        max_tokens=self.config.max_new_tokens,\n        top_p=self.config.top_p,\n        top_k=self.config.top_k,\n        temperature=self.config.temperature,\n        repeat_penalty=self.config.repetition_penalty,\n    )\n    output = next(res) if isinstance(res, Iterator) else res\n    return output[\"choices\"][0][\"text\"]\n
    "},{"location":"reference/gerd/loader/#gerd.loader.LlamaCppLLM.generate(prompt)","title":"prompt","text":""},{"location":"reference/gerd/loader/#gerd.loader.MockLLM","title":"MockLLM","text":"
    MockLLM(_: ModelConfig)\n

    Bases: LLM

    A mock language model for testing purposes.

    A language model is initialized with a configuration.

    Parameters:

    Name Type Description Default ModelConfig

    The configuration for the language model

    required

    Methods:

    Name Description create_chat_completion

    Create a chat completion based on a list of messages.

    generate

    Generate text based on a prompt.

    Source code in gerd/loader.py
    @override\ndef __init__(self, _: ModelConfig) -> None:\n    self.ret_value = \"MockLLM\"\n    pass\n
    "},{"location":"reference/gerd/loader/#gerd.loader.MockLLM(config)","title":"config","text":""},{"location":"reference/gerd/loader/#gerd.loader.MockLLM.create_chat_completion","title":"create_chat_completion","text":"
    create_chat_completion(_: list[ChatMessage]) -> tuple[ChatRole, str]\n

    Create a chat completion based on a list of messages.

    Parameters:

    Name Type Description Default list[ChatMessage]

    The list of messages in the chat history

    required

    Returns:

    Type Description tuple[ChatRole, str]

    The role of the generated message and the content

    Source code in gerd/loader.py
    @override\ndef create_chat_completion(self, _: list[ChatMessage]) -> tuple[ChatRole, str]:\n    return (\"assistant\", self.ret_value)\n
    "},{"location":"reference/gerd/loader/#gerd.loader.MockLLM.create_chat_completion(messages)","title":"messages","text":""},{"location":"reference/gerd/loader/#gerd.loader.MockLLM.generate","title":"generate","text":"
    generate(_: str) -> str\n

    Generate text based on a prompt.

    Parameters:

    Name Type Description Default str

    The prompt to generate text from

    required

    Returns:

    Type Description str

    The generated text

    Source code in gerd/loader.py
    @override\ndef generate(self, _: str) -> str:\n    return self.ret_value\n
    "},{"location":"reference/gerd/loader/#gerd.loader.MockLLM.generate(prompt)","title":"prompt","text":""},{"location":"reference/gerd/loader/#gerd.loader.RemoteLLM","title":"RemoteLLM","text":"
    RemoteLLM(config: ModelConfig)\n

    Bases: LLM

    A language model using a remote endpoint.

    The endpoint can be any service that are compatible with llama.cpp and openai API. For further information, please refer to the llama.cpp server API.

    A language model is initialized with a configuration.

    Parameters:

    Name Type Description Default ModelConfig

    The configuration for the language model

    required

    Methods:

    Name Description create_chat_completion

    Create a chat completion based on a list of messages.

    generate

    Generate text based on a prompt.

    Source code in gerd/loader.py
    @override\ndef __init__(self, config: ModelConfig) -> None:\n    self.config = config\n    if self.config.endpoint is None:\n        msg = \"Endpoint is required for remote LLM\"\n        raise ValueError(msg)\n\n    self._ep: ModelEndpoint = self.config.endpoint\n    self._prompt_field = \"prompt\"\n    self._header = {\"Content-Type\": \"application/json\"}\n    if self.config.endpoint.key:\n        self._header[\"Authorization\"] = (\n            f\"Bearer {self.config.endpoint.key.get_secret_value()}\"\n        )\n\n    if self.config.endpoint.type == \"openai\":\n        self.msg_template = {\n            \"model\": self.config.name,\n            \"temperature\": self.config.temperature,\n            \"frequency_penalty\": self.config.repetition_penalty,\n            \"max_completion_tokens\": self.config.max_new_tokens,\n            \"n\": 1,\n            \"stop\": self.config.stop,\n            \"top_p\": self.config.top_p,\n        }\n    elif self.config.endpoint.type == \"llama.cpp\":\n        self.msg_template = {\n            \"temperature\": self.config.temperature,\n            \"top_k\": self.config.top_k,\n            \"top_p\": self.config.top_p,\n            \"repeat_penalty\": self.config.repetition_penalty,\n            \"n_predict\": self.config.max_new_tokens,\n            \"stop\": self.config.stop or [],\n        }\n    else:\n        msg = f\"Unknown endpoint type: {self.config.endpoint.type}\"\n        raise ValueError(msg)\n
    "},{"location":"reference/gerd/loader/#gerd.loader.RemoteLLM(config)","title":"config","text":""},{"location":"reference/gerd/loader/#gerd.loader.RemoteLLM.create_chat_completion","title":"create_chat_completion","text":"
    create_chat_completion(messages: list[ChatMessage]) -> tuple[ChatRole, str]\n

    Create a chat completion based on a list of messages.

    Parameters:

    Name Type Description Default list[ChatMessage]

    The list of messages in the chat history

    required

    Returns:

    Type Description tuple[ChatRole, str]

    The role of the generated message and the content

    Source code in gerd/loader.py
    @override\ndef create_chat_completion(\n    self, messages: list[ChatMessage]\n) -> tuple[ChatRole, str]:\n    import json\n\n    import requests\n\n    self.msg_template[\"messages\"] = messages\n    res = requests.post(\n        self._ep.url + \"/v1/chat/completions\",\n        headers=self._header,\n        data=json.dumps(self.msg_template),\n        timeout=300,\n    )\n    if res.status_code == 200:\n        msg = res.json()[\"choices\"][0][\"message\"]\n        return (msg[\"role\"], msg[\"content\"].strip())\n    else:\n        _LOGGER.warning(\"Server returned error code %d\", res.status_code)\n    return (\"assistant\", \"\")\n
    "},{"location":"reference/gerd/loader/#gerd.loader.RemoteLLM.create_chat_completion(messages)","title":"messages","text":""},{"location":"reference/gerd/loader/#gerd.loader.RemoteLLM.generate","title":"generate","text":"
    generate(prompt: str) -> str\n

    Generate text based on a prompt.

    Parameters:

    Name Type Description Default str

    The prompt to generate text from

    required

    Returns:

    Type Description str

    The generated text

    Source code in gerd/loader.py
    @override\ndef generate(self, prompt: str) -> str:\n    import json\n\n    import requests\n\n    if self.config.endpoint and self.config.endpoint.type != \"llama.cpp\":\n        msg = (\n            \"Only llama.cpp supports simple completion yet. \"\n            \"Use chat completion instead.\"\n        )\n        raise NotImplementedError(msg)\n\n    self.msg_template[self._prompt_field] = prompt\n    res = requests.post(\n        self._ep.url + \"/completion\",\n        headers=self._header,\n        data=json.dumps(self.msg_template),\n        timeout=300,\n    )\n    if res.status_code == 200:\n        return str(res.json()[\"content\"])\n    else:\n        _LOGGER.warning(\"Server returned error code %d\", res.status_code)\n    return \"\"\n
    "},{"location":"reference/gerd/loader/#gerd.loader.RemoteLLM.generate(prompt)","title":"prompt","text":""},{"location":"reference/gerd/loader/#gerd.loader.TransformerLLM","title":"TransformerLLM","text":"
    TransformerLLM(config: ModelConfig)\n

    Bases: LLM

    A language model using the transformers library.

    A language model is initialized with a configuration.

    Parameters:

    Name Type Description Default ModelConfig

    The configuration for the language model

    required

    Methods:

    Name Description create_chat_completion

    Create a chat completion based on a list of messages.

    generate

    Generate text based on a prompt.

    Source code in gerd/loader.py
    @override\ndef __init__(self, config: ModelConfig) -> None:\n    import torch\n    from transformers import (\n        AutoModelForCausalLM,\n        AutoTokenizer,\n        PreTrainedModel,\n        pipeline,\n    )\n\n    # use_fast=False is ignored by transformers\n    os.environ[\"TOKENIZERS_PARALLELISM\"] = \"false\"\n\n    self.config = config\n    torch_dtypes: dict[str, torch.dtype] = {\n        \"bfloat16\": torch.bfloat16,\n        \"float16\": torch.float16,\n        \"float32\": torch.float32,\n        \"float64\": torch.float64,\n    }\n\n    model_kwargs = config.extra_kwargs or {}\n    if config.torch_dtype in torch_dtypes:\n        model_kwargs[\"torch_dtype\"] = torch_dtypes[config.torch_dtype]\n\n    tokenizer = AutoTokenizer.from_pretrained(config.name, use_fast=False)\n    model: PreTrainedModel = AutoModelForCausalLM.from_pretrained(  # type: ignore[no-any-unimported]\n        config.name, **model_kwargs\n    )\n\n    loaded_loras = set()\n    for lora in config.loras:\n        _LOGGER.info(\"Loading adapter %s\", lora)\n        if not Path(lora / \"adapter_model.safetensors\").exists():\n            _LOGGER.warning(\"Adapter %s does not exist\", lora)\n            continue\n        model.load_adapter(lora)\n        loaded_loras.add(lora)\n        train_params = Path(lora) / \"training_parameters.json\"\n        if train_params.exists() and tokenizer.pad_token_id is None:\n            from gerd.training.lora import LoraTrainingConfig\n\n            with open(train_params, \"r\") as f:\n                lora_config = LoraTrainingConfig.model_validate_json(f.read())\n                tokenizer.pad_token_id = lora_config.pad_token_id\n                # https://github.com/huggingface/transformers/issues/34842#issuecomment-2490994584\n                tokenizer.padding_side = (\n                    \"left\" if lora_config.padding_side == \"right\" else \"right\"\n                )\n                # tokenizer.padding_side = lora_config.padding_side\n\n    if loaded_loras:\n        model.enable_adapters()\n\n    self._pipe = pipeline(\n        task=\"text-generation\",\n        model=model,\n        tokenizer=tokenizer,\n        # device_map=\"auto\",  # https://github.com/huggingface/transformers/issues/31922\n        device=(\n            \"cuda\"\n            if config.gpu_layers > 0\n            else \"mps\"\n            if torch.backends.mps.is_available()\n            else \"cpu\"\n        ),\n        framework=\"pt\",\n        use_fast=False,\n    )\n
    "},{"location":"reference/gerd/loader/#gerd.loader.TransformerLLM(config)","title":"config","text":""},{"location":"reference/gerd/loader/#gerd.loader.TransformerLLM.create_chat_completion","title":"create_chat_completion","text":"
    create_chat_completion(messages: list[ChatMessage]) -> tuple[ChatRole, str]\n

    Create a chat completion based on a list of messages.

    Parameters:

    Name Type Description Default list[ChatMessage]

    The list of messages in the chat history

    required

    Returns:

    Type Description tuple[ChatRole, str]

    The role of the generated message and the content

    Source code in gerd/loader.py
    @override\ndef create_chat_completion(\n    self, messages: list[ChatMessage]\n) -> tuple[ChatRole, str]:\n    msg = self._pipe(\n        messages,\n        max_new_tokens=self.config.max_new_tokens,\n        repetition_penalty=self.config.repetition_penalty,\n        top_k=self.config.top_k,\n        top_p=self.config.top_p,\n        temperature=self.config.temperature,\n        do_sample=True,\n    )[0][\"generated_text\"][-1]\n    return (msg[\"role\"], msg[\"content\"].strip())\n
    "},{"location":"reference/gerd/loader/#gerd.loader.TransformerLLM.create_chat_completion(messages)","title":"messages","text":""},{"location":"reference/gerd/loader/#gerd.loader.TransformerLLM.generate","title":"generate","text":"
    generate(prompt: str) -> str\n

    Generate text based on a prompt.

    Parameters:

    Name Type Description Default str

    The prompt to generate text from

    required

    Returns:

    Type Description str

    The generated text

    Source code in gerd/loader.py
    @override\ndef generate(self, prompt: str) -> str:\n    res = self._pipe(\n        prompt,\n        max_new_tokens=self.config.max_new_tokens,\n        repetition_penalty=self.config.repetition_penalty,\n        top_k=self.config.top_k,\n        top_p=self.config.top_p,\n        temperature=self.config.temperature,\n        do_sample=True,\n    )\n    output: str = res[0][\"generated_text\"]\n    return output\n
    "},{"location":"reference/gerd/loader/#gerd.loader.TransformerLLM.generate(prompt)","title":"prompt","text":""},{"location":"reference/gerd/loader/#gerd.loader.load_model_from_config","title":"load_model_from_config","text":"
    load_model_from_config(config: ModelConfig) -> LLM\n

    Loads a language model based on the configuration.

    Which language model is loaded depends on the configuration. For instance, if an endpoint is provided, a remote language model is loaded. If a file is provided, Llama.cpp is used. Otherwise, transformers is used.

    Parameters:

    Name Type Description Default ModelConfig

    The configuration for the language model

    required

    Returns:

    Type Description LLM

    The loaded language model

    Source code in gerd/loader.py
    def load_model_from_config(config: ModelConfig) -> LLM:\n    \"\"\"Loads a language model based on the configuration.\n\n    Which language model is loaded depends on the configuration.\n    For instance, if an endpoint is provided, a remote language model is loaded.\n    If a file is provided, Llama.cpp is used.\n    Otherwise, transformers is used.\n\n    Parameters:\n        config: The configuration for the language model\n\n    Returns:\n        The loaded language model\n\n    \"\"\"\n    if config.endpoint:\n        _LOGGER.info(\"Using remote endpoint %s\", config.endpoint.url)\n        return RemoteLLM(config)\n    if config.file:\n        _LOGGER.info(\n            \"Using Llama.cpp with model %s and file %s\", config.name, config.file\n        )\n        return LlamaCppLLM(config)\n    _LOGGER.info(\"Using transformers with model %s\", config.name)\n    return TransformerLLM(config)\n
    "},{"location":"reference/gerd/loader/#gerd.loader.load_model_from_config(config)","title":"config","text":""},{"location":"reference/gerd/models/","title":"gerd.models","text":""},{"location":"reference/gerd/models/#gerd.models","title":"gerd.models","text":"

    Pydantic model definitions and data classes that are share accross modules.

    Modules:

    Name Description gen

    Models for the generation and chat service.

    label

    Data definitions for Label Studio tasks.

    logging

    Logging configuration and utilities.

    model

    Model configuration for supported model classes.

    qa

    Data definitions for QA model configuration.

    server

    Server configuration model for REST backends.

    "},{"location":"reference/gerd/models/gen/","title":"gerd.models.gen","text":""},{"location":"reference/gerd/models/gen/#gerd.models.gen","title":"gerd.models.gen","text":"

    Models for the generation and chat service.

    Classes:

    Name Description GenerationConfig

    Configuration for the generation services.

    GenerationFeaturesConfig

    Configuration for the generation-specific features.

    "},{"location":"reference/gerd/models/gen/#gerd.models.gen.GenerationConfig","title":"GenerationConfig","text":"

    Bases: BaseSettings

    Configuration for the generation services.

    A configuration can be used for the GenerationService or the ChatService. Both support to generate text based on a prompt.

    Methods:

    Name Description settings_customise_sources

    Customize the settings sources used by pydantic-settings.

    Attributes:

    Name Type Description features GenerationFeaturesConfig

    The extra features to be used for the generation service.

    model ModelConfig

    The model to be used for the generation service.

    "},{"location":"reference/gerd/models/gen/#gerd.models.gen.GenerationConfig.features","title":"features class-attribute instance-attribute","text":"
    features: GenerationFeaturesConfig = GenerationFeaturesConfig()\n

    The extra features to be used for the generation service.

    "},{"location":"reference/gerd/models/gen/#gerd.models.gen.GenerationConfig.model","title":"model class-attribute instance-attribute","text":"
    model: ModelConfig = ModelConfig()\n

    The model to be used for the generation service.

    "},{"location":"reference/gerd/models/gen/#gerd.models.gen.GenerationConfig.settings_customise_sources","title":"settings_customise_sources classmethod","text":"
    settings_customise_sources(_: Type[BaseSettings], init_settings: PydanticBaseSettingsSource, env_settings: PydanticBaseSettingsSource, dotenv_settings: PydanticBaseSettingsSource, file_secret_settings: PydanticBaseSettingsSource) -> Tuple[PydanticBaseSettingsSource, ...]\n

    Customize the settings sources used by pydantic-settings.

    The order of the sources is important. The first source has the highest priority.

    Parameters:

    Name Type Description Default

    The class of the settings.

    required PydanticBaseSettingsSource

    The settings from the initialization.

    required PydanticBaseSettingsSource

    The settings from the environment.

    required PydanticBaseSettingsSource

    The settings from the dotenv file.

    required PydanticBaseSettingsSource

    The settings from the secret file.

    required

    Returns:

    Type Description Tuple[PydanticBaseSettingsSource, ...]

    The customized settings sources.

    Source code in gerd/models/gen.py
    @classmethod\ndef settings_customise_sources(\n    cls,\n    _: Type[BaseSettings],\n    init_settings: PydanticBaseSettingsSource,\n    env_settings: PydanticBaseSettingsSource,\n    dotenv_settings: PydanticBaseSettingsSource,\n    file_secret_settings: PydanticBaseSettingsSource,\n) -> Tuple[PydanticBaseSettingsSource, ...]:\n    \"\"\"Customize the settings sources used by pydantic-settings.\n\n    The order of the sources is important.\n    The first source has the highest priority.\n\n    Parameters:\n        cls: The class of the settings.\n        init_settings: The settings from the initialization.\n        env_settings: The settings from the environment.\n        dotenv_settings: The settings from the dotenv file.\n        file_secret_settings: The settings from the secret file.\n\n    Returns:\n        The customized settings sources.\n    \"\"\"\n    return (\n        file_secret_settings,\n        env_settings,\n        dotenv_settings,\n        init_settings,\n    )\n
    "},{"location":"reference/gerd/models/gen/#gerd.models.gen.GenerationConfig.settings_customise_sources(cls)","title":"cls","text":""},{"location":"reference/gerd/models/gen/#gerd.models.gen.GenerationConfig.settings_customise_sources(init_settings)","title":"init_settings","text":""},{"location":"reference/gerd/models/gen/#gerd.models.gen.GenerationConfig.settings_customise_sources(env_settings)","title":"env_settings","text":""},{"location":"reference/gerd/models/gen/#gerd.models.gen.GenerationConfig.settings_customise_sources(dotenv_settings)","title":"dotenv_settings","text":""},{"location":"reference/gerd/models/gen/#gerd.models.gen.GenerationConfig.settings_customise_sources(file_secret_settings)","title":"file_secret_settings","text":""},{"location":"reference/gerd/models/gen/#gerd.models.gen.GenerationFeaturesConfig","title":"GenerationFeaturesConfig","text":"

    Bases: BaseModel

    Configuration for the generation-specific features.

    Attributes:

    Name Type Description prompt_chaining PromptChainingConfig | None

    Configuration for prompt chaining.

    "},{"location":"reference/gerd/models/gen/#gerd.models.gen.GenerationFeaturesConfig.prompt_chaining","title":"prompt_chaining class-attribute instance-attribute","text":"
    prompt_chaining: PromptChainingConfig | None = None\n

    Configuration for prompt chaining.

    "},{"location":"reference/gerd/models/label/","title":"gerd.models.label","text":""},{"location":"reference/gerd/models/label/#gerd.models.label","title":"gerd.models.label","text":"

    Data definitions for Label Studio tasks.

    The defined models and enums are used to parse and work with Label Studio data exported as JSON.

    Classes:

    Name Description LabelStudioAnnotation

    Annotation of a Label Studio task.

    LabelStudioAnnotationResult

    Result of a Label Studio annotation.

    LabelStudioAnnotationValue

    Value of a Label Studio annotation.

    LabelStudioLabel

    Labels for the GRASCCO Label Studio annotations.

    LabelStudioTask

    Task of a Label Studio project.

    Functions:

    Name Description load_label_studio_tasks

    Load Label Studio tasks from a JSON file.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotation","title":"LabelStudioAnnotation","text":"

    Bases: BaseModel

    Annotation of a Label Studio task.

    A collection of annotations is associated with a task.

    Attributes:

    Name Type Description completed_by int

    The user ID of the user who completed the annotation.

    created_at str

    The creation date of the annotation.

    draft_created_at Optional[str]

    The creation date of the draft.

    ground_truth bool

    Whether the annotation is ground truth.

    id int

    The ID of the annotation.

    import_id Optional[str]

    The import ID of the annotation.

    last_action Optional[str]

    The last action of the annotation.

    last_created_by Optional[int]

    The user ID of the user who last created the annotation.

    lead_time float

    The lead time of the annotation.

    parent_annotation Optional[str]

    The parent annotation.

    parent_prediction Optional[str]

    The parent prediction.

    prediction Dict[str, str]

    The prediction of the annotation.

    project int

    The project ID of the annotation.

    result List[LabelStudioAnnotationResult]

    The results of the annotation.

    result_count int

    The number of results.

    task int

    The task ID of the annotation.

    unique_id str

    The unique ID of the annotation.

    updated_at str

    The update date of the annotation.

    updated_by int

    The user ID of the user who updated the annotation.

    was_cancelled bool

    Whether the annotation was cancelled.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotation.completed_by","title":"completed_by instance-attribute","text":"
    completed_by: int\n

    The user ID of the user who completed the annotation.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotation.created_at","title":"created_at instance-attribute","text":"
    created_at: str\n

    The creation date of the annotation.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotation.draft_created_at","title":"draft_created_at instance-attribute","text":"
    draft_created_at: Optional[str]\n

    The creation date of the draft.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotation.ground_truth","title":"ground_truth instance-attribute","text":"
    ground_truth: bool\n

    Whether the annotation is ground truth.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotation.id","title":"id instance-attribute","text":"
    id: int\n

    The ID of the annotation.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotation.import_id","title":"import_id instance-attribute","text":"
    import_id: Optional[str]\n

    The import ID of the annotation.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotation.last_action","title":"last_action instance-attribute","text":"
    last_action: Optional[str]\n

    The last action of the annotation.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotation.last_created_by","title":"last_created_by instance-attribute","text":"
    last_created_by: Optional[int]\n

    The user ID of the user who last created the annotation.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotation.lead_time","title":"lead_time instance-attribute","text":"
    lead_time: float\n

    The lead time of the annotation.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotation.parent_annotation","title":"parent_annotation instance-attribute","text":"
    parent_annotation: Optional[str]\n

    The parent annotation.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotation.parent_prediction","title":"parent_prediction instance-attribute","text":"
    parent_prediction: Optional[str]\n

    The parent prediction.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotation.prediction","title":"prediction instance-attribute","text":"
    prediction: Dict[str, str]\n

    The prediction of the annotation.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotation.project","title":"project instance-attribute","text":"
    project: int\n

    The project ID of the annotation.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotation.result","title":"result instance-attribute","text":"
    result: List[LabelStudioAnnotationResult]\n

    The results of the annotation.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotation.result_count","title":"result_count instance-attribute","text":"
    result_count: int\n

    The number of results.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotation.task","title":"task instance-attribute","text":"
    task: int\n

    The task ID of the annotation.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotation.unique_id","title":"unique_id instance-attribute","text":"
    unique_id: str\n

    The unique ID of the annotation.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotation.updated_at","title":"updated_at instance-attribute","text":"
    updated_at: str\n

    The update date of the annotation.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotation.updated_by","title":"updated_by instance-attribute","text":"
    updated_by: int\n

    The user ID of the user who updated the annotation.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotation.was_cancelled","title":"was_cancelled instance-attribute","text":"
    was_cancelled: bool\n

    Whether the annotation was cancelled.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotationResult","title":"LabelStudioAnnotationResult","text":"

    Bases: BaseModel

    Result of a Label Studio annotation.

    Attributes:

    Name Type Description from_name str

    The name of the source.

    id str

    The ID of the result.

    origin str

    The origin of the result.

    to_name str

    The name of the target.

    type str

    The type of the result.

    value LabelStudioAnnotationValue

    The value of the result.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotationResult.from_name","title":"from_name instance-attribute","text":"
    from_name: str\n

    The name of the source.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotationResult.id","title":"id instance-attribute","text":"
    id: str\n

    The ID of the result.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotationResult.origin","title":"origin instance-attribute","text":"
    origin: str\n

    The origin of the result.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotationResult.to_name","title":"to_name instance-attribute","text":"
    to_name: str\n

    The name of the target.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotationResult.type","title":"type instance-attribute","text":"
    type: str\n

    The type of the result.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotationResult.value","title":"value instance-attribute","text":"
    value: LabelStudioAnnotationValue\n

    The value of the result.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotationValue","title":"LabelStudioAnnotationValue","text":"

    Bases: BaseModel

    Value of a Label Studio annotation.

    Attributes:

    Name Type Description end int

    The end of the annotation.

    labels List[LabelStudioLabel]

    The labels of the annotation.

    start int

    The start of the annotation.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotationValue.end","title":"end instance-attribute","text":"
    end: int\n

    The end of the annotation.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotationValue.labels","title":"labels instance-attribute","text":"
    labels: List[LabelStudioLabel]\n

    The labels of the annotation.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotationValue.start","title":"start instance-attribute","text":"
    start: int\n

    The start of the annotation.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioLabel","title":"LabelStudioLabel","text":"

    Bases: Enum

    Labels for the GRASCCO Label Studio annotations.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioTask","title":"LabelStudioTask","text":"

    Bases: BaseModel

    Task of a Label Studio project.

    A task is a single unit of work that can be annotated by a user. Tasks can be used to train an auto labeler or to evaluate the performance of a model.

    Attributes:

    Name Type Description annotations List[LabelStudioAnnotation]

    The annotations of the task.

    cancelled_annotations int

    The number of cancelled annotations.

    comment_authors List[str]

    The authors of the comments.

    comment_count int

    The number of comments.

    created_at str

    The creation date of the task.

    data Optional[Dict[str, str]]

    The data of the task.

    drafts List[str]

    The drafts of the task.

    file_name str

    Extracts the original file name from the file upload.

    file_upload str

    The file upload of the task.

    id int

    The ID of the task.

    inner_id int

    The inner ID of the task.

    last_comment_updated_at Optional[str]

    The update date of the last comment.

    meta Optional[Dict[str, str]]

    The meta data of the task.

    predictions List[str]

    The predictions of the task.

    project int

    The project ID of the task.

    total_annotations int

    The total number of annotations.

    total_predictions int

    The total number of predictions.

    unresolved_comment_count int

    The number of unresolved comments.

    updated_at str

    The update date of the task.

    updated_by int

    The user ID of the user who updated the task.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioTask.annotations","title":"annotations instance-attribute","text":"
    annotations: List[LabelStudioAnnotation]\n

    The annotations of the task.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioTask.cancelled_annotations","title":"cancelled_annotations instance-attribute","text":"
    cancelled_annotations: int\n

    The number of cancelled annotations.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioTask.comment_authors","title":"comment_authors instance-attribute","text":"
    comment_authors: List[str]\n

    The authors of the comments.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioTask.comment_count","title":"comment_count instance-attribute","text":"
    comment_count: int\n

    The number of comments.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioTask.created_at","title":"created_at instance-attribute","text":"
    created_at: str\n

    The creation date of the task.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioTask.data","title":"data instance-attribute","text":"
    data: Optional[Dict[str, str]]\n

    The data of the task.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioTask.drafts","title":"drafts instance-attribute","text":"
    drafts: List[str]\n

    The drafts of the task.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioTask.file_name","title":"file_name property","text":"
    file_name: str\n

    Extracts the original file name from the file upload.

    File uploads are stored as project-id-filename format to be unique.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioTask.file_upload","title":"file_upload instance-attribute","text":"
    file_upload: str\n

    The file upload of the task.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioTask.id","title":"id instance-attribute","text":"
    id: int\n

    The ID of the task.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioTask.inner_id","title":"inner_id instance-attribute","text":"
    inner_id: int\n

    The inner ID of the task.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioTask.last_comment_updated_at","title":"last_comment_updated_at instance-attribute","text":"
    last_comment_updated_at: Optional[str]\n

    The update date of the last comment.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioTask.meta","title":"meta instance-attribute","text":"
    meta: Optional[Dict[str, str]]\n

    The meta data of the task.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioTask.predictions","title":"predictions instance-attribute","text":"
    predictions: List[str]\n

    The predictions of the task.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioTask.project","title":"project instance-attribute","text":"
    project: int\n

    The project ID of the task.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioTask.total_annotations","title":"total_annotations instance-attribute","text":"
    total_annotations: int\n

    The total number of annotations.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioTask.total_predictions","title":"total_predictions instance-attribute","text":"
    total_predictions: int\n

    The total number of predictions.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioTask.unresolved_comment_count","title":"unresolved_comment_count instance-attribute","text":"
    unresolved_comment_count: int\n

    The number of unresolved comments.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioTask.updated_at","title":"updated_at instance-attribute","text":"
    updated_at: str\n

    The update date of the task.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioTask.updated_by","title":"updated_by instance-attribute","text":"
    updated_by: int\n

    The user ID of the user who updated the task.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.load_label_studio_tasks","title":"load_label_studio_tasks","text":"
    load_label_studio_tasks(file_path: str) -> List[LabelStudioTask]\n

    Load Label Studio tasks from a JSON file.

    Parameters:

    Name Type Description Default str

    The path to the JSON file.

    required

    Returns:

    Type Description List[LabelStudioTask]

    The loaded Label Studio tasks

    Source code in gerd/models/label.py
    def load_label_studio_tasks(file_path: str) -> List[LabelStudioTask]:\n    \"\"\"Load Label Studio tasks from a JSON file.\n\n    Parameters:\n        file_path: The path to the JSON file.\n\n    Returns:\n        The loaded Label Studio tasks\n    \"\"\"\n    with open(file_path, \"r\", encoding=\"utf-8\") as f:\n        obj = json.load(f)\n\n    tasks = TypeAdapter(List[LabelStudioTask]).validate_python(obj)\n    return tasks\n
    "},{"location":"reference/gerd/models/label/#gerd.models.label.load_label_studio_tasks(file_path)","title":"file_path","text":""},{"location":"reference/gerd/models/logging/","title":"gerd.models.logging","text":""},{"location":"reference/gerd/models/logging/#gerd.models.logging","title":"gerd.models.logging","text":"

    Logging configuration and utilities.

    Classes:

    Name Description LogLevel

    Wrapper for string-based log levels.

    LoggingConfig

    Configuration for logging.

    "},{"location":"reference/gerd/models/logging/#gerd.models.logging.LogLevel","title":"LogLevel","text":"

    Bases: Enum

    Wrapper for string-based log levels.

    Translates log levels to integers for Python's logging framework.

    Methods:

    Name Description as_int

    Convert the log level to an integer.

    "},{"location":"reference/gerd/models/logging/#gerd.models.logging.LogLevel.as_int","title":"as_int","text":"
    as_int() -> int\n

    Convert the log level to an integer.

    Source code in gerd/models/logging.py
    def as_int(self) -> int:\n    \"\"\"Convert the log level to an integer.\"\"\"\n    return {\n        LogLevel.DEBUG: 10,\n        LogLevel.INFO: 20,\n        LogLevel.WARNING: 30,\n        LogLevel.ERROR: 40,\n        LogLevel.FATAL: 50,\n    }[self]\n
    "},{"location":"reference/gerd/models/logging/#gerd.models.logging.LoggingConfig","title":"LoggingConfig","text":"

    Bases: BaseModel

    Configuration for logging.

    Attributes:

    Name Type Description level LogLevel

    The log level.

    "},{"location":"reference/gerd/models/logging/#gerd.models.logging.LoggingConfig.level","title":"level instance-attribute","text":"
    level: LogLevel\n

    The log level.

    "},{"location":"reference/gerd/models/model/","title":"gerd.models.model","text":""},{"location":"reference/gerd/models/model/#gerd.models.model","title":"gerd.models.model","text":"

    Model configuration for supported model classes.

    Classes:

    Name Description ChatMessage

    Data structure for chat messages.

    ModelConfig

    Configuration for large language models.

    ModelEndpoint

    Configuration for model endpoints where models are hosted remotely.

    PromptConfig

    Configuration for prompts.

    Attributes:

    Name Type Description ChatRole

    Currently supported chat roles.

    EndpointType

    Endpoint for remote llm services.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ChatRole","title":"ChatRole module-attribute","text":"
    ChatRole = Literal['system', 'user', 'assistant']\n

    Currently supported chat roles.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.EndpointType","title":"EndpointType module-attribute","text":"
    EndpointType = Literal['llama.cpp', 'openai']\n

    Endpoint for remote llm services.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ChatMessage","title":"ChatMessage","text":"

    Bases: TypedDict

    Data structure for chat messages.

    Attributes:

    Name Type Description content str

    The content of the chat message.

    role ChatRole

    The role or source of the chat message.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ChatMessage.content","title":"content instance-attribute","text":"
    content: str\n

    The content of the chat message.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ChatMessage.role","title":"role instance-attribute","text":"
    role: ChatRole\n

    The role or source of the chat message.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig","title":"ModelConfig","text":"

    Bases: BaseModel

    Configuration for large language models.

    Most llm libraries and/or services share common parameters for configuration. Explaining each parameter is out of scope for this documentation. The most essential parameters are explained for instance here. Default values have been chosen according to ctransformers library.

    Attributes:

    Name Type Description batch_size int

    The batch size for the generation.

    context_length int

    The context length for the model. Currently only LLaMA, MPT and Falcon

    endpoint Optional[ModelEndpoint]

    The endpoint of the model when hosted remotely.

    extra_kwargs Optional[dict[str, Any]]

    Additional keyword arguments for the model library.

    file Optional[str]

    The path to the model file. For local models only.

    gpu_layers int

    The number of layers to run on the GPU.

    last_n_tokens int

    The number of tokens to consider for the repetition penalty.

    loras set[Path]

    The list of additional LoRAs files to load.

    max_new_tokens int

    The maximum number of new tokens to generate.

    name str

    The name of the model. Can be a path to a local model or a huggingface handle.

    prompt_config PromptConfig

    The prompt configuration.

    prompt_setup List[Tuple[Literal['system', 'user', 'assistant'], PromptConfig]]

    A list of predefined prompts for the model.

    repetition_penalty float

    The repetition penalty.

    seed int

    The seed for the random number generator.

    stop Optional[List[str]]

    The stop tokens for the generation.

    stream bool

    Whether to stream the output.

    temperature float

    The temperature for the sampling.

    threads Optional[int]

    The number of threads to use for the generation.

    top_k int

    The number of tokens to consider for the top-k sampling.

    top_p float

    The cumulative probability for the top-p sampling.

    torch_dtype Optional[str]

    The torch data type for the model.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig.batch_size","title":"batch_size class-attribute instance-attribute","text":"
    batch_size: int = 8\n

    The batch size for the generation.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig.context_length","title":"context_length class-attribute instance-attribute","text":"
    context_length: int = 0\n

    The context length for the model. Currently only LLaMA, MPT and Falcon

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig.endpoint","title":"endpoint class-attribute instance-attribute","text":"
    endpoint: Optional[ModelEndpoint] = None\n

    The endpoint of the model when hosted remotely.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig.extra_kwargs","title":"extra_kwargs class-attribute instance-attribute","text":"
    extra_kwargs: Optional[dict[str, Any]] = None\n

    Additional keyword arguments for the model library.

    The accepted keys and values depend on the model library used.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig.file","title":"file class-attribute instance-attribute","text":"
    file: Optional[str] = None\n

    The path to the model file. For local models only.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig.gpu_layers","title":"gpu_layers class-attribute instance-attribute","text":"
    gpu_layers: int = 0\n

    The number of layers to run on the GPU.

    The actual number is only used llama.cpp. The other model libraries will determine whether to run on the GPU just by checking of this value is larger than 0.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig.last_n_tokens","title":"last_n_tokens class-attribute instance-attribute","text":"
    last_n_tokens: int = 64\n

    The number of tokens to consider for the repetition penalty.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig.loras","title":"loras class-attribute instance-attribute","text":"
    loras: set[Path] = set()\n

    The list of additional LoRAs files to load.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig.max_new_tokens","title":"max_new_tokens class-attribute instance-attribute","text":"
    max_new_tokens: int = 256\n

    The maximum number of new tokens to generate.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig.name","title":"name class-attribute instance-attribute","text":"
    name: str = 'Qwen/Qwen2.5-0.5B-Instruct'\n

    The name of the model. Can be a path to a local model or a huggingface handle.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig.prompt_config","title":"prompt_config class-attribute instance-attribute","text":"
    prompt_config: PromptConfig = PromptConfig()\n

    The prompt configuration.

    This is used to process the input passed to the services.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig.prompt_setup","title":"prompt_setup class-attribute instance-attribute","text":"
    prompt_setup: List[Tuple[Literal['system', 'user', 'assistant'], PromptConfig]] = []\n

    A list of predefined prompts for the model.

    When a model context is inialized or reset, this will be used to set up the context.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig.repetition_penalty","title":"repetition_penalty class-attribute instance-attribute","text":"
    repetition_penalty: float = 1.1\n

    The repetition penalty.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig.seed","title":"seed class-attribute instance-attribute","text":"
    seed: int = -1\n

    The seed for the random number generator.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig.stop","title":"stop class-attribute instance-attribute","text":"
    stop: Optional[List[str]] = None\n

    The stop tokens for the generation.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig.stream","title":"stream class-attribute instance-attribute","text":"
    stream: bool = False\n

    Whether to stream the output.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig.temperature","title":"temperature class-attribute instance-attribute","text":"
    temperature: float = 0.8\n

    The temperature for the sampling.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig.threads","title":"threads class-attribute instance-attribute","text":"
    threads: Optional[int] = None\n

    The number of threads to use for the generation.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig.top_k","title":"top_k class-attribute instance-attribute","text":"
    top_k: int = 40\n

    The number of tokens to consider for the top-k sampling.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig.top_p","title":"top_p class-attribute instance-attribute","text":"
    top_p: float = 0.95\n

    The cumulative probability for the top-p sampling.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig.torch_dtype","title":"torch_dtype class-attribute instance-attribute","text":"
    torch_dtype: Optional[str] = None\n

    The torch data type for the model.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelEndpoint","title":"ModelEndpoint","text":"

    Bases: BaseModel

    Configuration for model endpoints where models are hosted remotely.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.PromptConfig","title":"PromptConfig","text":"

    Bases: BaseModel

    Configuration for prompts.

    Methods:

    Name Description format

    Format the prompt with the given parameters.

    model_post_init

    Post-initialization hook for pyandic.

    Attributes:

    Name Type Description is_template bool

    Whether the config uses jinja2 templates.

    parameters list[str]

    Retrieves and returns the parameters of the prompt.

    path Optional[str]

    The path to an external prompt file.

    template Optional[Template]

    Optional template of the prompt. This should follow the Jinja2 syntax.

    text str

    The text of the prompt. Can contain placeholders.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.PromptConfig.is_template","title":"is_template class-attribute instance-attribute","text":"
    is_template: bool = False\n

    Whether the config uses jinja2 templates.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.PromptConfig.parameters","title":"parameters property","text":"
    parameters: list[str]\n

    Retrieves and returns the parameters of the prompt.

    This happens on-the-fly and is not stored in the model.

    Returns:

    Type Description list[str]

    The parameters of the prompt.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.PromptConfig.path","title":"path class-attribute instance-attribute","text":"
    path: Optional[str] = None\n

    The path to an external prompt file.

    This will overload the values of text and/or template.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.PromptConfig.template","title":"template class-attribute instance-attribute","text":"
    template: Optional[Template] = Field(exclude=True, default=None)\n

    Optional template of the prompt. This should follow the Jinja2 syntax.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.PromptConfig.text","title":"text class-attribute instance-attribute","text":"
    text: str = '{message}'\n

    The text of the prompt. Can contain placeholders.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.PromptConfig.format","title":"format","text":"
    format(parameters: Mapping[str, str | list[ChatMessage]] | None = None) -> str\n

    Format the prompt with the given parameters.

    Parameters:

    Name Type Description Default Mapping[str, str | list[ChatMessage]] | None

    The parameters to format the prompt with.

    None

    Returns:

    Type Description str

    The formatted prompt

    Source code in gerd/models/model.py
    def format(\n    self, parameters: Mapping[str, str | list[ChatMessage]] | None = None\n) -> str:\n    \"\"\"Format the prompt with the given parameters.\n\n    Parameters:\n        parameters: The parameters to format the prompt with.\n\n    Returns:\n        The formatted prompt\n    \"\"\"\n    if parameters is None:\n        parameters = {}\n    return (\n        self.template.render(**parameters)\n        if self.template\n        else (\n            self.text.format(**parameters)\n            if self.text\n            else \"\".join(str(parameters.values()))\n        )\n    )\n
    "},{"location":"reference/gerd/models/model/#gerd.models.model.PromptConfig.format(parameters)","title":"parameters","text":""},{"location":"reference/gerd/models/model/#gerd.models.model.PromptConfig.model_post_init","title":"model_post_init","text":"
    model_post_init(__context: Any) -> None\n

    Post-initialization hook for pyandic.

    When path is set, the text or template is read from the file and the template is created. Path ending with '.jinja2' will be treated as a template. If no path is set, the text parameter is used to initialize the template if is_template is set to True. Parameters: __context: The context of the model (not used)

    Source code in gerd/models/model.py
    def model_post_init(self, __context: Any) -> None:  # noqa: ANN401\n    \"\"\"Post-initialization hook for pyandic.\n\n    When path is set, the text or template is read from the file and\n    the template is created.\n    Path ending with '.jinja2' will be treated as a template.\n    If no path is set, the text parameter is used to initialize the template\n    if is_template is set to True.\n    Parameters:\n        __context: The context of the model (not used)\n    \"\"\"\n    if self.path:\n        # reset self.text when path is set\n        self.text = \"\"\n        path = Path(self.path)\n        if path.exists():\n            with path.open(\"r\", encoding=\"utf-8\") as f:\n                self.text = f.read()\n                if self.is_template or path.suffix == \".jinja2\":\n                    self.is_template = True\n                    loader = FileSystemLoader(path.parent)\n                    env = Environment(\n                        loader=loader,\n                        autoescape=select_autoescape(\n                            disabled_extensions=(\".jinja2\",),\n                            default_for_string=True,\n                            default=True,\n                        ),\n                    )\n                    self.template = env.get_template(path.name)\n        else:\n            msg = f\"'{self.path}' does not exist!\"\n            raise ValueError(msg)\n    elif self.text and self.is_template:\n        self.template = Environment(autoescape=True).from_string(self.text)\n
    "},{"location":"reference/gerd/models/qa/","title":"gerd.models.qa","text":""},{"location":"reference/gerd/models/qa/#gerd.models.qa","title":"gerd.models.qa","text":"

    Data definitions for QA model configuration.

    Classes:

    Name Description AnalyzeConfig

    The configuration for the analyze service.

    EmbeddingConfig

    Embedding specific model configuration.

    QAConfig

    Configuration for the QA services.

    QAFeaturesConfig

    Configuration for the QA-specific features.

    "},{"location":"reference/gerd/models/qa/#gerd.models.qa.AnalyzeConfig","title":"AnalyzeConfig","text":"

    Bases: BaseModel

    The configuration for the analyze service.

    Attributes:

    Name Type Description model ModelConfig

    The model to be used for the analyze service.

    "},{"location":"reference/gerd/models/qa/#gerd.models.qa.AnalyzeConfig.model","title":"model instance-attribute","text":"
    model: ModelConfig\n

    The model to be used for the analyze service.

    "},{"location":"reference/gerd/models/qa/#gerd.models.qa.EmbeddingConfig","title":"EmbeddingConfig","text":"

    Bases: BaseModel

    Embedding specific model configuration.

    Attributes:

    Name Type Description chunk_overlap int

    The overlap between chunks.

    chunk_size int

    The size of the chunks stored in the database.

    db_path Optional[str]

    The path to the database file.

    model ModelConfig

    The model used for the embedding.

    "},{"location":"reference/gerd/models/qa/#gerd.models.qa.EmbeddingConfig.chunk_overlap","title":"chunk_overlap instance-attribute","text":"
    chunk_overlap: int\n

    The overlap between chunks.

    "},{"location":"reference/gerd/models/qa/#gerd.models.qa.EmbeddingConfig.chunk_size","title":"chunk_size instance-attribute","text":"
    chunk_size: int\n

    The size of the chunks stored in the database.

    "},{"location":"reference/gerd/models/qa/#gerd.models.qa.EmbeddingConfig.db_path","title":"db_path class-attribute instance-attribute","text":"
    db_path: Optional[str] = None\n

    The path to the database file.

    "},{"location":"reference/gerd/models/qa/#gerd.models.qa.EmbeddingConfig.model","title":"model instance-attribute","text":"
    model: ModelConfig\n

    The model used for the embedding.

    This model should be rather small and fast to compute. Furthermore, not every model is suited for this task.

    "},{"location":"reference/gerd/models/qa/#gerd.models.qa.QAConfig","title":"QAConfig","text":"

    Bases: BaseSettings

    Configuration for the QA services.

    This model can be used to retrieve parameters from a variety of sources. The main source are YAML files (loaded as Settings) but dotenv files and environment variables can be used to situatively overwrite the values. Environment variables have to be prefixed with gerd_qa_ to be recognized.

    Methods:

    Name Description settings_customise_sources

    Customize the settings sources used by pydantic-settings.

    Attributes:

    Name Type Description device str

    The device to run the model on.

    embedding EmbeddingConfig

    The configuration for the embedding service.

    features QAFeaturesConfig

    The configuration for the QA-specific features.

    model ModelConfig

    The model to be used for the QA service.

    "},{"location":"reference/gerd/models/qa/#gerd.models.qa.QAConfig.device","title":"device class-attribute instance-attribute","text":"
    device: str = 'cpu'\n

    The device to run the model on.

    "},{"location":"reference/gerd/models/qa/#gerd.models.qa.QAConfig.embedding","title":"embedding instance-attribute","text":"
    embedding: EmbeddingConfig\n

    The configuration for the embedding service.

    "},{"location":"reference/gerd/models/qa/#gerd.models.qa.QAConfig.features","title":"features instance-attribute","text":"
    features: QAFeaturesConfig\n

    The configuration for the QA-specific features.

    "},{"location":"reference/gerd/models/qa/#gerd.models.qa.QAConfig.model","title":"model instance-attribute","text":"
    model: ModelConfig\n

    The model to be used for the QA service.

    "},{"location":"reference/gerd/models/qa/#gerd.models.qa.QAConfig.settings_customise_sources","title":"settings_customise_sources classmethod","text":"
    settings_customise_sources(_: Type[BaseSettings], init_settings: PydanticBaseSettingsSource, env_settings: PydanticBaseSettingsSource, dotenv_settings: PydanticBaseSettingsSource, file_secret_settings: PydanticBaseSettingsSource) -> Tuple[PydanticBaseSettingsSource, ...]\n

    Customize the settings sources used by pydantic-settings.

    The order of the sources is important. The first source has the highest priority.

    Parameters:

    Name Type Description Default

    The class of the settings.

    required PydanticBaseSettingsSource

    The settings from the initialization.

    required PydanticBaseSettingsSource

    The settings from the environment.

    required PydanticBaseSettingsSource

    The settings from the dotenv file.

    required PydanticBaseSettingsSource

    The settings from the secret file.

    required

    Returns:

    Type Description Tuple[PydanticBaseSettingsSource, ...]

    The customized settings sources.

    Source code in gerd/models/qa.py
    @classmethod\ndef settings_customise_sources(\n    cls,\n    _: Type[BaseSettings],\n    init_settings: PydanticBaseSettingsSource,\n    env_settings: PydanticBaseSettingsSource,\n    dotenv_settings: PydanticBaseSettingsSource,\n    file_secret_settings: PydanticBaseSettingsSource,\n) -> Tuple[PydanticBaseSettingsSource, ...]:\n    \"\"\"Customize the settings sources used by pydantic-settings.\n\n    The order of the sources is important.\n    The first source has the highest priority.\n\n    Parameters:\n        cls: The class of the settings.\n        init_settings: The settings from the initialization.\n        env_settings: The settings from the environment.\n        dotenv_settings: The settings from the dotenv file.\n        file_secret_settings: The settings from the secret file.\n\n    Returns:\n        The customized settings sources.\n    \"\"\"\n    return (\n        file_secret_settings,\n        env_settings,\n        dotenv_settings,\n        init_settings,\n    )\n
    "},{"location":"reference/gerd/models/qa/#gerd.models.qa.QAConfig.settings_customise_sources(cls)","title":"cls","text":""},{"location":"reference/gerd/models/qa/#gerd.models.qa.QAConfig.settings_customise_sources(init_settings)","title":"init_settings","text":""},{"location":"reference/gerd/models/qa/#gerd.models.qa.QAConfig.settings_customise_sources(env_settings)","title":"env_settings","text":""},{"location":"reference/gerd/models/qa/#gerd.models.qa.QAConfig.settings_customise_sources(dotenv_settings)","title":"dotenv_settings","text":""},{"location":"reference/gerd/models/qa/#gerd.models.qa.QAConfig.settings_customise_sources(file_secret_settings)","title":"file_secret_settings","text":""},{"location":"reference/gerd/models/qa/#gerd.models.qa.QAFeaturesConfig","title":"QAFeaturesConfig","text":"

    Bases: BaseModel

    Configuration for the QA-specific features.

    Attributes:

    Name Type Description analyze AnalyzeConfig

    Configuration to extract letter of discharge information from the text.

    analyze_mult_prompts AnalyzeConfig

    Configuration to extract predefined infos with multiple prompts from the text.

    return_source bool

    Whether to return the source in the response.

    "},{"location":"reference/gerd/models/qa/#gerd.models.qa.QAFeaturesConfig.analyze","title":"analyze instance-attribute","text":"
    analyze: AnalyzeConfig\n

    Configuration to extract letter of discharge information from the text.

    "},{"location":"reference/gerd/models/qa/#gerd.models.qa.QAFeaturesConfig.analyze_mult_prompts","title":"analyze_mult_prompts instance-attribute","text":"
    analyze_mult_prompts: AnalyzeConfig\n

    Configuration to extract predefined infos with multiple prompts from the text.

    "},{"location":"reference/gerd/models/qa/#gerd.models.qa.QAFeaturesConfig.return_source","title":"return_source instance-attribute","text":"
    return_source: bool\n

    Whether to return the source in the response.

    "},{"location":"reference/gerd/models/server/","title":"gerd.models.server","text":""},{"location":"reference/gerd/models/server/#gerd.models.server","title":"gerd.models.server","text":"

    Server configuration model for REST backends.

    Classes:

    Name Description ServerConfig

    Server configuration model for REST backends.

    "},{"location":"reference/gerd/models/server/#gerd.models.server.ServerConfig","title":"ServerConfig","text":"

    Bases: BaseModel

    Server configuration model for REST backends.

    Attributes:

    Name Type Description api_prefix str

    The prefix of the API.

    host str

    The host of the server.

    port int

    The port of the server.

    "},{"location":"reference/gerd/models/server/#gerd.models.server.ServerConfig.api_prefix","title":"api_prefix instance-attribute","text":"
    api_prefix: str\n

    The prefix of the API.

    "},{"location":"reference/gerd/models/server/#gerd.models.server.ServerConfig.host","title":"host instance-attribute","text":"
    host: str\n

    The host of the server.

    "},{"location":"reference/gerd/models/server/#gerd.models.server.ServerConfig.port","title":"port instance-attribute","text":"
    port: int\n

    The port of the server.

    "},{"location":"reference/gerd/qa/","title":"gerd.qa","text":""},{"location":"reference/gerd/qa/#gerd.qa","title":"gerd.qa","text":"

    Services and utilities for retrieval augmented generation (RAG).

    Modules:

    Name Description qa_service

    Implements the QAService class.

    "},{"location":"reference/gerd/qa/qa_service/","title":"gerd.qa.qa_service","text":""},{"location":"reference/gerd/qa/qa_service/#gerd.qa.qa_service","title":"gerd.qa.qa_service","text":"

    Implements the QAService class.

    The question and answer service is used to query a language model with questions related to a specific context. The context is usually a set of documents that are loaded into a vector store.

    Classes:

    Name Description QAService

    The question and answer service class.

    "},{"location":"reference/gerd/qa/qa_service/#gerd.qa.qa_service.QAService","title":"QAService","text":"
    QAService(config: QAConfig)\n

    The question and answer service class.

    The service is initialized with a configuration.

    Depending on the configuration, the service will create a new in-memory vector store or load an existing one from a file.

    Parameters:

    Name Type Description Default QAConfig

    The configuration for the QA service

    required

    Methods:

    Name Description add_file

    Add a document to the vectorstore.

    analyze_mult_prompts_query

    Reads a set of data from doc.

    analyze_query

    Read a set of data from a set of documents.

    db_embedding

    Converts a question to an embedding.

    db_query

    Queries the vector store with a question.

    get_prompt_config

    Returns the prompt config for the given mode.

    query

    Pass a question to the language model.

    remove_file

    Removes a document from the vectorstore.

    set_prompt_config

    Sets the prompt config for the given mode.

    Source code in gerd/qa/qa_service.py
    def __init__(self, config: QAConfig) -> None:\n    \"\"\"The service is initialized with a configuration.\n\n    Depending on the configuration, the service will create a new in-memory\n    vector store or load an existing one from a file.\n\n    Parameters:\n        config: The configuration for the QA service\n    \"\"\"\n    self.config = config\n    self._llm = gerd_loader.load_model_from_config(config.model)\n    self._vectorstore: Optional[FAISS] = None\n    self._database: Optional[Rag] = None\n    if (\n        config.embedding.db_path\n        and Path(config.embedding.db_path, \"index.faiss\").exists()\n    ):\n        _LOGGER.info(\n            \"Load existing vector store from '%s'.\", config.embedding.db_path\n        )\n        self._vectorstore = load_faiss(\n            Path(config.embedding.db_path, \"index.faiss\"),\n            config.embedding.model.name,\n            config.device,\n        )\n
    "},{"location":"reference/gerd/qa/qa_service/#gerd.qa.qa_service.QAService(config)","title":"config","text":""},{"location":"reference/gerd/qa/qa_service/#gerd.qa.qa_service.QAService.add_file","title":"add_file","text":"
    add_file(file: QAFileUpload) -> QAAnswer\n

    Add a document to the vectorstore.

    Parameters:

    Name Type Description Default QAFileUpload

    The file to add to the vectorstore

    required

    Returns:

    Type Description QAAnswer

    an answer object with status 200 if successful

    Source code in gerd/qa/qa_service.py
    def add_file(self, file: QAFileUpload) -> QAAnswer:\n    \"\"\"Add a document to the vectorstore.\n\n    Parameters:\n        file: The file to add to the vectorstore\n\n    Returns:\n        an answer object with status 200 if successful\n    \"\"\"\n    documents: Optional[List[Document]] = None\n    file_path = Path(file.name)\n    try:\n        with NamedTemporaryFile(\n            dir=\".\", suffix=file_path.suffix, delete=False\n        ) as f:\n            f.write(file.data)\n            f.flush()\n        file_type = FileTypes(file_path.suffix[1:])\n        loader: BaseLoader\n        if file_type == FileTypes.TEXT:\n            loader = TextLoader(f.name)\n        elif file_type == FileTypes.PDF:\n            loader = PyPDFLoader(f.name)\n        documents = loader.load()\n        # source must be overriden to not leak upload information\n        # about the temp file which are rather useless anyway\n        for doc in documents:\n            doc.metadata[\"source\"] = file_path.name\n    except BaseException as err:\n        _LOGGER.error(err)\n    finally:\n        if f:\n            unlink(f.name)\n    if not documents:\n        _LOGGER.warning(\"No document was loaded!\")\n        return QAAnswer(error_msg=\"No document was loaded!\", status=500)\n    text_splitter = RecursiveCharacterTextSplitter(\n        chunk_size=self.config.embedding.chunk_size,\n        chunk_overlap=self.config.embedding.chunk_overlap,\n    )\n    texts = text_splitter.split_documents(documents)\n    for i, text in enumerate(texts):\n        text.id = f\"{file_path.name}_{i}\"\n    if self._vectorstore is None:\n        _LOGGER.info(\"Create new vector store from document.\")\n        self._vectorstore = create_faiss(\n            texts, self.config.embedding.model.name, self.config.device\n        )\n    else:\n        _LOGGER.info(\"Adding document to existing vector store.\")\n        tmp = create_faiss(\n            texts, self.config.embedding.model.name, self.config.device\n        )\n        self._vectorstore.merge_from(tmp)\n    if self.config.embedding.db_path:\n        self._vectorstore.save_local(self.config.embedding.db_path)\n    return QAAnswer()\n
    "},{"location":"reference/gerd/qa/qa_service/#gerd.qa.qa_service.QAService.add_file(file)","title":"file","text":""},{"location":"reference/gerd/qa/qa_service/#gerd.qa.qa_service.QAService.analyze_mult_prompts_query","title":"analyze_mult_prompts_query","text":"
    analyze_mult_prompts_query() -> QAAnalyzeAnswer\n

    Reads a set of data from doc.

    Loads the data via multiple prompts by asking for each data field separately.

    Data - patient_name - patient_date_of_birth - attending_doctors - recording_date - release_date

    Returns:

    Type Description QAAnalyzeAnswer

    The answer from the language model

    Source code in gerd/qa/qa_service.py
    def analyze_mult_prompts_query(self) -> QAAnalyzeAnswer:\n    \"\"\"Reads a set of data from doc.\n\n    Loads the data via multiple prompts by asking for each data field\n    separately.\n\n    *Data*\n        - patient_name\n        - patient_date_of_birth\n        - attending_doctors\n        - recording_date\n        - release_date\n\n    Returns:\n        The answer from the language model\n    \"\"\"\n    if not self._vectorstore:\n        msg = \"No vector store initialized! Upload documents first.\"\n        _LOGGER.error(msg)\n        return QAAnalyzeAnswer(status=404, error_msg=msg)\n\n    config = self.config.features.analyze_mult_prompts\n\n    # check if prompt contains needed fields\n    qa_analyze_mult_prompts = config.model.prompt_config\n    if (\n        \"context\" not in qa_analyze_mult_prompts.parameters\n        or \"question\" not in qa_analyze_mult_prompts.parameters\n    ):\n        msg = \"Prompt does not include '{context}' or '{question}' variable.\"\n        _LOGGER.error(msg)\n        return QAAnalyzeAnswer(status=404, error_msg=msg)\n\n    # questions to search model and vectorstore\n    questions_model_dict: Dict[str, str] = {\n        \"Wie hei\u00dft der Patient?\": \"wir berichten \u00fcber unseren Patient oder Btr. oder Patient, wh, geboren oder  Patient, * 0.00.0000,\",  # noqa: E501\n        \"Wann hat der Patient Geburstag?\": \"wir berichten \u00fcber unseren Patient oder Btr. oder Patient, wh, geboren oder  Patient, * 0.00.0000,\",  # noqa: E501\n        \"Wie hei\u00dft der Arzt?\": \"Mit freundlichen kollegialen Gr\u00fc\u00dfen, Prof, Dr\",\n        \"Wann wurde der Patient bei uns aufgenommen?\": (\n            \"wir berichten \u00fcber unseren Patient oder Btr. oder Patient, wh, geboren\"\n        ),\n        \"Wann wurde der Patient bei uns entlassen?\": (\n            \"wir berichten \u00fcber unseren Patient oder Btr. oder Patient, wh, geboren\"\n        ),\n    }\n    fields: Dict[str, str] = {\n        \"Wie hei\u00dft der Patient?\": \"patient_name\",\n        \"Wann hat der Patient Geburstag?\": \"patient_date_of_birth\",\n        \"Wie hei\u00dft der Arzt?\": \"attending_doctors\",\n        \"Wann wurde der Patient bei uns aufgenommen?\": \"recording_date\",\n        \"Wann wurde der Patient bei uns entlassen?\": \"release_date\",\n    }\n    questions_dict: Dict[str, List[DocumentSource]] = {}\n    answer_dict: Dict[str, Any] = {}\n    responses: str = \"\"\n\n    # load context from vectorstore for each question\n    for question_m, question_v in questions_model_dict.items():\n        questions_dict, formatted_prompt = self._create_analyze_mult_prompt(\n            question_m, question_v, qa_analyze_mult_prompts.text\n        )\n\n        # query the model for each question\n        response = self._llm.generate(formatted_prompt)\n\n        # format the response\n        if response is not None:\n            response = self._clean_response(response)\n\n            # if enabled, collect response\n            if self.config.features.return_source:\n                responses = responses + \"; \" + question_m + \": \" + response\n        # format the response\n        answer_dict[fields[question_m]] = self._format_response_analyze_mult_prompt(\n            response, fields[question_m]\n        )\n\n        _LOGGER.info(\n            \"\\n===== Modelresult ====\\n\\n%s\\n\\n====================\", response\n        )\n\n    answer = QAAnalyzeAnswer(**answer_dict)\n\n    # if enabled, pass source data to answer\n    if self.config.features.return_source:\n        answer.response = responses\n        for question in questions_dict:\n            answer.sources = answer.sources + questions_dict[question]\n\n    _LOGGER.warning(\"\\n==== Answer ====\\n\\n%s\\n===============\", answer)\n    return answer\n
    "},{"location":"reference/gerd/qa/qa_service/#gerd.qa.qa_service.QAService.analyze_query","title":"analyze_query","text":"
    analyze_query() -> QAAnalyzeAnswer\n

    Read a set of data from a set of documents.

    Loads the data via single prompt.

    Data - patient_name - patient_date_of_birth - attending_doctors - recording_date - release_date

    Returns:

    Type Description QAAnalyzeAnswer

    The answer from the language model

    Source code in gerd/qa/qa_service.py
    def analyze_query(self) -> QAAnalyzeAnswer:\n    \"\"\"Read a set of data from a set of documents.\n\n    Loads the data via single prompt.\n\n    *Data*\n        - patient_name\n        - patient_date_of_birth\n        - attending_doctors\n        - recording_date\n        - release_date\n\n    Returns:\n        The answer from the language model\n    \"\"\"\n    if not self._vectorstore:\n        msg = \"No vector store initialized! Upload documents first.\"\n        _LOGGER.error(msg)\n        return QAAnalyzeAnswer(status=404, error_msg=msg)\n\n    config = self.config.features.analyze\n\n    # questions to search model and vectorstore\n    questions_model_dict: Dict[str, str] = {\n        \"Wie hei\u00dft der Patient?\": \"wir berichten \u00fcber unseren Patient oder Btr. oder Patient, wh, geboren oder  Patient, * 0.00.0000,\",  # noqa E501\n        \"Wann hat der Patient Geburstag?\": \"wir berichten \u00fcber unseren Patient oder Btr. oder Patient, wh, geboren oder  Patient, * 0.00.0000,\",  # noqa E501\n        \"Wie hei\u00dft der Arzt?\": \"Mit freundlichen kollegialen Gr\u00fc\u00dfen, Prof, Dr\",\n        \"Wann wurde der Patient bei uns aufgenommen?\": (\n            \"wir berichten \u00fcber unseren Patient oder Btr. oder Patient, wh, geboren\"\n        ),\n        \"Wann wurde der Patient bei uns entlassen?\": (\n            \"wir berichten \u00fcber unseren Patient oder Btr. oder Patient, wh, geboren\"\n        ),\n    }\n\n    # map model questions to jsonfields\n    fields: Dict[str, str] = {\n        \"Wie hei\u00dft der Patient?\": \"patient_name\",\n        \"Wann hat der Patient Geburstag?\": \"patient_date_of_birth\",\n        \"Wie hei\u00dft der Arzt?\": \"attending_doctors\",\n        \"Wann wurde der Patient bei uns aufgenommen?\": \"recording_date\",\n        \"Wann wurde der Patient bei uns entlassen?\": \"release_date\",\n    }\n\n    questions_dict: Dict[str, List[DocumentSource]] = {}\n    parameters: Dict[str, str] = {}\n    question_counter: int = 0\n\n    qa_analyze_prompt = config.model.prompt_config\n    # check if prompt contains needed fields\n    for i in range(0, len(questions_model_dict)):\n        if (\n            \"context\" + str(i) not in qa_analyze_prompt.parameters\n            or \"question\" + str(i) not in qa_analyze_prompt.parameters\n        ):\n            msg = (\n                \"Prompt does not include '{context\"\n                + str(i)\n                + \"}' or '{question\"\n                + str(i)\n                + \"}' variable.\"\n            )\n            _LOGGER.error(msg)\n            return QAAnalyzeAnswer(status=404, error_msg=msg)\n\n    # load context from vectorstore for each question\n    for question_m, question_v in questions_model_dict.items():\n        questions_dict[question_m] = list(\n            self.db_query(QAQuestion(question=question_v, max_sources=3))\n        )\n\n        parameters[\"context\" + str(question_counter)] = \" \".join(\n            doc.content for doc in questions_dict[question_m]\n        )\n        parameters[\"question\" + str(question_counter)] = question_m\n        parameters[\"field\" + str(question_counter)] = fields[question_m]\n\n        question_counter = question_counter + 1\n\n    formatted_prompt = qa_analyze_prompt.text.format(**parameters)\n\n    # query the model\n    response = self._llm.generate(formatted_prompt)\n\n    if response is not None:\n        response = self._clean_response(response)\n\n    _LOGGER.info(\"\\n===== Modelresult ====\\n\\n%s\\n\\n====================\", response)\n\n    # convert json to QAAnalyzerAnswerclass\n    answer = self._format_response_analyze(response)\n\n    # if enabled, pass source data to answer\n    if self.config.features.return_source:\n        answer.response = response\n        answer.prompt = formatted_prompt\n        for question in questions_dict:\n            answer.sources = answer.sources + questions_dict[question]\n        _LOGGER.info(\n            \"\\n===== Sources ====\\n\\n%s\\n\\n====================\", answer.sources\n        )\n\n    _LOGGER.warning(\"\\n==== Answer ====\\n\\n%s\\n===============\", answer)\n    return answer\n
    "},{"location":"reference/gerd/qa/qa_service/#gerd.qa.qa_service.QAService.db_embedding","title":"db_embedding","text":"
    db_embedding(question: QAQuestion) -> List[float]\n

    Converts a question to an embedding.

    The embedding to be used is defined by the vector store or more specifically by the configured parameters passed to initialize the vector store.

    Parameters:

    Name Type Description Default QAQuestion

    The question to convert to an embedding.

    required

    Returns:

    Type Description List[float]

    The embedding of the question

    Source code in gerd/qa/qa_service.py
    def db_embedding(self, question: QAQuestion) -> List[float]:\n    \"\"\"Converts a question to an embedding.\n\n    The embedding to be used is defined by the vector store or more specifically\n    by the configured parameters passed to initialize the vector store.\n\n    Parameters:\n        question: The question to convert to an embedding.\n\n    Returns:\n        The embedding of the question\n    \"\"\"\n    if self._vectorstore is None or self._vectorstore.embeddings is None:\n        return []\n    return self._vectorstore.embeddings.embed_documents([question.question])[0]\n
    "},{"location":"reference/gerd/qa/qa_service/#gerd.qa.qa_service.QAService.db_embedding(question)","title":"question","text":""},{"location":"reference/gerd/qa/qa_service/#gerd.qa.qa_service.QAService.db_query","title":"db_query","text":"
    db_query(question: QAQuestion) -> List[DocumentSource]\n

    Queries the vector store with a question.

    The number of sources that are returned is defined by the max_sources parameter of the service's configuration.

    Parameters:

    Name Type Description Default QAQuestion

    The question to query the vector store with.

    required

    Returns:

    Type Description List[DocumentSource]

    A list of document sources

    Source code in gerd/qa/qa_service.py
    def db_query(self, question: QAQuestion) -> List[DocumentSource]:\n    \"\"\"Queries the vector store with a question.\n\n    The number of sources that are returned is defined by the max_sources parameter\n    of the service's configuration.\n\n    Parameters:\n        question: The question to query the vector store with.\n\n    Returns:\n        A list of document sources\n    \"\"\"\n    if not self._vectorstore:\n        return []\n    return [\n        DocumentSource(\n            query=question.question,\n            content=doc.page_content,\n            name=doc.metadata.get(\"source\", \"unknown\"),\n            page=doc.metadata.get(\"page\", 1),\n        )\n        for doc in self._vectorstore.search(\n            question.question,\n            search_type=question.search_strategy,\n            k=question.max_sources,\n        )\n    ]\n
    "},{"location":"reference/gerd/qa/qa_service/#gerd.qa.qa_service.QAService.db_query(question)","title":"question","text":""},{"location":"reference/gerd/qa/qa_service/#gerd.qa.qa_service.QAService.get_prompt_config","title":"get_prompt_config","text":"
    get_prompt_config(qa_mode: QAModesEnum) -> PromptConfig\n

    Returns the prompt config for the given mode.

    Parameters:

    Name Type Description Default QAModesEnum

    The mode to get the prompt config for

    required

    Returns:

    Type Description PromptConfig

    The prompt config for the given mode

    Source code in gerd/qa/qa_service.py
    def get_prompt_config(self, qa_mode: QAModesEnum) -> PromptConfig:\n    \"\"\"Returns the prompt config for the given mode.\n\n    Parameters:\n        qa_mode: The mode to get the prompt config for\n\n    Returns:\n        The prompt config for the given mode\n    \"\"\"\n    if qa_mode == QAModesEnum.SEARCH:\n        return self.config.model.prompt_config\n    elif qa_mode == QAModesEnum.ANALYZE:\n        return self.config.features.analyze.model.prompt_config\n    elif qa_mode == QAModesEnum.ANALYZE_MULT_PROMPTS:\n        return self.config.features.analyze_mult_prompts.model.prompt_config\n    return PromptConfig()\n
    "},{"location":"reference/gerd/qa/qa_service/#gerd.qa.qa_service.QAService.get_prompt_config(qa_mode)","title":"qa_mode","text":""},{"location":"reference/gerd/qa/qa_service/#gerd.qa.qa_service.QAService.query","title":"query","text":"
    query(question: QAQuestion) -> QAAnswer\n

    Pass a question to the language model.

    The language model will generate an answer based on the question and the context derived from the vector store.

    Parameters:

    Name Type Description Default QAQuestion

    The question to be answered

    required

    Returns:

    Type Description QAAnswer

    The answer from the language model

    Source code in gerd/qa/qa_service.py
    def query(self, question: QAQuestion) -> QAAnswer:\n    \"\"\"Pass a question to the language model.\n\n    The language model will generate an answer based on the question and\n    the context derived from the vector store.\n\n    Parameters:\n        question: The question to be answered\n\n    Returns:\n        The answer from the language model\n    \"\"\"\n    if not self._database:\n        if not self._vectorstore:\n            return QAAnswer(error_msg=\"No database available!\", status=404)\n        self._database = Rag(\n            self._llm,\n            self.config.model,\n            self.config.model.prompt_config,\n            self._vectorstore,\n            self.config.features.return_source,\n        )\n    return self._database.query(question)\n
    "},{"location":"reference/gerd/qa/qa_service/#gerd.qa.qa_service.QAService.query(question)","title":"question","text":""},{"location":"reference/gerd/qa/qa_service/#gerd.qa.qa_service.QAService.remove_file","title":"remove_file","text":"
    remove_file(file_name: str) -> QAAnswer\n

    Removes a document from the vectorstore.

    Parameters:

    Name Type Description Default str

    The name of the file to remove

    required

    Returns:

    Type Description QAAnswer

    an answer object with status 200 if successful

    Source code in gerd/qa/qa_service.py
    def remove_file(self, file_name: str) -> QAAnswer:\n    \"\"\"Removes a document from the vectorstore.\n\n    Parameters:\n        file_name: The name of the file to remove\n\n    Returns:\n        an answer object with status 200 if successful\n    \"\"\"\n    if not self._vectorstore:\n        return QAAnswer(error_msg=\"No vector store initialized!\", status=404)\n    self._vectorstore.delete(\n        [\n            id\n            for id in self._vectorstore.index_to_docstore_id.values()\n            if id.startswith(file_name)\n        ]\n    )\n    return QAAnswer(status=200)\n
    "},{"location":"reference/gerd/qa/qa_service/#gerd.qa.qa_service.QAService.remove_file(file_name)","title":"file_name","text":""},{"location":"reference/gerd/qa/qa_service/#gerd.qa.qa_service.QAService.set_prompt_config","title":"set_prompt_config","text":"
    set_prompt_config(config: PromptConfig, qa_mode: QAModesEnum) -> QAAnswer\n

    Sets the prompt config for the given mode.

    Parameters:

    Name Type Description Default PromptConfig

    The prompt config to set

    required QAModesEnum

    The mode to set the prompt config for

    required

    Returns:

    Type Description QAAnswer

    an answer object with status 200 if successful

    Source code in gerd/qa/qa_service.py
    def set_prompt_config(self, config: PromptConfig, qa_mode: QAModesEnum) -> QAAnswer:\n    \"\"\"Sets the prompt config for the given mode.\n\n    Parameters:\n        config: The prompt config to set\n        qa_mode: The mode to set the prompt config for\n\n    Returns:\n        an answer object with status 200 if successful\n    \"\"\"\n    answer = QAAnswer()\n    if qa_mode == QAModesEnum.SEARCH:\n        self.config.model.prompt_config = config\n        if \"context\" not in config.parameters:\n            answer.error_msg = (\n                \"Prompt does not include '{context}' variable. \"\n                \"No context will be added. \"\n            )\n        if \"question\" not in config.parameters:\n            answer.error_msg += (\n                \"Prompt does not include '{question}' variable. \"\n                \"Questions will not be passed to the model.\"\n            )\n    elif qa_mode == QAModesEnum.ANALYZE:\n        self.config.features.analyze.model.prompt_config = config\n    elif qa_mode == QAModesEnum.ANALYZE_MULT_PROMPTS:\n        self.config.features.analyze_mult_prompts.model.prompt_config = config\n    return answer\n
    "},{"location":"reference/gerd/qa/qa_service/#gerd.qa.qa_service.QAService.set_prompt_config(config)","title":"config","text":""},{"location":"reference/gerd/qa/qa_service/#gerd.qa.qa_service.QAService.set_prompt_config(qa_mode)","title":"qa_mode","text":""},{"location":"reference/gerd/rag/","title":"gerd.rag","text":""},{"location":"reference/gerd/rag/#gerd.rag","title":"gerd.rag","text":"

    Retrieval-Augmented Generation (RAG) backend.

    This module provides the RAG backend for the GERD system which is currently based on FAISS.

    Classes:

    Name Description Rag

    The RAG backend for GERD.

    Functions:

    Name Description create_faiss

    Create a new FAISS store from a list of documents.

    load_faiss

    Load a FAISS store from a disk path.

    "},{"location":"reference/gerd/rag/#gerd.rag.Rag","title":"Rag","text":"
    Rag(model: LLM, model_config: ModelConfig, prompt: PromptConfig, store: FAISS, return_source: bool)\n

    The RAG backend for GERD.

    The RAG backend will check for a context parameter in the prompt.

    If the context parameter is not included, a warning will be logged. Without the context parameter, no context will be added to the query.

    Parameters:

    Name Type Description Default LLM

    The LLM model to use

    required ModelConfig

    The model configuration

    required PromptConfig

    The prompt configuration

    required FAISS

    The FAISS store to use

    required bool

    Whether to return the source documents

    required

    Methods:

    Name Description query

    Query the RAG backend with a question.

    Source code in gerd/rag.py
    def __init__(\n    self,\n    model: LLM,\n    model_config: ModelConfig,\n    prompt: PromptConfig,\n    store: FAISS,\n    return_source: bool,\n) -> None:\n    \"\"\"The RAG backend will check for a context parameter in the prompt.\n\n    If the context parameter is not included, a warning will be logged.\n    Without the context parameter, no context will be added to the query.\n\n    Parameters:\n        model: The LLM model to use\n        model_config: The model configuration\n        prompt: The prompt configuration\n        store: The FAISS store to use\n        return_source: Whether to return the source documents\n    \"\"\"\n    self.model = model\n    self.model_config = model_config\n    self.prompt = prompt\n    self.store = store\n    self.return_source = return_source\n\n    if \"context\" not in prompt.parameters:\n        _LOGGER.warning(\n            \"Prompt does not include '{context}' variable! \"\n            \"No context will be added to the query.\"\n        )\n
    "},{"location":"reference/gerd/rag/#gerd.rag.Rag(model)","title":"model","text":""},{"location":"reference/gerd/rag/#gerd.rag.Rag(model_config)","title":"model_config","text":""},{"location":"reference/gerd/rag/#gerd.rag.Rag(prompt)","title":"prompt","text":""},{"location":"reference/gerd/rag/#gerd.rag.Rag(store)","title":"store","text":""},{"location":"reference/gerd/rag/#gerd.rag.Rag(return_source)","title":"return_source","text":""},{"location":"reference/gerd/rag/#gerd.rag.Rag.query","title":"query","text":"
    query(question: QAQuestion) -> QAAnswer\n

    Query the RAG backend with a question.

    Parameters:

    Name Type Description Default QAQuestion

    The question to ask

    required

    Returns:

    Type Description QAAnswer

    The answer to the question including the sources

    Source code in gerd/rag.py
    def query(self, question: QAQuestion) -> QAAnswer:\n    \"\"\"Query the RAG backend with a question.\n\n    Parameters:\n        question: The question to ask\n\n    Returns:\n        The answer to the question including the sources\n    \"\"\"\n    docs = self.store.search(\n        question.question,\n        search_type=question.search_strategy,\n        k=question.max_sources,\n    )\n    context = \"\\n\".join(doc.page_content for doc in docs)\n    resolved = self.prompt.text.format(context=context, question=question.question)\n    _, response = self.model.create_chat_completion(\n        [{\"role\": \"user\", \"content\": resolved}]\n    )\n    answer = QAAnswer(response=response)\n    if self.return_source:\n        for doc in docs:\n            answer.sources.append(\n                DocumentSource(\n                    query=question.question,\n                    content=doc.page_content,\n                    name=doc.metadata.get(\"source\", \"unknown\"),\n                    page=doc.metadata.get(\"page\", 1),\n                )\n            )\n    return answer\n
    "},{"location":"reference/gerd/rag/#gerd.rag.Rag.query(question)","title":"question","text":""},{"location":"reference/gerd/rag/#gerd.rag.create_faiss","title":"create_faiss","text":"
    create_faiss(documents: list[Document], model_name: str, device: str) -> FAISS\n

    Create a new FAISS store from a list of documents.

    Parameters:

    Name Type Description Default list[Document]

    The list of documents to index

    required str

    The name of the Hugging Face model to for the embeddings

    required str

    The device to use for the model

    required

    Returns:

    Type Description FAISS

    The newly created FAISS store

    Source code in gerd/rag.py
    def create_faiss(documents: list[Document], model_name: str, device: str) -> FAISS:\n    \"\"\"Create a new FAISS store from a list of documents.\n\n    Parameters:\n        documents: The list of documents to index\n        model_name: The name of the Hugging Face model to for the embeddings\n        device: The device to use for the model\n\n    Returns:\n        The newly created FAISS store\n    \"\"\"\n    return FAISS.from_documents(\n        documents,\n        HuggingFaceEmbeddings(\n            model_name=model_name,\n            model_kwargs={\"device\": device},\n        ),\n    )\n
    "},{"location":"reference/gerd/rag/#gerd.rag.create_faiss(documents)","title":"documents","text":""},{"location":"reference/gerd/rag/#gerd.rag.create_faiss(model_name)","title":"model_name","text":""},{"location":"reference/gerd/rag/#gerd.rag.create_faiss(device)","title":"device","text":""},{"location":"reference/gerd/rag/#gerd.rag.load_faiss","title":"load_faiss","text":"
    load_faiss(dp_path: Path, model_name: str, device: str) -> FAISS\n

    Load a FAISS store from a disk path.

    Parameters:

    Name Type Description Default Path

    The path to the disk path

    required str

    The name of the Hugging Face model to for the embeddings

    required str

    The device to use for the model

    required

    Returns:

    Type Description FAISS

    The loaded FAISS store

    Source code in gerd/rag.py
    def load_faiss(dp_path: Path, model_name: str, device: str) -> FAISS:\n    \"\"\"Load a FAISS store from a disk path.\n\n    Parameters:\n        dp_path: The path to the disk path\n        model_name: The name of the Hugging Face model to for the embeddings\n        device: The device to use for the model\n\n    Returns:\n        The loaded FAISS store\n    \"\"\"\n    return FAISS.load_local(\n        dp_path.as_posix(),\n        HuggingFaceEmbeddings(\n            model_name=model_name,\n            model_kwargs={\"device\": device},\n        ),\n    )\n
    "},{"location":"reference/gerd/rag/#gerd.rag.load_faiss(dp_path)","title":"dp_path","text":""},{"location":"reference/gerd/rag/#gerd.rag.load_faiss(model_name)","title":"model_name","text":""},{"location":"reference/gerd/rag/#gerd.rag.load_faiss(device)","title":"device","text":""},{"location":"reference/gerd/training/","title":"gerd.training","text":""},{"location":"reference/gerd/training/#gerd.training","title":"gerd.training","text":"

    Collections of training routines for GERD.

    Modules:

    Name Description data

    Data utilities for training and data processing.

    instruct

    Training module for instruction text sets.

    lora

    Configuration dataclasses for training LoRA models.

    trainer

    Training module for LoRA models.

    unstructured

    Training of LoRA models on unstructured text data.

    "},{"location":"reference/gerd/training/data/","title":"gerd.training.data","text":""},{"location":"reference/gerd/training/data/#gerd.training.data","title":"gerd.training.data","text":"

    Data utilities for training and data processing.

    Functions:

    Name Description despacyfy

    Removes spacy-specific tokens from a text.

    encode

    Encodes a text using a tokenizer.

    split_chunks

    Splits a list of encoded tokens into chunks of a given size.

    tokenize

    Converts a prompt into a tokenized input for a model.

    "},{"location":"reference/gerd/training/data/#gerd.training.data.despacyfy","title":"despacyfy","text":"
    despacyfy(text: str) -> str\n

    Removes spacy-specific tokens from a text.

    For instance, -RRB- is replaced with ')', -LRB- with '(' and -UNK- with '*'.

    Parameters:

    Name Type Description Default str

    The text to despacyfy.

    required

    Returns:

    Type Description str

    The despacyfied text

    Source code in gerd/training/data.py
    def despacyfy(text: str) -> str:\n    \"\"\"Removes spacy-specific tokens from a text.\n\n    For instance, -RRB- is replaced with ')', -LRB- with '(' and -UNK- with '*'.\n\n    Parameters:\n        text: The text to despacyfy.\n\n    Returns:\n        The despacyfied text\n    \"\"\"\n    res = (\n        text.replace(\"-RRB-\", \")\")\n        .replace(\"-LRB-\", \"(\")\n        .replace(\"-UNK-\", \"*\")\n        .replace(\"( \", \"(\")\n        .replace(\" )\", \")\")\n        .replace(\"  \", \" \")\n    )\n    check = re.findall(r\"-(RRB|UNK|LRB)-\", res)\n    if len(check) != 0:\n        msg = f\"Did not expect to find {check} in\\n{res}.\"\n        raise RuntimeError(msg)\n    return res\n
    "},{"location":"reference/gerd/training/data/#gerd.training.data.despacyfy(text)","title":"text","text":""},{"location":"reference/gerd/training/data/#gerd.training.data.encode","title":"encode","text":"
    encode(text: str, add_bos_token: bool, tokenizer: PreTrainedTokenizer, cutoff_len: int) -> List[int]\n

    Encodes a text using a tokenizer.

    Parameters:

    Name Type Description Default str

    The text to encode

    required bool

    Whether to add the beginning of sentence token

    required PreTrainedTokenizer

    The tokenizer to use

    required int

    The maximum length of the encoded text

    required

    Returns:

    Type Description List[int]

    The text encoded as a list of tokenizer tokens

    Source code in gerd/training/data.py
    def encode(\n    text: str, add_bos_token: bool, tokenizer: PreTrainedTokenizer, cutoff_len: int\n) -> List[int]:\n    \"\"\"Encodes a text using a tokenizer.\n\n    Parameters:\n        text: The text to encode\n        add_bos_token: Whether to add the beginning of sentence token\n        tokenizer: The tokenizer to use\n        cutoff_len: The maximum length of the encoded text\n\n    Returns:\n        The text encoded as a list of tokenizer tokens\n    \"\"\"\n    result: list[int] = tokenizer.encode(text, truncation=True, max_length=cutoff_len)\n    # Check if the first two tokens are BOS\n    if len(result) >= 2 and result[:2] == [\n        tokenizer.bos_token_id,\n        tokenizer.bos_token_id,\n    ]:\n        result = result[1:]\n\n    if not add_bos_token and result[0] == tokenizer.bos_token_id:\n        result = result[1:]\n    return result\n
    "},{"location":"reference/gerd/training/data/#gerd.training.data.encode(text)","title":"text","text":""},{"location":"reference/gerd/training/data/#gerd.training.data.encode(add_bos_token)","title":"add_bos_token","text":""},{"location":"reference/gerd/training/data/#gerd.training.data.encode(tokenizer)","title":"tokenizer","text":""},{"location":"reference/gerd/training/data/#gerd.training.data.encode(cutoff_len)","title":"cutoff_len","text":""},{"location":"reference/gerd/training/data/#gerd.training.data.split_chunks","title":"split_chunks","text":"
    split_chunks(arr: List[int], size: int, step: int) -> Generator[List[int], None, None]\n

    Splits a list of encoded tokens into chunks of a given size.

    Parameters:

    Name Type Description Default List[int]

    The list of encoded tokens.

    required int

    The size of the chunks.

    required int

    The step size for the chunks.

    required

    Returns:

    Type Description None

    A generator that yields the chunks

    Source code in gerd/training/data.py
    def split_chunks(\n    arr: List[int], size: int, step: int\n) -> Generator[List[int], None, None]:\n    \"\"\"Splits a list of encoded tokens into chunks of a given size.\n\n    Parameters:\n        arr: The list of encoded tokens.\n        size: The size of the chunks.\n        step: The step size for the chunks.\n\n    Returns:\n        A generator that yields the chunks\n    \"\"\"\n    for i in range(0, len(arr), step):\n        yield arr[i : i + size]\n
    "},{"location":"reference/gerd/training/data/#gerd.training.data.split_chunks(arr)","title":"arr","text":""},{"location":"reference/gerd/training/data/#gerd.training.data.split_chunks(size)","title":"size","text":""},{"location":"reference/gerd/training/data/#gerd.training.data.split_chunks(step)","title":"step","text":""},{"location":"reference/gerd/training/data/#gerd.training.data.tokenize","title":"tokenize","text":"
    tokenize(prompt: str, tokenizer: PreTrainedTokenizer, cutoff_len: int, append_eos_token: bool = False) -> Dict[str, torch.Tensor | list[int]]\n

    Converts a prompt into a tokenized input for a model.

    The methods returns the tokenized input as a dictionary with the keys \"input_ids\", \"labels\" and \"attention_mask\" where the input_ids are the tokenized input, the labels assign the same label ('1') to each token and the attention_mask masks out the padding tokens. Parameters: prompt: The prompt to tokenize tokenizer: The tokenizer to use cutoff_len: The maximum length of the encoded text append_eos_token: Whether to append an end of sentence token

    Returns:

    Type Description Dict[str, Tensor | list[int]]

    The tokenized input as a dictionary

    Source code in gerd/training/data.py
    def tokenize(\n    prompt: str,\n    tokenizer: PreTrainedTokenizer,\n    cutoff_len: int,\n    append_eos_token: bool = False,\n) -> Dict[str, torch.Tensor | list[int]]:\n    \"\"\"Converts a prompt into a tokenized input for a model.\n\n    The methods returns the tokenized input as a dictionary with the keys\n    \"input_ids\", \"labels\" and \"attention_mask\" where the input_ids are the\n    tokenized input, the labels assign the same label ('1') to each token\n    and the attention_mask masks out the padding tokens.\n    Parameters:\n        prompt: The prompt to tokenize\n        tokenizer: The tokenizer to use\n        cutoff_len: The maximum length of the encoded text\n        append_eos_token: Whether to append an end of sentence token\n\n    Returns:\n        The tokenized input as a dictionary\n    \"\"\"\n    input_ids = encode(prompt, True, tokenizer, cutoff_len)\n\n    if tokenizer.pad_token_id is None or tokenizer.padding_side is None:\n        msg = (\n            \"Tokenizing implies tokenizer.pad_token_id \"\n            \"and tokenizer.padding_side to be set!\"\n        )\n        raise AttributeError(msg)\n\n    if (\n        append_eos_token\n        and input_ids[-1] != tokenizer.eos_token_id\n        and len(input_ids) < cutoff_len\n    ):\n        input_ids.append(tokenizer.eos_token_id)\n\n    input_ids = [tokenizer.pad_token_id] * (cutoff_len - len(input_ids)) + input_ids\n    labels = [1] * len(input_ids)\n\n    input_tensors = torch.tensor(input_ids)\n    return {\n        \"input_ids\": input_tensors,\n        \"labels\": labels,\n        \"attention_mask\": input_tensors.ne(tokenizer.pad_token_id),\n    }\n
    "},{"location":"reference/gerd/training/instruct/","title":"gerd.training.instruct","text":""},{"location":"reference/gerd/training/instruct/#gerd.training.instruct","title":"gerd.training.instruct","text":"

    Training module for instruction text sets.

    In contrast to the unstructured training module, data must be prepared in a specific format to train LoRA models. Make sure to provide the training data in the correct format.

    Classes:

    Name Description InstructTrainingData

    Dataclass to hold training data for instruction text sets.

    InstructTrainingSample

    Dataclass to hold a training sample for instruction text sets.

    Functions:

    Name Description train_lora

    Train a LoRA model on instruction text sets.

    "},{"location":"reference/gerd/training/instruct/#gerd.training.instruct.InstructTrainingData","title":"InstructTrainingData","text":"

    Bases: BaseModel

    Dataclass to hold training data for instruction text sets.

    A training data object consists of a list of training samples.

    Attributes:

    Name Type Description samples list[InstructTrainingSample]

    The list of training samples.

    "},{"location":"reference/gerd/training/instruct/#gerd.training.instruct.InstructTrainingData.samples","title":"samples class-attribute instance-attribute","text":"
    samples: list[InstructTrainingSample] = []\n

    The list of training samples.

    "},{"location":"reference/gerd/training/instruct/#gerd.training.instruct.InstructTrainingSample","title":"InstructTrainingSample","text":"

    Bases: BaseModel

    Dataclass to hold a training sample for instruction text sets.

    A training sample consists of a list of chat messages.

    Attributes:

    Name Type Description messages list[ChatMessage]

    The list of chat messages.

    "},{"location":"reference/gerd/training/instruct/#gerd.training.instruct.InstructTrainingSample.messages","title":"messages instance-attribute","text":"
    messages: list[ChatMessage]\n

    The list of chat messages.

    "},{"location":"reference/gerd/training/instruct/#gerd.training.instruct.train_lora","title":"train_lora","text":"
    train_lora(config: str | LoraTrainingConfig, data: InstructTrainingData | None = None) -> Trainer\n

    Train a LoRA model on instruction text sets.

    Parameters:

    Name Type Description Default str | LoraTrainingConfig

    The configuration name or the configuration itself

    required InstructTrainingData | None

    The training data to train on, if None, the input_glob from the config is used

    None

    Returns:

    Type Description Trainer

    The trainer instance that is used for training

    Source code in gerd/training/instruct.py
    def train_lora(\n    config: str | LoraTrainingConfig, data: InstructTrainingData | None = None\n) -> Trainer:\n    \"\"\"Train a LoRA model on instruction text sets.\n\n    Parameters:\n        config: The configuration name or the configuration itself\n        data: The training data to train on, if None,\n            the input_glob from the config is used\n\n    Returns:\n        The trainer instance that is used for training\n    \"\"\"\n    # Disable parallelism to avoid issues with transformers\n    os.environ[\"TOKENIZERS_PARALLELISM\"] = \"false\"\n\n    lora_config = load_training_config(config) if isinstance(config, str) else config\n\n    if Path(lora_config.output_dir).joinpath(\"adapter_model.safetensors\").exists():\n        if lora_config.override_existing:\n            # check that we do not delete anything vital\n            if lora_config.output_dir == Path(\"/\"):\n                msg = \"Cannot delete root directory.\"\n                raise RuntimeError(msg)\n\n            _LOGGER.warning(\n                \"Overriding existing LoRA adapter in %s ...\", lora_config.output_dir\n            )\n            shutil.rmtree(lora_config.output_dir)\n        else:\n            msg = (\n                f\"LoRA target directory {lora_config.output_dir}\"\n                \" must not contain another lora adapter.\"\n            )\n            raise AssertionError(msg)\n\n    _LOGGER.info(\"Tokenizing training data ...\")\n\n    if data is None:\n        data = InstructTrainingData()\n        # currently, we expect the input glob to point to a local directory\n        # in the future, we might want to support other sources\n        glob_pattern = lora_config.input_glob.replace(\"file://\", \"\")\n        for file in Path().glob(glob_pattern):\n            if not file.is_file():\n                msg = \"Can only read files for now.\"\n                raise NotImplementedError(msg)\n            if file.suffix == \".json\":\n                with open(file, \"r\") as f:\n                    data.samples.extend(\n                        InstructTrainingData.model_validate_json(f.read()).samples\n                    )\n            elif file.suffix == \".yml\":\n                with open(file, \"r\") as f:\n                    obj = yaml.safe_load(f)\n                    data.samples.extend(\n                        InstructTrainingData.model_validate(obj).samples\n                    )\n            else:\n                msg = f\"Unsupported file format: {file.suffix}\"\n                raise NotImplementedError(msg)\n\n    train_data = Dataset.from_list(\n        [\n            lora_config.tokenizer(\n                lora_config.tokenizer.apply_chat_template(\n                    sample.messages, tokenize=False\n                )\n            )\n            for sample in data.samples\n        ]\n    )\n    _LOGGER.info(\"Decoding sample data ...\")\n    decoded_entries = []\n    for i in range(min(10, len(train_data))):\n        decoded_text = lora_config.tokenizer.decode(train_data[i][\"input_ids\"])\n        decoded_entries.append({\"value\": decoded_text})\n\n    log_dir = lora_config.output_dir / \"logs\"\n    log_dir.mkdir(exist_ok=True, parents=True)\n    _LOGGER.info(\"Writing sample to %s ...\", log_dir)\n    with open(Path(f\"{log_dir}/train_dataset_sample.json\"), \"w\") as json_file:\n        json.dump(decoded_entries, json_file, indent=4)\n\n    trainer = Trainer(config=lora_config)\n    trainer.setup_training(\n        train_data=train_data, train_template={\"template_type\": \"dataset\"}\n    )\n\n    trainer.train()\n    return trainer\n
    "},{"location":"reference/gerd/training/instruct/#gerd.training.instruct.train_lora(config)","title":"config","text":""},{"location":"reference/gerd/training/instruct/#gerd.training.instruct.train_lora(data)","title":"data","text":""},{"location":"reference/gerd/training/lora/","title":"gerd.training.lora","text":""},{"location":"reference/gerd/training/lora/#gerd.training.lora","title":"gerd.training.lora","text":"

    Configuration dataclasses for training LoRA models.

    Classes:

    Name Description LLMModelProto

    Protocol for the LoRA model.

    LoraModules

    Configuration for the modules to be trained in LoRA models.

    LoraTrainingConfig

    Configuration for training LoRA models.

    TrainingFlags

    Training flags for LoRA models.

    Functions:

    Name Description load_training_config

    Load the LLM model configuration.

    "},{"location":"reference/gerd/training/lora/#gerd.training.lora.LLMModelProto","title":"LLMModelProto","text":"

    Bases: Protocol

    Protocol for the LoRA model.

    A model model needs to implement the named_modules method for it to be used in LoRA Training.

    Methods:

    Name Description named_modules

    Get the named modules of the model.

    "},{"location":"reference/gerd/training/lora/#gerd.training.lora.LLMModelProto.named_modules","title":"named_modules","text":"
    named_modules() -> list[tuple[str, torch.nn.Module]]\n

    Get the named modules of the model.

    Returns:

    Type Description list[tuple[str, Module]]

    The named modules.

    Source code in gerd/training/lora.py
    def named_modules(self) -> list[tuple[str, torch.nn.Module]]:\n    \"\"\"Get the named modules of the model.\n\n    Returns:\n        The named modules.\n    \"\"\"\n    pass\n
    "},{"location":"reference/gerd/training/lora/#gerd.training.lora.LoraModules","title":"LoraModules","text":"

    Bases: BaseModel

    Configuration for the modules to be trained in LoRA models.

    Methods:

    Name Description target_modules

    Get the target modules for the given model.

    "},{"location":"reference/gerd/training/lora/#gerd.training.lora.LoraModules.target_modules","title":"target_modules","text":"
    target_modules(model: LLMModelProto) -> List[str]\n

    Get the target modules for the given model.

    Parameters:

    Name Type Description Default LLMModelProto

    The model to be trained.

    required

    Returns:

    Type Description List[str]

    The list of target modules

    Source code in gerd/training/lora.py
    def target_modules(self, model: LLMModelProto) -> List[str]:\n    \"\"\"Get the target modules for the given model.\n\n    Parameters:\n        model: The model to be trained.\n\n    Returns:\n        The list of target modules\n    \"\"\"\n    avail = _find_target_modules(model)\n    return [\n        f\"{name}_proj\"\n        for name, enabled in self.model_dump().items()\n        if (enabled is True or (enabled is None and self.default is True))\n        and f\"{name}_proj\" in avail\n    ]\n
    "},{"location":"reference/gerd/training/lora/#gerd.training.lora.LoraModules.target_modules(model)","title":"model","text":""},{"location":"reference/gerd/training/lora/#gerd.training.lora.LoraTrainingConfig","title":"LoraTrainingConfig","text":"

    Bases: BaseSettings

    Configuration for training LoRA models.

    Methods:

    Name Description model_post_init

    Post-initialization hook for the model.

    reset_tokenizer

    Resets the tokenizer.

    settings_customise_sources

    Customize the settings sources used by pydantic-settings.

    Attributes:

    Name Type Description tokenizer PreTrainedTokenizer

    Get the tokenizer for the model.

    "},{"location":"reference/gerd/training/lora/#gerd.training.lora.LoraTrainingConfig.tokenizer","title":"tokenizer property","text":"
    tokenizer: PreTrainedTokenizer\n

    Get the tokenizer for the model.

    "},{"location":"reference/gerd/training/lora/#gerd.training.lora.LoraTrainingConfig.model_post_init","title":"model_post_init","text":"
    model_post_init(_: Any) -> None\n

    Post-initialization hook for the model.

    This method currently checks whether cutoff is larger than overlap.

    Source code in gerd/training/lora.py
    def model_post_init(self, _: Any) -> None:  # noqa: ANN401\n    \"\"\"Post-initialization hook for the model.\n\n    This method currently checks whether cutoff is larger than overlap.\n    \"\"\"\n    if self.cutoff_len <= self.overlap_len:\n        msg = (\n            \"Overlap must be smaller than cutoff\"\n            f\"({self.cutoff_len}) but is {self.overlap_len}\"\n        )\n        raise ValueError(msg)\n    self._tokenizer = None\n
    "},{"location":"reference/gerd/training/lora/#gerd.training.lora.LoraTrainingConfig.reset_tokenizer","title":"reset_tokenizer","text":"
    reset_tokenizer() -> None\n

    Resets the tokenizer.

    When a tokenizer has been used it needs to be reset before changig parameters to avoid issues with parallelism.

    Source code in gerd/training/lora.py
    def reset_tokenizer(self) -> None:\n    \"\"\"Resets the tokenizer.\n\n    When a tokenizer has been used it needs to be reset\n    before changig parameters to avoid issues with parallelism.\n    \"\"\"\n    self._tokenizer = transformers.AutoTokenizer.from_pretrained(\n        self.model.name, trust_remote_code=False, use_fast=True\n    )\n    if self._tokenizer.pad_token_id is None:\n        self._tokenizer.pad_token_id = self.pad_token_id\n    if self.padding_side:\n        self._tokenizer.padding_side = self.padding_side\n
    "},{"location":"reference/gerd/training/lora/#gerd.training.lora.LoraTrainingConfig.settings_customise_sources","title":"settings_customise_sources classmethod","text":"
    settings_customise_sources(_: Type[BaseSettings], init_settings: PydanticBaseSettingsSource, env_settings: PydanticBaseSettingsSource, dotenv_settings: PydanticBaseSettingsSource, file_secret_settings: PydanticBaseSettingsSource) -> tuple[PydanticBaseSettingsSource, ...]\n

    Customize the settings sources used by pydantic-settings.

    The order of the sources is important. The first source has the highest priority.

    Parameters:

    Name Type Description Default

    The class of the settings.

    required PydanticBaseSettingsSource

    The settings from the initialization.

    required PydanticBaseSettingsSource

    The settings from the environment.

    required PydanticBaseSettingsSource

    The settings from the dotenv file.

    required PydanticBaseSettingsSource

    The settings from the secret file.

    required

    Returns:

    Type Description tuple[PydanticBaseSettingsSource, ...]

    The customized settings sources.

    Source code in gerd/training/lora.py
    @classmethod\ndef settings_customise_sources(\n    cls,\n    _: Type[BaseSettings],\n    init_settings: PydanticBaseSettingsSource,\n    env_settings: PydanticBaseSettingsSource,\n    dotenv_settings: PydanticBaseSettingsSource,\n    file_secret_settings: PydanticBaseSettingsSource,\n) -> tuple[PydanticBaseSettingsSource, ...]:\n    \"\"\"Customize the settings sources used by pydantic-settings.\n\n    The order of the sources is important.\n    The first source has the highest priority.\n\n    Parameters:\n        cls: The class of the settings.\n        init_settings: The settings from the initialization.\n        env_settings: The settings from the environment.\n        dotenv_settings: The settings from the dotenv file.\n        file_secret_settings: The settings from the secret file.\n\n    Returns:\n        The customized settings sources.\n    \"\"\"\n    return (\n        file_secret_settings,\n        env_settings,\n        dotenv_settings,\n        init_settings,\n    )\n
    "},{"location":"reference/gerd/training/lora/#gerd.training.lora.LoraTrainingConfig.settings_customise_sources(cls)","title":"cls","text":""},{"location":"reference/gerd/training/lora/#gerd.training.lora.LoraTrainingConfig.settings_customise_sources(init_settings)","title":"init_settings","text":""},{"location":"reference/gerd/training/lora/#gerd.training.lora.LoraTrainingConfig.settings_customise_sources(env_settings)","title":"env_settings","text":""},{"location":"reference/gerd/training/lora/#gerd.training.lora.LoraTrainingConfig.settings_customise_sources(dotenv_settings)","title":"dotenv_settings","text":""},{"location":"reference/gerd/training/lora/#gerd.training.lora.LoraTrainingConfig.settings_customise_sources(file_secret_settings)","title":"file_secret_settings","text":""},{"location":"reference/gerd/training/lora/#gerd.training.lora.TrainingFlags","title":"TrainingFlags","text":"

    Bases: BaseModel

    Training flags for LoRA models.

    "},{"location":"reference/gerd/training/lora/#gerd.training.lora.load_training_config","title":"load_training_config","text":"
    load_training_config(config: str) -> LoraTrainingConfig\n

    Load the LLM model configuration.

    Parameters:

    Name Type Description Default str

    The name of the configuration.

    required

    Returns:

    Type Description LoraTrainingConfig

    The model configuration.

    Source code in gerd/training/lora.py
    def load_training_config(config: str) -> LoraTrainingConfig:\n    \"\"\"Load the LLM model configuration.\n\n    Parameters:\n        config: The name of the configuration.\n\n    Returns:\n        The model configuration.\n    \"\"\"\n    config_path = (\n        Path(config)\n        if config.endswith(\"yml\")\n        else Path(PROJECT_DIR, \"config\", f\"{config}.yml\")\n    )\n    with config_path.open(\"r\", encoding=\"utf-8\") as f:\n        conf = LoraTrainingConfig.model_validate(safe_load(f))\n    return conf\n
    "},{"location":"reference/gerd/training/lora/#gerd.training.lora.load_training_config(config)","title":"config","text":""},{"location":"reference/gerd/training/trainer/","title":"gerd.training.trainer","text":""},{"location":"reference/gerd/training/trainer/#gerd.training.trainer","title":"gerd.training.trainer","text":"

    Training module for LoRA models.

    Can be used to train LoRA models on structured or unstructured data.

    Classes:

    Name Description Callbacks

    Custom callbacks for the LoRA training.

    Tracked

    Dataclass to track the training progress.

    Trainer

    The LoRA trainer class.

    "},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Callbacks","title":"Callbacks","text":"
    Callbacks(tracked: Tracked)\n

    Bases: TrainerCallback

    Custom callbacks for the LoRA training.

    Initialize the callbacks based on tracking data config.

    Parameters:

    Name Type Description Default Tracked

    The tracking data

    required

    Methods:

    Name Description on_log

    Callback to log the training progress.

    on_save

    Saves the training log when the model is saved.

    on_step_begin

    Update the training progress.

    on_substep_end

    Update the training progress and check for interruption.

    Source code in gerd/training/trainer.py
    def __init__(self, tracked: Tracked) -> None:\n    \"\"\"Initialize the callbacks based on tracking data config.\n\n    Parameters:\n        tracked: The tracking data\n    \"\"\"\n    super().__init__()\n    self.tracked = tracked\n    self.gradient_accumulation_steps = (\n        tracked.config.batch_size // tracked.config.micro_batch_size\n    )\n    self.actual_save_steps = math.ceil(\n        tracked.config.save_steps / self.gradient_accumulation_steps\n    )\n
    "},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Callbacks(tracked)","title":"tracked","text":""},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Callbacks.on_log","title":"on_log","text":"
    on_log(_args: transformers.TrainingArguments, _state: transformers.TrainerState, control: transformers.TrainerControl, logs: Dict, **kwargs: int) -> None\n

    Callback to log the training progress.

    Parameters:

    Name Type Description Default TrainingArguments

    The training arguments (not used)

    required TrainerState

    The trainer state (not used)

    required TrainerControl

    The trainer control

    required Dict

    The training logs

    required Source code in gerd/training/trainer.py
    def on_log(\n    self,\n    _args: transformers.TrainingArguments,  # noqa: ARG002\n    _state: transformers.TrainerState,  # noqa: ARG002\n    control: transformers.TrainerControl,\n    logs: Dict,\n    **kwargs: int,  # noqa: ARG002\n) -> None:\n    \"\"\"Callback to log the training progress.\n\n    Parameters:\n        _args: The training arguments (not used)\n        _state: The trainer state (not used)\n        control: The trainer control\n        logs: The training logs\n    \"\"\"\n    self.tracked.train_log.update(logs)\n    self.tracked.train_log.update({\"current_steps\": self.tracked.current_steps})\n    if self.tracked.interrupted:\n        _LOGGER.info(\"Interrupted by user\")\n\n    # print(f\"Step: {self.tracked.current_steps}\", end=\"\")\n    if \"loss\" in logs:\n        loss = float(logs[\"loss\"])\n        if loss <= self.tracked.config.stop_at_loss:\n            control.should_epoch_stop = True\n            control.should_training_stop = True\n            _LOGGER.info(\"Stop Loss %f reached.\", self.tracked.config.stop_at_loss)\n
    "},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Callbacks.on_log(_args)","title":"_args","text":""},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Callbacks.on_log(_state)","title":"_state","text":""},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Callbacks.on_log(control)","title":"control","text":""},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Callbacks.on_log(logs)","title":"logs","text":""},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Callbacks.on_save","title":"on_save","text":"
    on_save(_args: transformers.TrainingArguments, _state: transformers.TrainerState, _control: transformers.TrainerControl, **kwargs: int) -> None\n

    Saves the training log when the model is saved.

    Source code in gerd/training/trainer.py
    def on_save(\n    self,\n    _args: transformers.TrainingArguments,  # noqa: ARG002\n    _state: transformers.TrainerState,  # noqa: ARG002\n    _control: transformers.TrainerControl,  # noqa: ARG002\n    **kwargs: int,  # noqa: ARG002\n) -> None:\n    \"\"\"Saves the training log when the model is saved.\"\"\"\n    # Save log\n    with open(\n        f\"{self.tracked.config.output_dir}/{self.tracked.current_steps}-training_log.json\",\n        \"w\",\n        encoding=\"utf-8\",\n    ) as file:\n        json.dump(self.tracked.train_log, file, indent=2)\n
    "},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Callbacks.on_step_begin","title":"on_step_begin","text":"
    on_step_begin(_args: transformers.TrainingArguments, state: transformers.TrainerState, control: transformers.TrainerControl, **kwargs: int) -> None\n

    Update the training progress.

    This callback updates the current training steps and checks if the training was interrupted.

    Parameters:

    Name Type Description Default TrainingArguments

    The training arguments (not used)

    required TrainerState

    The trainer state

    required TrainerControl

    The trainer control

    required Source code in gerd/training/trainer.py
    def on_step_begin(\n    self,\n    _args: transformers.TrainingArguments,  # noqa: ARG002\n    state: transformers.TrainerState,\n    control: transformers.TrainerControl,\n    **kwargs: int,  # noqa: ARG002\n) -> None:\n    \"\"\"Update the training progress.\n\n    This callback updates the current training steps and\n    checks if the training was interrupted.\n\n    Parameters:\n        _args: The training arguments (not used)\n        state: The trainer state\n        control: The trainer control\n    \"\"\"\n    self.tracked.current_steps = (\n        state.global_step * self.gradient_accumulation_steps\n    )\n    self.tracked.max_steps = state.max_steps * self.gradient_accumulation_steps\n    if self.tracked.interrupted:\n        control.should_epoch_stop = True\n        control.should_training_stop = True\n
    "},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Callbacks.on_step_begin(_args)","title":"_args","text":""},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Callbacks.on_step_begin(state)","title":"state","text":""},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Callbacks.on_step_begin(control)","title":"control","text":""},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Callbacks.on_substep_end","title":"on_substep_end","text":"
    on_substep_end(_args: transformers.TrainingArguments, _state: transformers.TrainerState, control: transformers.TrainerControl, **kwargs: int) -> None\n

    Update the training progress and check for interruption.

    Parameters:

    Name Type Description Default TrainingArguments

    The training arguments (not used)

    required TrainerState

    The trainer state (not used)

    required TrainerControl

    The trainer control

    required Source code in gerd/training/trainer.py
    def on_substep_end(\n    self,\n    _args: transformers.TrainingArguments,  # noqa: ARG002\n    _state: transformers.TrainerState,  # noqa: ARG002\n    control: transformers.TrainerControl,\n    **kwargs: int,  # noqa: ARG002\n) -> None:\n    \"\"\"Update the training progress and check for interruption.\n\n    Parameters:\n        _args: The training arguments (not used)\n        _state: The trainer state (not used)\n        control: The trainer control\n    \"\"\"\n    self.tracked.current_steps += 1\n    if self.tracked.interrupted:\n        control.should_epoch_stop = True\n        control.should_training_stop = True\n
    "},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Callbacks.on_substep_end(_args)","title":"_args","text":""},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Callbacks.on_substep_end(_state)","title":"_state","text":""},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Callbacks.on_substep_end(control)","title":"control","text":""},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Tracked","title":"Tracked dataclass","text":"
    Tracked(lora_model: PeftModel, config: LoraTrainingConfig, train_log: Dict = dict(), current_steps: int = 0, interrupted: bool = False, max_steps: int = 0, did_save: bool = False)\n

    Dataclass to track the training progress.

    Attributes:

    Name Type Description config LoraTrainingConfig

    The training configuration.

    current_steps int

    The current training steps.

    did_save bool

    Whether the model was saved.

    interrupted bool

    Whether the training was interrupted.

    lora_model PeftModel

    The training model.

    max_steps int

    The maximum number of training steps.

    train_log Dict

    The training log.

    "},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Tracked.config","title":"config instance-attribute","text":"
    config: LoraTrainingConfig\n

    The training configuration.

    "},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Tracked.current_steps","title":"current_steps class-attribute instance-attribute","text":"
    current_steps: int = 0\n

    The current training steps.

    "},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Tracked.did_save","title":"did_save class-attribute instance-attribute","text":"
    did_save: bool = False\n

    Whether the model was saved.

    "},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Tracked.interrupted","title":"interrupted class-attribute instance-attribute","text":"
    interrupted: bool = False\n

    Whether the training was interrupted.

    "},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Tracked.lora_model","title":"lora_model instance-attribute","text":"
    lora_model: PeftModel\n

    The training model.

    "},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Tracked.max_steps","title":"max_steps class-attribute instance-attribute","text":"
    max_steps: int = 0\n

    The maximum number of training steps.

    "},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Tracked.train_log","title":"train_log class-attribute instance-attribute","text":"
    train_log: Dict = field(default_factory=dict)\n

    The training log.

    "},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Trainer","title":"Trainer","text":"
    Trainer(config: LoraTrainingConfig, callback_cls: Optional[List[Type[transformers.TrainerCallback]]] = None)\n

    The LoRA trainer class.

    This class is used to train LoRA models on structured or unstructured data. Since the training process is asynchronous, the trainer can be used to track or interrupt the training process.

    The LoRa traininer requires a configuration and optional list of callbacks.

    If no callbacks are provided, the default Callbacks class Tracked is used. Parameters: config: The training configuration callback_cls: The list of callbacks

    Methods:

    Name Description interrupt

    Interrupt the training process.

    save

    Save the model and log files to the path set in the trainer configuration.

    setup_training

    Setup the training process and initialize the transformer trainer.

    train

    Start the training process.

    Source code in gerd/training/trainer.py
    def __init__(\n    self,\n    config: LoraTrainingConfig,\n    callback_cls: Optional[List[Type[transformers.TrainerCallback]]] = None,\n) -> None:\n    \"\"\"The LoRa traininer requires a configuration and optional list of callbacks.\n\n    If no callbacks are provided,\n    the default Callbacks class [`Tracked`][gerd.training.trainer.Tracked] is used.\n    Parameters:\n        config: The training configuration\n        callback_cls: The list of callbacks\n    \"\"\"\n    self.config = config\n    self.base_model: transformers.PreTrainedModel = (\n        transformers.AutoModelForCausalLM.from_pretrained(\n            config.model.name,\n            load_in_4bit=config.flags.use_4bit,\n            load_in_8bit=config.flags.use_8bit,\n        )\n    )\n\n    training_config = LoraConfig(\n        lora_alpha=config.lora_alpha,\n        target_modules=config.modules.target_modules(self.base_model),\n        lora_dropout=config.lora_dropout,\n        bias=config.bias,\n        task_type=config.task_type,\n        r=config.r,\n    )\n\n    for param in self.base_model.parameters():\n        if param.requires_grad:\n            param.data = param.data.float()\n    if (\n        not hasattr(self.base_model, \"lm_head\")\n        or hasattr(self.base_model.lm_head, \"weight\")\n    ) and \"quantization_config\" in self.base_model.config.to_dict():\n        self.base_model = prepare_model_for_kbit_training(self.base_model)\n        _LOGGER.info(\"Quantization detected!\")\n\n    self.lora_model = get_peft_model(self.base_model, training_config)\n    if not hasattr(self.lora_model.config, \"use_cache\"):\n        msg = \"LoRA model config not have 'use_cache' attribute\"\n        raise AssertionError(msg)\n    self.lora_model.config.use_cache = False\n\n    self.tracked = Tracked(self.lora_model, self.config)\n    self.trainer = None\n    self.callbacks = (\n        [cls(self.tracked) for cls in callback_cls]\n        if callback_cls is not None\n        else [Callbacks(self.tracked)]\n    )\n\n    gradient_accumulation_steps = config.batch_size // config.micro_batch_size\n    actual_lr = float(config.learning_rate)\n\n    self.args = transformers.TrainingArguments(\n        per_device_train_batch_size=self.config.micro_batch_size,\n        gradient_accumulation_steps=gradient_accumulation_steps,\n        warmup_steps=math.ceil(config.warmup_steps / gradient_accumulation_steps),\n        num_train_epochs=config.epochs,\n        learning_rate=actual_lr,\n        fp16=not (config.flags.use_cpu or config.flags.use_bf16),\n        bf16=config.flags.use_bf16,\n        optim=config.optimizer,\n        logging_steps=2 if config.stop_at_loss > 0 else 5,\n        eval_strategy=\"no\",\n        eval_steps=None,\n        save_strategy=\"no\",\n        output_dir=config.output_dir,\n        lr_scheduler_type=config.lr_scheduler,\n        load_best_model_at_end=False,\n        # TODO: Enable multi-device support\n        ddp_find_unused_parameters=None,\n        use_ipex=config.flags.use_ipex,\n        save_steps=config.save_steps,\n        # any of these two will set `torch_compile=True`\n        # torch_compile_backend=\"inductor\",\n        # torch_compile_mode=\"reduce-overhead\",\n    )\n
    "},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Trainer.interrupt","title":"interrupt","text":"
    interrupt() -> None\n

    Interrupt the training process.

    Source code in gerd/training/trainer.py
    def interrupt(self) -> None:\n    \"\"\"Interrupt the training process.\"\"\"\n    self.tracked.interrupted = True\n
    "},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Trainer.save","title":"save","text":"
    save() -> None\n

    Save the model and log files to the path set in the trainer configuration.

    When the zip_output flag is set, the output directory is zipped as well.

    Source code in gerd/training/trainer.py
    def save(self) -> None:\n    \"\"\"Save the model and log files to the path set in the trainer configuration.\n\n    When the `zip_output` flag is set, the output directory is zipped as well.\n    \"\"\"\n    if self.trainer is not None:\n        self.trainer.save_model(self.config.output_dir)\n        if self.config.zip_output:\n            shutil.make_archive(\n                base_name=self.config.output_dir.as_posix(),\n                root_dir=self.config.output_dir,\n                # base_dir=self.config.output_dir,\n                format=\"zip\",\n            )\n    else:\n        _LOGGER.warning(\"Trainer not initialized\")\n
    "},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Trainer.setup_training","title":"setup_training","text":"
    setup_training(train_data: Dataset, train_template: Dict, torch_compile: bool = False) -> None\n

    Setup the training process and initialize the transformer trainer.

    Parameters:

    Name Type Description Default Dataset

    The training data

    required Dict

    The training template

    required bool

    Whether to use torch compile

    False Source code in gerd/training/trainer.py
    def setup_training(\n    self,\n    train_data: Dataset,\n    train_template: Dict,\n    torch_compile: bool = False,\n) -> None:\n    \"\"\"Setup the training process and initialize the transformer trainer.\n\n    Parameters:\n        train_data: The training data\n        train_template: The training template\n        torch_compile: Whether to use torch compile\n    \"\"\"\n    self.trainer = transformers.Trainer(\n        model=self.lora_model,\n        train_dataset=train_data,\n        eval_dataset=None,\n        args=self.args,\n        data_collator=transformers.DataCollatorForLanguageModeling(\n            self.config.tokenizer, mlm=False\n        ),\n        callbacks=self.callbacks,\n    )\n\n    # This must be done after Trainer init because otherwise\n    # the trainer cannot identify the relevant (as in 'trainable') parameters\n    # and will remove required information from the training data set.\n    # Whether it is useful to compile after reassignment.\n    if torch_compile and torch.__version__ >= \"2\" and sys.platform != \"win32\":\n        self.lora_model = torch.compile(self.lora_model)  # type: ignore[assignment]\n\n    # == Save parameters for reuse ==\n    with open(\n        f\"{self.config.output_dir}/training_parameters.json\", \"w\", encoding=\"utf-8\"\n    ) as file:\n        file.write(self.config.model_dump_json(indent=2))\n\n    # == Save training prompt ==\n    with open(\n        f\"{self.config.output_dir}/training_prompt.json\", \"w\", encoding=\"utf-8\"\n    ) as file:\n        json.dump(train_template, file, indent=2)\n
    "},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Trainer.setup_training(train_data)","title":"train_data","text":""},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Trainer.setup_training(train_template)","title":"train_template","text":""},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Trainer.setup_training(torch_compile)","title":"torch_compile","text":""},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Trainer.train","title":"train","text":"
    train() -> threading.Thread\n

    Start the training process.

    Returns:

    Type Description Thread

    The training thread

    Source code in gerd/training/trainer.py
    def train(self) -> threading.Thread:\n    \"\"\"Start the training process.\n\n    Returns:\n        The training thread\n    \"\"\"\n    model_type = type(self.base_model).__name__\n    model_id = MODEL_CLASSES[model_type]\n\n    projections_string = \", \".join(\n        [\n            projection.replace(\"_proj\", \"\")\n            for projection in self.config.modules.target_modules(self.base_model)\n        ]\n    )\n\n    _LOGGER.info(\n        \"Training '%s' model using (%s) projections\", model_id, projections_string\n    )\n    # == Main run and monitor loop ==\n\n    log = self.tracked.train_log\n    log.update({\"base_model_config\": self.config.model.name})\n    log.update({\"base_model_class\": self.base_model.__class__.__name__})\n    log.update(\n        {\n            \"base_loaded_in_4bit\": getattr(\n                self.lora_model, \"is_loaded_in_4bit\", False\n            )\n        }\n    )\n    log.update(\n        {\n            \"base_loaded_in_8bit\": getattr(\n                self.lora_model, \"is_loaded_in_8bit\", False\n            )\n        }\n    )\n    log.update({\"projections\": projections_string})\n\n    if self.config.stop_at_loss > 0:\n        _LOGGER.info(\n            \"Monitoring loss \\033[1;31;1m(Auto-Stop at: %f)\\033[0;37;0m\",\n            self.config.stop_at_loss,\n        )\n\n    def threaded_run() -> None:\n        if self.trainer is None:\n            msg = \"Trainer not initialized\"\n            raise RuntimeError(msg)\n        self.trainer.train()\n        # Note: save in the thread in case the gradio thread breaks\n        # (eg browser closed)\n        self.save()\n        self.tracked.did_save = True\n        # Save log\n        with open(\n            f\"{self.config.output_dir}/training_log.json\", \"w\", encoding=\"utf-8\"\n        ) as file:\n            json.dump(self.tracked.train_log, file, indent=2)\n\n    self.thread = threading.Thread(target=threaded_run)\n    self.thread.start()\n    return self.thread\n
    "},{"location":"reference/gerd/training/unstructured/","title":"gerd.training.unstructured","text":""},{"location":"reference/gerd/training/unstructured/#gerd.training.unstructured","title":"gerd.training.unstructured","text":"

    Training of LoRA models on unstructured text data.

    This module provides functions to train LoRA models to 'imitate' the style of a given text corpus.

    Functions:

    Name Description train_lora

    Train a LoRA model on unstructured text data.

    "},{"location":"reference/gerd/training/unstructured/#gerd.training.unstructured.train_lora","title":"train_lora","text":"
    train_lora(config: str | LoraTrainingConfig, texts: list[str] | None = None) -> Trainer\n

    Train a LoRA model on unstructured text data.

    Parameters:

    Name Type Description Default str | LoraTrainingConfig

    The configuration name or the configuration itself

    required list[str] | None

    The list of texts to train on, if None, the input_glob from the config is used

    None

    Returns:

    Type Description Trainer

    The trainer instance that is used for training

    Source code in gerd/training/unstructured.py
    def train_lora(\n    config: str | LoraTrainingConfig, texts: list[str] | None = None\n) -> Trainer:\n    \"\"\"Train a LoRA model on unstructured text data.\n\n    Parameters:\n        config: The configuration name or the configuration itself\n        texts: The list of texts to train on, if None,\n            the input_glob from the config is used\n\n    Returns:\n        The trainer instance that is used for training\n    \"\"\"\n    # Disable parallelism to avoid issues with transformers\n    os.environ[\"TOKENIZERS_PARALLELISM\"] = \"false\"\n\n    lora_config = load_training_config(config) if isinstance(config, str) else config\n\n    if Path(lora_config.output_dir).joinpath(\"adapter_model.safetensors\").exists():\n        if lora_config.override_existing:\n            # check that we do not delete anything vital\n            if lora_config.output_dir == Path(\"/\"):\n                msg = \"Cannot delete root directory.\"\n                raise RuntimeError(msg)\n\n            _LOGGER.warning(\n                \"Overriding existing LoRA adapter in %s ...\", lora_config.output_dir\n            )\n            shutil.rmtree(lora_config.output_dir)\n        else:\n            msg = (\n                f\"LoRA target directory {lora_config.output_dir}\"\n                \" must not contain another lora adapter.\"\n            )\n            raise AssertionError(msg)\n\n    _LOGGER.info(\"Tokenizing training data ...\")\n\n    if not texts:\n        texts = []\n        # currently, we expect the input glob to point to a local directory\n        # in the future, we might want to support other sources\n        glob_pattern = lora_config.input_glob.replace(\"file://\", \"\")\n        for file in Path().glob(glob_pattern):\n            with open(file, \"r\") as f:\n                texts.append(f.read())\n\n    training_tokens: list[list[int]] = []\n    for text in texts:\n        if len(text) == 0:\n            continue\n        training_tokens.extend(\n            split_chunks(\n                lora_config.tokenizer.encode(text),\n                lora_config.cutoff_len,\n                lora_config.cutoff_len - lora_config.overlap_len,\n            )\n        )\n    text_chunks = [lora_config.tokenizer.decode(x) for x in training_tokens]\n    train_data = Dataset.from_list(\n        [\n            tokenize(x, lora_config.tokenizer, lora_config.cutoff_len)\n            for x in text_chunks\n        ]\n    )\n    _LOGGER.info(\"Decoding sample data ...\")\n    decoded_entries = []\n    for i in range(min(10, len(train_data))):\n        decoded_text = lora_config.tokenizer.decode(train_data[i][\"input_ids\"])\n        decoded_entries.append({\"value\": decoded_text})\n\n    log_dir = lora_config.output_dir / \"logs\"\n    log_dir.mkdir(exist_ok=True, parents=True)\n    _LOGGER.info(\"Writing sample to %s ...\", log_dir)\n    with open(Path(f\"{log_dir}/train_dataset_sample.json\"), \"w\") as json_file:\n        json.dump(decoded_entries, json_file, indent=4)\n\n    trainer = Trainer(config=lora_config)\n    trainer.setup_training(\n        train_data=train_data, train_template={\"template_type\": \"raw_text\"}\n    )\n\n    trainer.train()\n    return trainer\n
    "},{"location":"reference/gerd/training/unstructured/#gerd.training.unstructured.train_lora(config)","title":"config","text":""},{"location":"reference/gerd/training/unstructured/#gerd.training.unstructured.train_lora(texts)","title":"texts","text":""},{"location":"reference/gerd/transport/","title":"gerd.transport","text":""},{"location":"reference/gerd/transport/#gerd.transport","title":"gerd.transport","text":"

    Module to define the transport protocol.

    The transport protocol is used to connect the backend and frontend services. Implemetations of the transport protocol can be found in the gerd.backends module.

    Classes:

    Name Description DocumentSource

    Dataclass to hold a document source.

    FileTypes

    Enum to hold all supported file types.

    GenResponse

    Dataclass to hold a response from the generation service.

    QAAnalyzeAnswer

    Dataclass to hold an answer from the predefined queries to the QA service.

    QAAnswer

    Dataclass to hold an answer from the QA service.

    QAFileUpload

    Dataclass to hold a file upload.

    QAModesEnum

    Enum to hold all supported QA modes.

    QAPromptConfig

    Prompt configuration for the QA service.

    QAQuestion

    Dataclass to hold a question for the QA service.

    Transport

    Transport protocol to connect backend and frontend services.

    "},{"location":"reference/gerd/transport/#gerd.transport.DocumentSource","title":"DocumentSource","text":"

    Bases: BaseModel

    Dataclass to hold a document source.

    Attributes:

    Name Type Description content str

    The content of the document.

    name str

    The name of the document.

    page int

    The page of the document.

    query str

    The query that was used to find the document.

    "},{"location":"reference/gerd/transport/#gerd.transport.DocumentSource.content","title":"content instance-attribute","text":"
    content: str\n

    The content of the document.

    "},{"location":"reference/gerd/transport/#gerd.transport.DocumentSource.name","title":"name instance-attribute","text":"
    name: str\n

    The name of the document.

    "},{"location":"reference/gerd/transport/#gerd.transport.DocumentSource.page","title":"page instance-attribute","text":"
    page: int\n

    The page of the document.

    "},{"location":"reference/gerd/transport/#gerd.transport.DocumentSource.query","title":"query instance-attribute","text":"
    query: str\n

    The query that was used to find the document.

    "},{"location":"reference/gerd/transport/#gerd.transport.FileTypes","title":"FileTypes","text":"

    Bases: Enum

    Enum to hold all supported file types.

    Attributes:

    Name Type Description PDF

    PDF file type.

    TEXT

    Text file type.

    "},{"location":"reference/gerd/transport/#gerd.transport.FileTypes.PDF","title":"PDF class-attribute instance-attribute","text":"
    PDF = 'pdf'\n

    PDF file type.

    "},{"location":"reference/gerd/transport/#gerd.transport.FileTypes.TEXT","title":"TEXT class-attribute instance-attribute","text":"
    TEXT = 'txt'\n

    Text file type.

    "},{"location":"reference/gerd/transport/#gerd.transport.GenResponse","title":"GenResponse","text":"

    Bases: BaseModel

    Dataclass to hold a response from the generation service.

    Attributes:

    Name Type Description error_msg str

    The error message if the status code is not 200.

    prompt str | None

    The custom prompt that was used to generate the text.

    status int

    The status code of the response.

    text str

    The generated text if the status code is 200.

    "},{"location":"reference/gerd/transport/#gerd.transport.GenResponse.error_msg","title":"error_msg class-attribute instance-attribute","text":"
    error_msg: str = ''\n

    The error message if the status code is not 200.

    "},{"location":"reference/gerd/transport/#gerd.transport.GenResponse.prompt","title":"prompt class-attribute instance-attribute","text":"
    prompt: str | None = None\n

    The custom prompt that was used to generate the text.

    "},{"location":"reference/gerd/transport/#gerd.transport.GenResponse.status","title":"status class-attribute instance-attribute","text":"
    status: int = 200\n

    The status code of the response.

    "},{"location":"reference/gerd/transport/#gerd.transport.GenResponse.text","title":"text class-attribute instance-attribute","text":"
    text: str = ''\n

    The generated text if the status code is 200.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAAnalyzeAnswer","title":"QAAnalyzeAnswer","text":"

    Bases: BaseModel

    Dataclass to hold an answer from the predefined queries to the QA service.

    Attributes:

    Name Type Description error_msg str

    The error message of the answer if the status code is not 200.

    status int

    The status code of the answer.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAAnalyzeAnswer.error_msg","title":"error_msg class-attribute instance-attribute","text":"
    error_msg: str = ''\n

    The error message of the answer if the status code is not 200.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAAnalyzeAnswer.status","title":"status class-attribute instance-attribute","text":"
    status: int = 200\n

    The status code of the answer.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAAnswer","title":"QAAnswer","text":"

    Bases: BaseModel

    Dataclass to hold an answer from the QA service.

    Attributes:

    Name Type Description error_msg str

    The error message of the answer if the status code is not 200.

    response str

    The response of the answer.

    sources List[DocumentSource]

    The sources of the answer.

    status int

    The status code of the answer.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAAnswer.error_msg","title":"error_msg class-attribute instance-attribute","text":"
    error_msg: str = ''\n

    The error message of the answer if the status code is not 200.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAAnswer.response","title":"response class-attribute instance-attribute","text":"
    response: str = ''\n

    The response of the answer.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAAnswer.sources","title":"sources class-attribute instance-attribute","text":"
    sources: List[DocumentSource] = []\n

    The sources of the answer.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAAnswer.status","title":"status class-attribute instance-attribute","text":"
    status: int = 200\n

    The status code of the answer.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAFileUpload","title":"QAFileUpload","text":"

    Bases: BaseModel

    Dataclass to hold a file upload.

    Attributes:

    Name Type Description data bytes

    The file data.

    name str

    The name of the file.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAFileUpload.data","title":"data instance-attribute","text":"
    data: bytes\n

    The file data.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAFileUpload.name","title":"name instance-attribute","text":"
    name: str\n

    The name of the file.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAModesEnum","title":"QAModesEnum","text":"

    Bases: Enum

    Enum to hold all supported QA modes.

    Attributes:

    Name Type Description ANALYZE

    Analyze mode.

    ANALYZE_MULT_PROMPTS

    Analyze multiple prompts mode.

    NONE

    No mode.

    SEARCH

    Search mode.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAModesEnum.ANALYZE","title":"ANALYZE class-attribute instance-attribute","text":"
    ANALYZE = 2\n

    Analyze mode.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAModesEnum.ANALYZE_MULT_PROMPTS","title":"ANALYZE_MULT_PROMPTS class-attribute instance-attribute","text":"
    ANALYZE_MULT_PROMPTS = 3\n

    Analyze multiple prompts mode.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAModesEnum.NONE","title":"NONE class-attribute instance-attribute","text":"
    NONE = 0\n

    No mode.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAModesEnum.SEARCH","title":"SEARCH class-attribute instance-attribute","text":"
    SEARCH = 1\n

    Search mode.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAPromptConfig","title":"QAPromptConfig","text":"

    Bases: BaseModel

    Prompt configuration for the QA service.

    Attributes:

    Name Type Description config PromptConfig

    The prompt configuration.

    mode QAModesEnum

    The mode to set the prompt configuration for.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAPromptConfig.config","title":"config instance-attribute","text":"
    config: PromptConfig\n

    The prompt configuration.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAPromptConfig.mode","title":"mode instance-attribute","text":"
    mode: QAModesEnum\n

    The mode to set the prompt configuration for.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAQuestion","title":"QAQuestion","text":"

    Bases: BaseModel

    Dataclass to hold a question for the QA service.

    Attributes:

    Name Type Description max_sources int

    The maximum number of sources to return.

    question str

    The question to ask the QA service.

    search_strategy str

    The search strategy to use.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAQuestion.max_sources","title":"max_sources class-attribute instance-attribute","text":"
    max_sources: int = 3\n

    The maximum number of sources to return.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAQuestion.question","title":"question instance-attribute","text":"
    question: str\n

    The question to ask the QA service.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAQuestion.search_strategy","title":"search_strategy class-attribute instance-attribute","text":"
    search_strategy: str = 'similarity'\n

    The search strategy to use.

    "},{"location":"reference/gerd/transport/#gerd.transport.Transport","title":"Transport","text":"

    Bases: Protocol

    Transport protocol to connect backend and frontend services.

    Transport should be implemented by a class that provides the necessary methods to interact with the backend.

    Methods:

    Name Description add_file

    Add a file to the vector store.

    analyze_mult_prompts_query

    Queries the vector store with a set of predefined queries.

    analyze_query

    Queries the vector store with a predefined query.

    db_embedding

    Converts a question to an embedding.

    db_query

    Queries the vector store with a question.

    generate

    Generates text with the generation service.

    get_gen_prompt

    Gets the prompt configuration for the generation service.

    get_qa_prompt

    Gets the prompt configuration for a mode of the QA service.

    qa_query

    Query the QA service with a question.

    remove_file

    Remove a file from the vector store.

    set_gen_prompt

    Sets the prompt configuration for the generation service.

    set_qa_prompt

    Sets the prompt configuration for the QA service.

    "},{"location":"reference/gerd/transport/#gerd.transport.Transport.add_file","title":"add_file","text":"
    add_file(file: QAFileUpload) -> QAAnswer\n

    Add a file to the vector store.

    The returned answer has a status code of 200 if the file was added successfully. Parameters: file: The file to add to the vector store.

    Returns:

    Type Description QAAnswer

    The answer from the QA service

    Source code in gerd/transport.py
    def add_file(self, file: QAFileUpload) -> QAAnswer:\n    \"\"\"Add a file to the vector store.\n\n    The returned answer has a status code of 200 if the file was added successfully.\n    Parameters:\n        file: The file to add to the vector store.\n\n    Returns:\n        The answer from the QA service\n\n    \"\"\"\n    pass\n
    "},{"location":"reference/gerd/transport/#gerd.transport.Transport.analyze_mult_prompts_query","title":"analyze_mult_prompts_query","text":"
    analyze_mult_prompts_query() -> QAAnalyzeAnswer\n

    Queries the vector store with a set of predefined queries.

    In contrast to analyze_query, this method queries the vector store with multiple prompts.

    Returns:

    Type Description QAAnalyzeAnswer

    The answer from the QA service.

    Source code in gerd/transport.py
    def analyze_mult_prompts_query(self) -> QAAnalyzeAnswer:\n    \"\"\"Queries the vector store with a set of predefined queries.\n\n    In contrast to [`analyze_query`][gerd.transport.Transport.analyze_query],\n    this method queries the vector store with multiple prompts.\n\n    Returns:\n        The answer from the QA service.\n    \"\"\"\n    pass\n
    "},{"location":"reference/gerd/transport/#gerd.transport.Transport.analyze_query","title":"analyze_query","text":"
    analyze_query() -> QAAnalyzeAnswer\n

    Queries the vector store with a predefined query.

    The query should return vital information gathered from letters of discharge.

    Returns:

    Type Description QAAnalyzeAnswer

    The answer from the QA service.

    Source code in gerd/transport.py
    def analyze_query(self) -> QAAnalyzeAnswer:\n    \"\"\"Queries the vector store with a predefined query.\n\n    The query should return vital information gathered\n    from letters of discharge.\n\n    Returns:\n        The answer from the QA service.\n    \"\"\"\n    pass\n
    "},{"location":"reference/gerd/transport/#gerd.transport.Transport.db_embedding","title":"db_embedding","text":"
    db_embedding(question: QAQuestion) -> List[float]\n

    Converts a question to an embedding.

    The embedding is defined by the vector store.

    Parameters:

    Name Type Description Default QAQuestion

    The question to convert to an embedding.

    required

    Returns:

    Type Description List[float]

    The embedding of the question

    Source code in gerd/transport.py
    def db_embedding(self, question: QAQuestion) -> List[float]:\n    \"\"\"Converts a question to an embedding.\n\n    The embedding is defined by the vector store.\n\n    Parameters:\n        question: The question to convert to an embedding.\n\n    Returns:\n        The embedding of the question\n    \"\"\"\n    pass\n
    "},{"location":"reference/gerd/transport/#gerd.transport.Transport.db_embedding(question)","title":"question","text":""},{"location":"reference/gerd/transport/#gerd.transport.Transport.db_query","title":"db_query","text":"
    db_query(question: QAQuestion) -> List[DocumentSource]\n

    Queries the vector store with a question.

    Parameters:

    Name Type Description Default QAQuestion

    The question to query the vector store with.

    required

    Returns:

    Type Description List[DocumentSource]

    A list of document sources

    Source code in gerd/transport.py
    def db_query(self, question: QAQuestion) -> List[DocumentSource]:\n    \"\"\"Queries the vector store with a question.\n\n    Parameters:\n        question: The question to query the vector store with.\n\n    Returns:\n        A list of document sources\n    \"\"\"\n    pass\n
    "},{"location":"reference/gerd/transport/#gerd.transport.Transport.db_query(question)","title":"question","text":""},{"location":"reference/gerd/transport/#gerd.transport.Transport.generate","title":"generate","text":"
    generate(parameters: Dict[str, str]) -> GenResponse\n

    Generates text with the generation service.

    Parameters:

    Name Type Description Default Dict[str, str]

    The parameters to generate text with

    required

    Returns:

    Type Description GenResponse

    The generation result

    Source code in gerd/transport.py
    def generate(self, parameters: Dict[str, str]) -> GenResponse:\n    \"\"\"Generates text with the generation service.\n\n    Parameters:\n        parameters: The parameters to generate text with\n\n    Returns:\n        The generation result\n    \"\"\"\n    pass\n
    "},{"location":"reference/gerd/transport/#gerd.transport.Transport.generate(parameters)","title":"parameters","text":""},{"location":"reference/gerd/transport/#gerd.transport.Transport.get_gen_prompt","title":"get_gen_prompt","text":"
    get_gen_prompt() -> PromptConfig\n

    Gets the prompt configuration for the generation service.

    Returns:

    Type Description PromptConfig

    The current prompt configuration

    Source code in gerd/transport.py
    def get_gen_prompt(self) -> PromptConfig:\n    \"\"\"Gets the prompt configuration for the generation service.\n\n    Returns:\n        The current prompt configuration\n    \"\"\"\n    pass\n
    "},{"location":"reference/gerd/transport/#gerd.transport.Transport.get_qa_prompt","title":"get_qa_prompt","text":"
    get_qa_prompt(qa_mode: QAModesEnum) -> PromptConfig\n

    Gets the prompt configuration for a mode of the QA service.

    Parameters:

    Name Type Description Default QAModesEnum

    The mode to get the prompt configuration for

    required

    Returns:

    Type Description PromptConfig

    The prompt configuration for the QA service

    Source code in gerd/transport.py
    def get_qa_prompt(self, qa_mode: QAModesEnum) -> PromptConfig:\n    \"\"\"Gets the prompt configuration for a mode of the QA service.\n\n    Parameters:\n        qa_mode: The mode to get the prompt configuration for\n\n    Returns:\n        The prompt configuration for the QA service\n    \"\"\"\n    pass\n
    "},{"location":"reference/gerd/transport/#gerd.transport.Transport.get_qa_prompt(qa_mode)","title":"qa_mode","text":""},{"location":"reference/gerd/transport/#gerd.transport.Transport.qa_query","title":"qa_query","text":"
    qa_query(query: QAQuestion) -> QAAnswer\n

    Query the QA service with a question.

    Parameters:

    Name Type Description Default QAQuestion

    The question to query the QA service with.

    required

    Returns:

    Type Description QAAnswer

    The answer from the QA service.

    Source code in gerd/transport.py
    def qa_query(self, query: QAQuestion) -> QAAnswer:\n    \"\"\"Query the QA service with a question.\n\n    Parameters:\n        query: The question to query the QA service with.\n\n    Returns:\n       The answer from the QA service.\n    \"\"\"\n    pass\n
    "},{"location":"reference/gerd/transport/#gerd.transport.Transport.qa_query(query)","title":"query","text":""},{"location":"reference/gerd/transport/#gerd.transport.Transport.remove_file","title":"remove_file","text":"
    remove_file(file_name: str) -> QAAnswer\n

    Remove a file from the vector store.

    The returned answer has a status code of 200 if the file was removed successfully. Parameters: file_name: The name of the file to remove from the vector store.

    Returns:

    Type Description QAAnswer

    The answer from the QA service

    Source code in gerd/transport.py
    def remove_file(self, file_name: str) -> QAAnswer:\n    \"\"\"Remove a file from the vector store.\n\n    The returned answer has a status code of 200\n    if the file was removed successfully.\n    Parameters:\n        file_name: The name of the file to remove from the vector store.\n\n    Returns:\n        The answer from the QA service\n    \"\"\"\n    pass\n
    "},{"location":"reference/gerd/transport/#gerd.transport.Transport.set_gen_prompt","title":"set_gen_prompt","text":"
    set_gen_prompt(config: PromptConfig) -> PromptConfig\n

    Sets the prompt configuration for the generation service.

    The prompt configuration that is returned should in most cases be the same as the one that was set. Parameters: config: The prompt configuration to set

    Returns:

    Type Description PromptConfig

    The prompt configuration that was set

    Source code in gerd/transport.py
    def set_gen_prompt(self, config: PromptConfig) -> PromptConfig:\n    \"\"\"Sets the prompt configuration for the generation service.\n\n    The prompt configuration that is returned should in most cases\n    be the same as the one that was set.\n    Parameters:\n        config: The prompt configuration to set\n\n    Returns:\n        The prompt configuration that was set\n    \"\"\"\n    pass\n
    "},{"location":"reference/gerd/transport/#gerd.transport.Transport.set_qa_prompt","title":"set_qa_prompt","text":"
    set_qa_prompt(config: PromptConfig, qa_mode: QAModesEnum) -> QAAnswer\n

    Sets the prompt configuration for the QA service.

    Since the QA service uses multiple prompt configurations, the mode should be specified. For more details, see the documentation of QAService.set_prompt_config.

    Parameters:

    Name Type Description Default PromptConfig

    The prompt configuration to set

    required QAModesEnum

    The mode to set the prompt configuration for

    required

    Returns:

    Type Description QAAnswer

    The answer from the QA service

    Source code in gerd/transport.py
    def set_qa_prompt(self, config: PromptConfig, qa_mode: QAModesEnum) -> QAAnswer:\n    \"\"\"Sets the prompt configuration for the QA service.\n\n    Since the QA service uses multiple prompt configurations,\n    the mode should be specified. For more details, see the documentation\n    of [`QAService.set_prompt_config`][gerd.qa.QAService.set_prompt_config].\n\n    Parameters:\n        config: The prompt configuration to set\n        qa_mode: The mode to set the prompt configuration for\n\n    Returns:\n        The answer from the QA service\n    \"\"\"\n    pass\n
    "},{"location":"reference/gerd/transport/#gerd.transport.Transport.set_qa_prompt(config)","title":"config","text":""},{"location":"reference/gerd/transport/#gerd.transport.Transport.set_qa_prompt(qa_mode)","title":"qa_mode","text":""}]} \ No newline at end of file +{"config":{"lang":["en"],"separator":"[\\s\\-]+","pipeline":["stopWordFilter"]},"docs":[{"location":"","title":"Generating and evaluating relevant documentation","text":"

    GERD is developed as an experimental library to investigate how large language models (LLMs) can be used to generate and analyze (sets of) documents.

    This project was initially forked from Llama-2-Open-Source-LLM-CPU-Inference by Kenneth Leung.

    "},{"location":"#quickstart","title":"Quickstart","text":"

    If you just want to it try out, you can clone the project and install dependencies with pip:

    git clone https://github.com/caretech-owl/gerd.git\ncd gerd\npip install -e \".[full]\"\npython examples/hello.py\n
    Source: examples/hello.py
    \"\"\"A 'hello world' example for GERD.\"\"\"\n\nimport logging\n\nfrom gerd.config import load_gen_config\nfrom gerd.gen.chat_service import ChatService\nfrom gerd.models.model import PromptConfig\n\nlogging.basicConfig(level=logging.WARNING)\nlogging.getLogger(\"gerd\").setLevel(logging.DEBUG)\n\nlogging.info(\n    \"Loading chat service...\"\n    \" When this is the first time you run this script, it will download the model.\"\n    \" This may take a few minutes.\"\n)\n\nchat = ChatService(load_gen_config(\"hello\"))\nres = chat.submit_user_message({\"word\": \"teleportation\"})\nlogging.info(res)\n\nchat.set_prompt_config(PromptConfig.model_validate({\"text\": \"{message}\"}))\nres = chat.submit_user_message({\"message\": \"Hello! What is one plus one?\"})\nlogging.info(res)\n

    If you want to try this out in your browser, head over to binder \ud83d\udc49 . Note that running LLMs on the CPU (and especially on limited virtual machines like binder) takes some time.

    "},{"location":"#question-and-answer-example","title":"Question and Answer Example","text":"

    Follow quickstart but execute gradio with the qa_frontend instead of the example file. When the server is done loading, open http://127.0.0.1:7860 in your browser.

    gradio gerd/frontends/qa_frontend.py\n# Some Llama.cpp outut\n# ...\n# * Running on local URL:  http://127.0.0.1:7860\n

    Click the 'Click to Upload' button and search for a GRASCCO document named Caja.txt which is located in the tests/data/grascoo folder and upload it into the vector store. Next, you can query information from the document. For instance Wie hei\u00dft der Patient? (What is the patient called?).

    "},{"location":"#prompt-chaining","title":"Prompt Chaining","text":"

    Prompt chaining is a prompt engineering approach to increase the 'reflection' of a large language model onto its given answer. Check examples/chaining.py for an illustration.

    python examples/chaining.py\n# ...\n====== Resolved prompt =====\n\nsystem: You are a helpful assistant. Please answer the following question in a truthful and brief manner.\nuser: What type of mammal lays the biggest eggs?\n\n# ...\nResult: Based on the given information, the largest egg-laying mammal is the blue whale, which can lay up to 100 million eggs per year. However, the other assertions provided do not align with this information.\n
    Source: examples/chaining.py
    \"\"\"A chaining example for the chat service.\"\"\"\n\nimport logging\n\nfrom gerd.config import load_gen_config\nfrom gerd.gen.chat_service import ChatService\n\nlogging.basicConfig(level=logging.DEBUG)\n\ngen = ChatService(load_gen_config(\"gen_chaining\"))\nres = gen.generate({\"question\": \"What type of mammal lays the biggest eggs?\"})\nprint(f\"Result: {res.text}\")  # noqa: T201\n
    Config: config/gen_chaining.yml
    model:\n  name: \"Qwen/Qwen2.5-0.5B-Instruct\"\n  temperature: 0.05\n  max_new_tokens: 512\n  context_length: 2048\n  prompt_setup:\n    - [\"system\", text: \"You are a helpful assistant. Please answer the following question in a truthful and brief manner.\"]\nfeatures:\n  prompt_chaining:\n    prompts:\n      - text: \"{question}\"\n      - text: \"Here is a statement:\\n{response_1}\\n\\nMake a bullet point list of the assumptions you made when producing the above statement.\\n\\n\"\n      - text: \"Here is a bullet point list of assertions:\\n{response_2}\\n\\nFor each assertion, determine whether it is true or false. If it is false, explain why.\\n\\n\"\n      - text: \"{response_3}\\n\\nIn light of the above facts, how would you answer the question '{question}'\"\n

    As you see, the answer does not make much sense with the default model which is rather small. Give it a try with meta-llama/Llama-3.2-3B. To use this model, you need to login with the huggingface cli and accept the Meta Community License Agreement.

    "},{"location":"#full-documentation","title":"Full Documentation","text":"

    A more detailled documentation can be found here \ud83d\udc49 .

    "},{"location":"#used-tools","title":"Used Tools","text":"
    • LangChain: Framework for developing applications powered by language models
    • C Transformers: Python bindings for the Transformer models implemented in C/C++ using GGML library
    • FAISS: Open-source library for efficient similarity search and clustering of dense vectors.
    • Sentence-Transformers (all-MiniLM-L6-v2): Open-source pre-trained transformer model for embedding text to a 384-dimensional dense vector space for tasks like
    • Poetry: Tool for dependency management and Python packaging
    "},{"location":"#files-and-content","title":"Files and Content","text":"
    • /assets: Images relevant to the project
    • /config: Configuration files for LLM applications
    • /examples: Examples that demonstrate the different usage scenarios
    • /gerd: Code related to GERD
    • /images: Images for the documentation
    • /models: Binary file of GGML quantized LLM model (i.e., Llama-2-7B-Chat)
    • /prompts: Plain text prompt files
    • /templates: Prompt files as jinja2 templates
    • /tests: Unit tests for GERD
    • /vectorstore: FAISS vector store for documents
    • pyproject.toml: TOML file to specify which versions of the dependencies used (Poetry)
    "},{"location":"#references","title":"References","text":"
    • https://github.com/kennethleungty/Llama-2-Open-Source-LLM-CPU-Inference
    • https://pubmed.ncbi.nlm.nih.gov/36073490
    • https://huggingface.co
    "},{"location":"concepts/","title":"Concepts","text":"

    GERD is primarly a tool for prototyping workflows for working with Large Language Models. It is meant to act as 'glue' between different tools and services and should ease the access to these tools.

    In general, there should be only be two components involved in a GERD workflow: A configuration and a service. The configuration can be assembled from different sources and should be able to be used in different services. The foundation of such a configration is a YAML file. GERD provides a set of those which can be found in the config directory. Configurations in this directly can be accessed by name as shown in the following example. A simple configutation file may look like this:

    model:\n  name: \"Qwen/Qwen2.5-0.5B-Instruct\"\n  temperature: 0.1\n  top_p: 0.90\n  max_new_tokens: 256\n  repetition_penalty: 1.1\n  prompt_setup:\n    - [system, text: \"You are a helpful assistant. Start your answers with 'Sure thing, buddy!'.\"]\n  prompt_config:\n    text: \"Please say the word '{word}' three times.\"\n

    And can be used with a ChatService via load_gen_config shown in the following example:

    \"\"\"A 'hello world' example for GERD.\"\"\"\n\nimport logging\n\nfrom gerd.config import load_gen_config\nfrom gerd.gen.chat_service import ChatService\nfrom gerd.models.model import PromptConfig\n\nlogging.basicConfig(level=logging.WARNING)\nlogging.getLogger(\"gerd\").setLevel(logging.DEBUG)\n\nlogging.info(\n    \"Loading chat service...\"\n    \" When this is the first time you run this script, it will download the model.\"\n    \" This may take a few minutes.\"\n)\n\nchat = ChatService(load_gen_config(\"hello\"))\nres = chat.submit_user_message({\"word\": \"teleportation\"})\nlogging.info(res)\n\nchat.set_prompt_config(PromptConfig.model_validate({\"text\": \"{message}\"}))\nres = chat.submit_user_message({\"message\": \"Hello! What is one plus one?\"})\nlogging.info(res)\n
    "},{"location":"develop/","title":"Development Guide","text":""},{"location":"develop/#basics","title":"Basics","text":"

    To get started on development you need to install uv. You can use pip, pipx or conda to do so:

    pip install uv\n

    Next install the package and all dependencies with uv sync.

    # cd <gerd_project_root>\nuv sync\n

    After that, it should be possible to run scripts without further issues:

    uv run examples/hello.py\n

    To add a new runtime dependency, just run uv add:

    uv add langchain\n

    To add a new development dependency, run uv add with the --dev flag:

    uv add mypy --dev\n
    "},{"location":"develop/#pre-commit-hooks-recommended","title":"Pre-commit hooks (recommended)","text":"

    Pre-commit hooks are used to check linting and run tests before commit changes to prevent faulty commits. Thus, it is recommended to use these hooks! Hooks should not include long running actions (such as tests) since committing should be fast. To install pre-commit hooks, execute this once:

    uv run pre-commit install\n
    "},{"location":"develop/#further-tools","title":"Further tools","text":""},{"location":"develop/#poe-task-runner","title":"Poe Task Runner","text":"

    Task runner configuration are stored in the pyproject.toml file. You can run most of the tools mentioned above with a (shorter) call to poe.

    uv run poe lint  # do some linting (with mypy)\n
    "},{"location":"develop/#pytest","title":"PyTest","text":"

    Test case are run via pytest. Tests can be found in the /tests folder. Tests will not be run via pre-commit since they might be too complex to be done before commits. To run the standard set of tests use the poe task test:

    uv run poe test\n

    More excessive testing can be trigger with test_manual which will NOT mock calls to the used models:

    uv run poe test_manual\n
    "},{"location":"develop/#ruff","title":"Ruff","text":"

    Ruff is used for linting and code formatting. Ruff follows black styling ref. Ruff will be run automatically before commits when pre-commit hooks are installed. To run ruff manually, use uv:

    uv run ruff check gerd\n

    There is a VSCode extension that handles formatting and linting.

    "},{"location":"develop/#mypy","title":"MyPy","text":"

    MyPy does static type checking. It will not be run automatically. To run MyPy manually use uv with the folder to be checked:

    uv run mypy gerd\n
    "},{"location":"develop/#implemented-guis","title":"Implemented GUIs","text":""},{"location":"develop/#run-frontend","title":"Run Frontend","text":"

    Either run Generate Frontend:

    uv run poe gen_dev\n

    or QA Frontend:

    uv run poe qa_dev\n

    or the GERD Router:

    # add _dev for the gradio live reload version\n# omit in 'prod'\nuv run router[_dev]\n
    "},{"location":"develop/#cicd-and-distribution","title":"CI/CD and Distribution","text":""},{"location":"develop/#github-actions","title":"GitHub Actions","text":"

    GitHub Actions can be found under .github/workflows. There is currently one main CI workflow called python-ci.yml:

    # This workflow will install Python dependencies, run tests and lint with a variety of Python versions\n# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions\n\nname: Linting/Testing Python\n\non:\n  push:\n    branches: [main, dev-gha]\n  pull_request:\n    branches: [main]\n\njobs:\n  linting:\n    runs-on: ubuntu-latest\n    steps:\n      - uses: actions/checkout@v4\n      - name: Set up Python 3.12\n        uses: actions/setup-python@v5\n        with:\n          python-version: \"3.12\"\n      - name: Install uv and development dependencies with extras\n        run: |\n          pipx install uv \n          uv sync\n      - name: Run linting\n        run: uv run poe lint\n  testing:\n    needs: linting\n    strategy:\n      matrix:\n        os: [ubuntu-latest, windows-latest]\n        python-version: [\"3.12\", \"3.10\"]\n    runs-on: ${{ matrix.os }}\n    steps:\n      - uses: actions/checkout@v4\n      - name: Cache HuggingFace models and data\n        uses: actions/cache@v4\n        with:\n          path: ~/.cache/huggingface\n          key: ${{ runner.os }}-hf\n      - name: Install uv\n        run: pipx install uv\n      - name: Set up Python ${{ matrix.python-version }}\n        uses: actions/setup-python@v5\n        with:\n          python-version: ${{ matrix.python-version }}\n      - name: Install development dependencies with extras\n        run: uv sync\n      - name: Run Tests\n        run: uv run poe cov\n      - name: Report Coverage\n        run: uv run coverage report --skip-covered --omit=\"tests/*\" --format=markdown >> $GITHUB_STEP_SUMMARY\n

    In its current config it will only be executed when a PR for main is created or when a special dev-gha branch is created. It will also trigger actions when commits are pushed to main directly but this should be avoided.

    "},{"location":"develop/#github-issue-templates","title":"GitHub Issue Templates","text":"

    This project uses GitHub issue templates. Currently, there are three templates available.

    "},{"location":"develop/#bug-report","title":"Bug Report","text":"
    name: Bug\ndescription: File a bug report\ntitle: \"[Bug]: \"\nlabels: [\"bug\"]\n# assignees:\n#   - aleneum\nbody:\n  - type: markdown\n    attributes:\n      value: |\n        Thanks for taking the time to fill out this bug report!\n  - type: textarea\n    id: expected-behavior\n    attributes:\n      label: Expected Behavior\n      description: What should happen\n    validations:\n      required: true\n  - type: textarea\n    id: actual-behavior\n    attributes:\n      label: Actual Behavior\n      description: What happened instead\n    validations:\n      required: true\n  - type: textarea\n    id: steps-reproduce\n    attributes:\n      label: Steps to Reproduce the Problem\n      value: |\n        1.\n        2.\n        3.\n\n    validations:\n      required: true\n  - type: input\n    id: affected-version\n    attributes:\n      label: Affected version\n      description: Version number or commit hash\n      placeholder: \"0.1.0\"\n    validations:\n      required: true\n  - type: dropdown\n    id: affected-components\n    attributes:\n      label: Affected components\n      description: Which components are affected\n      multiple: true\n      options:\n        - Gen Service\n        - Qa Service\n        - Frontend\n        - Backend\n    validations:\n      required: false\n  - type: dropdown\n    id: affected-plattforms\n    attributes:\n      label: Affected plattforms\n      description: Which plattforms are affected\n      multiple: true\n      options:\n        - Windows\n        - Linux\n        - MacOS\n    validations:\n      required: false\n  - type: textarea\n    id: further-information\n    attributes:\n      label: Further contextual information and suggestions\n      description: More detailed descriptions or possible solutions\n    validations:\n      required: false\n
    "},{"location":"develop/#feature-request","title":"Feature Request","text":"
    name: Feature Request\ndescription: Suggest an idea for this project\ntitle: \"[FR]: \"\nlabels: [\"enhancement\"]\n# assignees:\n#   - aleneum\nbody:\n  - type: markdown\n    attributes:\n      value: Do you have an idea for a feature that would make this project more helpful or easier to use?\n  - type: textarea\n    id: problem-description\n    attributes:\n      label: Problem description\n      description: Is your feature request related to a problem? Please describe.\n    validations:\n      required: true\n  - type: textarea\n    id: solution\n    attributes:\n      label: Solution\n      description: A clear and concise description of what you want to happen.\n    validations:\n      required: true\n  - type: textarea\n    id: additional-context\n    attributes:\n      label: Additional context\n      description: Add any other context or screenshots about the feature request here.\n    validations:\n      required: false\n  - type: dropdown\n    id: affected-components\n    attributes:\n      label: Affected components\n      description: Which components are affected\n      multiple: true\n      options:\n        - Gen Service\n        - Qa Service\n        - Frontend\n        - Backend\n    validations:\n      required: false\n
    "},{"location":"develop/#use-case","title":"Use Case","text":"
    name: Use Case\ndescription: A description of a user-centered system requirement\ntitle: \"[UC]: \"\nlabels: [\"use case\"]\n# assignees:\n#   - aleneum\nbody:\n  - type: textarea\n    id: summary\n    attributes:\n      label: Summary\n      description: A concise description of the use case\n    validations:\n      required: true\n  - type: textarea\n    id: rationale\n    attributes:\n      label: Rationale\n      description: The motivation and value added by the described use case\n    validations:\n      required: true\n  - type: dropdown\n    id: level\n    attributes:\n      label: Level\n      multiple: false\n      options:\n        - user goal\n        - subfunction\n  - type: input\n    id: actors\n    attributes:\n      label: Actors\n      description: Someone or something with behavior, e.g. a person (identified by role), a computer system, or an organization\n    validations:\n      required: true\n  - type: textarea\n    id: preconditions\n    attributes:\n      label: Preconditions\n      description: What needs to be true on start\n      placeholder: |\n        - Use Cases: #1, #2\n        - User is authenticated\n    validations:\n      required: true\n  - type: textarea\n    id: postconditions\n    attributes:\n      label: Postconditions\n      description: What must be true on successful completion\n      placeholder: \"The form \"\n    validations:\n      required: true\n  - type: textarea\n    id: basic-flow\n    attributes:\n      label: Basic Flow\n      description: A typical, unconditional happy path\n      value: |\n        1.\n        2.\n        3.\n\n    validations:\n      required: true\n  - type: textarea\n    id: alternative-paths\n    attributes:\n      label: Alternative Paths\n      description: Alternate scenarios of success or failure\n      placeholder: |\n        2.1\n        2.2\n        3.1\n\n    validations:\n      required: false\n  - type: textarea\n    id: visualisation\n    attributes:\n      label: Visualisation\n      description: A mermaid diagram or image depiciting the action flow\n      value: |\n        ```mermaid\n        flowchart LR;\n          1-->2\n          2-->3\n          3-->4\n          3-->3.1\n          3.1-->3.2\n          3.2-->2\n          4-->4.1\n          4.1-->4\n        ```\n    validations:\n      required: false\n  - type: textarea\n    id: related-issues\n    attributes:\n      label: Other related issues, use cases, features\n      description: What must be true on successful completion\n      placeholder: \"#1 #2 #3\"\n    validations:\n      required: false\n
    "},{"location":"reference/gerd/","title":"gerd","text":""},{"location":"reference/gerd/#gerd","title":"gerd","text":"

    Generating and evaluating relevant documentation (GERD).

    This package provides the GERD system for working with large language models (LLMs). This includes means to generate texts using different backends and frontends. The system is designed to be flexible and extensible to support different use cases. It can also be used for Retrieval Augmented Generation (RAG) tasks or as a chatbot.

    Modules:

    Name Description backends

    This module contains backend implementations that manage services.

    config

    Configuration for the application.

    features

    Special features to extend the functionality of GERD services.

    frontends

    A collection of several gradio frontends.

    gen

    Services and utilities for text generation with LLMs.

    loader

    Module for loading language models.

    models

    Pydantic model definitions and data classes that are share accross modules.

    qa

    Services and utilities for retrieval augmented generation (RAG).

    rag

    Retrieval-Augmented Generation (RAG) backend.

    training

    Collections of training routines for GERD.

    transport

    Module to define the transport protocol.

    "},{"location":"reference/gerd/backends/","title":"gerd.backends","text":""},{"location":"reference/gerd/backends/#gerd.backends","title":"gerd.backends","text":"

    This module contains backend implementations that manage services.

    These backends can be used by frontends such as gradio. Furthermore, the backend module contains service implementations for loading LLMs or vector stores for Retrieval Augmented Generation.

    Modules:

    Name Description bridge

    The Bridge connects backend and frontend services directly for local use.

    rest_client

    REST client for the GERD server.

    rest_server

    REST server as a GERD backend.

    Attributes:

    Name Type Description TRANSPORTER Transport

    The default transporter that connects the backend services to the frontend.

    "},{"location":"reference/gerd/backends/#gerd.backends.TRANSPORTER","title":"TRANSPORTER module-attribute","text":"
    TRANSPORTER: Transport = Bridge()\n

    The default transporter that connects the backend services to the frontend.

    "},{"location":"reference/gerd/backends/bridge/","title":"gerd.backends.bridge","text":""},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge","title":"gerd.backends.bridge","text":"

    The Bridge connects backend and frontend services directly for local use.

    Classes:

    Name Description Bridge

    Direct connection between backend services and frontend.

    "},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge","title":"Bridge","text":"
    Bridge()\n

    Bases: Transport

    Direct connection between backend services and frontend.

    Frontends that make use of the Transport abstraction can use Bridge to get accessto generation and QA services directly. This is useful for local use cases where the frontend and backend are running in the same process.

    The services associated with the bridge are initialized lazily.

    Methods:

    Name Description add_file

    Add a file to the vector store.

    analyze_mult_prompts_query

    Queries the vector store with a set of predefined queries.

    analyze_query

    Queries the vector store with a predefined query.

    db_embedding

    Converts a question to an embedding.

    db_query

    Queries the vector store with a question.

    generate

    Generates text with the generation service.

    get_gen_prompt

    Gets the prompt configuration for the generation service.

    get_qa_prompt

    Gets the prompt configuration for a mode of the QA service.

    qa_query

    Query the QA service with a question.

    remove_file

    Remove a file from the vector store.

    set_gen_prompt

    Sets the prompt configuration for the generation service.

    set_qa_prompt

    Sets the prompt configuration for the QA service.

    Attributes:

    Name Type Description gen GenerationService

    Get the generation service instance.

    qa QAService

    Get the QA service instance. It will be created if it does not exist.

    Source code in gerd/backends/bridge.py
    def __init__(self) -> None:\n    \"\"\"The services associated with the bridge are initialized lazily.\"\"\"\n    super().__init__()\n    self._qa: Optional[QAService] = None\n    self._gen: Optional[GenerationService] = None\n
    "},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge.gen","title":"gen property","text":"
    gen: GenerationService\n

    Get the generation service instance.

    It will be created if it does not exist.

    "},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge.qa","title":"qa property","text":"
    qa: QAService\n

    Get the QA service instance. It will be created if it does not exist.

    "},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge.add_file","title":"add_file","text":"
    add_file(file: QAFileUpload) -> QAAnswer\n

    Add a file to the vector store.

    The returned answer has a status code of 200 if the file was added successfully. Parameters: file: The file to add to the vector store.

    Returns:

    Type Description QAAnswer

    The answer from the QA service

    Source code in gerd/backends/bridge.py
    @override\ndef add_file(self, file: QAFileUpload) -> QAAnswer:\n    return self.qa.add_file(file)\n
    "},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge.analyze_mult_prompts_query","title":"analyze_mult_prompts_query","text":"
    analyze_mult_prompts_query() -> QAAnalyzeAnswer\n

    Queries the vector store with a set of predefined queries.

    In contrast to analyze_query, this method queries the vector store with multiple prompts.

    Returns:

    Type Description QAAnalyzeAnswer

    The answer from the QA service.

    Source code in gerd/backends/bridge.py
    @override\ndef analyze_mult_prompts_query(self) -> QAAnalyzeAnswer:\n    return self.qa.analyze_mult_prompts_query()\n
    "},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge.analyze_query","title":"analyze_query","text":"
    analyze_query() -> QAAnalyzeAnswer\n

    Queries the vector store with a predefined query.

    The query should return vital information gathered from letters of discharge.

    Returns:

    Type Description QAAnalyzeAnswer

    The answer from the QA service.

    Source code in gerd/backends/bridge.py
    @override\ndef analyze_query(self) -> QAAnalyzeAnswer:\n    return self.qa.analyze_query()\n
    "},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge.db_embedding","title":"db_embedding","text":"
    db_embedding(question: QAQuestion) -> List[float]\n

    Converts a question to an embedding.

    The embedding is defined by the vector store.

    Parameters:

    Name Type Description Default QAQuestion

    The question to convert to an embedding.

    required

    Returns:

    Type Description List[float]

    The embedding of the question

    Source code in gerd/backends/bridge.py
    @override\ndef db_embedding(self, question: QAQuestion) -> List[float]:\n    return self.qa.db_embedding(question)\n
    "},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge.db_embedding(question)","title":"question","text":""},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge.db_query","title":"db_query","text":"
    db_query(question: QAQuestion) -> List[DocumentSource]\n

    Queries the vector store with a question.

    Parameters:

    Name Type Description Default QAQuestion

    The question to query the vector store with.

    required

    Returns:

    Type Description List[DocumentSource]

    A list of document sources

    Source code in gerd/backends/bridge.py
    @override\ndef db_query(self, question: QAQuestion) -> List[DocumentSource]:\n    return self.qa.db_query(question)\n
    "},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge.db_query(question)","title":"question","text":""},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge.generate","title":"generate","text":"
    generate(parameters: Dict[str, str]) -> GenResponse\n

    Generates text with the generation service.

    Parameters:

    Name Type Description Default Dict[str, str]

    The parameters to generate text with

    required

    Returns:

    Type Description GenResponse

    The generation result

    Source code in gerd/backends/bridge.py
    @override\ndef generate(self, parameters: Dict[str, str]) -> GenResponse:\n    return self.gen.generate(parameters)\n
    "},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge.generate(parameters)","title":"parameters","text":""},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge.get_gen_prompt","title":"get_gen_prompt","text":"
    get_gen_prompt() -> PromptConfig\n

    Gets the prompt configuration for the generation service.

    Returns:

    Type Description PromptConfig

    The current prompt configuration

    Source code in gerd/backends/bridge.py
    @override\ndef get_gen_prompt(self) -> PromptConfig:\n    return self.gen.get_prompt_config()\n
    "},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge.get_qa_prompt","title":"get_qa_prompt","text":"
    get_qa_prompt(qa_mode: QAModesEnum) -> PromptConfig\n

    Gets the prompt configuration for a mode of the QA service.

    Parameters:

    Name Type Description Default QAModesEnum

    The mode to get the prompt configuration for

    required

    Returns:

    Type Description PromptConfig

    The prompt configuration for the QA service

    Source code in gerd/backends/bridge.py
    @override\ndef get_qa_prompt(self, qa_mode: QAModesEnum) -> PromptConfig:\n    return self.qa.get_prompt_config(qa_mode)\n
    "},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge.get_qa_prompt(qa_mode)","title":"qa_mode","text":""},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge.qa_query","title":"qa_query","text":"
    qa_query(question: QAQuestion) -> QAAnswer\n

    Query the QA service with a question.

    Parameters:

    Name Type Description Default QAQuestion

    The question to query the QA service with.

    required

    Returns:

    Type Description QAAnswer

    The answer from the QA service.

    Source code in gerd/backends/bridge.py
    @override\ndef qa_query(self, question: QAQuestion) -> QAAnswer:\n    return self.qa.query(question)\n
    "},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge.qa_query(query)","title":"query","text":""},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge.remove_file","title":"remove_file","text":"
    remove_file(file_name: str) -> QAAnswer\n

    Remove a file from the vector store.

    The returned answer has a status code of 200 if the file was removed successfully. Parameters: file_name: The name of the file to remove from the vector store.

    Returns:

    Type Description QAAnswer

    The answer from the QA service

    Source code in gerd/backends/bridge.py
    @override\ndef remove_file(self, file_name: str) -> QAAnswer:\n    return self.qa.remove_file(file_name)\n
    "},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge.set_gen_prompt","title":"set_gen_prompt","text":"
    set_gen_prompt(config: PromptConfig) -> PromptConfig\n

    Sets the prompt configuration for the generation service.

    The prompt configuration that is returned should in most cases be the same as the one that was set. Parameters: config: The prompt configuration to set

    Returns:

    Type Description PromptConfig

    The prompt configuration that was set

    Source code in gerd/backends/bridge.py
    @override\ndef set_gen_prompt(self, config: PromptConfig) -> PromptConfig:\n    return self.gen.set_prompt_config(config)\n
    "},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge.set_qa_prompt","title":"set_qa_prompt","text":"
    set_qa_prompt(config: PromptConfig, qa_mode: QAModesEnum) -> QAAnswer\n

    Sets the prompt configuration for the QA service.

    Since the QA service uses multiple prompt configurations, the mode should be specified. For more details, see the documentation of QAService.set_prompt_config.

    Parameters:

    Name Type Description Default PromptConfig

    The prompt configuration to set

    required QAModesEnum

    The mode to set the prompt configuration for

    required

    Returns:

    Type Description QAAnswer

    The answer from the QA service

    Source code in gerd/backends/bridge.py
    @override\ndef set_qa_prompt(self, config: PromptConfig, qa_mode: QAModesEnum) -> QAAnswer:\n    return self.qa.set_prompt_config(config, qa_mode)\n
    "},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge.set_qa_prompt(config)","title":"config","text":""},{"location":"reference/gerd/backends/bridge/#gerd.backends.bridge.Bridge.set_qa_prompt(qa_mode)","title":"qa_mode","text":""},{"location":"reference/gerd/backends/rest_client/","title":"gerd.backends.rest_client","text":""},{"location":"reference/gerd/backends/rest_client/#gerd.backends.rest_client","title":"gerd.backends.rest_client","text":"

    REST client for the GERD server.

    Classes:

    Name Description RestClient

    REST client for the GERD server.

    "},{"location":"reference/gerd/backends/rest_client/#gerd.backends.rest_client.RestClient","title":"RestClient","text":"
    RestClient()\n

    Bases: Transport

    REST client for the GERD server.

    The client initalizes the server URL.

    It is retrieved from the global CONFIG. Other (timeout) settings are also set here but not configurable as of now.

    Methods:

    Name Description add_file

    Add a file to the vector store.

    analyze_mult_prompts_query

    Queries the vector store with a set of predefined queries.

    analyze_query

    Queries the vector store with a predefined query.

    db_embedding

    Converts a question to an embedding.

    db_query

    Queries the vector store with a question.

    generate

    Generates text with the generation service.

    get_gen_prompt

    Gets the prompt configuration for the generation service.

    get_qa_prompt

    Gets the prompt configuration for a mode of the QA service.

    qa_query

    Query the QA service with a question.

    remove_file

    Remove a file from the vector store.

    set_gen_prompt

    Sets the prompt configuration for the generation service.

    set_qa_prompt

    Sets the prompt configuration for the QA service.

    Source code in gerd/backends/rest_client.py
    def __init__(self) -> None:\n    \"\"\"The client initalizes the server URL.\n\n    It is retrieved from the global [CONFIG][gerd.config.CONFIG].\n    Other (timeout) settings are also set here but not configurable as of now.\n    \"\"\"\n    super().__init__()\n    self._url = f\"http://{CONFIG.server.host}:{CONFIG.server.port}{CONFIG.server.api_prefix}\"\n    self.timeout = 10\n    self.longtimeout = 10000\n
    "},{"location":"reference/gerd/backends/rest_client/#gerd.backends.rest_client.RestClient.add_file","title":"add_file","text":"
    add_file(file: QAFileUpload) -> QAAnswer\n

    Add a file to the vector store.

    The returned answer has a status code of 200 if the file was added successfully. Parameters: file: The file to add to the vector store.

    Returns:

    Type Description QAAnswer

    The answer from the QA service

    Source code in gerd/backends/rest_client.py
    @override\ndef add_file(self, file: QAFileUpload) -> QAAnswer:\n    t = file.model_dump_json().encode(\"utf-8\")\n    return QAAnswer.model_validate(\n        requests.post(\n            f\"{self._url}/qa/file\",\n            data=file.model_dump_json().encode(\"utf-8\"),\n            timeout=self.timeout,\n        ).json()\n    )\n
    "},{"location":"reference/gerd/backends/rest_client/#gerd.backends.rest_client.RestClient.analyze_mult_prompts_query","title":"analyze_mult_prompts_query","text":"
    analyze_mult_prompts_query() -> QAAnalyzeAnswer\n

    Queries the vector store with a set of predefined queries.

    In contrast to analyze_query, this method queries the vector store with multiple prompts.

    Returns:

    Type Description QAAnalyzeAnswer

    The answer from the QA service.

    Source code in gerd/backends/rest_client.py
    @override\ndef analyze_mult_prompts_query(self) -> QAAnalyzeAnswer:\n    return QAAnalyzeAnswer.model_validate(\n        requests.post(\n            f\"{self._url}/qa/query_analyze_mult_prompt\",\n            timeout=self.longtimeout,\n        ).json()\n    )\n
    "},{"location":"reference/gerd/backends/rest_client/#gerd.backends.rest_client.RestClient.analyze_query","title":"analyze_query","text":"
    analyze_query() -> QAAnalyzeAnswer\n

    Queries the vector store with a predefined query.

    The query should return vital information gathered from letters of discharge.

    Returns:

    Type Description QAAnalyzeAnswer

    The answer from the QA service.

    Source code in gerd/backends/rest_client.py
    @override\ndef analyze_query(self) -> QAAnalyzeAnswer:\n    return QAAnalyzeAnswer.model_validate(\n        requests.post(\n            f\"{self._url}/qa/query_analyze\",\n            timeout=self.longtimeout,\n        ).json()\n    )\n
    "},{"location":"reference/gerd/backends/rest_client/#gerd.backends.rest_client.RestClient.db_embedding","title":"db_embedding","text":"
    db_embedding(question: QAQuestion) -> List[float]\n

    Converts a question to an embedding.

    The embedding is defined by the vector store.

    Parameters:

    Name Type Description Default QAQuestion

    The question to convert to an embedding.

    required

    Returns:

    Type Description List[float]

    The embedding of the question

    Source code in gerd/backends/rest_client.py
    @override\ndef db_embedding(self, question: QAQuestion) -> List[float]:\n    request = question.model_dump_json()\n    _LOGGER.debug(\"db_embedding - request: %s\", request)\n    response = requests.post(\n        f\"{self._url}/qa/db_embedding\",\n        data=question.model_dump_json().encode(\"utf-8\"),\n        timeout=self.timeout,\n    )\n    _LOGGER.debug(\"db_embedding - response: %s\", response.json())\n    return TypeAdapter(List[float]).validate_python(response.json())\n
    "},{"location":"reference/gerd/backends/rest_client/#gerd.backends.rest_client.RestClient.db_embedding(question)","title":"question","text":""},{"location":"reference/gerd/backends/rest_client/#gerd.backends.rest_client.RestClient.db_query","title":"db_query","text":"
    db_query(question: QAQuestion) -> List[DocumentSource]\n

    Queries the vector store with a question.

    Parameters:

    Name Type Description Default QAQuestion

    The question to query the vector store with.

    required

    Returns:

    Type Description List[DocumentSource]

    A list of document sources

    Source code in gerd/backends/rest_client.py
    @override\ndef db_query(self, question: QAQuestion) -> List[DocumentSource]:\n    request = question.model_dump_json()\n    _LOGGER.debug(\"db_query - request: %s\", request)\n    response = requests.post(\n        f\"{self._url}/qa/db_query\",\n        data=question.model_dump_json(),\n        timeout=self.timeout,\n    )\n    _LOGGER.debug(\"db_query - response: %s\", response.json())\n    return TypeAdapter(List[DocumentSource]).validate_python(response.json())\n
    "},{"location":"reference/gerd/backends/rest_client/#gerd.backends.rest_client.RestClient.db_query(question)","title":"question","text":""},{"location":"reference/gerd/backends/rest_client/#gerd.backends.rest_client.RestClient.generate","title":"generate","text":"
    generate(parameters: Dict[str, str]) -> GenResponse\n

    Generates text with the generation service.

    Parameters:

    Name Type Description Default Dict[str, str]

    The parameters to generate text with

    required

    Returns:

    Type Description GenResponse

    The generation result

    Source code in gerd/backends/rest_client.py
    @override\ndef generate(self, parameters: Dict[str, str]) -> GenResponse:\n    return GenResponse.model_validate(\n        requests.post(\n            f\"{self._url}/gen/generate\",\n            json=parameters,\n            timeout=self.timeout,\n        ).json()\n    )\n
    "},{"location":"reference/gerd/backends/rest_client/#gerd.backends.rest_client.RestClient.generate(parameters)","title":"parameters","text":""},{"location":"reference/gerd/backends/rest_client/#gerd.backends.rest_client.RestClient.get_gen_prompt","title":"get_gen_prompt","text":"
    get_gen_prompt() -> PromptConfig\n

    Gets the prompt configuration for the generation service.

    Returns:

    Type Description PromptConfig

    The current prompt configuration

    Source code in gerd/backends/rest_client.py
    @override\ndef get_gen_prompt(self) -> PromptConfig:\n    return PromptConfig.model_validate(\n        requests.get(f\"{self._url}/gen/prompt\", timeout=self.timeout).json()\n    )\n
    "},{"location":"reference/gerd/backends/rest_client/#gerd.backends.rest_client.RestClient.get_qa_prompt","title":"get_qa_prompt","text":"
    get_qa_prompt(qa_mode: QAModesEnum) -> PromptConfig\n

    Gets the prompt configuration for a mode of the QA service.

    Parameters:

    Name Type Description Default QAModesEnum

    The mode to get the prompt configuration for

    required

    Returns:

    Type Description PromptConfig

    The prompt configuration for the QA service

    Source code in gerd/backends/rest_client.py
    @override\ndef get_qa_prompt(self, qa_mode: QAModesEnum) -> PromptConfig:\n    return PromptConfig.model_validate(\n        requests.get(\n            f\"{self._url}/qa/prompt\",\n            timeout=self.timeout,\n            params={\"qa_mode\": qa_mode.value},\n        ).json()\n    )\n
    "},{"location":"reference/gerd/backends/rest_client/#gerd.backends.rest_client.RestClient.get_qa_prompt(qa_mode)","title":"qa_mode","text":""},{"location":"reference/gerd/backends/rest_client/#gerd.backends.rest_client.RestClient.qa_query","title":"qa_query","text":"
    qa_query(question: QAQuestion) -> QAAnswer\n

    Query the QA service with a question.

    Parameters:

    Name Type Description Default QAQuestion

    The question to query the QA service with.

    required

    Returns:

    Type Description QAAnswer

    The answer from the QA service.

    Source code in gerd/backends/rest_client.py
    @override\ndef qa_query(self, question: QAQuestion) -> QAAnswer:\n    return QAAnswer.model_validate(\n        requests.post(\n            f\"{self._url}/qa/query\",\n            data=question.model_dump_json().encode(\"utf-8\"),\n            timeout=self.longtimeout,\n        ).json()\n    )\n
    "},{"location":"reference/gerd/backends/rest_client/#gerd.backends.rest_client.RestClient.qa_query(query)","title":"query","text":""},{"location":"reference/gerd/backends/rest_client/#gerd.backends.rest_client.RestClient.remove_file","title":"remove_file","text":"
    remove_file(file_name: str) -> QAAnswer\n

    Remove a file from the vector store.

    The returned answer has a status code of 200 if the file was removed successfully. Parameters: file_name: The name of the file to remove from the vector store.

    Returns:

    Type Description QAAnswer

    The answer from the QA service

    Source code in gerd/backends/rest_client.py
    @override\ndef remove_file(self, file_name: str) -> QAAnswer:\n    return QAAnswer.model_validate(\n        requests.delete(\n            f\"{self._url}/qa/file\",\n            data=file_name.encode(\"utf-8\"),\n            timeout=self.timeout,\n        ).json()\n    )\n
    "},{"location":"reference/gerd/backends/rest_client/#gerd.backends.rest_client.RestClient.set_gen_prompt","title":"set_gen_prompt","text":"
    set_gen_prompt(config: PromptConfig) -> PromptConfig\n

    Sets the prompt configuration for the generation service.

    The prompt configuration that is returned should in most cases be the same as the one that was set. Parameters: config: The prompt configuration to set

    Returns:

    Type Description PromptConfig

    The prompt configuration that was set

    Source code in gerd/backends/rest_client.py
    @override\ndef set_gen_prompt(self, config: PromptConfig) -> PromptConfig:\n    return PromptConfig.model_validate(\n        requests.post(\n            f\"{self._url}/gen/prompt\",\n            data=config.model_dump_json(),\n            timeout=self.timeout,\n        ).json()\n    )\n
    "},{"location":"reference/gerd/backends/rest_client/#gerd.backends.rest_client.RestClient.set_qa_prompt","title":"set_qa_prompt","text":"
    set_qa_prompt(config: PromptConfig, qa_mode: QAModesEnum) -> QAAnswer\n

    Sets the prompt configuration for the QA service.

    Since the QA service uses multiple prompt configurations, the mode should be specified. For more details, see the documentation of QAService.set_prompt_config.

    Parameters:

    Name Type Description Default PromptConfig

    The prompt configuration to set

    required QAModesEnum

    The mode to set the prompt configuration for

    required

    Returns:

    Type Description QAAnswer

    The answer from the QA service

    Source code in gerd/backends/rest_client.py
    @override\ndef set_qa_prompt(self, config: PromptConfig, qa_mode: QAModesEnum) -> QAAnswer:\n    return QAAnswer.model_validate(\n        requests.post(\n            f\"{self._url}/qa/prompt\",\n            data=QAPromptConfig(config=config, mode=qa_mode).model_dump_json(),\n            timeout=self.timeout,\n        ).json()\n    )\n
    "},{"location":"reference/gerd/backends/rest_client/#gerd.backends.rest_client.RestClient.set_qa_prompt(config)","title":"config","text":""},{"location":"reference/gerd/backends/rest_client/#gerd.backends.rest_client.RestClient.set_qa_prompt(qa_mode)","title":"qa_mode","text":""},{"location":"reference/gerd/backends/rest_server/","title":"gerd.backends.rest_server","text":""},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server","title":"gerd.backends.rest_server","text":"

    REST server as a GERD backend.

    Classes:

    Name Description RestServer

    REST server as a GERD backend.

    "},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer","title":"RestServer","text":"
    RestServer()\n

    Bases: Transport

    REST server as a GERD backend.

    The REST server initializes a private bridge and an API router.

    The API router is used to define the endpoints for the REST server.

    Methods:

    Name Description add_file

    Add a file to the vector store.

    analyze_mult_prompts_query

    Queries the vector store with a set of predefined queries.

    analyze_query

    Queries the vector store with a predefined query.

    db_embedding

    Converts a question to an embedding.

    db_query

    Queries the vector store with a question.

    generate

    Generates text with the generation service.

    get_gen_prompt

    Gets the prompt configuration for the generation service.

    get_qa_prompt

    Gets the prompt configuration for a mode of the QA service.

    get_qa_prompt_rest

    Get the QA prompt configuration.

    qa_query

    Query the QA service with a question.

    remove_file

    Remove a file from the vector store.

    set_gen_prompt

    Sets the prompt configuration for the generation service.

    set_qa_prompt

    Sets the prompt configuration for the QA service.

    set_qa_prompt_rest

    Set the QA prompt configuration.

    Source code in gerd/backends/rest_server.py
    def __init__(self) -> None:\n    \"\"\"The REST server initializes a private bridge and an API router.\n\n    The API router is used to define the endpoints for the REST server.\n    \"\"\"\n    super().__init__()\n\n    prefix = CONFIG.server.api_prefix\n    self._bridge = Bridge()\n    self.router = APIRouter()\n    self.router.add_api_route(f\"{prefix}/qa/query\", self.qa_query, methods=[\"POST\"])\n    self.router.add_api_route(\n        f\"{prefix}/qa/query_analyze\", self.analyze_query, methods=[\"POST\"]\n    )\n    self.router.add_api_route(\n        f\"{prefix}/qa/query_analyze_mult_prompt\",\n        self.analyze_mult_prompts_query,\n        methods=[\"POST\"],\n    )\n    self.router.add_api_route(\n        f\"{prefix}/qa/db_query\", self.db_query, methods=[\"POST\"]\n    )\n    self.router.add_api_route(\n        f\"{prefix}/qa/db_embedding\", self.db_embedding, methods=[\"POST\"]\n    )\n    self.router.add_api_route(f\"{prefix}/qa/file\", self.add_file, methods=[\"POST\"])\n    self.router.add_api_route(\n        f\"{prefix}/qa/file\", self.remove_file, methods=[\"DELETE\"]\n    )\n    self.router.add_api_route(\n        f\"{prefix}/gen/prompt\", self.set_gen_prompt, methods=[\"POST\"]\n    )\n    self.router.add_api_route(\n        f\"{prefix}/gen/prompt\", self.get_gen_prompt, methods=[\"GET\"]\n    )\n    self.router.add_api_route(\n        f\"{prefix}/qa/prompt\", self.set_qa_prompt_rest, methods=[\"POST\"]\n    )\n    self.router.add_api_route(\n        f\"{prefix}/qa/prompt\", self.get_qa_prompt_rest, methods=[\"GET\"]\n    )\n    self.router.add_api_route(\n        f\"{prefix}/gen/generate\", self.generate, methods=[\"POST\"]\n    )\n
    "},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer.add_file","title":"add_file","text":"
    add_file(file: QAFileUpload) -> QAAnswer\n

    Add a file to the vector store.

    The returned answer has a status code of 200 if the file was added successfully. Parameters: file: The file to add to the vector store.

    Returns:

    Type Description QAAnswer

    The answer from the QA service

    Source code in gerd/backends/rest_server.py
    @override\ndef add_file(self, file: QAFileUpload) -> QAAnswer:\n    return self._bridge.add_file(file)\n
    "},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer.analyze_mult_prompts_query","title":"analyze_mult_prompts_query","text":"
    analyze_mult_prompts_query() -> QAAnalyzeAnswer\n

    Queries the vector store with a set of predefined queries.

    In contrast to analyze_query, this method queries the vector store with multiple prompts.

    Returns:

    Type Description QAAnalyzeAnswer

    The answer from the QA service.

    Source code in gerd/backends/rest_server.py
    @override\ndef analyze_mult_prompts_query(self) -> QAAnalyzeAnswer:\n    return self._bridge.qa.analyze_mult_prompts_query()\n
    "},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer.analyze_query","title":"analyze_query","text":"
    analyze_query() -> QAAnalyzeAnswer\n

    Queries the vector store with a predefined query.

    The query should return vital information gathered from letters of discharge.

    Returns:

    Type Description QAAnalyzeAnswer

    The answer from the QA service.

    Source code in gerd/backends/rest_server.py
    @override\ndef analyze_query(self) -> QAAnalyzeAnswer:\n    return self._bridge.qa.analyze_query()\n
    "},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer.db_embedding","title":"db_embedding","text":"
    db_embedding(question: QAQuestion) -> List[float]\n

    Converts a question to an embedding.

    The embedding is defined by the vector store.

    Parameters:

    Name Type Description Default QAQuestion

    The question to convert to an embedding.

    required

    Returns:

    Type Description List[float]

    The embedding of the question

    Source code in gerd/backends/rest_server.py
    @override\ndef db_embedding(self, question: QAQuestion) -> List[float]:\n    return self._bridge.db_embedding(question)\n
    "},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer.db_embedding(question)","title":"question","text":""},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer.db_query","title":"db_query","text":"
    db_query(question: QAQuestion) -> List[DocumentSource]\n

    Queries the vector store with a question.

    Parameters:

    Name Type Description Default QAQuestion

    The question to query the vector store with.

    required

    Returns:

    Type Description List[DocumentSource]

    A list of document sources

    Source code in gerd/backends/rest_server.py
    @override\ndef db_query(self, question: QAQuestion) -> List[DocumentSource]:\n    # _LOGGER.debug(\"dq_query - request: %s\", question)\n    response = self._bridge.db_query(question)\n    # _LOGGER.debug(\"dq_query - response: %s\", response)\n    return response\n
    "},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer.db_query(question)","title":"question","text":""},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer.generate","title":"generate","text":"
    generate(parameters: Dict[str, str]) -> GenResponse\n

    Generates text with the generation service.

    Parameters:

    Name Type Description Default Dict[str, str]

    The parameters to generate text with

    required

    Returns:

    Type Description GenResponse

    The generation result

    Source code in gerd/backends/rest_server.py
    @override\ndef generate(self, parameters: Dict[str, str]) -> GenResponse:\n    return self._bridge.generate(parameters)\n
    "},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer.generate(parameters)","title":"parameters","text":""},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer.get_gen_prompt","title":"get_gen_prompt","text":"
    get_gen_prompt() -> PromptConfig\n

    Gets the prompt configuration for the generation service.

    Returns:

    Type Description PromptConfig

    The current prompt configuration

    Source code in gerd/backends/rest_server.py
    @override\ndef get_gen_prompt(self) -> PromptConfig:\n    return self._bridge.get_gen_prompt()\n
    "},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer.get_qa_prompt","title":"get_qa_prompt","text":"
    get_qa_prompt(qa_mode: QAModesEnum) -> PromptConfig\n

    Gets the prompt configuration for a mode of the QA service.

    Parameters:

    Name Type Description Default QAModesEnum

    The mode to get the prompt configuration for

    required

    Returns:

    Type Description PromptConfig

    The prompt configuration for the QA service

    Source code in gerd/backends/rest_server.py
    @override\ndef get_qa_prompt(self, qa_mode: QAModesEnum) -> PromptConfig:\n    return self._bridge.get_qa_prompt(qa_mode)\n
    "},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer.get_qa_prompt(qa_mode)","title":"qa_mode","text":""},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer.get_qa_prompt_rest","title":"get_qa_prompt_rest","text":"
    get_qa_prompt_rest(qa_mode: int) -> PromptConfig\n

    Get the QA prompt configuration.

    The call is forwarded to the bridge. Parameters: qa_mode: The QA mode Returns: The QA prompt configuration

    Source code in gerd/backends/rest_server.py
    def get_qa_prompt_rest(self, qa_mode: int) -> PromptConfig:\n    \"\"\"Get the QA prompt configuration.\n\n    The call is forwarded to the bridge.\n    Parameters:\n        qa_mode: The QA mode\n    Returns:\n        The QA prompt configuration\n    \"\"\"\n    return self._bridge.get_qa_prompt(QAModesEnum(qa_mode))\n
    "},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer.qa_query","title":"qa_query","text":"
    qa_query(question: QAQuestion) -> QAAnswer\n

    Query the QA service with a question.

    Parameters:

    Name Type Description Default QAQuestion

    The question to query the QA service with.

    required

    Returns:

    Type Description QAAnswer

    The answer from the QA service.

    Source code in gerd/backends/rest_server.py
    @override\ndef qa_query(self, question: QAQuestion) -> QAAnswer:\n    return self._bridge.qa_query(question)\n
    "},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer.qa_query(query)","title":"query","text":""},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer.remove_file","title":"remove_file","text":"
    remove_file(file_name: str) -> QAAnswer\n

    Remove a file from the vector store.

    The returned answer has a status code of 200 if the file was removed successfully. Parameters: file_name: The name of the file to remove from the vector store.

    Returns:

    Type Description QAAnswer

    The answer from the QA service

    Source code in gerd/backends/rest_server.py
    @override\ndef remove_file(self, file_name: str) -> QAAnswer:\n    return self._bridge.remove_file(file_name)\n
    "},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer.set_gen_prompt","title":"set_gen_prompt","text":"
    set_gen_prompt(config: PromptConfig) -> PromptConfig\n

    Sets the prompt configuration for the generation service.

    The prompt configuration that is returned should in most cases be the same as the one that was set. Parameters: config: The prompt configuration to set

    Returns:

    Type Description PromptConfig

    The prompt configuration that was set

    Source code in gerd/backends/rest_server.py
    @override\ndef set_gen_prompt(self, config: PromptConfig) -> PromptConfig:\n    return self._bridge.set_gen_prompt(config)\n
    "},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer.set_qa_prompt","title":"set_qa_prompt","text":"
    set_qa_prompt(config: PromptConfig, qa_mode: QAModesEnum) -> QAAnswer\n

    Sets the prompt configuration for the QA service.

    Since the QA service uses multiple prompt configurations, the mode should be specified. For more details, see the documentation of QAService.set_prompt_config.

    Parameters:

    Name Type Description Default PromptConfig

    The prompt configuration to set

    required QAModesEnum

    The mode to set the prompt configuration for

    required

    Returns:

    Type Description QAAnswer

    The answer from the QA service

    Source code in gerd/backends/rest_server.py
    @override\ndef set_qa_prompt(self, config: PromptConfig, qa_mode: QAModesEnum) -> QAAnswer:\n    return self._bridge.set_qa_prompt(config, qa_mode)\n
    "},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer.set_qa_prompt(config)","title":"config","text":""},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer.set_qa_prompt(qa_mode)","title":"qa_mode","text":""},{"location":"reference/gerd/backends/rest_server/#gerd.backends.rest_server.RestServer.set_qa_prompt_rest","title":"set_qa_prompt_rest","text":"
    set_qa_prompt_rest(config: QAPromptConfig) -> QAAnswer\n

    Set the QA prompt configuration.

    The call is forwarded to the bridge. Parameters: config: The QA prompt configuration Returns: The QA prompt configuration; Should be the same as the input in most cases

    Source code in gerd/backends/rest_server.py
    def set_qa_prompt_rest(self, config: QAPromptConfig) -> QAAnswer:\n    \"\"\"Set the QA prompt configuration.\n\n    The call is forwarded to the bridge.\n    Parameters:\n        config: The QA prompt configuration\n    Returns:\n        The QA prompt configuration; Should be the same as the input in most cases\n    \"\"\"\n    return self._bridge.set_qa_prompt(config.config, config.mode)\n
    "},{"location":"reference/gerd/config/","title":"gerd.config","text":""},{"location":"reference/gerd/config/#gerd.config","title":"gerd.config","text":"

    Configuration for the application.

    Classes:

    Name Description EnvVariables

    Environment variables.

    Settings

    Settings for the application.

    YamlConfig

    YAML configuration source.

    Functions:

    Name Description load_gen_config

    Load the LLM model configuration.

    load_qa_config

    Load the LLM model configuration.

    Attributes:

    Name Type Description CONFIG

    The global configuration object.

    "},{"location":"reference/gerd/config/#gerd.config.CONFIG","title":"CONFIG module-attribute","text":"
    CONFIG = Settings()\n

    The global configuration object.

    "},{"location":"reference/gerd/config/#gerd.config.EnvVariables","title":"EnvVariables","text":"

    Bases: BaseModel

    Environment variables.

    "},{"location":"reference/gerd/config/#gerd.config.Settings","title":"Settings","text":"

    Bases: BaseSettings

    Settings for the application.

    Methods:

    Name Description settings_customise_sources

    Customize the settings sources used by pydantic-settings.

    "},{"location":"reference/gerd/config/#gerd.config.Settings.settings_customise_sources","title":"settings_customise_sources classmethod","text":"
    settings_customise_sources(settings_cls: Type[BaseSettings], init_settings: PydanticBaseSettingsSource, env_settings: PydanticBaseSettingsSource, dotenv_settings: PydanticBaseSettingsSource, file_secret_settings: PydanticBaseSettingsSource) -> Tuple[PydanticBaseSettingsSource, ...]\n

    Customize the settings sources used by pydantic-settings.

    The order of the sources is important. The first source has the highest priority.

    Parameters:

    Name Type Description Default

    The class of the settings.

    required PydanticBaseSettingsSource

    The settings from the initialization.

    required PydanticBaseSettingsSource

    The settings from the environment.

    required PydanticBaseSettingsSource

    The settings from the dotenv file.

    required PydanticBaseSettingsSource

    The settings from the secret file.

    required

    Returns:

    Type Description Tuple[PydanticBaseSettingsSource, ...]

    The customized settings sources.

    Source code in gerd/config.py
    @classmethod\ndef settings_customise_sources(\n    cls,\n    settings_cls: Type[BaseSettings],\n    init_settings: PydanticBaseSettingsSource,\n    env_settings: PydanticBaseSettingsSource,\n    dotenv_settings: PydanticBaseSettingsSource,\n    file_secret_settings: PydanticBaseSettingsSource,\n) -> Tuple[PydanticBaseSettingsSource, ...]:\n    \"\"\"Customize the settings sources used by pydantic-settings.\n\n    The order of the sources is important.\n    The first source has the highest priority.\n\n    Parameters:\n        cls: The class of the settings.\n        init_settings: The settings from the initialization.\n        env_settings: The settings from the environment.\n        dotenv_settings: The settings from the dotenv file.\n        file_secret_settings: The settings from the secret file.\n\n    Returns:\n        The customized settings sources.\n    \"\"\"\n    return (\n        file_secret_settings,\n        env_settings,\n        dotenv_settings,\n        init_settings,\n        YamlConfig(settings_cls),\n    )\n
    "},{"location":"reference/gerd/config/#gerd.config.Settings.settings_customise_sources(cls)","title":"cls","text":""},{"location":"reference/gerd/config/#gerd.config.Settings.settings_customise_sources(init_settings)","title":"init_settings","text":""},{"location":"reference/gerd/config/#gerd.config.Settings.settings_customise_sources(env_settings)","title":"env_settings","text":""},{"location":"reference/gerd/config/#gerd.config.Settings.settings_customise_sources(dotenv_settings)","title":"dotenv_settings","text":""},{"location":"reference/gerd/config/#gerd.config.Settings.settings_customise_sources(file_secret_settings)","title":"file_secret_settings","text":""},{"location":"reference/gerd/config/#gerd.config.YamlConfig","title":"YamlConfig","text":"

    Bases: PydanticBaseSettingsSource

    YAML configuration source.

    Methods:

    Name Description get_field_value

    Overrides a method from PydanticBaseSettingsSource.

    "},{"location":"reference/gerd/config/#gerd.config.YamlConfig.get_field_value","title":"get_field_value","text":"
    get_field_value(field: FieldInfo, field_name: str) -> Tuple[Any, str, bool]\n

    Overrides a method from PydanticBaseSettingsSource.

    Fails if it should ever be called. Parameters: field: The field to get the value for. field_name: The name of the field.

    Raises:

    Type Description NotImplementedError

    Always.

    Source code in gerd/config.py
    def get_field_value(\n    self, field: FieldInfo, field_name: str\n) -> Tuple[Any, str, bool]:\n    \"\"\"Overrides a method from `PydanticBaseSettingsSource`.\n\n    Fails if it should ever be called.\n    Parameters:\n        field: The field to get the value for.\n        field_name: The name of the field.\n\n    Raises:\n        NotImplementedError: Always.\n    \"\"\"\n    raise NotImplementedError()\n
    "},{"location":"reference/gerd/config/#gerd.config.load_gen_config","title":"load_gen_config","text":"
    load_gen_config(config: str = 'gen_default') -> GenerationConfig\n

    Load the LLM model configuration.

    Parameters:

    Name Type Description Default str

    The name of the configuration.

    'gen_default'

    Returns:

    Type Description GenerationConfig

    The model configuration.

    Source code in gerd/config.py
    def load_gen_config(config: str = \"gen_default\") -> GenerationConfig:\n    \"\"\"Load the LLM model configuration.\n\n    Parameters:\n        config: The name of the configuration.\n\n    Returns:\n        The model configuration.\n    \"\"\"\n    config_path = (\n        Path(config)\n        if config.endswith(\"yml\")\n        else Path(PROJECT_DIR, \"config\", f\"{config}.yml\")\n    )\n    with config_path.open(\"r\", encoding=\"utf-8\") as f:\n        conf = GenerationConfig.model_validate(safe_load(f))\n    if CONFIG.env and CONFIG.env.api_token and conf.model.endpoint:\n        conf.model.endpoint.key = CONFIG.env.api_token\n    return conf\n
    "},{"location":"reference/gerd/config/#gerd.config.load_gen_config(config)","title":"config","text":""},{"location":"reference/gerd/config/#gerd.config.load_qa_config","title":"load_qa_config","text":"
    load_qa_config(config: str = 'qa_default') -> QAConfig\n

    Load the LLM model configuration.

    Parameters:

    Name Type Description Default str

    The name of the configuration.

    'qa_default'

    Returns:

    Type Description QAConfig

    The model configuration.

    Source code in gerd/config.py
    def load_qa_config(config: str = \"qa_default\") -> QAConfig:\n    \"\"\"Load the LLM model configuration.\n\n    Parameters:\n        config: The name of the configuration.\n\n    Returns:\n        The model configuration.\n    \"\"\"\n    config_path = (\n        Path(config)\n        if config.endswith(\"yml\")\n        else Path(PROJECT_DIR, \"config\", f\"{config}.yml\")\n    )\n    with config_path.open(\"r\", encoding=\"utf-8\") as f:\n        conf = QAConfig.model_validate(safe_load(f))\n\n    if CONFIG.env and CONFIG.env.api_token and conf.model.endpoint:\n        conf.model.endpoint.key = CONFIG.env.api_token\n    return conf\n
    "},{"location":"reference/gerd/config/#gerd.config.load_qa_config(config)","title":"config","text":""},{"location":"reference/gerd/features/","title":"gerd.features","text":""},{"location":"reference/gerd/features/#gerd.features","title":"gerd.features","text":"

    Special features to extend the functionality of GERD services.

    Modules:

    Name Description prompt_chaining

    The prompt chaining extension.

    "},{"location":"reference/gerd/features/prompt_chaining/","title":"gerd.features.prompt_chaining","text":""},{"location":"reference/gerd/features/prompt_chaining/#gerd.features.prompt_chaining","title":"gerd.features.prompt_chaining","text":"

    The prompt chaining extension.

    Prompt chaining is a method to improve the factual accuracy of the model's output. To do this, the model generates a series of prompts and uses the output of each prompt as the input for the next prompt. This allows the model to reflect on its own output and generate a more coherent response.

    Classes:

    Name Description PromptChaining

    The prompt chaining extension.

    PromptChainingConfig

    Configuration for prompt chaining.

    "},{"location":"reference/gerd/features/prompt_chaining/#gerd.features.prompt_chaining.PromptChaining","title":"PromptChaining","text":"
    PromptChaining(config: PromptChainingConfig, llm: LLM, prompt: PromptConfig)\n

    The prompt chaining extension.

    The service is initialized with a chaining configuration and an LLM.

    Parameters:

    Name Type Description Default PromptChainingConfig

    The configuration for the prompt chaining

    required LLM

    The language model to use for the generation

    required PromptConfig

    The prompt that is used to wrap the questions

    required

    Methods:

    Name Description generate

    Generate text based on the prompt configuration and use chaining.

    Source code in gerd/features/prompt_chaining.py
    def __init__(\n    self, config: PromptChainingConfig, llm: LLM, prompt: PromptConfig\n) -> None:\n    \"\"\"The service is initialized with a chaining configuration and an LLM.\n\n    Parameters:\n        config: The configuration for the prompt chaining\n        llm: The language model to use for the generation\n        prompt: The prompt that is used to wrap the questions\n    \"\"\"\n    self.llm = llm\n    self.config = config\n    self.prompt = prompt\n
    "},{"location":"reference/gerd/features/prompt_chaining/#gerd.features.prompt_chaining.PromptChaining(config)","title":"config","text":""},{"location":"reference/gerd/features/prompt_chaining/#gerd.features.prompt_chaining.PromptChaining(llm)","title":"llm","text":""},{"location":"reference/gerd/features/prompt_chaining/#gerd.features.prompt_chaining.PromptChaining(prompt)","title":"prompt","text":""},{"location":"reference/gerd/features/prompt_chaining/#gerd.features.prompt_chaining.PromptChaining.generate","title":"generate","text":"
    generate(parameters: dict[str, str]) -> str\n

    Generate text based on the prompt configuration and use chaining.

    Parameters:

    Name Type Description Default dict[str, str]

    The parameters to format the prompt with

    required

    Returns:

    Type Description str

    The result of the last prompt that was chained

    Source code in gerd/features/prompt_chaining.py
    def generate(self, parameters: dict[str, str]) -> str:\n    \"\"\"Generate text based on the prompt configuration and use chaining.\n\n    Parameters:\n        parameters: The parameters to format the prompt with\n\n    Returns:\n        The result of the last prompt that was chained\n    \"\"\"\n    res = \"\"\n    for i, prompt in enumerate(self.config.prompts, 1):\n        resolved = self.prompt.format({\"prompt\": prompt.format(parameters)})\n        _LOGGER.debug(\"\\n===== Input =====\\n\\n%s\\n\\n====================\", resolved)\n        res = self.llm.generate(resolved).strip()\n        _LOGGER.debug(\"\\n===== Response =====\\n\\n%s\\n\\n====================\", res)\n        parameters[f\"response_{i}\"] = res\n    return res\n
    "},{"location":"reference/gerd/features/prompt_chaining/#gerd.features.prompt_chaining.PromptChaining.generate(parameters)","title":"parameters","text":""},{"location":"reference/gerd/features/prompt_chaining/#gerd.features.prompt_chaining.PromptChainingConfig","title":"PromptChainingConfig","text":"

    Bases: BaseModel

    Configuration for prompt chaining.

    Note that prompts should contain placeholders for the responses to be inserted. The initial question can be used with {question} if it is passed as a parameter with this key. The responses will be indexed as response_1, response_2, etc. This way any prompt can refer to all previous responses and the initial question if needed.

    Attributes:

    Name Type Description prompts list[PromptConfig]

    The list of prompts to chain.

    "},{"location":"reference/gerd/features/prompt_chaining/#gerd.features.prompt_chaining.PromptChainingConfig.prompts","title":"prompts instance-attribute","text":"
    prompts: list[PromptConfig]\n

    The list of prompts to chain.

    "},{"location":"reference/gerd/frontends/","title":"gerd.frontends","text":""},{"location":"reference/gerd/frontends/#gerd.frontends","title":"gerd.frontends","text":"

    A collection of several gradio frontends.

    A variety of frontends to interact with GERD services and backends.

    Modules:

    Name Description gen_frontend

    A gradio frontend to interact with the generation service.

    generate

    A simple gradio frontend to interact with the GERD chat and generate service.

    instruct

    A gradio frontend to interact with the GERD instruct service.

    qa_frontend

    A gradio frontend to query the QA service and upload files to the vectorstore.

    router

    A gradio frontend to start and stop the GERD services.

    training

    A gradio frontend to train LoRAs with.

    "},{"location":"reference/gerd/frontends/gen_frontend/","title":"gerd.frontends.gen_frontend","text":""},{"location":"reference/gerd/frontends/gen_frontend/#gerd.frontends.gen_frontend","title":"gerd.frontends.gen_frontend","text":"

    A gradio frontend to interact with the generation service.

    This frontend is tailored to the letter of discharge generation task. For a more general frontend see gerd.frontend.generate.

    Functions:

    Name Description compare_paragraphs

    Compare paragraphs of two documents and return the modified parts.

    generate

    Generate a letter of discharge based on the provided fields.

    insert_paragraphs

    Insert modified paragraphs into the source document.

    response_parser

    Parse the response from the generation service.

    "},{"location":"reference/gerd/frontends/gen_frontend/#gerd.frontends.gen_frontend.compare_paragraphs","title":"compare_paragraphs","text":"
    compare_paragraphs(src_doc: str, mod_doc: str) -> Dict[str, str]\n

    Compare paragraphs of two documents and return the modified parts.

    Parameters:

    Name Type Description Default str

    The source document

    required str

    The modified document

    required

    Returns:

    Type Description Dict[str, str]

    The modified parts of the document

    Source code in gerd/frontends/gen_frontend.py
    def compare_paragraphs(src_doc: str, mod_doc: str) -> Dict[str, str]:\n    \"\"\"Compare paragraphs of two documents and return the modified parts.\n\n    Parameters:\n        src_doc: The source document\n        mod_doc: The modified document\n\n    Returns:\n        The modified parts of the document\n    \"\"\"\n    mod_parts = {}\n    src_doc_split = src_doc.split(\"\\n\\n\")\n    mod_doc_split = mod_doc.split(\"\\n\\n\")\n    for section_order, src_para in zip(sections, src_doc_split, strict=True):\n        mod_para = mod_doc_split[sections.index(section_order)]\n        if src_para != mod_para:\n            mod_parts[section_order] = mod_para\n    return mod_parts\n
    "},{"location":"reference/gerd/frontends/gen_frontend/#gerd.frontends.gen_frontend.compare_paragraphs(src_doc)","title":"src_doc","text":""},{"location":"reference/gerd/frontends/gen_frontend/#gerd.frontends.gen_frontend.compare_paragraphs(mod_doc)","title":"mod_doc","text":""},{"location":"reference/gerd/frontends/gen_frontend/#gerd.frontends.gen_frontend.generate","title":"generate","text":"
    generate(*fields: str) -> Tuple[str, str, gr.TextArea, gr.Button]\n

    Generate a letter of discharge based on the provided fields.

    Parameters:

    Name Type Description Default str

    The fields to generate the letter of discharge from.

    ()

    Returns:

    Type Description str

    The generated letter of discharge, a text area to display it,

    str

    and a button state to continue the generation

    Source code in gerd/frontends/gen_frontend.py
    def generate(*fields: str) -> Tuple[str, str, gr.TextArea, gr.Button]:\n    \"\"\"Generate a letter of discharge based on the provided fields.\n\n    Parameters:\n        *fields: The fields to generate the letter of discharge from.\n\n    Returns:\n        The generated letter of discharge, a text area to display it,\n        and a button state to continue the generation\n    \"\"\"\n    params = {}\n    for key, name, value in _pairwise(fields):\n        if not value:\n            msg = f\"Feld '{name}' darf nicht leer sein!\"\n            raise gr.Error(msg)\n        params[key] = value\n    response = TRANSPORTER.generate(params)\n    return (\n        response.text,\n        response.text,\n        gr.TextArea(label=\"Dokument\", interactive=True),\n        gr.Button(\"Kontinuiere Dokument\", visible=True),\n    )\n
    "},{"location":"reference/gerd/frontends/gen_frontend/#gerd.frontends.gen_frontend.generate(*fields)","title":"*fields","text":""},{"location":"reference/gerd/frontends/gen_frontend/#gerd.frontends.gen_frontend.insert_paragraphs","title":"insert_paragraphs","text":"
    insert_paragraphs(src_doc: str, new_para: Dict[str, str]) -> str\n

    Insert modified paragraphs into the source document.

    Parameters:

    Name Type Description Default str

    The source document

    required Dict[str, str]

    The modified paragraphs

    required

    Returns:

    Type Description str

    The updated document

    Source code in gerd/frontends/gen_frontend.py
    def insert_paragraphs(src_doc: str, new_para: Dict[str, str]) -> str:\n    \"\"\"Insert modified paragraphs into the source document.\n\n    Parameters:\n        src_doc: The source document\n        new_para: The modified paragraphs\n\n    Returns:\n        The updated document\n    \"\"\"\n    for section_order, mod_para in new_para.items():\n        split_doc = src_doc.split(\"\\n\\n\")[sections.index(section_order)]\n        src_doc = src_doc.replace(split_doc, mod_para)\n    return src_doc\n
    "},{"location":"reference/gerd/frontends/gen_frontend/#gerd.frontends.gen_frontend.insert_paragraphs(src_doc)","title":"src_doc","text":""},{"location":"reference/gerd/frontends/gen_frontend/#gerd.frontends.gen_frontend.insert_paragraphs(new_para)","title":"new_para","text":""},{"location":"reference/gerd/frontends/gen_frontend/#gerd.frontends.gen_frontend.response_parser","title":"response_parser","text":"
    response_parser(response: str) -> Dict[str, str]\n

    Parse the response from the generation service.

    Parameters:

    Name Type Description Default str

    The response from the generation service

    required

    Returns:

    Type Description Dict[str, str]

    The parsed response

    Source code in gerd/frontends/gen_frontend.py
    def response_parser(response: str) -> Dict[str, str]:\n    \"\"\"Parse the response from the generation service.\n\n    Parameters:\n        response: The response from the generation service\n\n    Returns:\n        The parsed response\n    \"\"\"\n    parsed_response = {}\n    split_response = response.split(\"\\n\\n\")\n    for paragraph in split_response:\n        for section in sections:\n            if section in paragraph:\n                parsed_response[section] = paragraph\n                break\n    return parsed_response\n
    "},{"location":"reference/gerd/frontends/gen_frontend/#gerd.frontends.gen_frontend.response_parser(response)","title":"response","text":""},{"location":"reference/gerd/frontends/generate/","title":"gerd.frontends.generate","text":""},{"location":"reference/gerd/frontends/generate/#gerd.frontends.generate","title":"gerd.frontends.generate","text":"

    A simple gradio frontend to interact with the GERD chat and generate service.

    Classes:

    Name Description Global

    Singleton to store the service.

    Functions:

    Name Description generate

    Generate text from the model.

    load_model

    Load a global large language model.

    upload_lora

    Upload a LoRA archive.

    Attributes:

    Name Type Description KIOSK_MODE

    Whether the frontend is running in kiosk mode.

    "},{"location":"reference/gerd/frontends/generate/#gerd.frontends.generate.KIOSK_MODE","title":"KIOSK_MODE module-attribute","text":"
    KIOSK_MODE = kiosk_mode\n

    Whether the frontend is running in kiosk mode.

    Kiosk mode reduces the number of options to a minimum and automatically loads the model.

    "},{"location":"reference/gerd/frontends/generate/#gerd.frontends.generate.Global","title":"Global","text":"

    Singleton to store the service.

    "},{"location":"reference/gerd/frontends/generate/#gerd.frontends.generate.generate","title":"generate","text":"
    generate(textbox: str, temp: float, top_p: float, max_tokens: int) -> str\n

    Generate text from the model.

    Parameters:

    Name Type Description Default str

    The text to generate from

    required float

    The temperature for the generation

    required float

    The top p value for the generation

    required int

    The maximum number of tokens to generate

    required

    Returns:

    Type Description str

    The generated text

    Source code in gerd/frontends/generate.py
    def generate(textbox: str, temp: float, top_p: float, max_tokens: int) -> str:\n    \"\"\"Generate text from the model.\n\n    Parameters:\n        textbox: The text to generate from\n        temp: The temperature for the generation\n        top_p: The top p value for the generation\n        max_tokens: The maximum number of tokens to generate\n\n    Returns:\n        The generated text\n    \"\"\"\n    if Global.service is None:\n        msg = \"Model not loaded\"\n        raise gr.Error(msg)\n    Global.service.config.model.top_p = top_p\n    Global.service.config.model.temperature = temp\n    Global.service.config.model.max_new_tokens = max_tokens\n    return Global.service.generate({\"message\": textbox}).text\n
    "},{"location":"reference/gerd/frontends/generate/#gerd.frontends.generate.generate(textbox)","title":"textbox","text":""},{"location":"reference/gerd/frontends/generate/#gerd.frontends.generate.generate(temp)","title":"temp","text":""},{"location":"reference/gerd/frontends/generate/#gerd.frontends.generate.generate(top_p)","title":"top_p","text":""},{"location":"reference/gerd/frontends/generate/#gerd.frontends.generate.generate(max_tokens)","title":"max_tokens","text":""},{"location":"reference/gerd/frontends/generate/#gerd.frontends.generate.load_model","title":"load_model","text":"
    load_model(model_name: str, origin: str) -> dict[str, Any]\n

    Load a global large language model.

    Parameters:

    Name Type Description Default str

    The name of the model

    required str

    Whether to use an extra LoRA

    required

    Returns:

    Type Description dict[str, Any]

    The updated interactive state, returns interactive=True when the model is loaded

    Source code in gerd/frontends/generate.py
    def load_model(model_name: str, origin: str) -> dict[str, Any]:\n    \"\"\"Load a global large language model.\n\n    Parameters:\n        model_name: The name of the model\n        origin: Whether to use an extra LoRA\n\n    Returns:\n        The updated interactive state, returns interactive=True when the model is loaded\n    \"\"\"\n    if Global.service is not None:\n        del Global.service\n    model_config = config.model_copy(deep=True)\n    model_config.model.name = model_name\n    if origin != \"None\":\n        model_config.model.loras.add(lora_dir / origin)\n    Global.service = ChatService(model_config)\n    return gr.update(interactive=True)\n
    "},{"location":"reference/gerd/frontends/generate/#gerd.frontends.generate.load_model(model_name)","title":"model_name","text":""},{"location":"reference/gerd/frontends/generate/#gerd.frontends.generate.load_model(origin)","title":"origin","text":""},{"location":"reference/gerd/frontends/generate/#gerd.frontends.generate.upload_lora","title":"upload_lora","text":"
    upload_lora(file_upload: str) -> str\n

    Upload a LoRA archive.

    Parameters:

    Name Type Description Default str

    The path to the uploaded archive

    required

    Returns:

    Type Description str

    an empty string to clear the input

    Source code in gerd/frontends/generate.py
    def upload_lora(file_upload: str) -> str:\n    \"\"\"Upload a LoRA archive.\n\n    Parameters:\n        file_upload: The path to the uploaded archive\n\n    Returns:\n        an empty string to clear the input\n    \"\"\"\n    if file_upload is None:\n        gr.Warning(\"No file uploaded\")\n    p = Path(file_upload)\n    if p.exists() is False:\n        gr.Warning(\"File does not exist\")\n    shutil.unpack_archive(p.as_posix(), lora_dir / p.stem)\n    gr.Info(f\"Uploaded '{p.stem}'\")\n    return \"\"\n
    "},{"location":"reference/gerd/frontends/generate/#gerd.frontends.generate.upload_lora(file_upload)","title":"file_upload","text":""},{"location":"reference/gerd/frontends/instruct/","title":"gerd.frontends.instruct","text":""},{"location":"reference/gerd/frontends/instruct/#gerd.frontends.instruct","title":"gerd.frontends.instruct","text":"

    A gradio frontend to interact with the GERD instruct service.

    Classes:

    Name Description Global

    Singleton to store the service.

    Functions:

    Name Description generate

    Generate text from the model.

    load_model

    Load a global large language model.

    Attributes:

    Name Type Description KIOSK_MODE

    Whether the frontend is running in kiosk mode.

    "},{"location":"reference/gerd/frontends/instruct/#gerd.frontends.instruct.KIOSK_MODE","title":"KIOSK_MODE module-attribute","text":"
    KIOSK_MODE = kiosk_mode\n

    Whether the frontend is running in kiosk mode.

    Kiosk mode reduces the number of options to a minimum and automatically loads the model.

    "},{"location":"reference/gerd/frontends/instruct/#gerd.frontends.instruct.Global","title":"Global","text":"

    Singleton to store the service.

    "},{"location":"reference/gerd/frontends/instruct/#gerd.frontends.instruct.generate","title":"generate","text":"
    generate(temperature: float, top_p: float, max_tokens: int, system_text: str, *args: str) -> str\n

    Generate text from the model.

    Parameters:

    Name Type Description Default float

    The temperature for the generation

    required float

    The top-p value for the generation

    required int

    The maximum number of tokens to generate

    required str

    The system text to set up the context

    required str

    The user input

    () Source code in gerd/frontends/instruct.py
    def generate(\n    temperature: float,\n    top_p: float,\n    max_tokens: int,\n    system_text: str,\n    *args: str,\n) -> str:\n    \"\"\"Generate text from the model.\n\n    Parameters:\n        temperature: The temperature for the generation\n        top_p: The top-p value for the generation\n        max_tokens: The maximum number of tokens to generate\n        system_text: The system text to set up the context\n        args: The user input\n    \"\"\"\n    fields = dict(zip(config.model.prompt_config.parameters, args, strict=True))\n    if Global.service is None:\n        msg = \"Model not loaded\"\n        raise gr.Error(msg)\n    if system_text:\n        Global.service.config.model.prompt_setup = [\n            (\"system\", PromptConfig(text=system_text))\n        ]\n    Global.service.config.model.top_p = top_p\n    Global.service.config.model.temperature = temperature\n    Global.service.config.model.max_new_tokens = max_tokens\n    Global.service.reset()\n    return Global.service.submit_user_message(fields).text\n
    "},{"location":"reference/gerd/frontends/instruct/#gerd.frontends.instruct.generate(temperature)","title":"temperature","text":""},{"location":"reference/gerd/frontends/instruct/#gerd.frontends.instruct.generate(top_p)","title":"top_p","text":""},{"location":"reference/gerd/frontends/instruct/#gerd.frontends.instruct.generate(max_tokens)","title":"max_tokens","text":""},{"location":"reference/gerd/frontends/instruct/#gerd.frontends.instruct.generate(system_text)","title":"system_text","text":""},{"location":"reference/gerd/frontends/instruct/#gerd.frontends.instruct.generate(args)","title":"args","text":""},{"location":"reference/gerd/frontends/instruct/#gerd.frontends.instruct.load_model","title":"load_model","text":"
    load_model(model_name: str, origin: str = 'None') -> dict[str, Any]\n

    Load a global large language model.

    Parameters:

    Name Type Description Default str

    The name of the model

    required str

    Whether to use an extra LoRA

    'None' Source code in gerd/frontends/instruct.py
    def load_model(model_name: str, origin: str = \"None\") -> dict[str, Any]:\n    \"\"\"Load a global large language model.\n\n    Parameters:\n        model_name: The name of the model\n        origin: Whether to use an extra LoRA\n    \"\"\"\n    if Global.service is not None:\n        _LOGGER.debug(\"Unloading model\")\n        del Global.service\n    model_config = config.model_copy()\n    model_config.model.name = model_name\n    if origin != \"None\" and (lora_dir / origin).is_dir():\n        model_config.model.loras.add(lora_dir / origin)\n    _LOGGER.info(\"Loading model %s\", model_config.model.name)\n    Global.service = ChatService(model_config)\n    _LOGGER.info(\"Model loaded\")\n    return gr.update(interactive=True)\n
    "},{"location":"reference/gerd/frontends/instruct/#gerd.frontends.instruct.load_model(model_name)","title":"model_name","text":""},{"location":"reference/gerd/frontends/instruct/#gerd.frontends.instruct.load_model(origin)","title":"origin","text":""},{"location":"reference/gerd/frontends/qa_frontend/","title":"gerd.frontends.qa_frontend","text":""},{"location":"reference/gerd/frontends/qa_frontend/#gerd.frontends.qa_frontend","title":"gerd.frontends.qa_frontend","text":"

    A gradio frontend to query the QA service and upload files to the vectorstore.

    Functions:

    Name Description files_changed

    Check if the file upload element has changed.

    get_qa_mode

    Get QAMode from string.

    handle_developer_mode_checkbox_change

    Enable/disable developermode.

    handle_type_radio_selection_change

    Enable/disable gui elements depend on which mode is selected.

    query

    Starts the selected QA Mode.

    set_prompt

    Updates the prompt of the selected QA Mode.

    "},{"location":"reference/gerd/frontends/qa_frontend/#gerd.frontends.qa_frontend.files_changed","title":"files_changed","text":"
    files_changed(file_paths: Optional[list[str]]) -> None\n

    Check if the file upload element has changed.

    If so, upload the new files to the vectorstore and delete the one that have been removed.

    Parameters:

    Name Type Description Default Optional[list[str]]

    The file paths to upload

    required Source code in gerd/frontends/qa_frontend.py
    def files_changed(file_paths: Optional[list[str]]) -> None:\n    \"\"\"Check if the file upload element has changed.\n\n    If so, upload the new files to the vectorstore and delete the one that\n    have been removed.\n\n    Parameters:\n        file_paths: The file paths to upload\n    \"\"\"\n    file_paths = file_paths or []\n    progress = gr.Progress()\n    new_set = set(file_paths)\n    new_files = new_set - store_set\n    delete_files = store_set - new_set\n    for new_file in new_files:\n        store_set.add(new_file)\n        with pathlib.Path(new_file).open(\"rb\") as file:\n            data = QAFileUpload(\n                data=file.read(),\n                name=pathlib.Path(new_file).name,\n            )\n        res = TRANSPORTER.add_file(data)\n        if res.status != 200:\n            _LOGGER.warning(\n                \"Data upload failed with error code: %d\\nReason: %s\",\n                res.status,\n                res.error_msg,\n            )\n            msg = (\n                f\"Datei konnte nicht hochgeladen werden: {res.error_msg}\"\n                \"(Error Code {res.status})\"\n            )\n            raise gr.Error(msg)\n    for delete_file in delete_files:\n        store_set.remove(delete_file)\n        res = TRANSPORTER.remove_file(pathlib.Path(delete_file).name)\n    progress(100, desc=\"Fertig!\")\n
    "},{"location":"reference/gerd/frontends/qa_frontend/#gerd.frontends.qa_frontend.files_changed(file_paths)","title":"file_paths","text":""},{"location":"reference/gerd/frontends/qa_frontend/#gerd.frontends.qa_frontend.get_qa_mode","title":"get_qa_mode","text":"
    get_qa_mode(search_type: str) -> QAModesEnum\n

    Get QAMode from string.

    Parameters:

    Name Type Description Default str

    The search type

    required

    Returns:

    Type Description QAModesEnum

    The QAMode

    Source code in gerd/frontends/qa_frontend.py
    def get_qa_mode(search_type: str) -> QAModesEnum:\n    \"\"\"Get QAMode from string.\n\n    Parameters:\n        search_type: The search type\n\n    Returns:\n        The QAMode\n    \"\"\"\n    if search_type in qa_modes_dict:\n        return qa_modes_dict[search_type]\n    else:\n        return QAModesEnum.NONE\n
    "},{"location":"reference/gerd/frontends/qa_frontend/#gerd.frontends.qa_frontend.get_qa_mode(search_type)","title":"search_type","text":""},{"location":"reference/gerd/frontends/qa_frontend/#gerd.frontends.qa_frontend.handle_developer_mode_checkbox_change","title":"handle_developer_mode_checkbox_change","text":"
    handle_developer_mode_checkbox_change(check: bool) -> List[Any]\n

    Enable/disable developermode.

    Enables or disables the developer mode and the corresponding GUI elements. Parameters: check: The current state of the developer mode checkbox Returns: The list of GUI element property changes to update

    Source code in gerd/frontends/qa_frontend.py
    def handle_developer_mode_checkbox_change(check: bool) -> List[Any]:\n    \"\"\"Enable/disable developermode.\n\n    Enables or disables the developer mode and the corresponding GUI elements.\n    Parameters:\n        check: The current state of the developer mode checkbox\n    Returns:\n        The list of GUI element property changes to update\n    \"\"\"\n    return [\n        gr.update(visible=check),\n        gr.update(visible=check),\n        gr.update(visible=check),\n        gr.update(visible=check),\n        gr.update(\n            choices=(\n                [\"LLM\", \"Analyze\", \"Analyze mult.\", \"VectorDB\"]\n                if check\n                else [\"LLM\", \"Analyze mult.\"]\n            )\n        ),\n    ]\n
    "},{"location":"reference/gerd/frontends/qa_frontend/#gerd.frontends.qa_frontend.handle_type_radio_selection_change","title":"handle_type_radio_selection_change","text":"
    handle_type_radio_selection_change(search_type: str) -> List[Any]\n

    Enable/disable gui elements depend on which mode is selected.

    This order of the updates elements must be considered
    • input text
    • prompt
    • k_slider
    • search strategy_dropdown

    Parameters:

    Name Type Description Default str

    The current search type

    required

    Returns:

    Type Description List[Any]

    The list of GUI element property changes to update

    Source code in gerd/frontends/qa_frontend.py
    def handle_type_radio_selection_change(search_type: str) -> List[Any]:\n    \"\"\"Enable/disable gui elements depend on which mode is selected.\n\n    This order of the updates elements must be considered:\n        - input text\n        - prompt\n        - k_slider\n        - search strategy_dropdown\n\n    Parameters:\n        search_type: The current search type\n\n    Returns:\n        The list of GUI element property changes to update\n    \"\"\"\n    if search_type == \"LLM\":\n        return [\n            gr.update(interactive=True, placeholder=\"Wie hei\u00dft der Patient?\"),\n            gr.update(value=TRANSPORTER.get_qa_prompt(get_qa_mode(search_type)).text),\n            gr.update(interactive=False),\n            gr.update(interactive=False),\n        ]\n    elif search_type == \"VectorDB\":\n        return [\n            gr.update(interactive=True, placeholder=\"Wie hei\u00dft der Patient?\"),\n            gr.update(value=TRANSPORTER.get_qa_prompt(get_qa_mode(search_type)).text),\n            gr.update(interactive=True),\n            gr.update(interactive=True),\n        ]\n\n    return [\n        gr.update(interactive=False, placeholder=\"\"),\n        gr.update(value=TRANSPORTER.get_qa_prompt(get_qa_mode(search_type)).text),\n        gr.update(interactive=False),\n        gr.update(interactive=False),\n    ]\n
    "},{"location":"reference/gerd/frontends/qa_frontend/#gerd.frontends.qa_frontend.handle_type_radio_selection_change(search_type)","title":"search_type","text":""},{"location":"reference/gerd/frontends/qa_frontend/#gerd.frontends.qa_frontend.query","title":"query","text":"
    query(question: str, search_type: str, k_source: int, search_strategy: str) -> str\n

    Starts the selected QA Mode.

    Parameters:

    Name Type Description Default str

    The question to ask

    required str

    The search type

    required int

    The number of sources

    required str

    The search strategy

    required

    Returns:

    Type Description str

    The response from the QA service

    Source code in gerd/frontends/qa_frontend.py
    def query(question: str, search_type: str, k_source: int, search_strategy: str) -> str:\n    \"\"\"Starts the selected QA Mode.\n\n    Parameters:\n        question: The question to ask\n        search_type: The search type\n        k_source: The number of sources\n        search_strategy: The search strategy\n\n    Returns:\n        The response from the QA service\n    \"\"\"\n    q = QAQuestion(\n        question=question, search_strategy=search_strategy, max_sources=k_source\n    )\n    # start search mode\n    if search_type == \"LLM\":\n        qa_res = TRANSPORTER.qa_query(q)\n        if qa_res.status != 200:\n            msg = (\n                f\"Query was unsuccessful: {qa_res.error_msg}\"\n                f\" (Error Code {qa_res.status})\"\n            )\n            raise gr.Error(msg)\n        return qa_res.response\n    # start analyze mode\n    elif search_type == \"Analyze\":\n        qa_analyze_res = TRANSPORTER.analyze_query()\n        if qa_analyze_res.status != 200:\n            msg = (\n                f\"Query was unsuccessful: {qa_analyze_res.error_msg}\"\n                f\" (Error Code {qa_analyze_res.status})\"\n            )\n            raise gr.Error(msg)\n        # remove unwanted fields from answer\n        qa_res_dic = {\n            key: value\n            for key, value in vars(qa_analyze_res).items()\n            if value is not None\n            and value != \"\"\n            and key not in qa_analyze_res.__class__.__dict__\n            and key != \"sources\"\n            and key != \"status\"\n            and key != \"response\"\n            and key != \"prompt\"\n        }\n        qa_res_str = \", \".join(f\"{key}={value}\" for key, value in qa_res_dic.items())\n        return qa_res_str\n    # start analyze mult prompts mode\n    elif search_type == \"Analyze mult.\":\n        qa_analyze_mult_res = TRANSPORTER.analyze_mult_prompts_query()\n        if qa_analyze_mult_res.status != 200:\n            msg = (\n                f\"Query was unsuccessful: {qa_analyze_mult_res.error_msg}\"\n                f\" (Error Code {qa_analyze_mult_res.status})\"\n            )\n            raise gr.Error(msg)\n        # remove unwanted fields from answer\n        qa_res_dic = {\n            key: value\n            for key, value in vars(qa_analyze_mult_res).items()\n            if value is not None\n            and value != \"\"\n            and key not in qa_analyze_mult_res.__class__.__dict__\n            and key != \"sources\"\n            and key != \"status\"\n            and key != \"response\"\n            and key != \"prompt\"\n        }\n        qa_res_str = \", \".join(f\"{key}={value}\" for key, value in qa_res_dic.items())\n        return qa_res_str\n    # start db search mode\n    db_res = TRANSPORTER.db_query(q)\n    if not db_res:\n        msg = f\"Database query returned empty!\"\n        raise gr.Error(msg)\n    output = \"\"\n    for doc in db_res:\n        output += f\"{doc.content}\\n\"\n        output += f\"({doc.name} / {doc.page})\\n----------\\n\\n\"\n    return output\n
    "},{"location":"reference/gerd/frontends/qa_frontend/#gerd.frontends.qa_frontend.query(question)","title":"question","text":""},{"location":"reference/gerd/frontends/qa_frontend/#gerd.frontends.qa_frontend.query(search_type)","title":"search_type","text":""},{"location":"reference/gerd/frontends/qa_frontend/#gerd.frontends.qa_frontend.query(k_source)","title":"k_source","text":""},{"location":"reference/gerd/frontends/qa_frontend/#gerd.frontends.qa_frontend.query(search_strategy)","title":"search_strategy","text":""},{"location":"reference/gerd/frontends/qa_frontend/#gerd.frontends.qa_frontend.set_prompt","title":"set_prompt","text":"
    set_prompt(prompt: str, search_type: str, progress: Optional[gr.Progress] = None) -> None\n

    Updates the prompt of the selected QA Mode.

    Parameters:

    Name Type Description Default str

    The new prompt

    required str

    The search type

    required Optional[Progress]

    The progress bar to update

    None Source code in gerd/frontends/qa_frontend.py
    def set_prompt(\n    prompt: str, search_type: str, progress: Optional[gr.Progress] = None\n) -> None:\n    \"\"\"Updates the prompt of the selected QA Mode.\n\n    Parameters:\n        prompt: The new prompt\n        search_type: The search type\n        progress: The progress bar to update\n    \"\"\"\n    if progress is None:\n        progress = gr.Progress()\n    progress(0, \"Aktualisiere Prompt...\")\n\n    answer = TRANSPORTER.set_qa_prompt(\n        PromptConfig(text=prompt), get_qa_mode(search_type)\n    )\n    if answer.error_msg:\n        if answer.status != 200:\n            msg = (\n                f\"Prompt konnte nicht aktualisiert werden: {answer.error_msg}\"\n                f\" (Error Code {answer.status})\"\n            )\n            raise gr.Error(msg)\n        else:\n            msg = f\"{answer.error_msg}\"\n            gr.Warning(msg)\n    else:\n        gr.Info(\"Prompt wurde aktualisiert!\")\n    progress(100, \"Fertig!\")\n
    "},{"location":"reference/gerd/frontends/qa_frontend/#gerd.frontends.qa_frontend.set_prompt(prompt)","title":"prompt","text":""},{"location":"reference/gerd/frontends/qa_frontend/#gerd.frontends.qa_frontend.set_prompt(search_type)","title":"search_type","text":""},{"location":"reference/gerd/frontends/qa_frontend/#gerd.frontends.qa_frontend.set_prompt(progress)","title":"progress","text":""},{"location":"reference/gerd/frontends/router/","title":"gerd.frontends.router","text":""},{"location":"reference/gerd/frontends/router/#gerd.frontends.router","title":"gerd.frontends.router","text":"

    A gradio frontend to start and stop the GERD services.

    Since most hosts that use a frontend will not have enough memory to run multiple services at the same time this router is used to start and stop the services as needed.

    Classes:

    Name Description AppController

    The controller for the app.

    AppState

    The state of the service.

    Functions:

    Name Description check_state

    Checks the app state and waits for the service to start.

    Attributes:

    Name Type Description GRADIO_ROUTER_PORT

    The port the router is running on.

    GRADIO_SERVER_PORT

    The port the gradio server is running on.

    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.GRADIO_ROUTER_PORT","title":"GRADIO_ROUTER_PORT module-attribute","text":"
    GRADIO_ROUTER_PORT = get('GRADIO_ROUTER_PORT', '7860')\n

    The port the router is running on.

    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.GRADIO_SERVER_PORT","title":"GRADIO_SERVER_PORT module-attribute","text":"
    GRADIO_SERVER_PORT = get('GRADIO_SERVER_PORT', '12121')\n

    The port the gradio server is running on.

    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppController","title":"AppController","text":"
    AppController()\n

    The controller for the app.

    The controlller is initialized in the stopped state.

    Methods:

    Name Description check_port

    Check if the service port is open.

    instance

    Get the instance of the controller.

    start

    Start the service with the given frontend.

    start_gen

    Start the generation service.

    start_instruct

    Start the instruct service.

    start_qa

    Start the QA service.

    start_simple

    Start the simple generation service.

    start_training

    Start the training service.

    stop

    Stop the service when it is running.

    Source code in gerd/frontends/router.py
    def __init__(self) -> None:\n    \"\"\"The controlller is initialized in the stopped state.\"\"\"\n    self.process: subprocess.Popen | None = None\n    self.state = AppState.STOPPED\n
    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppController.check_port","title":"check_port staticmethod","text":"
    check_port(port: int) -> bool\n

    Check if the service port is open.

    Parameters:

    Name Type Description Default int

    The port to check

    required

    Returns:

    Type Description bool

    True if the port us open, False otherwise.

    Source code in gerd/frontends/router.py
    @staticmethod\ndef check_port(port: int) -> bool:\n    \"\"\"Check if the service port is open.\n\n    Parameters:\n        port: The port to check\n\n    Returns:\n        True if the port us open, False otherwise.\n    \"\"\"\n    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n    result = sock.connect_ex((\"127.0.0.1\", port)) == 0\n    sock.close()\n    return result\n
    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppController.check_port(port)","title":"port","text":""},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppController.instance","title":"instance classmethod","text":"
    instance() -> AppController\n

    Get the instance of the controller.

    Source code in gerd/frontends/router.py
    @classmethod\ndef instance(cls) -> \"AppController\":\n    \"\"\"Get the instance of the controller.\"\"\"\n    if cls._instance is None:\n        cls._instance = AppController()\n    return cls._instance\n
    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppController.start","title":"start","text":"
    start(frontend: str) -> None\n

    Start the service with the given frontend.

    Parameters:

    Name Type Description Default str

    The frontend service name to start.

    required Source code in gerd/frontends/router.py
    def start(self, frontend: str) -> None:\n    \"\"\"Start the service with the given frontend.\n\n    Parameters:\n        frontend: The frontend service name to start.\n    \"\"\"\n    if not re.match(\"^[a-zA-Z0-9_]+$\", frontend):\n        msg = \"Invalid frontend name\"\n        raise gr.Error(msg)\n    self.stop()\n    cmd = [sys.executable, \"-m\", f\"gerd.frontends.{frontend}\"]\n    self.process = subprocess.Popen(  # noqa: S603\n        cmd,\n        env=os.environ | {\"GRADIO_SERVER_PORT\": GRADIO_SERVER_PORT},\n    )\n
    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppController.start(frontend)","title":"frontend","text":""},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppController.start_gen","title":"start_gen","text":"
    start_gen() -> str\n

    Start the generation service.

    Returns:

    Type Description str

    The name of the current app state.

    Source code in gerd/frontends/router.py
    def start_gen(self) -> str:\n    \"\"\"Start the generation service.\n\n    Returns:\n        The name of the current app state.\n    \"\"\"\n    self.start(\"gen_frontend\")\n    self.state = AppState.GENERATE_STARTING\n    return self.state.name\n
    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppController.start_instruct","title":"start_instruct","text":"
    start_instruct() -> str\n

    Start the instruct service.

    Returns:

    Type Description str

    The name of the current app state

    Source code in gerd/frontends/router.py
    def start_instruct(self) -> str:\n    \"\"\"Start the instruct service.\n\n    Returns:\n        The name of the current app state\n    \"\"\"\n    self.start(\"instruct\")\n    self.state = AppState.INSTRUCT_STARTING\n    return self.state.name\n
    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppController.start_qa","title":"start_qa","text":"
    start_qa() -> str\n

    Start the QA service.

    Returns:

    Type Description str

    The name of the current app state

    Source code in gerd/frontends/router.py
    def start_qa(self) -> str:\n    \"\"\"Start the QA service.\n\n    Returns:\n        The name of the current app state\n    \"\"\"\n    self.start(\"qa_frontend\")\n    self.state = AppState.QA_STARTING\n    return self.state.name\n
    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppController.start_simple","title":"start_simple","text":"
    start_simple() -> str\n

    Start the simple generation service.

    Returns:

    Type Description str

    The name of the current app state

    Source code in gerd/frontends/router.py
    def start_simple(self) -> str:\n    \"\"\"Start the simple generation service.\n\n    Returns:\n        The name of the current app state\n    \"\"\"\n    self.start(\"generate\")\n    self.state = AppState.SIMPLE_STARTING\n    return self.state.name\n
    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppController.start_training","title":"start_training","text":"
    start_training() -> str\n

    Start the training service.

    Returns:

    Type Description str

    The name of the current app state

    Source code in gerd/frontends/router.py
    def start_training(self) -> str:\n    \"\"\"Start the training service.\n\n    Returns:\n        The name of the current app state\n    \"\"\"\n    self.start(\"training\")\n    self.state = AppState.TRAINING_STARTING\n    return self.state.name\n
    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppController.stop","title":"stop","text":"
    stop() -> str\n

    Stop the service when it is running.

    Returns:

    Type Description str

    The name of the current app state.

    Source code in gerd/frontends/router.py
    def stop(self) -> str:\n    \"\"\"Stop the service when it is running.\n\n    Returns:\n        The name of the current app state.\n    \"\"\"\n    if self.state != AppState.STOPPED and self.process:\n        gr.Info(f\"Trying to stop process... {self.process.pid}\")\n        self.process.terminate()\n        ret = self.process.wait(5)\n        if ret is None:\n            gr.Info(\"Process did not stop in time, killing it\")\n            self.process.kill()\n        gr.Info(\"Stopped\")\n    if self.check_port(int(GRADIO_SERVER_PORT)):\n        gr.Info(\"Stopping service\")\n        prt = int(GRADIO_SERVER_PORT)\n        res = subprocess.check_output([\"lsof\", \"-i\", f\":{prt}\"])  # noqa: S603, S607\n        m = re.search(r\"[Py]ython\\s+(\\d+)\", res.decode(encoding=\"utf-8\"))\n        if m:\n            subprocess.check_call([\"kill\", m.group(1)])  # noqa: S603, S607\n            gr.Warning(f\"Killed service on port {prt}\")\n        else:\n            msg = \"Service could not be stopped\"\n            raise gr.Error(msg)\n    self.state = AppState.STOPPED\n    return self.state.name\n
    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppState","title":"AppState","text":"

    Bases: Enum

    The state of the service.

    Attributes:

    Name Type Description GENERATE_STARTED

    The generation service is started.

    GENERATE_STARTING

    The generation service is starting.

    INSTRUCT_STARTED

    The instruct service is started.

    INSTRUCT_STARTING

    The instruct service is starting.

    QA_STARTED

    The QA service is started.

    QA_STARTING

    The QA service is starting.

    SIMPLE_STARTED

    The simple generation service is started.

    SIMPLE_STARTING

    The simple generation service is starting.

    STOPPED

    All services is stopped.

    TRAINING_STARTED

    The training service is started.

    TRAINING_STARTING

    The training service is starting.

    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppState.GENERATE_STARTED","title":"GENERATE_STARTED class-attribute instance-attribute","text":"
    GENERATE_STARTED = auto()\n

    The generation service is started.

    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppState.GENERATE_STARTING","title":"GENERATE_STARTING class-attribute instance-attribute","text":"
    GENERATE_STARTING = auto()\n

    The generation service is starting.

    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppState.INSTRUCT_STARTED","title":"INSTRUCT_STARTED class-attribute instance-attribute","text":"
    INSTRUCT_STARTED = auto()\n

    The instruct service is started.

    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppState.INSTRUCT_STARTING","title":"INSTRUCT_STARTING class-attribute instance-attribute","text":"
    INSTRUCT_STARTING = auto()\n

    The instruct service is starting.

    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppState.QA_STARTED","title":"QA_STARTED class-attribute instance-attribute","text":"
    QA_STARTED = auto()\n

    The QA service is started.

    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppState.QA_STARTING","title":"QA_STARTING class-attribute instance-attribute","text":"
    QA_STARTING = auto()\n

    The QA service is starting.

    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppState.SIMPLE_STARTED","title":"SIMPLE_STARTED class-attribute instance-attribute","text":"
    SIMPLE_STARTED = auto()\n

    The simple generation service is started.

    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppState.SIMPLE_STARTING","title":"SIMPLE_STARTING class-attribute instance-attribute","text":"
    SIMPLE_STARTING = auto()\n

    The simple generation service is starting.

    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppState.STOPPED","title":"STOPPED class-attribute instance-attribute","text":"
    STOPPED = auto()\n

    All services is stopped.

    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppState.TRAINING_STARTED","title":"TRAINING_STARTED class-attribute instance-attribute","text":"
    TRAINING_STARTED = auto()\n

    The training service is started.

    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.AppState.TRAINING_STARTING","title":"TRAINING_STARTING class-attribute instance-attribute","text":"
    TRAINING_STARTING = auto()\n

    The training service is starting.

    "},{"location":"reference/gerd/frontends/router/#gerd.frontends.router.check_state","title":"check_state","text":"
    check_state() -> str\n

    Checks the app state and waits for the service to start.

    Returns:

    Type Description str

    The name of the current app state.

    Source code in gerd/frontends/router.py
    def check_state() -> str:\n    \"\"\"Checks the app state and waits for the service to start.\n\n    Returns:\n        The name of the current app state.\n    \"\"\"\n    app = AppController.instance()\n    cnt = 0\n    while not app.check_port(int(GRADIO_SERVER_PORT)):\n        _LOGGER.info(\"Waiting for service to start\")\n        sleep(1)\n        cnt += 1\n        if cnt > 30:\n            app.state = AppState.STOPPED\n            msg = \"Service did not start in time\"\n            raise Exception(msg)\n    app.state = AppState(app.state.value + 1)\n    gr.Success(f\"Service started on port {GRADIO_SERVER_PORT}\")\n    return app.state.name\n
    "},{"location":"reference/gerd/frontends/training/","title":"gerd.frontends.training","text":""},{"location":"reference/gerd/frontends/training/#gerd.frontends.training","title":"gerd.frontends.training","text":"

    A gradio frontend to train LoRAs with.

    Classes:

    Name Description Global

    A singleton class handle to store the current trainer instance.

    Functions:

    Name Description check_trainer

    Check if the trainer is (still) running.

    get_file_list

    Get a list of files matching the glob pattern.

    get_loras

    Get a list of available LoRAs.

    start_training

    Start the training process.

    validate_files

    Validate the uploaded files.

    "},{"location":"reference/gerd/frontends/training/#gerd.frontends.training.Global","title":"Global","text":"

    A singleton class handle to store the current trainer instance.

    "},{"location":"reference/gerd/frontends/training/#gerd.frontends.training.check_trainer","title":"check_trainer","text":"
    check_trainer() -> dict[str, Any]\n

    Check if the trainer is (still) running.

    When the trainer is running, a progress bar is shown. The method returns a gradio property update of 'visible' which can be used to activate and deactivate elements based on the current training status.

    Returns:

    Type Description dict[str, Any]

    A dictionary with the status of gradio 'visible' property

    Source code in gerd/frontends/training.py
    def check_trainer() -> dict[str, Any]:\n    \"\"\"Check if the trainer is (still) running.\n\n    When the trainer is running, a progress bar is shown.\n    The method returns a gradio property update of 'visible'\n    which can be used to activate and deactivate elements based\n    on the current training status.\n\n    Returns:\n        A dictionary with the status of gradio 'visible' property\n    \"\"\"\n    if Global.trainer is not None:\n        progress = gr.Progress()\n        progress(0)\n        while Global.trainer is not None and Global.trainer.thread.is_alive():\n            max_steps = max(Global.trainer.tracked.max_steps, 1)\n            progress(Global.trainer.tracked.current_steps / max_steps)\n            time.sleep(0.5)\n        return gr.update(visible=True, value=\"Training complete\")\n    return gr.update(visible=False)\n
    "},{"location":"reference/gerd/frontends/training/#gerd.frontends.training.get_file_list","title":"get_file_list","text":"
    get_file_list(glob_pattern: str) -> str\n

    Get a list of files matching the glob pattern.

    Parameters:

    Name Type Description Default str

    The glob pattern to search for files

    required

    Returns:

    Type Description str

    A string with the list of files

    Source code in gerd/frontends/training.py
    def get_file_list(glob_pattern: str) -> str:\n    \"\"\"Get a list of files matching the glob pattern.\n\n    Parameters:\n        glob_pattern: The glob pattern to search for files\n\n    Returns:\n        A string with the list of files\n    \"\"\"\n    if not glob_pattern:\n        return \"\"\n    res = [str(f) for f in Path().glob(glob_pattern)]\n    return \"\\n\".join(res) if res else \"<No files found>\"\n
    "},{"location":"reference/gerd/frontends/training/#gerd.frontends.training.get_file_list(glob_pattern)","title":"glob_pattern","text":""},{"location":"reference/gerd/frontends/training/#gerd.frontends.training.get_loras","title":"get_loras","text":"
    get_loras() -> dict[str, Path]\n

    Get a list of available LoRAs.

    LORAs are loaded from the path defined in the default LoraTrainingConfig.

    Returns:

    Type Description dict[str, Path]

    A dictionary with the LoRA names as keys and the paths as values

    Source code in gerd/frontends/training.py
    def get_loras() -> dict[str, Path]:\n    \"\"\"Get a list of available LoRAs.\n\n    LORAs are loaded from the path defined in the default\n    [LoraTrainingConfig][gerd.training.lora.LoraTrainingConfig].\n\n    Returns:\n        A dictionary with the LoRA names as keys and the paths as values\n    \"\"\"\n    return {\n        path.stem: path\n        for path in default_config.output_dir.parent.iterdir()\n        if path.is_file() and path.suffix == \".zip\"\n    }\n
    "},{"location":"reference/gerd/frontends/training/#gerd.frontends.training.start_training","title":"start_training","text":"
    start_training(files: list[str] | None, model_name: str, lora_name: str, mode: str, data_source: str, input_glob: str, override: bool, modules: list[str], flags: list[str], epochs: int, batch_size: int, micro_batch_size: int, cutoff_len: int, overlap_len: int) -> str\n

    Start the training process.

    While training, the method will update the progress bar.

    Parameters:

    Name Type Description Default list[str] | None

    The list of files to train on

    required str

    The name of the model to train

    required str

    The name of the LoRA to train

    required str

    The training mode

    required str

    The source of the data

    required str

    The glob pattern to search for files

    required bool

    Whether to override existing models

    required list[str]

    The modules to train

    required list[str]

    The flags to set

    required int

    The number of epochs to train

    required int

    The batch size

    required int

    The micro batch size

    required int

    The cutoff length

    required int

    The overlap length

    required

    Returns:

    Type Description str

    A string with the status of the training

    Source code in gerd/frontends/training.py
    def start_training(\n    files: list[str] | None,\n    model_name: str,\n    lora_name: str,\n    mode: str,\n    data_source: str,\n    input_glob: str,\n    override: bool,\n    modules: list[str],\n    flags: list[str],\n    epochs: int,\n    batch_size: int,\n    micro_batch_size: int,\n    cutoff_len: int,\n    overlap_len: int,\n) -> str:\n    \"\"\"Start the training process.\n\n    While training, the method will update the progress bar.\n\n    Parameters:\n        files: The list of files to train on\n        model_name: The name of the model to train\n        lora_name: The name of the LoRA to train\n        mode: The training mode\n        data_source: The source of the data\n        input_glob: The glob pattern to search for files\n        override: Whether to override existing models\n        modules: The modules to train\n        flags: The flags to set\n        epochs: The number of epochs to train\n        batch_size: The batch size\n        micro_batch_size: The micro batch size\n        cutoff_len: The cutoff length\n        overlap_len: The overlap length\n\n    Returns:\n        A string with the status of the training\n    \"\"\"\n    if \"..\" in lora_name:\n        msg = \"Invalid LoRA name\"\n        raise gr.Error(msg)\n    progress = gr.Progress()\n    # a bit verbose conversion to appease mypy\n    # since training_mode is a Literal type\n    if mode.lower() not in [\"unstructured\", \"instructions\"]:\n        msg = f\"Invalid training mode '{mode}'\"\n        raise AssertionError(msg)\n    training_mode: Literal[\"unstructured\", \"instructions\"] = (\n        \"unstructured\" if mode.lower() == \"unstructured\" else \"instructions\"\n    )\n    train_config = LoraTrainingConfig(\n        model=ModelConfig(name=model_name),\n        mode=training_mode,\n        output_dir=default_config.output_dir.parent / lora_name,\n        override_existing=override,\n        modules=LoraModules(default=False, **{mod: True for mod in modules}),\n        flags=TrainingFlags(**{flag: True for flag in flags}),\n        epochs=epochs,\n        batch_size=batch_size,\n        micro_batch_size=micro_batch_size,\n        cutoff_len=cutoff_len,\n        input_glob=input_glob,\n        overlap_len=overlap_len,\n        zip_output=True,\n    )\n    # init settings will be overriden by env settings\n    # we need to set the output_dir explicitly\n    train_config.output_dir = default_config.output_dir.parent / lora_name\n\n    if files is None or not files and data_source == \"Upload\":\n        msg = \"No files uploaded\"\n        raise gr.Error(msg)\n    progress(0)\n    try:\n        if train_config.mode == \"unstructured\":\n            Global.trainer = train_unstructured(\n                train_config,\n                (\n                    [Path(f).read_text() for f in files]\n                    if data_source == \"Upload\"\n                    else None\n                ),\n            )\n        elif train_config.mode == \"instructions\":\n\n            def load_data() -> InstructTrainingData:\n                data = InstructTrainingData()\n                for f in files:\n                    with Path(f).open(\"r\") as file:\n                        data.samples.extend(\n                            InstructTrainingData.model_validate(json.load(file)).samples\n                        )\n                return data\n\n            Global.trainer = train_instruct(\n                train_config, load_data() if data_source == \"Upload\" else None\n            )\n        else:\n            msg = \"Invalid training mode\"\n            raise AssertionError(msg)\n    except AssertionError as e:\n        msg = str(e)\n        raise gr.Error(msg) from e\n    return \"Training started\"\n
    "},{"location":"reference/gerd/frontends/training/#gerd.frontends.training.start_training(files)","title":"files","text":""},{"location":"reference/gerd/frontends/training/#gerd.frontends.training.start_training(model_name)","title":"model_name","text":""},{"location":"reference/gerd/frontends/training/#gerd.frontends.training.start_training(lora_name)","title":"lora_name","text":""},{"location":"reference/gerd/frontends/training/#gerd.frontends.training.start_training(mode)","title":"mode","text":""},{"location":"reference/gerd/frontends/training/#gerd.frontends.training.start_training(data_source)","title":"data_source","text":""},{"location":"reference/gerd/frontends/training/#gerd.frontends.training.start_training(input_glob)","title":"input_glob","text":""},{"location":"reference/gerd/frontends/training/#gerd.frontends.training.start_training(override)","title":"override","text":""},{"location":"reference/gerd/frontends/training/#gerd.frontends.training.start_training(modules)","title":"modules","text":""},{"location":"reference/gerd/frontends/training/#gerd.frontends.training.start_training(flags)","title":"flags","text":""},{"location":"reference/gerd/frontends/training/#gerd.frontends.training.start_training(epochs)","title":"epochs","text":""},{"location":"reference/gerd/frontends/training/#gerd.frontends.training.start_training(batch_size)","title":"batch_size","text":""},{"location":"reference/gerd/frontends/training/#gerd.frontends.training.start_training(micro_batch_size)","title":"micro_batch_size","text":""},{"location":"reference/gerd/frontends/training/#gerd.frontends.training.start_training(cutoff_len)","title":"cutoff_len","text":""},{"location":"reference/gerd/frontends/training/#gerd.frontends.training.start_training(overlap_len)","title":"overlap_len","text":""},{"location":"reference/gerd/frontends/training/#gerd.frontends.training.validate_files","title":"validate_files","text":"
    validate_files(file_paths: list[str] | None, mode: str) -> tuple[list[str], dict[str, bool]]\n

    Validate the uploaded files.

    Whether the property 'interactive' is True depends on whether any files were valid. Parameters: file_paths: The list of file paths mode: The training mode

    Returns:

    Type Description tuple[list[str], dict[str, bool]]

    A tuple with the validated file paths and gradio property 'interactive'

    Source code in gerd/frontends/training.py
    def validate_files(\n    file_paths: list[str] | None, mode: str\n) -> tuple[list[str], dict[str, bool]]:\n    \"\"\"Validate the uploaded files.\n\n    Whether the property 'interactive' is True depends on whether\n    any files were valid.\n    Parameters:\n        file_paths: The list of file paths\n        mode: The training mode\n\n    Returns:\n        A tuple with the validated file paths and gradio property 'interactive'\n    \"\"\"\n    file_paths = file_paths or []\n    if mode.lower() == \"instructions\":\n\n        def validate(path: Path) -> bool:\n            if path.suffix != \".json\":\n                gr.Warning(f\"{path.name} has an invalid file type\")\n                return False\n            try:\n                with path.open(\"r\") as f:\n                    InstructTrainingData.model_validate(json.load(f))\n            except Exception as e:\n                gr.Warning(f\"{path.name} could not be validated: {e}\")\n                return False\n            return True\n\n    elif mode.lower() == \"unstructured\":\n\n        def validate(path: Path) -> bool:\n            if path.suffix != \".txt\":\n                gr.Warning(f\"{path.name} has an invalid file type\")\n                return False\n            return True\n\n    else:\n        gr.Error(\"Invalid training mode\")\n        return (file_paths, gr.update(interactive=False))\n\n    res = [f_path for f_path in file_paths if validate(Path(f_path).resolve())]\n    return (res, gr.update(interactive=len(res) > 0))\n
    "},{"location":"reference/gerd/gen/","title":"gerd.gen","text":""},{"location":"reference/gerd/gen/#gerd.gen","title":"gerd.gen","text":"

    Services and utilities for text generation with LLMs.

    Modules:

    Name Description chat_service

    Implementation of the ChatService class.

    generation_service

    Implements the Generation class.

    "},{"location":"reference/gerd/gen/chat_service/","title":"gerd.gen.chat_service","text":""},{"location":"reference/gerd/gen/chat_service/#gerd.gen.chat_service","title":"gerd.gen.chat_service","text":"

    Implementation of the ChatService class.

    This features the currently favoured approach of instruction-based work with large language models. Thus, models fined tuned for chat or instructions work best with this service. The service can be used to generate text as well as long as the model features a chat template. In this case this service should be prefered over the GenerationService since it is easier to setup a prompt according to the model's requirements.

    Classes:

    Name Description ChatService

    Service to generate text based on a chat history.

    "},{"location":"reference/gerd/gen/chat_service/#gerd.gen.chat_service.ChatService","title":"ChatService","text":"
    ChatService(config: GenerationConfig, parameters: Dict[str, str] | None = None)\n

    Service to generate text based on a chat history.

    The service is initialized with a config and parameters.

    The parameters are used to initialize the message history. However, future reset will not consider them. loads a model according to this config. The used LLM is loaded according to the model configuration right on initialization.

    Methods:

    Name Description add_message

    Add a message to the chat history.

    generate

    Generate a response based on the chat history.

    get_prompt_config

    Get the prompt configuration.

    reset

    Reset the chat history.

    set_prompt_config

    Set the prompt configuration.

    submit_user_message

    Submit a message with the user role and generates a response.

    Source code in gerd/gen/chat_service.py
    def __init__(\n    self, config: GenerationConfig, parameters: Dict[str, str] | None = None\n) -> None:\n    \"\"\"The service is initialized with a config and parameters.\n\n    The parameters are used to initialize the message history.\n    However, future reset will not consider them.\n    loads a model according to this config.\n    The used LLM is loaded according to the model configuration\n    right on initialization.\n    \"\"\"\n    self.config = config\n    self._model = gerd_loader.load_model_from_config(self.config.model)\n    self.messages: list[ChatMessage] = []\n    self.reset(parameters)\n
    "},{"location":"reference/gerd/gen/chat_service/#gerd.gen.chat_service.ChatService.add_message","title":"add_message","text":"
    add_message(parameters: Dict[str, str] | None = None, role: Literal['user', 'system', 'assistant'] = 'user', prompt_config: Optional[PromptConfig] = None) -> None\n

    Add a message to the chat history.

    Source code in gerd/gen/chat_service.py
    def add_message(\n    self,\n    parameters: Dict[str, str] | None = None,\n    role: Literal[\"user\", \"system\", \"assistant\"] = \"user\",\n    prompt_config: Optional[PromptConfig] = None,\n) -> None:\n    \"\"\"Add a message to the chat history.\"\"\"\n    parameters = parameters or {}\n    user_prompt: PromptConfig = prompt_config or self.config.model.prompt_config\n    self.messages.append(\n        {\n            \"role\": role,\n            \"content\": user_prompt.format(parameters),\n        }\n    )\n
    "},{"location":"reference/gerd/gen/chat_service/#gerd.gen.chat_service.ChatService.generate","title":"generate","text":"
    generate(parameters: Dict[str, str]) -> GenResponse\n

    Generate a response based on the chat history.

    This method can be used as a replacement for GenerationService.generate in cases where the used model provides a chat template. When this is the case, using this method is more reliable as it requires less manual configuration to set up the prompt according to the model's requirements.

    Parameters:

    Name Type Description Default Dict[str, str]

    The parameters to format the prompt with

    required

    Returns:

    Type Description GenResponse

    The generation result

    Source code in gerd/gen/chat_service.py
    def generate(self, parameters: Dict[str, str]) -> GenResponse:\n    \"\"\"Generate a response based on the chat history.\n\n    This method can be used as a replacement for\n    [GenerationService.generate][gerd.gen.generation_service.GenerationService.generate]\n    in cases where the used model provides a chat template.\n    When this is the case, using this method is more reliable as it requires less\n    manual configuration to set up the prompt according to the model's requirements.\n\n    Parameters:\n        parameters: The parameters to format the prompt with\n\n    Returns:\n        The generation result\n    \"\"\"\n    self.reset(parameters)\n    self.add_message(parameters, role=\"user\")\n\n    if self.config.features.prompt_chaining:\n        for i, prompt_config in enumerate(\n            self.config.features.prompt_chaining.prompts, 1\n        ):\n            self.reset()\n            res = self.submit_user_message(parameters, prompt_config=prompt_config)\n            parameters[f\"response_{i}\"] = res.text\n        response = res.text\n        resolved = \"\\n\".join(parameters.values())\n    else:\n        resolved = \"\\n\".join(m[\"content\"] for m in self.messages)\n        _LOGGER.debug(\n            \"\\n\"\n            \"===== Resolved prompt ======\\n\\n\"\n            \"%s\\n\\n\"\n            \"============================\",\n            resolved,\n        )\n        response = self._model.generate(resolved)\n        _LOGGER.debug(\n            \"\\n\"\n            \"========= Response =========\\n\\n\"\n            \"%s\\n\\n\"\n            \"============================\",\n            response,\n        )\n    return GenResponse(text=response, prompt=resolved)\n
    "},{"location":"reference/gerd/gen/chat_service/#gerd.gen.chat_service.ChatService.generate(parameters)","title":"parameters","text":""},{"location":"reference/gerd/gen/chat_service/#gerd.gen.chat_service.ChatService.get_prompt_config","title":"get_prompt_config","text":"
    get_prompt_config() -> PromptConfig\n

    Get the prompt configuration.

    Source code in gerd/gen/chat_service.py
    def get_prompt_config(self) -> PromptConfig:\n    \"\"\"Get the prompt configuration.\"\"\"\n    return self.config.model.prompt_config\n
    "},{"location":"reference/gerd/gen/chat_service/#gerd.gen.chat_service.ChatService.reset","title":"reset","text":"
    reset(parameters: Dict[str, str] | None = None) -> None\n

    Reset the chat history.

    Source code in gerd/gen/chat_service.py
    def reset(self, parameters: Dict[str, str] | None = None) -> None:\n    \"\"\"Reset the chat history.\"\"\"\n    parameters = parameters or {}\n    self.messages.clear()\n    for role, message in self.config.model.prompt_setup:\n        self.messages.append(\n            {\n                \"role\": role,\n                \"content\": message.format(parameters),\n            }\n        )\n
    "},{"location":"reference/gerd/gen/chat_service/#gerd.gen.chat_service.ChatService.set_prompt_config","title":"set_prompt_config","text":"
    set_prompt_config(config: PromptConfig) -> PromptConfig\n

    Set the prompt configuration.

    Source code in gerd/gen/chat_service.py
    def set_prompt_config(\n    self,\n    config: PromptConfig,\n) -> PromptConfig:\n    \"\"\"Set the prompt configuration.\"\"\"\n    self.config.model.prompt_config = config\n    return self.config.model.prompt_config\n
    "},{"location":"reference/gerd/gen/chat_service/#gerd.gen.chat_service.ChatService.submit_user_message","title":"submit_user_message","text":"
    submit_user_message(parameters: Dict[str, str] | None = None, prompt_config: Optional[PromptConfig] = None) -> GenResponse\n

    Submit a message with the user role and generates a response.

    The service's prompt configuration is used to format the prompt unless a different prompt configuration is provided. Parameters: parameters: The parameters to format the prompt with prompt_config: The optional prompt configuration to be used

    Returns:

    Type Description GenResponse

    The generation result

    Source code in gerd/gen/chat_service.py
    def submit_user_message(\n    self,\n    parameters: Dict[str, str] | None = None,\n    prompt_config: Optional[PromptConfig] = None,\n) -> GenResponse:\n    \"\"\"Submit a message with the user role and generates a response.\n\n    The service's prompt configuration is used to format the prompt unless\n    a different prompt configuration is provided.\n    Parameters:\n        parameters: The parameters to format the prompt with\n        prompt_config: The optional prompt configuration to be used\n\n    Returns:\n        The generation result\n    \"\"\"\n    self.add_message(parameters, role=\"user\", prompt_config=prompt_config)\n    _LOGGER.debug(\n        \"\\n\"\n        \"===== Resolved prompt ======\\n\\n\"\n        \"%s\\n\\n\"\n        \"============================\",\n        \"\\n\".join(m[\"role\"] + \": \" + str(m[\"content\"]) for m in self.messages),\n    )\n    role, response = self._model.create_chat_completion(self.messages)\n    _LOGGER.debug(\n        \"\\n\"\n        \"========= Response =========\\n\\n\"\n        \"%s: %s\\n\\n\"\n        \"============================\",\n        role,\n        response,\n    )\n    self.messages.append({\"role\": role, \"content\": response})\n    return GenResponse(text=response, prompt=self.messages[-2][\"content\"])\n
    "},{"location":"reference/gerd/gen/generation_service/","title":"gerd.gen.generation_service","text":""},{"location":"reference/gerd/gen/generation_service/#gerd.gen.generation_service","title":"gerd.gen.generation_service","text":"

    Implements the Generation class.

    The generation services is meant to generate text based on a prompt and/or the continuation of a provided text.

    Classes:

    Name Description GenerationService

    Service to generate text based on a prompt.

    "},{"location":"reference/gerd/gen/generation_service/#gerd.gen.generation_service.GenerationService","title":"GenerationService","text":"
    GenerationService(config: GenerationConfig)\n

    Service to generate text based on a prompt.

    Initialize the generation service and loads the model.

    Parameters:

    Name Type Description Default GenerationConfig

    The configuration for the generation service

    required

    Methods:

    Name Description generate

    Generate text based on the prompt configuration.

    get_prompt_config

    Get the prompt configuration.

    set_prompt_config

    Sets the prompt configuration.

    Source code in gerd/gen/generation_service.py
    def __init__(self, config: GenerationConfig) -> None:\n    \"\"\"Initialize the generation service and loads the model.\n\n    Parameters:\n        config: The configuration for the generation service\n    \"\"\"\n    self.config = config\n    self._model = gerd_loader.load_model_from_config(self.config.model)\n
    "},{"location":"reference/gerd/gen/generation_service/#gerd.gen.generation_service.GenerationService(config)","title":"config","text":""},{"location":"reference/gerd/gen/generation_service/#gerd.gen.generation_service.GenerationService.generate","title":"generate","text":"
    generate(parameters: Dict[str, str], add_prompt: bool = False) -> GenResponse\n

    Generate text based on the prompt configuration.

    The actual prompt is provided by the prompt configuration. The list of parameters is used to format the prompt and replace the placeholders. The list can be empty if the prompt does not contain any placeholders.

    Parameters:

    Name Type Description Default Dict[str, str]

    The parameters to format the prompt with

    required bool

    Whether to add the prompt to the response

    False

    Returns:

    Type Description GenResponse

    The generation result

    Source code in gerd/gen/generation_service.py
    def generate(\n    self, parameters: Dict[str, str], add_prompt: bool = False\n) -> GenResponse:\n    \"\"\"Generate text based on the prompt configuration.\n\n    The actual prompt is provided by the prompt configuration.\n    The list of parameters is used to format the prompt\n    and replace the placeholders. The list can be empty if\n    the prompt does not contain any placeholders.\n\n    Parameters:\n        parameters: The parameters to format the prompt with\n        add_prompt: Whether to add the prompt to the response\n\n    Returns:\n        The generation result\n    \"\"\"\n    if self.config.features.prompt_chaining:\n        from gerd.features.prompt_chaining import PromptChaining\n\n        response = PromptChaining(\n            self.config.features.prompt_chaining,\n            self._model,\n            self.config.model.prompt_config,\n        ).generate(parameters)\n    else:\n        template = self.config.model.prompt_config.template\n        resolved = (\n            template.render(**parameters)\n            if template\n            else self.config.model.prompt_config.text.format(**parameters)\n        )\n        _LOGGER.debug(\n            \"\\n====== Resolved prompt =====\\n\\n%s\\n\\n=============================\",\n            resolved,\n        )\n        response = self._model.generate(resolved)\n        _LOGGER.debug(\n            \"\\n====== Response =====\\n\\n%s\\n\\n=============================\",\n            response,\n        )\n    return GenResponse(text=response, prompt=resolved if add_prompt else None)\n
    "},{"location":"reference/gerd/gen/generation_service/#gerd.gen.generation_service.GenerationService.generate(parameters)","title":"parameters","text":""},{"location":"reference/gerd/gen/generation_service/#gerd.gen.generation_service.GenerationService.generate(add_prompt)","title":"add_prompt","text":""},{"location":"reference/gerd/gen/generation_service/#gerd.gen.generation_service.GenerationService.get_prompt_config","title":"get_prompt_config","text":"
    get_prompt_config() -> PromptConfig\n

    Get the prompt configuration.

    Returns:

    Type Description PromptConfig

    The prompt configuration

    Source code in gerd/gen/generation_service.py
    def get_prompt_config(self) -> PromptConfig:\n    \"\"\"Get the prompt configuration.\n\n    Returns:\n        The prompt configuration\n    \"\"\"\n    return self.config.model.prompt_config\n
    "},{"location":"reference/gerd/gen/generation_service/#gerd.gen.generation_service.GenerationService.set_prompt_config","title":"set_prompt_config","text":"
    set_prompt_config(config: PromptConfig) -> PromptConfig\n

    Sets the prompt configuration.

    Parameters:

    Name Type Description Default PromptConfig

    The prompt configuration

    required

    Returns: The prompt configuration; Should be the same as the input in most cases

    Source code in gerd/gen/generation_service.py
    def set_prompt_config(\n    self,\n    config: PromptConfig,\n) -> PromptConfig:\n    \"\"\"Sets the prompt configuration.\n\n    Parameters:\n        config: The prompt configuration\n    Returns:\n        The prompt configuration; Should be the same as the input in most cases\n    \"\"\"\n    self.config.model.prompt_config = config\n    return self.config.model.prompt_config\n
    "},{"location":"reference/gerd/gen/generation_service/#gerd.gen.generation_service.GenerationService.set_prompt_config(config)","title":"config","text":""},{"location":"reference/gerd/loader/","title":"gerd.loader","text":""},{"location":"reference/gerd/loader/#gerd.loader","title":"gerd.loader","text":"

    Module for loading language models.

    Depending on the configuration, different language models are loaded and different libraries are used. The main goal is to provide a unified interface to the different models and libraries.

    Classes:

    Name Description LLM

    The abstract base class for large language models.

    LlamaCppLLM

    A language model using the Llama.cpp library.

    MockLLM

    A mock language model for testing purposes.

    RemoteLLM

    A language model using a remote endpoint.

    TransformerLLM

    A language model using the transformers library.

    Functions:

    Name Description load_model_from_config

    Loads a language model based on the configuration.

    "},{"location":"reference/gerd/loader/#gerd.loader.LLM","title":"LLM","text":"
    LLM(config: ModelConfig)\n

    The abstract base class for large language models.

    Should be implemented by all language model backends.

    A language model is initialized with a configuration.

    Parameters:

    Name Type Description Default ModelConfig

    The configuration for the language model

    required

    Methods:

    Name Description create_chat_completion

    Create a chat completion based on a list of messages.

    generate

    Generate text based on a prompt.

    Source code in gerd/loader.py
    @abc.abstractmethod\ndef __init__(self, config: ModelConfig) -> None:\n    \"\"\"A language model is initialized with a configuration.\n\n    Parameters:\n        config: The configuration for the language model\n    \"\"\"\n    pass\n
    "},{"location":"reference/gerd/loader/#gerd.loader.LLM(config)","title":"config","text":""},{"location":"reference/gerd/loader/#gerd.loader.LLM.create_chat_completion","title":"create_chat_completion abstractmethod","text":"
    create_chat_completion(messages: list[ChatMessage]) -> tuple[ChatRole, str]\n

    Create a chat completion based on a list of messages.

    Parameters:

    Name Type Description Default list[ChatMessage]

    The list of messages in the chat history

    required

    Returns:

    Type Description tuple[ChatRole, str]

    The role of the generated message and the content

    Source code in gerd/loader.py
    @abc.abstractmethod\ndef create_chat_completion(\n    self, messages: list[ChatMessage]\n) -> tuple[ChatRole, str]:\n    \"\"\"Create a chat completion based on a list of messages.\n\n    Parameters:\n        messages: The list of messages in the chat history\n\n    Returns:\n        The role of the generated message and the content\n    \"\"\"\n    pass\n
    "},{"location":"reference/gerd/loader/#gerd.loader.LLM.create_chat_completion(messages)","title":"messages","text":""},{"location":"reference/gerd/loader/#gerd.loader.LLM.generate","title":"generate abstractmethod","text":"
    generate(prompt: str) -> str\n

    Generate text based on a prompt.

    Parameters:

    Name Type Description Default str

    The prompt to generate text from

    required

    Returns:

    Type Description str

    The generated text

    Source code in gerd/loader.py
    @abc.abstractmethod\ndef generate(self, prompt: str) -> str:\n    \"\"\"Generate text based on a prompt.\n\n    Parameters:\n        prompt: The prompt to generate text from\n\n    Returns:\n        The generated text\n    \"\"\"\n    pass\n
    "},{"location":"reference/gerd/loader/#gerd.loader.LLM.generate(prompt)","title":"prompt","text":""},{"location":"reference/gerd/loader/#gerd.loader.LlamaCppLLM","title":"LlamaCppLLM","text":"
    LlamaCppLLM(config: ModelConfig)\n

    Bases: LLM

    A language model using the Llama.cpp library.

    A language model is initialized with a configuration.

    Parameters:

    Name Type Description Default ModelConfig

    The configuration for the language model

    required

    Methods:

    Name Description create_chat_completion

    Create a chat completion based on a list of messages.

    generate

    Generate text based on a prompt.

    Source code in gerd/loader.py
    @override\ndef __init__(self, config: ModelConfig) -> None:\n    from llama_cpp import Llama\n\n    self.config = config\n    self._model = Llama.from_pretrained(\n        repo_id=config.name,\n        filename=config.file,\n        n_ctx=config.context_length,\n        n_gpu_layers=config.gpu_layers,\n        n_threads=config.threads,\n        **config.extra_kwargs or {},\n    )\n
    "},{"location":"reference/gerd/loader/#gerd.loader.LlamaCppLLM(config)","title":"config","text":""},{"location":"reference/gerd/loader/#gerd.loader.LlamaCppLLM.create_chat_completion","title":"create_chat_completion","text":"
    create_chat_completion(messages: list[ChatMessage]) -> tuple[ChatRole, str]\n

    Create a chat completion based on a list of messages.

    Parameters:

    Name Type Description Default list[ChatMessage]

    The list of messages in the chat history

    required

    Returns:

    Type Description tuple[ChatRole, str]

    The role of the generated message and the content

    Source code in gerd/loader.py
    @override\ndef create_chat_completion(\n    self, messages: list[ChatMessage]\n) -> tuple[ChatRole, str]:\n    res = self._model.create_chat_completion(\n        # mypy cannot resolve the role parameter even though\n        # is is defined on compatible literals\n        [{\"role\": m[\"role\"], \"content\": m[\"content\"]} for m in messages],  # type: ignore[misc]\n        stop=self.config.stop,\n        max_tokens=self.config.max_new_tokens,\n        top_p=self.config.top_p,\n        top_k=self.config.top_k,\n        temperature=self.config.temperature,\n        repeat_penalty=self.config.repetition_penalty,\n    )\n    if not isinstance(res, Iterator):\n        msg = res[\"choices\"][0][\"message\"]\n        if msg[\"role\"] == \"function\":\n            error_msg = \"function role not expected\"\n            raise NotImplementedError(error_msg)\n        return (msg[\"role\"], msg[\"content\"].strip() if msg[\"content\"] else \"\")\n\n    error_msg = \"Cannot process stream responses for now\"\n    raise NotImplementedError(error_msg)\n
    "},{"location":"reference/gerd/loader/#gerd.loader.LlamaCppLLM.create_chat_completion(messages)","title":"messages","text":""},{"location":"reference/gerd/loader/#gerd.loader.LlamaCppLLM.generate","title":"generate","text":"
    generate(prompt: str) -> str\n

    Generate text based on a prompt.

    Parameters:

    Name Type Description Default str

    The prompt to generate text from

    required

    Returns:

    Type Description str

    The generated text

    Source code in gerd/loader.py
    @override\ndef generate(self, prompt: str) -> str:\n    res = self._model(\n        prompt,\n        stop=self.config.stop,\n        max_tokens=self.config.max_new_tokens,\n        top_p=self.config.top_p,\n        top_k=self.config.top_k,\n        temperature=self.config.temperature,\n        repeat_penalty=self.config.repetition_penalty,\n    )\n    output = next(res) if isinstance(res, Iterator) else res\n    return output[\"choices\"][0][\"text\"]\n
    "},{"location":"reference/gerd/loader/#gerd.loader.LlamaCppLLM.generate(prompt)","title":"prompt","text":""},{"location":"reference/gerd/loader/#gerd.loader.MockLLM","title":"MockLLM","text":"
    MockLLM(_: ModelConfig)\n

    Bases: LLM

    A mock language model for testing purposes.

    A language model is initialized with a configuration.

    Parameters:

    Name Type Description Default ModelConfig

    The configuration for the language model

    required

    Methods:

    Name Description create_chat_completion

    Create a chat completion based on a list of messages.

    generate

    Generate text based on a prompt.

    Source code in gerd/loader.py
    @override\ndef __init__(self, _: ModelConfig) -> None:\n    self.ret_value = \"MockLLM\"\n    pass\n
    "},{"location":"reference/gerd/loader/#gerd.loader.MockLLM(config)","title":"config","text":""},{"location":"reference/gerd/loader/#gerd.loader.MockLLM.create_chat_completion","title":"create_chat_completion","text":"
    create_chat_completion(_: list[ChatMessage]) -> tuple[ChatRole, str]\n

    Create a chat completion based on a list of messages.

    Parameters:

    Name Type Description Default list[ChatMessage]

    The list of messages in the chat history

    required

    Returns:

    Type Description tuple[ChatRole, str]

    The role of the generated message and the content

    Source code in gerd/loader.py
    @override\ndef create_chat_completion(self, _: list[ChatMessage]) -> tuple[ChatRole, str]:\n    return (\"assistant\", self.ret_value)\n
    "},{"location":"reference/gerd/loader/#gerd.loader.MockLLM.create_chat_completion(messages)","title":"messages","text":""},{"location":"reference/gerd/loader/#gerd.loader.MockLLM.generate","title":"generate","text":"
    generate(_: str) -> str\n

    Generate text based on a prompt.

    Parameters:

    Name Type Description Default str

    The prompt to generate text from

    required

    Returns:

    Type Description str

    The generated text

    Source code in gerd/loader.py
    @override\ndef generate(self, _: str) -> str:\n    return self.ret_value\n
    "},{"location":"reference/gerd/loader/#gerd.loader.MockLLM.generate(prompt)","title":"prompt","text":""},{"location":"reference/gerd/loader/#gerd.loader.RemoteLLM","title":"RemoteLLM","text":"
    RemoteLLM(config: ModelConfig)\n

    Bases: LLM

    A language model using a remote endpoint.

    The endpoint can be any service that are compatible with llama.cpp and openai API. For further information, please refer to the llama.cpp server API.

    A language model is initialized with a configuration.

    Parameters:

    Name Type Description Default ModelConfig

    The configuration for the language model

    required

    Methods:

    Name Description create_chat_completion

    Create a chat completion based on a list of messages.

    generate

    Generate text based on a prompt.

    Source code in gerd/loader.py
    @override\ndef __init__(self, config: ModelConfig) -> None:\n    self.config = config\n    if self.config.endpoint is None:\n        msg = \"Endpoint is required for remote LLM\"\n        raise ValueError(msg)\n\n    self._ep: ModelEndpoint = self.config.endpoint\n    self._prompt_field = \"prompt\"\n    self._header = {\"Content-Type\": \"application/json\"}\n    if self.config.endpoint.key:\n        self._header[\"Authorization\"] = (\n            f\"Bearer {self.config.endpoint.key.get_secret_value()}\"\n        )\n\n    if self.config.endpoint.type == \"openai\":\n        self.msg_template = {\n            \"model\": self.config.name,\n            \"temperature\": self.config.temperature,\n            \"frequency_penalty\": self.config.repetition_penalty,\n            \"max_completion_tokens\": self.config.max_new_tokens,\n            \"n\": 1,\n            \"stop\": self.config.stop,\n            \"top_p\": self.config.top_p,\n        }\n    elif self.config.endpoint.type == \"llama.cpp\":\n        self.msg_template = {\n            \"temperature\": self.config.temperature,\n            \"top_k\": self.config.top_k,\n            \"top_p\": self.config.top_p,\n            \"repeat_penalty\": self.config.repetition_penalty,\n            \"n_predict\": self.config.max_new_tokens,\n            \"stop\": self.config.stop or [],\n        }\n    else:\n        msg = f\"Unknown endpoint type: {self.config.endpoint.type}\"\n        raise ValueError(msg)\n
    "},{"location":"reference/gerd/loader/#gerd.loader.RemoteLLM(config)","title":"config","text":""},{"location":"reference/gerd/loader/#gerd.loader.RemoteLLM.create_chat_completion","title":"create_chat_completion","text":"
    create_chat_completion(messages: list[ChatMessage]) -> tuple[ChatRole, str]\n

    Create a chat completion based on a list of messages.

    Parameters:

    Name Type Description Default list[ChatMessage]

    The list of messages in the chat history

    required

    Returns:

    Type Description tuple[ChatRole, str]

    The role of the generated message and the content

    Source code in gerd/loader.py
    @override\ndef create_chat_completion(\n    self, messages: list[ChatMessage]\n) -> tuple[ChatRole, str]:\n    import json\n\n    import requests\n\n    self.msg_template[\"messages\"] = messages\n    res = requests.post(\n        self._ep.url + \"/v1/chat/completions\",\n        headers=self._header,\n        data=json.dumps(self.msg_template),\n        timeout=300,\n    )\n    if res.status_code == 200:\n        msg = res.json()[\"choices\"][0][\"message\"]\n        return (msg[\"role\"], msg[\"content\"].strip())\n    else:\n        _LOGGER.warning(\"Server returned error code %d\", res.status_code)\n    return (\"assistant\", \"\")\n
    "},{"location":"reference/gerd/loader/#gerd.loader.RemoteLLM.create_chat_completion(messages)","title":"messages","text":""},{"location":"reference/gerd/loader/#gerd.loader.RemoteLLM.generate","title":"generate","text":"
    generate(prompt: str) -> str\n

    Generate text based on a prompt.

    Parameters:

    Name Type Description Default str

    The prompt to generate text from

    required

    Returns:

    Type Description str

    The generated text

    Source code in gerd/loader.py
    @override\ndef generate(self, prompt: str) -> str:\n    import json\n\n    import requests\n\n    if self.config.endpoint and self.config.endpoint.type != \"llama.cpp\":\n        msg = (\n            \"Only llama.cpp supports simple completion yet. \"\n            \"Use chat completion instead.\"\n        )\n        raise NotImplementedError(msg)\n\n    self.msg_template[self._prompt_field] = prompt\n    res = requests.post(\n        self._ep.url + \"/completion\",\n        headers=self._header,\n        data=json.dumps(self.msg_template),\n        timeout=300,\n    )\n    if res.status_code == 200:\n        return str(res.json()[\"content\"])\n    else:\n        _LOGGER.warning(\"Server returned error code %d\", res.status_code)\n    return \"\"\n
    "},{"location":"reference/gerd/loader/#gerd.loader.RemoteLLM.generate(prompt)","title":"prompt","text":""},{"location":"reference/gerd/loader/#gerd.loader.TransformerLLM","title":"TransformerLLM","text":"
    TransformerLLM(config: ModelConfig)\n

    Bases: LLM

    A language model using the transformers library.

    A language model is initialized with a configuration.

    Parameters:

    Name Type Description Default ModelConfig

    The configuration for the language model

    required

    Methods:

    Name Description create_chat_completion

    Create a chat completion based on a list of messages.

    generate

    Generate text based on a prompt.

    Source code in gerd/loader.py
    @override\ndef __init__(self, config: ModelConfig) -> None:\n    import torch\n    from transformers import (\n        AutoModelForCausalLM,\n        AutoTokenizer,\n        PreTrainedModel,\n        pipeline,\n    )\n\n    # use_fast=False is ignored by transformers\n    os.environ[\"TOKENIZERS_PARALLELISM\"] = \"false\"\n\n    self.config = config\n    torch_dtypes: dict[str, torch.dtype] = {\n        \"bfloat16\": torch.bfloat16,\n        \"float16\": torch.float16,\n        \"float32\": torch.float32,\n        \"float64\": torch.float64,\n    }\n\n    model_kwargs = config.extra_kwargs or {}\n    if config.torch_dtype in torch_dtypes:\n        model_kwargs[\"torch_dtype\"] = torch_dtypes[config.torch_dtype]\n\n    tokenizer = AutoTokenizer.from_pretrained(config.name, use_fast=False)\n    model: PreTrainedModel = AutoModelForCausalLM.from_pretrained(  # type: ignore[no-any-unimported]\n        config.name, **model_kwargs\n    )\n\n    loaded_loras = set()\n    for lora in config.loras:\n        _LOGGER.info(\"Loading adapter %s\", lora)\n        if not Path(lora / \"adapter_model.safetensors\").exists():\n            _LOGGER.warning(\"Adapter %s does not exist\", lora)\n            continue\n        model.load_adapter(lora)\n        loaded_loras.add(lora)\n        train_params = Path(lora) / \"training_parameters.json\"\n        if train_params.exists() and tokenizer.pad_token_id is None:\n            from gerd.training.lora import LoraTrainingConfig\n\n            with open(train_params, \"r\") as f:\n                lora_config = LoraTrainingConfig.model_validate_json(f.read())\n                tokenizer.pad_token_id = lora_config.pad_token_id\n                # https://github.com/huggingface/transformers/issues/34842#issuecomment-2490994584\n                tokenizer.padding_side = (\n                    \"left\" if lora_config.padding_side == \"right\" else \"right\"\n                )\n                # tokenizer.padding_side = lora_config.padding_side\n\n    if loaded_loras:\n        model.enable_adapters()\n\n    self._pipe = pipeline(\n        task=\"text-generation\",\n        model=model,\n        tokenizer=tokenizer,\n        # device_map=\"auto\",  # https://github.com/huggingface/transformers/issues/31922\n        device=(\n            \"cuda\"\n            if config.gpu_layers > 0\n            else \"mps\"\n            if torch.backends.mps.is_available()\n            else \"cpu\"\n        ),\n        framework=\"pt\",\n        use_fast=False,\n    )\n
    "},{"location":"reference/gerd/loader/#gerd.loader.TransformerLLM(config)","title":"config","text":""},{"location":"reference/gerd/loader/#gerd.loader.TransformerLLM.create_chat_completion","title":"create_chat_completion","text":"
    create_chat_completion(messages: list[ChatMessage]) -> tuple[ChatRole, str]\n

    Create a chat completion based on a list of messages.

    Parameters:

    Name Type Description Default list[ChatMessage]

    The list of messages in the chat history

    required

    Returns:

    Type Description tuple[ChatRole, str]

    The role of the generated message and the content

    Source code in gerd/loader.py
    @override\ndef create_chat_completion(\n    self, messages: list[ChatMessage]\n) -> tuple[ChatRole, str]:\n    msg = self._pipe(\n        messages,\n        max_new_tokens=self.config.max_new_tokens,\n        repetition_penalty=self.config.repetition_penalty,\n        top_k=self.config.top_k,\n        top_p=self.config.top_p,\n        temperature=self.config.temperature,\n        do_sample=True,\n    )[0][\"generated_text\"][-1]\n    return (msg[\"role\"], msg[\"content\"].strip())\n
    "},{"location":"reference/gerd/loader/#gerd.loader.TransformerLLM.create_chat_completion(messages)","title":"messages","text":""},{"location":"reference/gerd/loader/#gerd.loader.TransformerLLM.generate","title":"generate","text":"
    generate(prompt: str) -> str\n

    Generate text based on a prompt.

    Parameters:

    Name Type Description Default str

    The prompt to generate text from

    required

    Returns:

    Type Description str

    The generated text

    Source code in gerd/loader.py
    @override\ndef generate(self, prompt: str) -> str:\n    res = self._pipe(\n        prompt,\n        max_new_tokens=self.config.max_new_tokens,\n        repetition_penalty=self.config.repetition_penalty,\n        top_k=self.config.top_k,\n        top_p=self.config.top_p,\n        temperature=self.config.temperature,\n        do_sample=True,\n    )\n    output: str = res[0][\"generated_text\"]\n    return output\n
    "},{"location":"reference/gerd/loader/#gerd.loader.TransformerLLM.generate(prompt)","title":"prompt","text":""},{"location":"reference/gerd/loader/#gerd.loader.load_model_from_config","title":"load_model_from_config","text":"
    load_model_from_config(config: ModelConfig) -> LLM\n

    Loads a language model based on the configuration.

    Which language model is loaded depends on the configuration. For instance, if an endpoint is provided, a remote language model is loaded. If a file is provided, Llama.cpp is used. Otherwise, transformers is used.

    Parameters:

    Name Type Description Default ModelConfig

    The configuration for the language model

    required

    Returns:

    Type Description LLM

    The loaded language model

    Source code in gerd/loader.py
    def load_model_from_config(config: ModelConfig) -> LLM:\n    \"\"\"Loads a language model based on the configuration.\n\n    Which language model is loaded depends on the configuration.\n    For instance, if an endpoint is provided, a remote language model is loaded.\n    If a file is provided, Llama.cpp is used.\n    Otherwise, transformers is used.\n\n    Parameters:\n        config: The configuration for the language model\n\n    Returns:\n        The loaded language model\n\n    \"\"\"\n    if config.endpoint:\n        _LOGGER.info(\"Using remote endpoint %s\", config.endpoint.url)\n        return RemoteLLM(config)\n    if config.file:\n        _LOGGER.info(\n            \"Using Llama.cpp with model %s and file %s\", config.name, config.file\n        )\n        return LlamaCppLLM(config)\n    _LOGGER.info(\"Using transformers with model %s\", config.name)\n    return TransformerLLM(config)\n
    "},{"location":"reference/gerd/loader/#gerd.loader.load_model_from_config(config)","title":"config","text":""},{"location":"reference/gerd/models/","title":"gerd.models","text":""},{"location":"reference/gerd/models/#gerd.models","title":"gerd.models","text":"

    Pydantic model definitions and data classes that are share accross modules.

    Modules:

    Name Description gen

    Models for the generation and chat service.

    label

    Data definitions for Label Studio tasks.

    logging

    Logging configuration and utilities.

    model

    Model configuration for supported model classes.

    qa

    Data definitions for QA model configuration.

    server

    Server configuration model for REST backends.

    "},{"location":"reference/gerd/models/gen/","title":"gerd.models.gen","text":""},{"location":"reference/gerd/models/gen/#gerd.models.gen","title":"gerd.models.gen","text":"

    Models for the generation and chat service.

    Classes:

    Name Description GenerationConfig

    Configuration for the generation services.

    GenerationFeaturesConfig

    Configuration for the generation-specific features.

    "},{"location":"reference/gerd/models/gen/#gerd.models.gen.GenerationConfig","title":"GenerationConfig","text":"

    Bases: BaseSettings

    Configuration for the generation services.

    A configuration can be used for the GenerationService or the ChatService. Both support to generate text based on a prompt.

    Methods:

    Name Description settings_customise_sources

    Customize the settings sources used by pydantic-settings.

    Attributes:

    Name Type Description features GenerationFeaturesConfig

    The extra features to be used for the generation service.

    model ModelConfig

    The model to be used for the generation service.

    "},{"location":"reference/gerd/models/gen/#gerd.models.gen.GenerationConfig.features","title":"features class-attribute instance-attribute","text":"
    features: GenerationFeaturesConfig = GenerationFeaturesConfig()\n

    The extra features to be used for the generation service.

    "},{"location":"reference/gerd/models/gen/#gerd.models.gen.GenerationConfig.model","title":"model class-attribute instance-attribute","text":"
    model: ModelConfig = ModelConfig()\n

    The model to be used for the generation service.

    "},{"location":"reference/gerd/models/gen/#gerd.models.gen.GenerationConfig.settings_customise_sources","title":"settings_customise_sources classmethod","text":"
    settings_customise_sources(_: Type[BaseSettings], init_settings: PydanticBaseSettingsSource, env_settings: PydanticBaseSettingsSource, dotenv_settings: PydanticBaseSettingsSource, file_secret_settings: PydanticBaseSettingsSource) -> Tuple[PydanticBaseSettingsSource, ...]\n

    Customize the settings sources used by pydantic-settings.

    The order of the sources is important. The first source has the highest priority.

    Parameters:

    Name Type Description Default

    The class of the settings.

    required PydanticBaseSettingsSource

    The settings from the initialization.

    required PydanticBaseSettingsSource

    The settings from the environment.

    required PydanticBaseSettingsSource

    The settings from the dotenv file.

    required PydanticBaseSettingsSource

    The settings from the secret file.

    required

    Returns:

    Type Description Tuple[PydanticBaseSettingsSource, ...]

    The customized settings sources.

    Source code in gerd/models/gen.py
    @classmethod\ndef settings_customise_sources(\n    cls,\n    _: Type[BaseSettings],\n    init_settings: PydanticBaseSettingsSource,\n    env_settings: PydanticBaseSettingsSource,\n    dotenv_settings: PydanticBaseSettingsSource,\n    file_secret_settings: PydanticBaseSettingsSource,\n) -> Tuple[PydanticBaseSettingsSource, ...]:\n    \"\"\"Customize the settings sources used by pydantic-settings.\n\n    The order of the sources is important.\n    The first source has the highest priority.\n\n    Parameters:\n        cls: The class of the settings.\n        init_settings: The settings from the initialization.\n        env_settings: The settings from the environment.\n        dotenv_settings: The settings from the dotenv file.\n        file_secret_settings: The settings from the secret file.\n\n    Returns:\n        The customized settings sources.\n    \"\"\"\n    return (\n        file_secret_settings,\n        env_settings,\n        dotenv_settings,\n        init_settings,\n    )\n
    "},{"location":"reference/gerd/models/gen/#gerd.models.gen.GenerationConfig.settings_customise_sources(cls)","title":"cls","text":""},{"location":"reference/gerd/models/gen/#gerd.models.gen.GenerationConfig.settings_customise_sources(init_settings)","title":"init_settings","text":""},{"location":"reference/gerd/models/gen/#gerd.models.gen.GenerationConfig.settings_customise_sources(env_settings)","title":"env_settings","text":""},{"location":"reference/gerd/models/gen/#gerd.models.gen.GenerationConfig.settings_customise_sources(dotenv_settings)","title":"dotenv_settings","text":""},{"location":"reference/gerd/models/gen/#gerd.models.gen.GenerationConfig.settings_customise_sources(file_secret_settings)","title":"file_secret_settings","text":""},{"location":"reference/gerd/models/gen/#gerd.models.gen.GenerationFeaturesConfig","title":"GenerationFeaturesConfig","text":"

    Bases: BaseModel

    Configuration for the generation-specific features.

    Attributes:

    Name Type Description prompt_chaining PromptChainingConfig | None

    Configuration for prompt chaining.

    "},{"location":"reference/gerd/models/gen/#gerd.models.gen.GenerationFeaturesConfig.prompt_chaining","title":"prompt_chaining class-attribute instance-attribute","text":"
    prompt_chaining: PromptChainingConfig | None = None\n

    Configuration for prompt chaining.

    "},{"location":"reference/gerd/models/label/","title":"gerd.models.label","text":""},{"location":"reference/gerd/models/label/#gerd.models.label","title":"gerd.models.label","text":"

    Data definitions for Label Studio tasks.

    The defined models and enums are used to parse and work with Label Studio data exported as JSON.

    Classes:

    Name Description LabelStudioAnnotation

    Annotation of a Label Studio task.

    LabelStudioAnnotationResult

    Result of a Label Studio annotation.

    LabelStudioAnnotationValue

    Value of a Label Studio annotation.

    LabelStudioLabel

    Labels for the GRASCCO Label Studio annotations.

    LabelStudioTask

    Task of a Label Studio project.

    Functions:

    Name Description load_label_studio_tasks

    Load Label Studio tasks from a JSON file.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotation","title":"LabelStudioAnnotation","text":"

    Bases: BaseModel

    Annotation of a Label Studio task.

    A collection of annotations is associated with a task.

    Attributes:

    Name Type Description completed_by int

    The user ID of the user who completed the annotation.

    created_at str

    The creation date of the annotation.

    draft_created_at Optional[str]

    The creation date of the draft.

    ground_truth bool

    Whether the annotation is ground truth.

    id int

    The ID of the annotation.

    import_id Optional[str]

    The import ID of the annotation.

    last_action Optional[str]

    The last action of the annotation.

    last_created_by Optional[int]

    The user ID of the user who last created the annotation.

    lead_time float

    The lead time of the annotation.

    parent_annotation Optional[str]

    The parent annotation.

    parent_prediction Optional[str]

    The parent prediction.

    prediction Dict[str, str]

    The prediction of the annotation.

    project int

    The project ID of the annotation.

    result List[LabelStudioAnnotationResult]

    The results of the annotation.

    result_count int

    The number of results.

    task int

    The task ID of the annotation.

    unique_id str

    The unique ID of the annotation.

    updated_at str

    The update date of the annotation.

    updated_by int

    The user ID of the user who updated the annotation.

    was_cancelled bool

    Whether the annotation was cancelled.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotation.completed_by","title":"completed_by instance-attribute","text":"
    completed_by: int\n

    The user ID of the user who completed the annotation.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotation.created_at","title":"created_at instance-attribute","text":"
    created_at: str\n

    The creation date of the annotation.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotation.draft_created_at","title":"draft_created_at instance-attribute","text":"
    draft_created_at: Optional[str]\n

    The creation date of the draft.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotation.ground_truth","title":"ground_truth instance-attribute","text":"
    ground_truth: bool\n

    Whether the annotation is ground truth.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotation.id","title":"id instance-attribute","text":"
    id: int\n

    The ID of the annotation.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotation.import_id","title":"import_id instance-attribute","text":"
    import_id: Optional[str]\n

    The import ID of the annotation.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotation.last_action","title":"last_action instance-attribute","text":"
    last_action: Optional[str]\n

    The last action of the annotation.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotation.last_created_by","title":"last_created_by instance-attribute","text":"
    last_created_by: Optional[int]\n

    The user ID of the user who last created the annotation.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotation.lead_time","title":"lead_time instance-attribute","text":"
    lead_time: float\n

    The lead time of the annotation.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotation.parent_annotation","title":"parent_annotation instance-attribute","text":"
    parent_annotation: Optional[str]\n

    The parent annotation.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotation.parent_prediction","title":"parent_prediction instance-attribute","text":"
    parent_prediction: Optional[str]\n

    The parent prediction.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotation.prediction","title":"prediction instance-attribute","text":"
    prediction: Dict[str, str]\n

    The prediction of the annotation.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotation.project","title":"project instance-attribute","text":"
    project: int\n

    The project ID of the annotation.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotation.result","title":"result instance-attribute","text":"
    result: List[LabelStudioAnnotationResult]\n

    The results of the annotation.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotation.result_count","title":"result_count instance-attribute","text":"
    result_count: int\n

    The number of results.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotation.task","title":"task instance-attribute","text":"
    task: int\n

    The task ID of the annotation.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotation.unique_id","title":"unique_id instance-attribute","text":"
    unique_id: str\n

    The unique ID of the annotation.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotation.updated_at","title":"updated_at instance-attribute","text":"
    updated_at: str\n

    The update date of the annotation.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotation.updated_by","title":"updated_by instance-attribute","text":"
    updated_by: int\n

    The user ID of the user who updated the annotation.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotation.was_cancelled","title":"was_cancelled instance-attribute","text":"
    was_cancelled: bool\n

    Whether the annotation was cancelled.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotationResult","title":"LabelStudioAnnotationResult","text":"

    Bases: BaseModel

    Result of a Label Studio annotation.

    Attributes:

    Name Type Description from_name str

    The name of the source.

    id str

    The ID of the result.

    origin str

    The origin of the result.

    to_name str

    The name of the target.

    type str

    The type of the result.

    value LabelStudioAnnotationValue

    The value of the result.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotationResult.from_name","title":"from_name instance-attribute","text":"
    from_name: str\n

    The name of the source.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotationResult.id","title":"id instance-attribute","text":"
    id: str\n

    The ID of the result.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotationResult.origin","title":"origin instance-attribute","text":"
    origin: str\n

    The origin of the result.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotationResult.to_name","title":"to_name instance-attribute","text":"
    to_name: str\n

    The name of the target.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotationResult.type","title":"type instance-attribute","text":"
    type: str\n

    The type of the result.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotationResult.value","title":"value instance-attribute","text":"
    value: LabelStudioAnnotationValue\n

    The value of the result.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotationValue","title":"LabelStudioAnnotationValue","text":"

    Bases: BaseModel

    Value of a Label Studio annotation.

    Attributes:

    Name Type Description end int

    The end of the annotation.

    labels List[LabelStudioLabel]

    The labels of the annotation.

    start int

    The start of the annotation.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotationValue.end","title":"end instance-attribute","text":"
    end: int\n

    The end of the annotation.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotationValue.labels","title":"labels instance-attribute","text":"
    labels: List[LabelStudioLabel]\n

    The labels of the annotation.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioAnnotationValue.start","title":"start instance-attribute","text":"
    start: int\n

    The start of the annotation.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioLabel","title":"LabelStudioLabel","text":"

    Bases: Enum

    Labels for the GRASCCO Label Studio annotations.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioTask","title":"LabelStudioTask","text":"

    Bases: BaseModel

    Task of a Label Studio project.

    A task is a single unit of work that can be annotated by a user. Tasks can be used to train an auto labeler or to evaluate the performance of a model.

    Attributes:

    Name Type Description annotations List[LabelStudioAnnotation]

    The annotations of the task.

    cancelled_annotations int

    The number of cancelled annotations.

    comment_authors List[str]

    The authors of the comments.

    comment_count int

    The number of comments.

    created_at str

    The creation date of the task.

    data Optional[Dict[str, str]]

    The data of the task.

    drafts List[str]

    The drafts of the task.

    file_name str

    Extracts the original file name from the file upload.

    file_upload str

    The file upload of the task.

    id int

    The ID of the task.

    inner_id int

    The inner ID of the task.

    last_comment_updated_at Optional[str]

    The update date of the last comment.

    meta Optional[Dict[str, str]]

    The meta data of the task.

    predictions List[str]

    The predictions of the task.

    project int

    The project ID of the task.

    total_annotations int

    The total number of annotations.

    total_predictions int

    The total number of predictions.

    unresolved_comment_count int

    The number of unresolved comments.

    updated_at str

    The update date of the task.

    updated_by int

    The user ID of the user who updated the task.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioTask.annotations","title":"annotations instance-attribute","text":"
    annotations: List[LabelStudioAnnotation]\n

    The annotations of the task.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioTask.cancelled_annotations","title":"cancelled_annotations instance-attribute","text":"
    cancelled_annotations: int\n

    The number of cancelled annotations.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioTask.comment_authors","title":"comment_authors instance-attribute","text":"
    comment_authors: List[str]\n

    The authors of the comments.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioTask.comment_count","title":"comment_count instance-attribute","text":"
    comment_count: int\n

    The number of comments.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioTask.created_at","title":"created_at instance-attribute","text":"
    created_at: str\n

    The creation date of the task.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioTask.data","title":"data instance-attribute","text":"
    data: Optional[Dict[str, str]]\n

    The data of the task.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioTask.drafts","title":"drafts instance-attribute","text":"
    drafts: List[str]\n

    The drafts of the task.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioTask.file_name","title":"file_name property","text":"
    file_name: str\n

    Extracts the original file name from the file upload.

    File uploads are stored as project-id-filename format to be unique.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioTask.file_upload","title":"file_upload instance-attribute","text":"
    file_upload: str\n

    The file upload of the task.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioTask.id","title":"id instance-attribute","text":"
    id: int\n

    The ID of the task.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioTask.inner_id","title":"inner_id instance-attribute","text":"
    inner_id: int\n

    The inner ID of the task.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioTask.last_comment_updated_at","title":"last_comment_updated_at instance-attribute","text":"
    last_comment_updated_at: Optional[str]\n

    The update date of the last comment.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioTask.meta","title":"meta instance-attribute","text":"
    meta: Optional[Dict[str, str]]\n

    The meta data of the task.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioTask.predictions","title":"predictions instance-attribute","text":"
    predictions: List[str]\n

    The predictions of the task.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioTask.project","title":"project instance-attribute","text":"
    project: int\n

    The project ID of the task.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioTask.total_annotations","title":"total_annotations instance-attribute","text":"
    total_annotations: int\n

    The total number of annotations.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioTask.total_predictions","title":"total_predictions instance-attribute","text":"
    total_predictions: int\n

    The total number of predictions.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioTask.unresolved_comment_count","title":"unresolved_comment_count instance-attribute","text":"
    unresolved_comment_count: int\n

    The number of unresolved comments.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioTask.updated_at","title":"updated_at instance-attribute","text":"
    updated_at: str\n

    The update date of the task.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.LabelStudioTask.updated_by","title":"updated_by instance-attribute","text":"
    updated_by: int\n

    The user ID of the user who updated the task.

    "},{"location":"reference/gerd/models/label/#gerd.models.label.load_label_studio_tasks","title":"load_label_studio_tasks","text":"
    load_label_studio_tasks(file_path: str) -> List[LabelStudioTask]\n

    Load Label Studio tasks from a JSON file.

    Parameters:

    Name Type Description Default str

    The path to the JSON file.

    required

    Returns:

    Type Description List[LabelStudioTask]

    The loaded Label Studio tasks

    Source code in gerd/models/label.py
    def load_label_studio_tasks(file_path: str) -> List[LabelStudioTask]:\n    \"\"\"Load Label Studio tasks from a JSON file.\n\n    Parameters:\n        file_path: The path to the JSON file.\n\n    Returns:\n        The loaded Label Studio tasks\n    \"\"\"\n    with open(file_path, \"r\", encoding=\"utf-8\") as f:\n        obj = json.load(f)\n\n    tasks = TypeAdapter(List[LabelStudioTask]).validate_python(obj)\n    return tasks\n
    "},{"location":"reference/gerd/models/label/#gerd.models.label.load_label_studio_tasks(file_path)","title":"file_path","text":""},{"location":"reference/gerd/models/logging/","title":"gerd.models.logging","text":""},{"location":"reference/gerd/models/logging/#gerd.models.logging","title":"gerd.models.logging","text":"

    Logging configuration and utilities.

    Classes:

    Name Description LogLevel

    Wrapper for string-based log levels.

    LoggingConfig

    Configuration for logging.

    "},{"location":"reference/gerd/models/logging/#gerd.models.logging.LogLevel","title":"LogLevel","text":"

    Bases: Enum

    Wrapper for string-based log levels.

    Translates log levels to integers for Python's logging framework.

    Methods:

    Name Description as_int

    Convert the log level to an integer.

    "},{"location":"reference/gerd/models/logging/#gerd.models.logging.LogLevel.as_int","title":"as_int","text":"
    as_int() -> int\n

    Convert the log level to an integer.

    Source code in gerd/models/logging.py
    def as_int(self) -> int:\n    \"\"\"Convert the log level to an integer.\"\"\"\n    return {\n        LogLevel.DEBUG: 10,\n        LogLevel.INFO: 20,\n        LogLevel.WARNING: 30,\n        LogLevel.ERROR: 40,\n        LogLevel.FATAL: 50,\n    }[self]\n
    "},{"location":"reference/gerd/models/logging/#gerd.models.logging.LoggingConfig","title":"LoggingConfig","text":"

    Bases: BaseModel

    Configuration for logging.

    Attributes:

    Name Type Description level LogLevel

    The log level.

    "},{"location":"reference/gerd/models/logging/#gerd.models.logging.LoggingConfig.level","title":"level instance-attribute","text":"
    level: LogLevel\n

    The log level.

    "},{"location":"reference/gerd/models/model/","title":"gerd.models.model","text":""},{"location":"reference/gerd/models/model/#gerd.models.model","title":"gerd.models.model","text":"

    Model configuration for supported model classes.

    Classes:

    Name Description ChatMessage

    Data structure for chat messages.

    ModelConfig

    Configuration for large language models.

    ModelEndpoint

    Configuration for model endpoints where models are hosted remotely.

    PromptConfig

    Configuration for prompts.

    Attributes:

    Name Type Description ChatRole

    Currently supported chat roles.

    EndpointType

    Endpoint for remote llm services.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ChatRole","title":"ChatRole module-attribute","text":"
    ChatRole = Literal['system', 'user', 'assistant']\n

    Currently supported chat roles.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.EndpointType","title":"EndpointType module-attribute","text":"
    EndpointType = Literal['llama.cpp', 'openai']\n

    Endpoint for remote llm services.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ChatMessage","title":"ChatMessage","text":"

    Bases: TypedDict

    Data structure for chat messages.

    Attributes:

    Name Type Description content str

    The content of the chat message.

    role ChatRole

    The role or source of the chat message.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ChatMessage.content","title":"content instance-attribute","text":"
    content: str\n

    The content of the chat message.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ChatMessage.role","title":"role instance-attribute","text":"
    role: ChatRole\n

    The role or source of the chat message.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig","title":"ModelConfig","text":"

    Bases: BaseModel

    Configuration for large language models.

    Most llm libraries and/or services share common parameters for configuration. Explaining each parameter is out of scope for this documentation. The most essential parameters are explained for instance here. Default values have been chosen according to ctransformers library.

    Attributes:

    Name Type Description batch_size int

    The batch size for the generation.

    context_length int

    The context length for the model. Currently only LLaMA, MPT and Falcon

    endpoint Optional[ModelEndpoint]

    The endpoint of the model when hosted remotely.

    extra_kwargs Optional[dict[str, Any]]

    Additional keyword arguments for the model library.

    file Optional[str]

    The path to the model file. For local models only.

    gpu_layers int

    The number of layers to run on the GPU.

    last_n_tokens int

    The number of tokens to consider for the repetition penalty.

    loras set[Path]

    The list of additional LoRAs files to load.

    max_new_tokens int

    The maximum number of new tokens to generate.

    name str

    The name of the model. Can be a path to a local model or a huggingface handle.

    prompt_config PromptConfig

    The prompt configuration.

    prompt_setup List[Tuple[Literal['system', 'user', 'assistant'], PromptConfig]]

    A list of predefined prompts for the model.

    repetition_penalty float

    The repetition penalty.

    seed int

    The seed for the random number generator.

    stop Optional[List[str]]

    The stop tokens for the generation.

    stream bool

    Whether to stream the output.

    temperature float

    The temperature for the sampling.

    threads Optional[int]

    The number of threads to use for the generation.

    top_k int

    The number of tokens to consider for the top-k sampling.

    top_p float

    The cumulative probability for the top-p sampling.

    torch_dtype Optional[str]

    The torch data type for the model.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig.batch_size","title":"batch_size class-attribute instance-attribute","text":"
    batch_size: int = 8\n

    The batch size for the generation.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig.context_length","title":"context_length class-attribute instance-attribute","text":"
    context_length: int = 0\n

    The context length for the model. Currently only LLaMA, MPT and Falcon

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig.endpoint","title":"endpoint class-attribute instance-attribute","text":"
    endpoint: Optional[ModelEndpoint] = None\n

    The endpoint of the model when hosted remotely.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig.extra_kwargs","title":"extra_kwargs class-attribute instance-attribute","text":"
    extra_kwargs: Optional[dict[str, Any]] = None\n

    Additional keyword arguments for the model library.

    The accepted keys and values depend on the model library used.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig.file","title":"file class-attribute instance-attribute","text":"
    file: Optional[str] = None\n

    The path to the model file. For local models only.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig.gpu_layers","title":"gpu_layers class-attribute instance-attribute","text":"
    gpu_layers: int = 0\n

    The number of layers to run on the GPU.

    The actual number is only used llama.cpp. The other model libraries will determine whether to run on the GPU just by checking of this value is larger than 0.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig.last_n_tokens","title":"last_n_tokens class-attribute instance-attribute","text":"
    last_n_tokens: int = 64\n

    The number of tokens to consider for the repetition penalty.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig.loras","title":"loras class-attribute instance-attribute","text":"
    loras: set[Path] = set()\n

    The list of additional LoRAs files to load.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig.max_new_tokens","title":"max_new_tokens class-attribute instance-attribute","text":"
    max_new_tokens: int = 256\n

    The maximum number of new tokens to generate.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig.name","title":"name class-attribute instance-attribute","text":"
    name: str = 'Qwen/Qwen2.5-0.5B-Instruct'\n

    The name of the model. Can be a path to a local model or a huggingface handle.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig.prompt_config","title":"prompt_config class-attribute instance-attribute","text":"
    prompt_config: PromptConfig = PromptConfig()\n

    The prompt configuration.

    This is used to process the input passed to the services.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig.prompt_setup","title":"prompt_setup class-attribute instance-attribute","text":"
    prompt_setup: List[Tuple[Literal['system', 'user', 'assistant'], PromptConfig]] = []\n

    A list of predefined prompts for the model.

    When a model context is inialized or reset, this will be used to set up the context.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig.repetition_penalty","title":"repetition_penalty class-attribute instance-attribute","text":"
    repetition_penalty: float = 1.1\n

    The repetition penalty.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig.seed","title":"seed class-attribute instance-attribute","text":"
    seed: int = -1\n

    The seed for the random number generator.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig.stop","title":"stop class-attribute instance-attribute","text":"
    stop: Optional[List[str]] = None\n

    The stop tokens for the generation.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig.stream","title":"stream class-attribute instance-attribute","text":"
    stream: bool = False\n

    Whether to stream the output.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig.temperature","title":"temperature class-attribute instance-attribute","text":"
    temperature: float = 0.8\n

    The temperature for the sampling.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig.threads","title":"threads class-attribute instance-attribute","text":"
    threads: Optional[int] = None\n

    The number of threads to use for the generation.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig.top_k","title":"top_k class-attribute instance-attribute","text":"
    top_k: int = 40\n

    The number of tokens to consider for the top-k sampling.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig.top_p","title":"top_p class-attribute instance-attribute","text":"
    top_p: float = 0.95\n

    The cumulative probability for the top-p sampling.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelConfig.torch_dtype","title":"torch_dtype class-attribute instance-attribute","text":"
    torch_dtype: Optional[str] = None\n

    The torch data type for the model.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.ModelEndpoint","title":"ModelEndpoint","text":"

    Bases: BaseModel

    Configuration for model endpoints where models are hosted remotely.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.PromptConfig","title":"PromptConfig","text":"

    Bases: BaseModel

    Configuration for prompts.

    Methods:

    Name Description format

    Format the prompt with the given parameters.

    model_post_init

    Post-initialization hook for pyandic.

    Attributes:

    Name Type Description is_template bool

    Whether the config uses jinja2 templates.

    parameters list[str]

    Retrieves and returns the parameters of the prompt.

    path Optional[str]

    The path to an external prompt file.

    template Optional[Template]

    Optional template of the prompt. This should follow the Jinja2 syntax.

    text str

    The text of the prompt. Can contain placeholders.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.PromptConfig.is_template","title":"is_template class-attribute instance-attribute","text":"
    is_template: bool = False\n

    Whether the config uses jinja2 templates.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.PromptConfig.parameters","title":"parameters property","text":"
    parameters: list[str]\n

    Retrieves and returns the parameters of the prompt.

    This happens on-the-fly and is not stored in the model.

    Returns:

    Type Description list[str]

    The parameters of the prompt.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.PromptConfig.path","title":"path class-attribute instance-attribute","text":"
    path: Optional[str] = None\n

    The path to an external prompt file.

    This will overload the values of text and/or template.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.PromptConfig.template","title":"template class-attribute instance-attribute","text":"
    template: Optional[Template] = Field(exclude=True, default=None)\n

    Optional template of the prompt. This should follow the Jinja2 syntax.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.PromptConfig.text","title":"text class-attribute instance-attribute","text":"
    text: str = '{message}'\n

    The text of the prompt. Can contain placeholders.

    "},{"location":"reference/gerd/models/model/#gerd.models.model.PromptConfig.format","title":"format","text":"
    format(parameters: Mapping[str, str | list[ChatMessage]] | None = None) -> str\n

    Format the prompt with the given parameters.

    Parameters:

    Name Type Description Default Mapping[str, str | list[ChatMessage]] | None

    The parameters to format the prompt with.

    None

    Returns:

    Type Description str

    The formatted prompt

    Source code in gerd/models/model.py
    def format(\n    self, parameters: Mapping[str, str | list[ChatMessage]] | None = None\n) -> str:\n    \"\"\"Format the prompt with the given parameters.\n\n    Parameters:\n        parameters: The parameters to format the prompt with.\n\n    Returns:\n        The formatted prompt\n    \"\"\"\n    if parameters is None:\n        parameters = {}\n    return (\n        self.template.render(**parameters)\n        if self.template\n        else (\n            self.text.format(**parameters)\n            if self.text\n            else \"\".join(str(parameters.values()))\n        )\n    )\n
    "},{"location":"reference/gerd/models/model/#gerd.models.model.PromptConfig.format(parameters)","title":"parameters","text":""},{"location":"reference/gerd/models/model/#gerd.models.model.PromptConfig.model_post_init","title":"model_post_init","text":"
    model_post_init(__context: Any) -> None\n

    Post-initialization hook for pyandic.

    When path is set, the text or template is read from the file and the template is created. Path ending with '.jinja2' will be treated as a template. If no path is set, the text parameter is used to initialize the template if is_template is set to True. Parameters: __context: The context of the model (not used)

    Source code in gerd/models/model.py
    def model_post_init(self, __context: Any) -> None:  # noqa: ANN401\n    \"\"\"Post-initialization hook for pyandic.\n\n    When path is set, the text or template is read from the file and\n    the template is created.\n    Path ending with '.jinja2' will be treated as a template.\n    If no path is set, the text parameter is used to initialize the template\n    if is_template is set to True.\n    Parameters:\n        __context: The context of the model (not used)\n    \"\"\"\n    if self.path:\n        # reset self.text when path is set\n        self.text = \"\"\n        path = Path(self.path)\n        if path.exists():\n            with path.open(\"r\", encoding=\"utf-8\") as f:\n                self.text = f.read()\n                if self.is_template or path.suffix == \".jinja2\":\n                    self.is_template = True\n                    loader = FileSystemLoader(path.parent)\n                    env = Environment(\n                        loader=loader,\n                        autoescape=select_autoescape(\n                            disabled_extensions=(\".jinja2\",),\n                            default_for_string=True,\n                            default=True,\n                        ),\n                    )\n                    self.template = env.get_template(path.name)\n        else:\n            msg = f\"'{self.path}' does not exist!\"\n            raise ValueError(msg)\n    elif self.text and self.is_template:\n        self.template = Environment(autoescape=True).from_string(self.text)\n
    "},{"location":"reference/gerd/models/qa/","title":"gerd.models.qa","text":""},{"location":"reference/gerd/models/qa/#gerd.models.qa","title":"gerd.models.qa","text":"

    Data definitions for QA model configuration.

    Classes:

    Name Description AnalyzeConfig

    The configuration for the analyze service.

    EmbeddingConfig

    Embedding specific model configuration.

    QAConfig

    Configuration for the QA services.

    QAFeaturesConfig

    Configuration for the QA-specific features.

    "},{"location":"reference/gerd/models/qa/#gerd.models.qa.AnalyzeConfig","title":"AnalyzeConfig","text":"

    Bases: BaseModel

    The configuration for the analyze service.

    Attributes:

    Name Type Description model ModelConfig

    The model to be used for the analyze service.

    "},{"location":"reference/gerd/models/qa/#gerd.models.qa.AnalyzeConfig.model","title":"model instance-attribute","text":"
    model: ModelConfig\n

    The model to be used for the analyze service.

    "},{"location":"reference/gerd/models/qa/#gerd.models.qa.EmbeddingConfig","title":"EmbeddingConfig","text":"

    Bases: BaseModel

    Embedding specific model configuration.

    Attributes:

    Name Type Description chunk_overlap int

    The overlap between chunks.

    chunk_size int

    The size of the chunks stored in the database.

    db_path Optional[str]

    The path to the database file.

    model ModelConfig

    The model used for the embedding.

    "},{"location":"reference/gerd/models/qa/#gerd.models.qa.EmbeddingConfig.chunk_overlap","title":"chunk_overlap instance-attribute","text":"
    chunk_overlap: int\n

    The overlap between chunks.

    "},{"location":"reference/gerd/models/qa/#gerd.models.qa.EmbeddingConfig.chunk_size","title":"chunk_size instance-attribute","text":"
    chunk_size: int\n

    The size of the chunks stored in the database.

    "},{"location":"reference/gerd/models/qa/#gerd.models.qa.EmbeddingConfig.db_path","title":"db_path class-attribute instance-attribute","text":"
    db_path: Optional[str] = None\n

    The path to the database file.

    "},{"location":"reference/gerd/models/qa/#gerd.models.qa.EmbeddingConfig.model","title":"model instance-attribute","text":"
    model: ModelConfig\n

    The model used for the embedding.

    This model should be rather small and fast to compute. Furthermore, not every model is suited for this task.

    "},{"location":"reference/gerd/models/qa/#gerd.models.qa.QAConfig","title":"QAConfig","text":"

    Bases: BaseSettings

    Configuration for the QA services.

    This model can be used to retrieve parameters from a variety of sources. The main source are YAML files (loaded as Settings) but dotenv files and environment variables can be used to situatively overwrite the values. Environment variables have to be prefixed with gerd_qa_ to be recognized.

    Methods:

    Name Description settings_customise_sources

    Customize the settings sources used by pydantic-settings.

    Attributes:

    Name Type Description device str

    The device to run the model on.

    embedding EmbeddingConfig

    The configuration for the embedding service.

    features QAFeaturesConfig

    The configuration for the QA-specific features.

    model ModelConfig

    The model to be used for the QA service.

    "},{"location":"reference/gerd/models/qa/#gerd.models.qa.QAConfig.device","title":"device class-attribute instance-attribute","text":"
    device: str = 'cpu'\n

    The device to run the model on.

    "},{"location":"reference/gerd/models/qa/#gerd.models.qa.QAConfig.embedding","title":"embedding instance-attribute","text":"
    embedding: EmbeddingConfig\n

    The configuration for the embedding service.

    "},{"location":"reference/gerd/models/qa/#gerd.models.qa.QAConfig.features","title":"features instance-attribute","text":"
    features: QAFeaturesConfig\n

    The configuration for the QA-specific features.

    "},{"location":"reference/gerd/models/qa/#gerd.models.qa.QAConfig.model","title":"model instance-attribute","text":"
    model: ModelConfig\n

    The model to be used for the QA service.

    "},{"location":"reference/gerd/models/qa/#gerd.models.qa.QAConfig.settings_customise_sources","title":"settings_customise_sources classmethod","text":"
    settings_customise_sources(_: Type[BaseSettings], init_settings: PydanticBaseSettingsSource, env_settings: PydanticBaseSettingsSource, dotenv_settings: PydanticBaseSettingsSource, file_secret_settings: PydanticBaseSettingsSource) -> Tuple[PydanticBaseSettingsSource, ...]\n

    Customize the settings sources used by pydantic-settings.

    The order of the sources is important. The first source has the highest priority.

    Parameters:

    Name Type Description Default

    The class of the settings.

    required PydanticBaseSettingsSource

    The settings from the initialization.

    required PydanticBaseSettingsSource

    The settings from the environment.

    required PydanticBaseSettingsSource

    The settings from the dotenv file.

    required PydanticBaseSettingsSource

    The settings from the secret file.

    required

    Returns:

    Type Description Tuple[PydanticBaseSettingsSource, ...]

    The customized settings sources.

    Source code in gerd/models/qa.py
    @classmethod\ndef settings_customise_sources(\n    cls,\n    _: Type[BaseSettings],\n    init_settings: PydanticBaseSettingsSource,\n    env_settings: PydanticBaseSettingsSource,\n    dotenv_settings: PydanticBaseSettingsSource,\n    file_secret_settings: PydanticBaseSettingsSource,\n) -> Tuple[PydanticBaseSettingsSource, ...]:\n    \"\"\"Customize the settings sources used by pydantic-settings.\n\n    The order of the sources is important.\n    The first source has the highest priority.\n\n    Parameters:\n        cls: The class of the settings.\n        init_settings: The settings from the initialization.\n        env_settings: The settings from the environment.\n        dotenv_settings: The settings from the dotenv file.\n        file_secret_settings: The settings from the secret file.\n\n    Returns:\n        The customized settings sources.\n    \"\"\"\n    return (\n        file_secret_settings,\n        env_settings,\n        dotenv_settings,\n        init_settings,\n    )\n
    "},{"location":"reference/gerd/models/qa/#gerd.models.qa.QAConfig.settings_customise_sources(cls)","title":"cls","text":""},{"location":"reference/gerd/models/qa/#gerd.models.qa.QAConfig.settings_customise_sources(init_settings)","title":"init_settings","text":""},{"location":"reference/gerd/models/qa/#gerd.models.qa.QAConfig.settings_customise_sources(env_settings)","title":"env_settings","text":""},{"location":"reference/gerd/models/qa/#gerd.models.qa.QAConfig.settings_customise_sources(dotenv_settings)","title":"dotenv_settings","text":""},{"location":"reference/gerd/models/qa/#gerd.models.qa.QAConfig.settings_customise_sources(file_secret_settings)","title":"file_secret_settings","text":""},{"location":"reference/gerd/models/qa/#gerd.models.qa.QAFeaturesConfig","title":"QAFeaturesConfig","text":"

    Bases: BaseModel

    Configuration for the QA-specific features.

    Attributes:

    Name Type Description analyze AnalyzeConfig

    Configuration to extract letter of discharge information from the text.

    analyze_mult_prompts AnalyzeConfig

    Configuration to extract predefined infos with multiple prompts from the text.

    return_source bool

    Whether to return the source in the response.

    "},{"location":"reference/gerd/models/qa/#gerd.models.qa.QAFeaturesConfig.analyze","title":"analyze instance-attribute","text":"
    analyze: AnalyzeConfig\n

    Configuration to extract letter of discharge information from the text.

    "},{"location":"reference/gerd/models/qa/#gerd.models.qa.QAFeaturesConfig.analyze_mult_prompts","title":"analyze_mult_prompts instance-attribute","text":"
    analyze_mult_prompts: AnalyzeConfig\n

    Configuration to extract predefined infos with multiple prompts from the text.

    "},{"location":"reference/gerd/models/qa/#gerd.models.qa.QAFeaturesConfig.return_source","title":"return_source instance-attribute","text":"
    return_source: bool\n

    Whether to return the source in the response.

    "},{"location":"reference/gerd/models/server/","title":"gerd.models.server","text":""},{"location":"reference/gerd/models/server/#gerd.models.server","title":"gerd.models.server","text":"

    Server configuration model for REST backends.

    Classes:

    Name Description ServerConfig

    Server configuration model for REST backends.

    "},{"location":"reference/gerd/models/server/#gerd.models.server.ServerConfig","title":"ServerConfig","text":"

    Bases: BaseModel

    Server configuration model for REST backends.

    Attributes:

    Name Type Description api_prefix str

    The prefix of the API.

    host str

    The host of the server.

    port int

    The port of the server.

    "},{"location":"reference/gerd/models/server/#gerd.models.server.ServerConfig.api_prefix","title":"api_prefix instance-attribute","text":"
    api_prefix: str\n

    The prefix of the API.

    "},{"location":"reference/gerd/models/server/#gerd.models.server.ServerConfig.host","title":"host instance-attribute","text":"
    host: str\n

    The host of the server.

    "},{"location":"reference/gerd/models/server/#gerd.models.server.ServerConfig.port","title":"port instance-attribute","text":"
    port: int\n

    The port of the server.

    "},{"location":"reference/gerd/qa/","title":"gerd.qa","text":""},{"location":"reference/gerd/qa/#gerd.qa","title":"gerd.qa","text":"

    Services and utilities for retrieval augmented generation (RAG).

    Modules:

    Name Description qa_service

    Implements the QAService class.

    "},{"location":"reference/gerd/qa/qa_service/","title":"gerd.qa.qa_service","text":""},{"location":"reference/gerd/qa/qa_service/#gerd.qa.qa_service","title":"gerd.qa.qa_service","text":"

    Implements the QAService class.

    The question and answer service is used to query a language model with questions related to a specific context. The context is usually a set of documents that are loaded into a vector store.

    Classes:

    Name Description QAService

    The question and answer service class.

    "},{"location":"reference/gerd/qa/qa_service/#gerd.qa.qa_service.QAService","title":"QAService","text":"
    QAService(config: QAConfig)\n

    The question and answer service class.

    The service is initialized with a configuration.

    Depending on the configuration, the service will create a new in-memory vector store or load an existing one from a file.

    Parameters:

    Name Type Description Default QAConfig

    The configuration for the QA service

    required

    Methods:

    Name Description add_file

    Add a document to the vectorstore.

    analyze_mult_prompts_query

    Reads a set of data from doc.

    analyze_query

    Read a set of data from a set of documents.

    db_embedding

    Converts a question to an embedding.

    db_query

    Queries the vector store with a question.

    get_prompt_config

    Returns the prompt config for the given mode.

    query

    Pass a question to the language model.

    remove_file

    Removes a document from the vectorstore.

    set_prompt_config

    Sets the prompt config for the given mode.

    Source code in gerd/qa/qa_service.py
    def __init__(self, config: QAConfig) -> None:\n    \"\"\"The service is initialized with a configuration.\n\n    Depending on the configuration, the service will create a new in-memory\n    vector store or load an existing one from a file.\n\n    Parameters:\n        config: The configuration for the QA service\n    \"\"\"\n    self.config = config\n    self._llm = gerd_loader.load_model_from_config(config.model)\n    self._vectorstore: Optional[FAISS] = None\n    self._database: Optional[Rag] = None\n    if (\n        config.embedding.db_path\n        and Path(config.embedding.db_path, \"index.faiss\").exists()\n    ):\n        _LOGGER.info(\n            \"Load existing vector store from '%s'.\", config.embedding.db_path\n        )\n        self._vectorstore = load_faiss(\n            Path(config.embedding.db_path, \"index.faiss\"),\n            config.embedding.model.name,\n            config.device,\n        )\n
    "},{"location":"reference/gerd/qa/qa_service/#gerd.qa.qa_service.QAService(config)","title":"config","text":""},{"location":"reference/gerd/qa/qa_service/#gerd.qa.qa_service.QAService.add_file","title":"add_file","text":"
    add_file(file: QAFileUpload) -> QAAnswer\n

    Add a document to the vectorstore.

    Parameters:

    Name Type Description Default QAFileUpload

    The file to add to the vectorstore

    required

    Returns:

    Type Description QAAnswer

    an answer object with status 200 if successful

    Source code in gerd/qa/qa_service.py
    def add_file(self, file: QAFileUpload) -> QAAnswer:\n    \"\"\"Add a document to the vectorstore.\n\n    Parameters:\n        file: The file to add to the vectorstore\n\n    Returns:\n        an answer object with status 200 if successful\n    \"\"\"\n    documents: Optional[List[Document]] = None\n    file_path = Path(file.name)\n    try:\n        with NamedTemporaryFile(\n            dir=\".\", suffix=file_path.suffix, delete=False\n        ) as f:\n            f.write(file.data)\n            f.flush()\n        file_type = FileTypes(file_path.suffix[1:])\n        loader: BaseLoader\n        if file_type == FileTypes.TEXT:\n            loader = TextLoader(f.name)\n        elif file_type == FileTypes.PDF:\n            loader = PyPDFLoader(f.name)\n        documents = loader.load()\n        # source must be overriden to not leak upload information\n        # about the temp file which are rather useless anyway\n        for doc in documents:\n            doc.metadata[\"source\"] = file_path.name\n    except BaseException as err:\n        _LOGGER.error(err)\n    finally:\n        if f:\n            unlink(f.name)\n    if not documents:\n        _LOGGER.warning(\"No document was loaded!\")\n        return QAAnswer(error_msg=\"No document was loaded!\", status=500)\n    text_splitter = RecursiveCharacterTextSplitter(\n        chunk_size=self.config.embedding.chunk_size,\n        chunk_overlap=self.config.embedding.chunk_overlap,\n    )\n    texts = text_splitter.split_documents(documents)\n    for i, text in enumerate(texts):\n        text.id = f\"{file_path.name}_{i}\"\n    if self._vectorstore is None:\n        _LOGGER.info(\"Create new vector store from document.\")\n        self._vectorstore = create_faiss(\n            texts, self.config.embedding.model.name, self.config.device\n        )\n    else:\n        _LOGGER.info(\"Adding document to existing vector store.\")\n        tmp = create_faiss(\n            texts, self.config.embedding.model.name, self.config.device\n        )\n        self._vectorstore.merge_from(tmp)\n    if self.config.embedding.db_path:\n        self._vectorstore.save_local(self.config.embedding.db_path)\n    return QAAnswer()\n
    "},{"location":"reference/gerd/qa/qa_service/#gerd.qa.qa_service.QAService.add_file(file)","title":"file","text":""},{"location":"reference/gerd/qa/qa_service/#gerd.qa.qa_service.QAService.analyze_mult_prompts_query","title":"analyze_mult_prompts_query","text":"
    analyze_mult_prompts_query() -> QAAnalyzeAnswer\n

    Reads a set of data from doc.

    Loads the data via multiple prompts by asking for each data field separately.

    Data - patient_name - patient_date_of_birth - attending_doctors - recording_date - release_date

    Returns:

    Type Description QAAnalyzeAnswer

    The answer from the language model

    Source code in gerd/qa/qa_service.py
    def analyze_mult_prompts_query(self) -> QAAnalyzeAnswer:\n    \"\"\"Reads a set of data from doc.\n\n    Loads the data via multiple prompts by asking for each data field\n    separately.\n\n    *Data*\n        - patient_name\n        - patient_date_of_birth\n        - attending_doctors\n        - recording_date\n        - release_date\n\n    Returns:\n        The answer from the language model\n    \"\"\"\n    if not self._vectorstore:\n        msg = \"No vector store initialized! Upload documents first.\"\n        _LOGGER.error(msg)\n        return QAAnalyzeAnswer(status=404, error_msg=msg)\n\n    config = self.config.features.analyze_mult_prompts\n\n    # check if prompt contains needed fields\n    qa_analyze_mult_prompts = config.model.prompt_config\n    if (\n        \"context\" not in qa_analyze_mult_prompts.parameters\n        or \"question\" not in qa_analyze_mult_prompts.parameters\n    ):\n        msg = \"Prompt does not include '{context}' or '{question}' variable.\"\n        _LOGGER.error(msg)\n        return QAAnalyzeAnswer(status=404, error_msg=msg)\n\n    # questions to search model and vectorstore\n    questions_model_dict: Dict[str, str] = {\n        \"Wie hei\u00dft der Patient?\": \"wir berichten \u00fcber unseren Patient oder Btr. oder Patient, wh, geboren oder  Patient, * 0.00.0000,\",  # noqa: E501\n        \"Wann hat der Patient Geburstag?\": \"wir berichten \u00fcber unseren Patient oder Btr. oder Patient, wh, geboren oder  Patient, * 0.00.0000,\",  # noqa: E501\n        \"Wie hei\u00dft der Arzt?\": \"Mit freundlichen kollegialen Gr\u00fc\u00dfen, Prof, Dr\",\n        \"Wann wurde der Patient bei uns aufgenommen?\": (\n            \"wir berichten \u00fcber unseren Patient oder Btr. oder Patient, wh, geboren\"\n        ),\n        \"Wann wurde der Patient bei uns entlassen?\": (\n            \"wir berichten \u00fcber unseren Patient oder Btr. oder Patient, wh, geboren\"\n        ),\n    }\n    fields: Dict[str, str] = {\n        \"Wie hei\u00dft der Patient?\": \"patient_name\",\n        \"Wann hat der Patient Geburstag?\": \"patient_date_of_birth\",\n        \"Wie hei\u00dft der Arzt?\": \"attending_doctors\",\n        \"Wann wurde der Patient bei uns aufgenommen?\": \"recording_date\",\n        \"Wann wurde der Patient bei uns entlassen?\": \"release_date\",\n    }\n    questions_dict: Dict[str, List[DocumentSource]] = {}\n    answer_dict: Dict[str, Any] = {}\n    responses: str = \"\"\n\n    # load context from vectorstore for each question\n    for question_m, question_v in questions_model_dict.items():\n        questions_dict, formatted_prompt = self._create_analyze_mult_prompt(\n            question_m, question_v, qa_analyze_mult_prompts.text\n        )\n\n        # query the model for each question\n        response = self._llm.generate(formatted_prompt)\n\n        # format the response\n        if response is not None:\n            response = self._clean_response(response)\n\n            # if enabled, collect response\n            if self.config.features.return_source:\n                responses = responses + \"; \" + question_m + \": \" + response\n        # format the response\n        answer_dict[fields[question_m]] = self._format_response_analyze_mult_prompt(\n            response, fields[question_m]\n        )\n\n        _LOGGER.info(\n            \"\\n===== Modelresult ====\\n\\n%s\\n\\n====================\", response\n        )\n\n    answer = QAAnalyzeAnswer(**answer_dict)\n\n    # if enabled, pass source data to answer\n    if self.config.features.return_source:\n        answer.response = responses\n        for question in questions_dict:\n            answer.sources = answer.sources + questions_dict[question]\n\n    _LOGGER.warning(\"\\n==== Answer ====\\n\\n%s\\n===============\", answer)\n    return answer\n
    "},{"location":"reference/gerd/qa/qa_service/#gerd.qa.qa_service.QAService.analyze_query","title":"analyze_query","text":"
    analyze_query() -> QAAnalyzeAnswer\n

    Read a set of data from a set of documents.

    Loads the data via single prompt.

    Data - patient_name - patient_date_of_birth - attending_doctors - recording_date - release_date

    Returns:

    Type Description QAAnalyzeAnswer

    The answer from the language model

    Source code in gerd/qa/qa_service.py
    def analyze_query(self) -> QAAnalyzeAnswer:\n    \"\"\"Read a set of data from a set of documents.\n\n    Loads the data via single prompt.\n\n    *Data*\n        - patient_name\n        - patient_date_of_birth\n        - attending_doctors\n        - recording_date\n        - release_date\n\n    Returns:\n        The answer from the language model\n    \"\"\"\n    if not self._vectorstore:\n        msg = \"No vector store initialized! Upload documents first.\"\n        _LOGGER.error(msg)\n        return QAAnalyzeAnswer(status=404, error_msg=msg)\n\n    config = self.config.features.analyze\n\n    # questions to search model and vectorstore\n    questions_model_dict: Dict[str, str] = {\n        \"Wie hei\u00dft der Patient?\": \"wir berichten \u00fcber unseren Patient oder Btr. oder Patient, wh, geboren oder  Patient, * 0.00.0000,\",  # noqa E501\n        \"Wann hat der Patient Geburstag?\": \"wir berichten \u00fcber unseren Patient oder Btr. oder Patient, wh, geboren oder  Patient, * 0.00.0000,\",  # noqa E501\n        \"Wie hei\u00dft der Arzt?\": \"Mit freundlichen kollegialen Gr\u00fc\u00dfen, Prof, Dr\",\n        \"Wann wurde der Patient bei uns aufgenommen?\": (\n            \"wir berichten \u00fcber unseren Patient oder Btr. oder Patient, wh, geboren\"\n        ),\n        \"Wann wurde der Patient bei uns entlassen?\": (\n            \"wir berichten \u00fcber unseren Patient oder Btr. oder Patient, wh, geboren\"\n        ),\n    }\n\n    # map model questions to jsonfields\n    fields: Dict[str, str] = {\n        \"Wie hei\u00dft der Patient?\": \"patient_name\",\n        \"Wann hat der Patient Geburstag?\": \"patient_date_of_birth\",\n        \"Wie hei\u00dft der Arzt?\": \"attending_doctors\",\n        \"Wann wurde der Patient bei uns aufgenommen?\": \"recording_date\",\n        \"Wann wurde der Patient bei uns entlassen?\": \"release_date\",\n    }\n\n    questions_dict: Dict[str, List[DocumentSource]] = {}\n    parameters: Dict[str, str] = {}\n    question_counter: int = 0\n\n    qa_analyze_prompt = config.model.prompt_config\n    # check if prompt contains needed fields\n    for i in range(0, len(questions_model_dict)):\n        if (\n            \"context\" + str(i) not in qa_analyze_prompt.parameters\n            or \"question\" + str(i) not in qa_analyze_prompt.parameters\n        ):\n            msg = (\n                \"Prompt does not include '{context\"\n                + str(i)\n                + \"}' or '{question\"\n                + str(i)\n                + \"}' variable.\"\n            )\n            _LOGGER.error(msg)\n            return QAAnalyzeAnswer(status=404, error_msg=msg)\n\n    # load context from vectorstore for each question\n    for question_m, question_v in questions_model_dict.items():\n        questions_dict[question_m] = list(\n            self.db_query(QAQuestion(question=question_v, max_sources=3))\n        )\n\n        parameters[\"context\" + str(question_counter)] = \" \".join(\n            doc.content for doc in questions_dict[question_m]\n        )\n        parameters[\"question\" + str(question_counter)] = question_m\n        parameters[\"field\" + str(question_counter)] = fields[question_m]\n\n        question_counter = question_counter + 1\n\n    formatted_prompt = qa_analyze_prompt.text.format(**parameters)\n\n    # query the model\n    response = self._llm.generate(formatted_prompt)\n\n    if response is not None:\n        response = self._clean_response(response)\n\n    _LOGGER.info(\"\\n===== Modelresult ====\\n\\n%s\\n\\n====================\", response)\n\n    # convert json to QAAnalyzerAnswerclass\n    answer = self._format_response_analyze(response)\n\n    # if enabled, pass source data to answer\n    if self.config.features.return_source:\n        answer.response = response\n        answer.prompt = formatted_prompt\n        for question in questions_dict:\n            answer.sources = answer.sources + questions_dict[question]\n        _LOGGER.info(\n            \"\\n===== Sources ====\\n\\n%s\\n\\n====================\", answer.sources\n        )\n\n    _LOGGER.warning(\"\\n==== Answer ====\\n\\n%s\\n===============\", answer)\n    return answer\n
    "},{"location":"reference/gerd/qa/qa_service/#gerd.qa.qa_service.QAService.db_embedding","title":"db_embedding","text":"
    db_embedding(question: QAQuestion) -> List[float]\n

    Converts a question to an embedding.

    The embedding to be used is defined by the vector store or more specifically by the configured parameters passed to initialize the vector store.

    Parameters:

    Name Type Description Default QAQuestion

    The question to convert to an embedding.

    required

    Returns:

    Type Description List[float]

    The embedding of the question

    Source code in gerd/qa/qa_service.py
    def db_embedding(self, question: QAQuestion) -> List[float]:\n    \"\"\"Converts a question to an embedding.\n\n    The embedding to be used is defined by the vector store or more specifically\n    by the configured parameters passed to initialize the vector store.\n\n    Parameters:\n        question: The question to convert to an embedding.\n\n    Returns:\n        The embedding of the question\n    \"\"\"\n    if self._vectorstore is None or self._vectorstore.embeddings is None:\n        return []\n    return self._vectorstore.embeddings.embed_documents([question.question])[0]\n
    "},{"location":"reference/gerd/qa/qa_service/#gerd.qa.qa_service.QAService.db_embedding(question)","title":"question","text":""},{"location":"reference/gerd/qa/qa_service/#gerd.qa.qa_service.QAService.db_query","title":"db_query","text":"
    db_query(question: QAQuestion) -> List[DocumentSource]\n

    Queries the vector store with a question.

    The number of sources that are returned is defined by the max_sources parameter of the service's configuration.

    Parameters:

    Name Type Description Default QAQuestion

    The question to query the vector store with.

    required

    Returns:

    Type Description List[DocumentSource]

    A list of document sources

    Source code in gerd/qa/qa_service.py
    def db_query(self, question: QAQuestion) -> List[DocumentSource]:\n    \"\"\"Queries the vector store with a question.\n\n    The number of sources that are returned is defined by the max_sources parameter\n    of the service's configuration.\n\n    Parameters:\n        question: The question to query the vector store with.\n\n    Returns:\n        A list of document sources\n    \"\"\"\n    if not self._vectorstore:\n        return []\n    return [\n        DocumentSource(\n            query=question.question,\n            content=doc.page_content,\n            name=doc.metadata.get(\"source\", \"unknown\"),\n            page=doc.metadata.get(\"page\", 1),\n        )\n        for doc in self._vectorstore.search(\n            question.question,\n            search_type=question.search_strategy,\n            k=question.max_sources,\n        )\n    ]\n
    "},{"location":"reference/gerd/qa/qa_service/#gerd.qa.qa_service.QAService.db_query(question)","title":"question","text":""},{"location":"reference/gerd/qa/qa_service/#gerd.qa.qa_service.QAService.get_prompt_config","title":"get_prompt_config","text":"
    get_prompt_config(qa_mode: QAModesEnum) -> PromptConfig\n

    Returns the prompt config for the given mode.

    Parameters:

    Name Type Description Default QAModesEnum

    The mode to get the prompt config for

    required

    Returns:

    Type Description PromptConfig

    The prompt config for the given mode

    Source code in gerd/qa/qa_service.py
    def get_prompt_config(self, qa_mode: QAModesEnum) -> PromptConfig:\n    \"\"\"Returns the prompt config for the given mode.\n\n    Parameters:\n        qa_mode: The mode to get the prompt config for\n\n    Returns:\n        The prompt config for the given mode\n    \"\"\"\n    if qa_mode == QAModesEnum.SEARCH:\n        return self.config.model.prompt_config\n    elif qa_mode == QAModesEnum.ANALYZE:\n        return self.config.features.analyze.model.prompt_config\n    elif qa_mode == QAModesEnum.ANALYZE_MULT_PROMPTS:\n        return self.config.features.analyze_mult_prompts.model.prompt_config\n    return PromptConfig()\n
    "},{"location":"reference/gerd/qa/qa_service/#gerd.qa.qa_service.QAService.get_prompt_config(qa_mode)","title":"qa_mode","text":""},{"location":"reference/gerd/qa/qa_service/#gerd.qa.qa_service.QAService.query","title":"query","text":"
    query(question: QAQuestion) -> QAAnswer\n

    Pass a question to the language model.

    The language model will generate an answer based on the question and the context derived from the vector store.

    Parameters:

    Name Type Description Default QAQuestion

    The question to be answered

    required

    Returns:

    Type Description QAAnswer

    The answer from the language model

    Source code in gerd/qa/qa_service.py
    def query(self, question: QAQuestion) -> QAAnswer:\n    \"\"\"Pass a question to the language model.\n\n    The language model will generate an answer based on the question and\n    the context derived from the vector store.\n\n    Parameters:\n        question: The question to be answered\n\n    Returns:\n        The answer from the language model\n    \"\"\"\n    if not self._database:\n        if not self._vectorstore:\n            return QAAnswer(error_msg=\"No database available!\", status=404)\n        self._database = Rag(\n            self._llm,\n            self.config.model,\n            self.config.model.prompt_config,\n            self._vectorstore,\n            self.config.features.return_source,\n        )\n    return self._database.query(question)\n
    "},{"location":"reference/gerd/qa/qa_service/#gerd.qa.qa_service.QAService.query(question)","title":"question","text":""},{"location":"reference/gerd/qa/qa_service/#gerd.qa.qa_service.QAService.remove_file","title":"remove_file","text":"
    remove_file(file_name: str) -> QAAnswer\n

    Removes a document from the vectorstore.

    Parameters:

    Name Type Description Default str

    The name of the file to remove

    required

    Returns:

    Type Description QAAnswer

    an answer object with status 200 if successful

    Source code in gerd/qa/qa_service.py
    def remove_file(self, file_name: str) -> QAAnswer:\n    \"\"\"Removes a document from the vectorstore.\n\n    Parameters:\n        file_name: The name of the file to remove\n\n    Returns:\n        an answer object with status 200 if successful\n    \"\"\"\n    if not self._vectorstore:\n        return QAAnswer(error_msg=\"No vector store initialized!\", status=404)\n    self._vectorstore.delete(\n        [\n            id\n            for id in self._vectorstore.index_to_docstore_id.values()\n            if id.startswith(file_name)\n        ]\n    )\n    return QAAnswer(status=200)\n
    "},{"location":"reference/gerd/qa/qa_service/#gerd.qa.qa_service.QAService.remove_file(file_name)","title":"file_name","text":""},{"location":"reference/gerd/qa/qa_service/#gerd.qa.qa_service.QAService.set_prompt_config","title":"set_prompt_config","text":"
    set_prompt_config(config: PromptConfig, qa_mode: QAModesEnum) -> QAAnswer\n

    Sets the prompt config for the given mode.

    Parameters:

    Name Type Description Default PromptConfig

    The prompt config to set

    required QAModesEnum

    The mode to set the prompt config for

    required

    Returns:

    Type Description QAAnswer

    an answer object with status 200 if successful

    Source code in gerd/qa/qa_service.py
    def set_prompt_config(self, config: PromptConfig, qa_mode: QAModesEnum) -> QAAnswer:\n    \"\"\"Sets the prompt config for the given mode.\n\n    Parameters:\n        config: The prompt config to set\n        qa_mode: The mode to set the prompt config for\n\n    Returns:\n        an answer object with status 200 if successful\n    \"\"\"\n    answer = QAAnswer()\n    if qa_mode == QAModesEnum.SEARCH:\n        self.config.model.prompt_config = config\n        if \"context\" not in config.parameters:\n            answer.error_msg = (\n                \"Prompt does not include '{context}' variable. \"\n                \"No context will be added. \"\n            )\n        if \"question\" not in config.parameters:\n            answer.error_msg += (\n                \"Prompt does not include '{question}' variable. \"\n                \"Questions will not be passed to the model.\"\n            )\n    elif qa_mode == QAModesEnum.ANALYZE:\n        self.config.features.analyze.model.prompt_config = config\n    elif qa_mode == QAModesEnum.ANALYZE_MULT_PROMPTS:\n        self.config.features.analyze_mult_prompts.model.prompt_config = config\n    return answer\n
    "},{"location":"reference/gerd/qa/qa_service/#gerd.qa.qa_service.QAService.set_prompt_config(config)","title":"config","text":""},{"location":"reference/gerd/qa/qa_service/#gerd.qa.qa_service.QAService.set_prompt_config(qa_mode)","title":"qa_mode","text":""},{"location":"reference/gerd/rag/","title":"gerd.rag","text":""},{"location":"reference/gerd/rag/#gerd.rag","title":"gerd.rag","text":"

    Retrieval-Augmented Generation (RAG) backend.

    This module provides the RAG backend for the GERD system which is currently based on FAISS.

    Classes:

    Name Description Rag

    The RAG backend for GERD.

    Functions:

    Name Description create_faiss

    Create a new FAISS store from a list of documents.

    load_faiss

    Load a FAISS store from a disk path.

    "},{"location":"reference/gerd/rag/#gerd.rag.Rag","title":"Rag","text":"
    Rag(model: LLM, model_config: ModelConfig, prompt: PromptConfig, store: FAISS, return_source: bool)\n

    The RAG backend for GERD.

    The RAG backend will check for a context parameter in the prompt.

    If the context parameter is not included, a warning will be logged. Without the context parameter, no context will be added to the query.

    Parameters:

    Name Type Description Default LLM

    The LLM model to use

    required ModelConfig

    The model configuration

    required PromptConfig

    The prompt configuration

    required FAISS

    The FAISS store to use

    required bool

    Whether to return the source documents

    required

    Methods:

    Name Description query

    Query the RAG backend with a question.

    Source code in gerd/rag.py
    def __init__(\n    self,\n    model: LLM,\n    model_config: ModelConfig,\n    prompt: PromptConfig,\n    store: FAISS,\n    return_source: bool,\n) -> None:\n    \"\"\"The RAG backend will check for a context parameter in the prompt.\n\n    If the context parameter is not included, a warning will be logged.\n    Without the context parameter, no context will be added to the query.\n\n    Parameters:\n        model: The LLM model to use\n        model_config: The model configuration\n        prompt: The prompt configuration\n        store: The FAISS store to use\n        return_source: Whether to return the source documents\n    \"\"\"\n    self.model = model\n    self.model_config = model_config\n    self.prompt = prompt\n    self.store = store\n    self.return_source = return_source\n\n    if \"context\" not in prompt.parameters:\n        _LOGGER.warning(\n            \"Prompt does not include '{context}' variable! \"\n            \"No context will be added to the query.\"\n        )\n
    "},{"location":"reference/gerd/rag/#gerd.rag.Rag(model)","title":"model","text":""},{"location":"reference/gerd/rag/#gerd.rag.Rag(model_config)","title":"model_config","text":""},{"location":"reference/gerd/rag/#gerd.rag.Rag(prompt)","title":"prompt","text":""},{"location":"reference/gerd/rag/#gerd.rag.Rag(store)","title":"store","text":""},{"location":"reference/gerd/rag/#gerd.rag.Rag(return_source)","title":"return_source","text":""},{"location":"reference/gerd/rag/#gerd.rag.Rag.query","title":"query","text":"
    query(question: QAQuestion) -> QAAnswer\n

    Query the RAG backend with a question.

    Parameters:

    Name Type Description Default QAQuestion

    The question to ask

    required

    Returns:

    Type Description QAAnswer

    The answer to the question including the sources

    Source code in gerd/rag.py
    def query(self, question: QAQuestion) -> QAAnswer:\n    \"\"\"Query the RAG backend with a question.\n\n    Parameters:\n        question: The question to ask\n\n    Returns:\n        The answer to the question including the sources\n    \"\"\"\n    docs = self.store.search(\n        question.question,\n        search_type=question.search_strategy,\n        k=question.max_sources,\n    )\n    context = \"\\n\".join(doc.page_content for doc in docs)\n    resolved = self.prompt.text.format(context=context, question=question.question)\n    _, response = self.model.create_chat_completion(\n        [{\"role\": \"user\", \"content\": resolved}]\n    )\n    answer = QAAnswer(response=response)\n    if self.return_source:\n        for doc in docs:\n            answer.sources.append(\n                DocumentSource(\n                    query=question.question,\n                    content=doc.page_content,\n                    name=doc.metadata.get(\"source\", \"unknown\"),\n                    page=doc.metadata.get(\"page\", 1),\n                )\n            )\n    return answer\n
    "},{"location":"reference/gerd/rag/#gerd.rag.Rag.query(question)","title":"question","text":""},{"location":"reference/gerd/rag/#gerd.rag.create_faiss","title":"create_faiss","text":"
    create_faiss(documents: list[Document], model_name: str, device: str) -> FAISS\n

    Create a new FAISS store from a list of documents.

    Parameters:

    Name Type Description Default list[Document]

    The list of documents to index

    required str

    The name of the Hugging Face model to for the embeddings

    required str

    The device to use for the model

    required

    Returns:

    Type Description FAISS

    The newly created FAISS store

    Source code in gerd/rag.py
    def create_faiss(documents: list[Document], model_name: str, device: str) -> FAISS:\n    \"\"\"Create a new FAISS store from a list of documents.\n\n    Parameters:\n        documents: The list of documents to index\n        model_name: The name of the Hugging Face model to for the embeddings\n        device: The device to use for the model\n\n    Returns:\n        The newly created FAISS store\n    \"\"\"\n    return FAISS.from_documents(\n        documents,\n        HuggingFaceEmbeddings(\n            model_name=model_name,\n            model_kwargs={\"device\": device},\n        ),\n    )\n
    "},{"location":"reference/gerd/rag/#gerd.rag.create_faiss(documents)","title":"documents","text":""},{"location":"reference/gerd/rag/#gerd.rag.create_faiss(model_name)","title":"model_name","text":""},{"location":"reference/gerd/rag/#gerd.rag.create_faiss(device)","title":"device","text":""},{"location":"reference/gerd/rag/#gerd.rag.load_faiss","title":"load_faiss","text":"
    load_faiss(dp_path: Path, model_name: str, device: str) -> FAISS\n

    Load a FAISS store from a disk path.

    Parameters:

    Name Type Description Default Path

    The path to the disk path

    required str

    The name of the Hugging Face model to for the embeddings

    required str

    The device to use for the model

    required

    Returns:

    Type Description FAISS

    The loaded FAISS store

    Source code in gerd/rag.py
    def load_faiss(dp_path: Path, model_name: str, device: str) -> FAISS:\n    \"\"\"Load a FAISS store from a disk path.\n\n    Parameters:\n        dp_path: The path to the disk path\n        model_name: The name of the Hugging Face model to for the embeddings\n        device: The device to use for the model\n\n    Returns:\n        The loaded FAISS store\n    \"\"\"\n    return FAISS.load_local(\n        dp_path.as_posix(),\n        HuggingFaceEmbeddings(\n            model_name=model_name,\n            model_kwargs={\"device\": device},\n        ),\n    )\n
    "},{"location":"reference/gerd/rag/#gerd.rag.load_faiss(dp_path)","title":"dp_path","text":""},{"location":"reference/gerd/rag/#gerd.rag.load_faiss(model_name)","title":"model_name","text":""},{"location":"reference/gerd/rag/#gerd.rag.load_faiss(device)","title":"device","text":""},{"location":"reference/gerd/training/","title":"gerd.training","text":""},{"location":"reference/gerd/training/#gerd.training","title":"gerd.training","text":"

    Collections of training routines for GERD.

    Modules:

    Name Description data

    Data utilities for training and data processing.

    instruct

    Training module for instruction text sets.

    lora

    Configuration dataclasses for training LoRA models.

    trainer

    Training module for LoRA models.

    unstructured

    Training of LoRA models on unstructured text data.

    "},{"location":"reference/gerd/training/data/","title":"gerd.training.data","text":""},{"location":"reference/gerd/training/data/#gerd.training.data","title":"gerd.training.data","text":"

    Data utilities for training and data processing.

    Functions:

    Name Description despacyfy

    Removes spacy-specific tokens from a text.

    encode

    Encodes a text using a tokenizer.

    split_chunks

    Splits a list of encoded tokens into chunks of a given size.

    tokenize

    Converts a prompt into a tokenized input for a model.

    "},{"location":"reference/gerd/training/data/#gerd.training.data.despacyfy","title":"despacyfy","text":"
    despacyfy(text: str) -> str\n

    Removes spacy-specific tokens from a text.

    For instance, -RRB- is replaced with ')', -LRB- with '(' and -UNK- with '*'.

    Parameters:

    Name Type Description Default str

    The text to despacyfy.

    required

    Returns:

    Type Description str

    The despacyfied text

    Source code in gerd/training/data.py
    def despacyfy(text: str) -> str:\n    \"\"\"Removes spacy-specific tokens from a text.\n\n    For instance, -RRB- is replaced with ')', -LRB- with '(' and -UNK- with '*'.\n\n    Parameters:\n        text: The text to despacyfy.\n\n    Returns:\n        The despacyfied text\n    \"\"\"\n    res = (\n        text.replace(\"-RRB-\", \")\")\n        .replace(\"-LRB-\", \"(\")\n        .replace(\"-UNK-\", \"*\")\n        .replace(\"( \", \"(\")\n        .replace(\" )\", \")\")\n        .replace(\"  \", \" \")\n    )\n    check = re.findall(r\"-(RRB|UNK|LRB)-\", res)\n    if len(check) != 0:\n        msg = f\"Did not expect to find {check} in\\n{res}.\"\n        raise RuntimeError(msg)\n    return res\n
    "},{"location":"reference/gerd/training/data/#gerd.training.data.despacyfy(text)","title":"text","text":""},{"location":"reference/gerd/training/data/#gerd.training.data.encode","title":"encode","text":"
    encode(text: str, add_bos_token: bool, tokenizer: PreTrainedTokenizer, cutoff_len: int) -> List[int]\n

    Encodes a text using a tokenizer.

    Parameters:

    Name Type Description Default str

    The text to encode

    required bool

    Whether to add the beginning of sentence token

    required PreTrainedTokenizer

    The tokenizer to use

    required int

    The maximum length of the encoded text

    required

    Returns:

    Type Description List[int]

    The text encoded as a list of tokenizer tokens

    Source code in gerd/training/data.py
    def encode(\n    text: str, add_bos_token: bool, tokenizer: PreTrainedTokenizer, cutoff_len: int\n) -> List[int]:\n    \"\"\"Encodes a text using a tokenizer.\n\n    Parameters:\n        text: The text to encode\n        add_bos_token: Whether to add the beginning of sentence token\n        tokenizer: The tokenizer to use\n        cutoff_len: The maximum length of the encoded text\n\n    Returns:\n        The text encoded as a list of tokenizer tokens\n    \"\"\"\n    result: list[int] = tokenizer.encode(text, truncation=True, max_length=cutoff_len)\n    # Check if the first two tokens are BOS\n    if len(result) >= 2 and result[:2] == [\n        tokenizer.bos_token_id,\n        tokenizer.bos_token_id,\n    ]:\n        result = result[1:]\n\n    if not add_bos_token and result[0] == tokenizer.bos_token_id:\n        result = result[1:]\n    return result\n
    "},{"location":"reference/gerd/training/data/#gerd.training.data.encode(text)","title":"text","text":""},{"location":"reference/gerd/training/data/#gerd.training.data.encode(add_bos_token)","title":"add_bos_token","text":""},{"location":"reference/gerd/training/data/#gerd.training.data.encode(tokenizer)","title":"tokenizer","text":""},{"location":"reference/gerd/training/data/#gerd.training.data.encode(cutoff_len)","title":"cutoff_len","text":""},{"location":"reference/gerd/training/data/#gerd.training.data.split_chunks","title":"split_chunks","text":"
    split_chunks(arr: List[int], size: int, step: int) -> Generator[List[int], None, None]\n

    Splits a list of encoded tokens into chunks of a given size.

    Parameters:

    Name Type Description Default List[int]

    The list of encoded tokens.

    required int

    The size of the chunks.

    required int

    The step size for the chunks.

    required

    Returns:

    Type Description None

    A generator that yields the chunks

    Source code in gerd/training/data.py
    def split_chunks(\n    arr: List[int], size: int, step: int\n) -> Generator[List[int], None, None]:\n    \"\"\"Splits a list of encoded tokens into chunks of a given size.\n\n    Parameters:\n        arr: The list of encoded tokens.\n        size: The size of the chunks.\n        step: The step size for the chunks.\n\n    Returns:\n        A generator that yields the chunks\n    \"\"\"\n    for i in range(0, len(arr), step):\n        yield arr[i : i + size]\n
    "},{"location":"reference/gerd/training/data/#gerd.training.data.split_chunks(arr)","title":"arr","text":""},{"location":"reference/gerd/training/data/#gerd.training.data.split_chunks(size)","title":"size","text":""},{"location":"reference/gerd/training/data/#gerd.training.data.split_chunks(step)","title":"step","text":""},{"location":"reference/gerd/training/data/#gerd.training.data.tokenize","title":"tokenize","text":"
    tokenize(prompt: str, tokenizer: PreTrainedTokenizer, cutoff_len: int, append_eos_token: bool = False) -> Dict[str, torch.Tensor | list[int]]\n

    Converts a prompt into a tokenized input for a model.

    The methods returns the tokenized input as a dictionary with the keys \"input_ids\", \"labels\" and \"attention_mask\" where the input_ids are the tokenized input, the labels assign the same label ('1') to each token and the attention_mask masks out the padding tokens. Parameters: prompt: The prompt to tokenize tokenizer: The tokenizer to use cutoff_len: The maximum length of the encoded text append_eos_token: Whether to append an end of sentence token

    Returns:

    Type Description Dict[str, Tensor | list[int]]

    The tokenized input as a dictionary

    Source code in gerd/training/data.py
    def tokenize(\n    prompt: str,\n    tokenizer: PreTrainedTokenizer,\n    cutoff_len: int,\n    append_eos_token: bool = False,\n) -> Dict[str, torch.Tensor | list[int]]:\n    \"\"\"Converts a prompt into a tokenized input for a model.\n\n    The methods returns the tokenized input as a dictionary with the keys\n    \"input_ids\", \"labels\" and \"attention_mask\" where the input_ids are the\n    tokenized input, the labels assign the same label ('1') to each token\n    and the attention_mask masks out the padding tokens.\n    Parameters:\n        prompt: The prompt to tokenize\n        tokenizer: The tokenizer to use\n        cutoff_len: The maximum length of the encoded text\n        append_eos_token: Whether to append an end of sentence token\n\n    Returns:\n        The tokenized input as a dictionary\n    \"\"\"\n    input_ids = encode(prompt, True, tokenizer, cutoff_len)\n\n    if tokenizer.pad_token_id is None or tokenizer.padding_side is None:\n        msg = (\n            \"Tokenizing implies tokenizer.pad_token_id \"\n            \"and tokenizer.padding_side to be set!\"\n        )\n        raise AttributeError(msg)\n\n    if (\n        append_eos_token\n        and input_ids[-1] != tokenizer.eos_token_id\n        and len(input_ids) < cutoff_len\n    ):\n        input_ids.append(tokenizer.eos_token_id)\n\n    input_ids = [tokenizer.pad_token_id] * (cutoff_len - len(input_ids)) + input_ids\n    labels = [1] * len(input_ids)\n\n    input_tensors = torch.tensor(input_ids)\n    return {\n        \"input_ids\": input_tensors,\n        \"labels\": labels,\n        \"attention_mask\": input_tensors.ne(tokenizer.pad_token_id),\n    }\n
    "},{"location":"reference/gerd/training/instruct/","title":"gerd.training.instruct","text":""},{"location":"reference/gerd/training/instruct/#gerd.training.instruct","title":"gerd.training.instruct","text":"

    Training module for instruction text sets.

    In contrast to the unstructured training module, data must be prepared in a specific format to train LoRA models. Make sure to provide the training data in the correct format.

    Classes:

    Name Description InstructTrainingData

    Dataclass to hold training data for instruction text sets.

    InstructTrainingSample

    Dataclass to hold a training sample for instruction text sets.

    Functions:

    Name Description train_lora

    Train a LoRA model on instruction text sets.

    "},{"location":"reference/gerd/training/instruct/#gerd.training.instruct.InstructTrainingData","title":"InstructTrainingData","text":"

    Bases: BaseModel

    Dataclass to hold training data for instruction text sets.

    A training data object consists of a list of training samples.

    Attributes:

    Name Type Description samples list[InstructTrainingSample]

    The list of training samples.

    "},{"location":"reference/gerd/training/instruct/#gerd.training.instruct.InstructTrainingData.samples","title":"samples class-attribute instance-attribute","text":"
    samples: list[InstructTrainingSample] = []\n

    The list of training samples.

    "},{"location":"reference/gerd/training/instruct/#gerd.training.instruct.InstructTrainingSample","title":"InstructTrainingSample","text":"

    Bases: BaseModel

    Dataclass to hold a training sample for instruction text sets.

    A training sample consists of a list of chat messages.

    Attributes:

    Name Type Description messages list[ChatMessage]

    The list of chat messages.

    "},{"location":"reference/gerd/training/instruct/#gerd.training.instruct.InstructTrainingSample.messages","title":"messages instance-attribute","text":"
    messages: list[ChatMessage]\n

    The list of chat messages.

    "},{"location":"reference/gerd/training/instruct/#gerd.training.instruct.train_lora","title":"train_lora","text":"
    train_lora(config: str | LoraTrainingConfig, data: InstructTrainingData | None = None) -> Trainer\n

    Train a LoRA model on instruction text sets.

    Parameters:

    Name Type Description Default str | LoraTrainingConfig

    The configuration name or the configuration itself

    required InstructTrainingData | None

    The training data to train on, if None, the input_glob from the config is used

    None

    Returns:

    Type Description Trainer

    The trainer instance that is used for training

    Source code in gerd/training/instruct.py
    def train_lora(\n    config: str | LoraTrainingConfig, data: InstructTrainingData | None = None\n) -> Trainer:\n    \"\"\"Train a LoRA model on instruction text sets.\n\n    Parameters:\n        config: The configuration name or the configuration itself\n        data: The training data to train on, if None,\n            the input_glob from the config is used\n\n    Returns:\n        The trainer instance that is used for training\n    \"\"\"\n    # Disable parallelism to avoid issues with transformers\n    os.environ[\"TOKENIZERS_PARALLELISM\"] = \"false\"\n\n    lora_config = load_training_config(config) if isinstance(config, str) else config\n\n    if Path(lora_config.output_dir).joinpath(\"adapter_model.safetensors\").exists():\n        if lora_config.override_existing:\n            # check that we do not delete anything vital\n            if lora_config.output_dir == Path(\"/\"):\n                msg = \"Cannot delete root directory.\"\n                raise RuntimeError(msg)\n\n            _LOGGER.warning(\n                \"Overriding existing LoRA adapter in %s ...\", lora_config.output_dir\n            )\n            shutil.rmtree(lora_config.output_dir)\n        else:\n            msg = (\n                f\"LoRA target directory {lora_config.output_dir}\"\n                \" must not contain another lora adapter.\"\n            )\n            raise AssertionError(msg)\n\n    _LOGGER.info(\"Tokenizing training data ...\")\n\n    if data is None:\n        data = InstructTrainingData()\n        # currently, we expect the input glob to point to a local directory\n        # in the future, we might want to support other sources\n        glob_pattern = lora_config.input_glob.replace(\"file://\", \"\")\n        for file in Path().glob(glob_pattern):\n            if not file.is_file():\n                msg = \"Can only read files for now.\"\n                raise NotImplementedError(msg)\n            if file.suffix == \".json\":\n                with open(file, \"r\") as f:\n                    data.samples.extend(\n                        InstructTrainingData.model_validate_json(f.read()).samples\n                    )\n            elif file.suffix == \".yml\":\n                with open(file, \"r\") as f:\n                    obj = yaml.safe_load(f)\n                    data.samples.extend(\n                        InstructTrainingData.model_validate(obj).samples\n                    )\n            else:\n                msg = f\"Unsupported file format: {file.suffix}\"\n                raise NotImplementedError(msg)\n\n    train_data = Dataset.from_list(\n        [\n            lora_config.tokenizer(\n                lora_config.tokenizer.apply_chat_template(\n                    sample.messages, tokenize=False\n                )\n            )\n            for sample in data.samples\n        ]\n    )\n    _LOGGER.info(\"Decoding sample data ...\")\n    decoded_entries = []\n    for i in range(min(10, len(train_data))):\n        decoded_text = lora_config.tokenizer.decode(train_data[i][\"input_ids\"])\n        decoded_entries.append({\"value\": decoded_text})\n\n    log_dir = lora_config.output_dir / \"logs\"\n    log_dir.mkdir(exist_ok=True, parents=True)\n    _LOGGER.info(\"Writing sample to %s ...\", log_dir)\n    with open(Path(f\"{log_dir}/train_dataset_sample.json\"), \"w\") as json_file:\n        json.dump(decoded_entries, json_file, indent=4)\n\n    trainer = Trainer(config=lora_config)\n    trainer.setup_training(\n        train_data=train_data, train_template={\"template_type\": \"dataset\"}\n    )\n\n    trainer.train()\n    return trainer\n
    "},{"location":"reference/gerd/training/instruct/#gerd.training.instruct.train_lora(config)","title":"config","text":""},{"location":"reference/gerd/training/instruct/#gerd.training.instruct.train_lora(data)","title":"data","text":""},{"location":"reference/gerd/training/lora/","title":"gerd.training.lora","text":""},{"location":"reference/gerd/training/lora/#gerd.training.lora","title":"gerd.training.lora","text":"

    Configuration dataclasses for training LoRA models.

    Classes:

    Name Description LLMModelProto

    Protocol for the LoRA model.

    LoraModules

    Configuration for the modules to be trained in LoRA models.

    LoraTrainingConfig

    Configuration for training LoRA models.

    TrainingFlags

    Training flags for LoRA models.

    Functions:

    Name Description load_training_config

    Load the LLM model configuration.

    "},{"location":"reference/gerd/training/lora/#gerd.training.lora.LLMModelProto","title":"LLMModelProto","text":"

    Bases: Protocol

    Protocol for the LoRA model.

    A model model needs to implement the named_modules method for it to be used in LoRA Training.

    Methods:

    Name Description named_modules

    Get the named modules of the model.

    "},{"location":"reference/gerd/training/lora/#gerd.training.lora.LLMModelProto.named_modules","title":"named_modules","text":"
    named_modules() -> list[tuple[str, torch.nn.Module]]\n

    Get the named modules of the model.

    Returns:

    Type Description list[tuple[str, Module]]

    The named modules.

    Source code in gerd/training/lora.py
    def named_modules(self) -> list[tuple[str, torch.nn.Module]]:\n    \"\"\"Get the named modules of the model.\n\n    Returns:\n        The named modules.\n    \"\"\"\n    pass\n
    "},{"location":"reference/gerd/training/lora/#gerd.training.lora.LoraModules","title":"LoraModules","text":"

    Bases: BaseModel

    Configuration for the modules to be trained in LoRA models.

    Methods:

    Name Description target_modules

    Get the target modules for the given model.

    "},{"location":"reference/gerd/training/lora/#gerd.training.lora.LoraModules.target_modules","title":"target_modules","text":"
    target_modules(model: LLMModelProto) -> List[str]\n

    Get the target modules for the given model.

    Parameters:

    Name Type Description Default LLMModelProto

    The model to be trained.

    required

    Returns:

    Type Description List[str]

    The list of target modules

    Source code in gerd/training/lora.py
    def target_modules(self, model: LLMModelProto) -> List[str]:\n    \"\"\"Get the target modules for the given model.\n\n    Parameters:\n        model: The model to be trained.\n\n    Returns:\n        The list of target modules\n    \"\"\"\n    avail = _find_target_modules(model)\n    return [\n        f\"{name}_proj\"\n        for name, enabled in self.model_dump().items()\n        if (enabled is True or (enabled is None and self.default is True))\n        and f\"{name}_proj\" in avail\n    ]\n
    "},{"location":"reference/gerd/training/lora/#gerd.training.lora.LoraModules.target_modules(model)","title":"model","text":""},{"location":"reference/gerd/training/lora/#gerd.training.lora.LoraTrainingConfig","title":"LoraTrainingConfig","text":"

    Bases: BaseSettings

    Configuration for training LoRA models.

    Methods:

    Name Description model_post_init

    Post-initialization hook for the model.

    reset_tokenizer

    Resets the tokenizer.

    settings_customise_sources

    Customize the settings sources used by pydantic-settings.

    Attributes:

    Name Type Description tokenizer PreTrainedTokenizer

    Get the tokenizer for the model.

    "},{"location":"reference/gerd/training/lora/#gerd.training.lora.LoraTrainingConfig.tokenizer","title":"tokenizer property","text":"
    tokenizer: PreTrainedTokenizer\n

    Get the tokenizer for the model.

    "},{"location":"reference/gerd/training/lora/#gerd.training.lora.LoraTrainingConfig.model_post_init","title":"model_post_init","text":"
    model_post_init(_: Any) -> None\n

    Post-initialization hook for the model.

    This method currently checks whether cutoff is larger than overlap.

    Source code in gerd/training/lora.py
    def model_post_init(self, _: Any) -> None:  # noqa: ANN401\n    \"\"\"Post-initialization hook for the model.\n\n    This method currently checks whether cutoff is larger than overlap.\n    \"\"\"\n    if self.cutoff_len <= self.overlap_len:\n        msg = (\n            \"Overlap must be smaller than cutoff\"\n            f\"({self.cutoff_len}) but is {self.overlap_len}\"\n        )\n        raise ValueError(msg)\n    self._tokenizer = None\n
    "},{"location":"reference/gerd/training/lora/#gerd.training.lora.LoraTrainingConfig.reset_tokenizer","title":"reset_tokenizer","text":"
    reset_tokenizer() -> None\n

    Resets the tokenizer.

    When a tokenizer has been used it needs to be reset before changig parameters to avoid issues with parallelism.

    Source code in gerd/training/lora.py
    def reset_tokenizer(self) -> None:\n    \"\"\"Resets the tokenizer.\n\n    When a tokenizer has been used it needs to be reset\n    before changig parameters to avoid issues with parallelism.\n    \"\"\"\n    self._tokenizer = transformers.AutoTokenizer.from_pretrained(\n        self.model.name, trust_remote_code=False, use_fast=True\n    )\n    if self._tokenizer.pad_token_id is None:\n        self._tokenizer.pad_token_id = self.pad_token_id\n    if self.padding_side:\n        self._tokenizer.padding_side = self.padding_side\n
    "},{"location":"reference/gerd/training/lora/#gerd.training.lora.LoraTrainingConfig.settings_customise_sources","title":"settings_customise_sources classmethod","text":"
    settings_customise_sources(_: Type[BaseSettings], init_settings: PydanticBaseSettingsSource, env_settings: PydanticBaseSettingsSource, dotenv_settings: PydanticBaseSettingsSource, file_secret_settings: PydanticBaseSettingsSource) -> tuple[PydanticBaseSettingsSource, ...]\n

    Customize the settings sources used by pydantic-settings.

    The order of the sources is important. The first source has the highest priority.

    Parameters:

    Name Type Description Default

    The class of the settings.

    required PydanticBaseSettingsSource

    The settings from the initialization.

    required PydanticBaseSettingsSource

    The settings from the environment.

    required PydanticBaseSettingsSource

    The settings from the dotenv file.

    required PydanticBaseSettingsSource

    The settings from the secret file.

    required

    Returns:

    Type Description tuple[PydanticBaseSettingsSource, ...]

    The customized settings sources.

    Source code in gerd/training/lora.py
    @classmethod\ndef settings_customise_sources(\n    cls,\n    _: Type[BaseSettings],\n    init_settings: PydanticBaseSettingsSource,\n    env_settings: PydanticBaseSettingsSource,\n    dotenv_settings: PydanticBaseSettingsSource,\n    file_secret_settings: PydanticBaseSettingsSource,\n) -> tuple[PydanticBaseSettingsSource, ...]:\n    \"\"\"Customize the settings sources used by pydantic-settings.\n\n    The order of the sources is important.\n    The first source has the highest priority.\n\n    Parameters:\n        cls: The class of the settings.\n        init_settings: The settings from the initialization.\n        env_settings: The settings from the environment.\n        dotenv_settings: The settings from the dotenv file.\n        file_secret_settings: The settings from the secret file.\n\n    Returns:\n        The customized settings sources.\n    \"\"\"\n    return (\n        file_secret_settings,\n        env_settings,\n        dotenv_settings,\n        init_settings,\n    )\n
    "},{"location":"reference/gerd/training/lora/#gerd.training.lora.LoraTrainingConfig.settings_customise_sources(cls)","title":"cls","text":""},{"location":"reference/gerd/training/lora/#gerd.training.lora.LoraTrainingConfig.settings_customise_sources(init_settings)","title":"init_settings","text":""},{"location":"reference/gerd/training/lora/#gerd.training.lora.LoraTrainingConfig.settings_customise_sources(env_settings)","title":"env_settings","text":""},{"location":"reference/gerd/training/lora/#gerd.training.lora.LoraTrainingConfig.settings_customise_sources(dotenv_settings)","title":"dotenv_settings","text":""},{"location":"reference/gerd/training/lora/#gerd.training.lora.LoraTrainingConfig.settings_customise_sources(file_secret_settings)","title":"file_secret_settings","text":""},{"location":"reference/gerd/training/lora/#gerd.training.lora.TrainingFlags","title":"TrainingFlags","text":"

    Bases: BaseModel

    Training flags for LoRA models.

    "},{"location":"reference/gerd/training/lora/#gerd.training.lora.load_training_config","title":"load_training_config","text":"
    load_training_config(config: str) -> LoraTrainingConfig\n

    Load the LLM model configuration.

    Parameters:

    Name Type Description Default str

    The name of the configuration.

    required

    Returns:

    Type Description LoraTrainingConfig

    The model configuration.

    Source code in gerd/training/lora.py
    def load_training_config(config: str) -> LoraTrainingConfig:\n    \"\"\"Load the LLM model configuration.\n\n    Parameters:\n        config: The name of the configuration.\n\n    Returns:\n        The model configuration.\n    \"\"\"\n    config_path = (\n        Path(config)\n        if config.endswith(\"yml\")\n        else Path(PROJECT_DIR, \"config\", f\"{config}.yml\")\n    )\n    with config_path.open(\"r\", encoding=\"utf-8\") as f:\n        conf = LoraTrainingConfig.model_validate(safe_load(f))\n    return conf\n
    "},{"location":"reference/gerd/training/lora/#gerd.training.lora.load_training_config(config)","title":"config","text":""},{"location":"reference/gerd/training/trainer/","title":"gerd.training.trainer","text":""},{"location":"reference/gerd/training/trainer/#gerd.training.trainer","title":"gerd.training.trainer","text":"

    Training module for LoRA models.

    Can be used to train LoRA models on structured or unstructured data.

    Classes:

    Name Description Callbacks

    Custom callbacks for the LoRA training.

    Tracked

    Dataclass to track the training progress.

    Trainer

    The LoRA trainer class.

    "},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Callbacks","title":"Callbacks","text":"
    Callbacks(tracked: Tracked)\n

    Bases: TrainerCallback

    Custom callbacks for the LoRA training.

    Initialize the callbacks based on tracking data config.

    Parameters:

    Name Type Description Default Tracked

    The tracking data

    required

    Methods:

    Name Description on_log

    Callback to log the training progress.

    on_save

    Saves the training log when the model is saved.

    on_step_begin

    Update the training progress.

    on_substep_end

    Update the training progress and check for interruption.

    Source code in gerd/training/trainer.py
    def __init__(self, tracked: Tracked) -> None:\n    \"\"\"Initialize the callbacks based on tracking data config.\n\n    Parameters:\n        tracked: The tracking data\n    \"\"\"\n    super().__init__()\n    self.tracked = tracked\n    self.gradient_accumulation_steps = (\n        tracked.config.batch_size // tracked.config.micro_batch_size\n    )\n    self.actual_save_steps = math.ceil(\n        tracked.config.save_steps / self.gradient_accumulation_steps\n    )\n
    "},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Callbacks(tracked)","title":"tracked","text":""},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Callbacks.on_log","title":"on_log","text":"
    on_log(_args: transformers.TrainingArguments, _state: transformers.TrainerState, control: transformers.TrainerControl, logs: Dict, **kwargs: int) -> None\n

    Callback to log the training progress.

    Parameters:

    Name Type Description Default TrainingArguments

    The training arguments (not used)

    required TrainerState

    The trainer state (not used)

    required TrainerControl

    The trainer control

    required Dict

    The training logs

    required Source code in gerd/training/trainer.py
    def on_log(\n    self,\n    _args: transformers.TrainingArguments,  # noqa: ARG002\n    _state: transformers.TrainerState,  # noqa: ARG002\n    control: transformers.TrainerControl,\n    logs: Dict,\n    **kwargs: int,  # noqa: ARG002\n) -> None:\n    \"\"\"Callback to log the training progress.\n\n    Parameters:\n        _args: The training arguments (not used)\n        _state: The trainer state (not used)\n        control: The trainer control\n        logs: The training logs\n    \"\"\"\n    self.tracked.train_log.update(logs)\n    self.tracked.train_log.update({\"current_steps\": self.tracked.current_steps})\n    if self.tracked.interrupted:\n        _LOGGER.info(\"Interrupted by user\")\n\n    # print(f\"Step: {self.tracked.current_steps}\", end=\"\")\n    if \"loss\" in logs:\n        loss = float(logs[\"loss\"])\n        if loss <= self.tracked.config.stop_at_loss:\n            control.should_epoch_stop = True\n            control.should_training_stop = True\n            _LOGGER.info(\"Stop Loss %f reached.\", self.tracked.config.stop_at_loss)\n
    "},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Callbacks.on_log(_args)","title":"_args","text":""},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Callbacks.on_log(_state)","title":"_state","text":""},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Callbacks.on_log(control)","title":"control","text":""},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Callbacks.on_log(logs)","title":"logs","text":""},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Callbacks.on_save","title":"on_save","text":"
    on_save(_args: transformers.TrainingArguments, _state: transformers.TrainerState, _control: transformers.TrainerControl, **kwargs: int) -> None\n

    Saves the training log when the model is saved.

    Source code in gerd/training/trainer.py
    def on_save(\n    self,\n    _args: transformers.TrainingArguments,  # noqa: ARG002\n    _state: transformers.TrainerState,  # noqa: ARG002\n    _control: transformers.TrainerControl,  # noqa: ARG002\n    **kwargs: int,  # noqa: ARG002\n) -> None:\n    \"\"\"Saves the training log when the model is saved.\"\"\"\n    # Save log\n    with open(\n        f\"{self.tracked.config.output_dir}/{self.tracked.current_steps}-training_log.json\",\n        \"w\",\n        encoding=\"utf-8\",\n    ) as file:\n        json.dump(self.tracked.train_log, file, indent=2)\n
    "},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Callbacks.on_step_begin","title":"on_step_begin","text":"
    on_step_begin(_args: transformers.TrainingArguments, state: transformers.TrainerState, control: transformers.TrainerControl, **kwargs: int) -> None\n

    Update the training progress.

    This callback updates the current training steps and checks if the training was interrupted.

    Parameters:

    Name Type Description Default TrainingArguments

    The training arguments (not used)

    required TrainerState

    The trainer state

    required TrainerControl

    The trainer control

    required Source code in gerd/training/trainer.py
    def on_step_begin(\n    self,\n    _args: transformers.TrainingArguments,  # noqa: ARG002\n    state: transformers.TrainerState,\n    control: transformers.TrainerControl,\n    **kwargs: int,  # noqa: ARG002\n) -> None:\n    \"\"\"Update the training progress.\n\n    This callback updates the current training steps and\n    checks if the training was interrupted.\n\n    Parameters:\n        _args: The training arguments (not used)\n        state: The trainer state\n        control: The trainer control\n    \"\"\"\n    self.tracked.current_steps = (\n        state.global_step * self.gradient_accumulation_steps\n    )\n    self.tracked.max_steps = state.max_steps * self.gradient_accumulation_steps\n    if self.tracked.interrupted:\n        control.should_epoch_stop = True\n        control.should_training_stop = True\n
    "},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Callbacks.on_step_begin(_args)","title":"_args","text":""},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Callbacks.on_step_begin(state)","title":"state","text":""},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Callbacks.on_step_begin(control)","title":"control","text":""},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Callbacks.on_substep_end","title":"on_substep_end","text":"
    on_substep_end(_args: transformers.TrainingArguments, _state: transformers.TrainerState, control: transformers.TrainerControl, **kwargs: int) -> None\n

    Update the training progress and check for interruption.

    Parameters:

    Name Type Description Default TrainingArguments

    The training arguments (not used)

    required TrainerState

    The trainer state (not used)

    required TrainerControl

    The trainer control

    required Source code in gerd/training/trainer.py
    def on_substep_end(\n    self,\n    _args: transformers.TrainingArguments,  # noqa: ARG002\n    _state: transformers.TrainerState,  # noqa: ARG002\n    control: transformers.TrainerControl,\n    **kwargs: int,  # noqa: ARG002\n) -> None:\n    \"\"\"Update the training progress and check for interruption.\n\n    Parameters:\n        _args: The training arguments (not used)\n        _state: The trainer state (not used)\n        control: The trainer control\n    \"\"\"\n    self.tracked.current_steps += 1\n    if self.tracked.interrupted:\n        control.should_epoch_stop = True\n        control.should_training_stop = True\n
    "},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Callbacks.on_substep_end(_args)","title":"_args","text":""},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Callbacks.on_substep_end(_state)","title":"_state","text":""},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Callbacks.on_substep_end(control)","title":"control","text":""},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Tracked","title":"Tracked dataclass","text":"
    Tracked(lora_model: PeftModel, config: LoraTrainingConfig, train_log: Dict = dict(), current_steps: int = 0, interrupted: bool = False, max_steps: int = 0, did_save: bool = False)\n

    Dataclass to track the training progress.

    Attributes:

    Name Type Description config LoraTrainingConfig

    The training configuration.

    current_steps int

    The current training steps.

    did_save bool

    Whether the model was saved.

    interrupted bool

    Whether the training was interrupted.

    lora_model PeftModel

    The training model.

    max_steps int

    The maximum number of training steps.

    train_log Dict

    The training log.

    "},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Tracked.config","title":"config instance-attribute","text":"
    config: LoraTrainingConfig\n

    The training configuration.

    "},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Tracked.current_steps","title":"current_steps class-attribute instance-attribute","text":"
    current_steps: int = 0\n

    The current training steps.

    "},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Tracked.did_save","title":"did_save class-attribute instance-attribute","text":"
    did_save: bool = False\n

    Whether the model was saved.

    "},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Tracked.interrupted","title":"interrupted class-attribute instance-attribute","text":"
    interrupted: bool = False\n

    Whether the training was interrupted.

    "},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Tracked.lora_model","title":"lora_model instance-attribute","text":"
    lora_model: PeftModel\n

    The training model.

    "},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Tracked.max_steps","title":"max_steps class-attribute instance-attribute","text":"
    max_steps: int = 0\n

    The maximum number of training steps.

    "},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Tracked.train_log","title":"train_log class-attribute instance-attribute","text":"
    train_log: Dict = field(default_factory=dict)\n

    The training log.

    "},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Trainer","title":"Trainer","text":"
    Trainer(config: LoraTrainingConfig, callback_cls: Optional[List[Type[transformers.TrainerCallback]]] = None)\n

    The LoRA trainer class.

    This class is used to train LoRA models on structured or unstructured data. Since the training process is asynchronous, the trainer can be used to track or interrupt the training process.

    The LoRa traininer requires a configuration and optional list of callbacks.

    If no callbacks are provided, the default Callbacks class Tracked is used. Parameters: config: The training configuration callback_cls: The list of callbacks

    Methods:

    Name Description interrupt

    Interrupt the training process.

    save

    Save the model and log files to the path set in the trainer configuration.

    setup_training

    Setup the training process and initialize the transformer trainer.

    train

    Start the training process.

    Source code in gerd/training/trainer.py
    def __init__(\n    self,\n    config: LoraTrainingConfig,\n    callback_cls: Optional[List[Type[transformers.TrainerCallback]]] = None,\n) -> None:\n    \"\"\"The LoRa traininer requires a configuration and optional list of callbacks.\n\n    If no callbacks are provided,\n    the default Callbacks class [`Tracked`][gerd.training.trainer.Tracked] is used.\n    Parameters:\n        config: The training configuration\n        callback_cls: The list of callbacks\n    \"\"\"\n    self.config = config\n    self.base_model: transformers.PreTrainedModel = (\n        transformers.AutoModelForCausalLM.from_pretrained(\n            config.model.name,\n            load_in_4bit=config.flags.use_4bit,\n            load_in_8bit=config.flags.use_8bit,\n        )\n    )\n\n    training_config = LoraConfig(\n        lora_alpha=config.lora_alpha,\n        target_modules=config.modules.target_modules(self.base_model),\n        lora_dropout=config.lora_dropout,\n        bias=config.bias,\n        task_type=config.task_type,\n        r=config.r,\n    )\n\n    for param in self.base_model.parameters():\n        if param.requires_grad:\n            param.data = param.data.float()\n    if (\n        not hasattr(self.base_model, \"lm_head\")\n        or hasattr(self.base_model.lm_head, \"weight\")\n    ) and \"quantization_config\" in self.base_model.config.to_dict():\n        self.base_model = prepare_model_for_kbit_training(self.base_model)\n        _LOGGER.info(\"Quantization detected!\")\n\n    self.lora_model = get_peft_model(self.base_model, training_config)\n    if not hasattr(self.lora_model.config, \"use_cache\"):\n        msg = \"LoRA model config not have 'use_cache' attribute\"\n        raise AssertionError(msg)\n    self.lora_model.config.use_cache = False\n\n    self.tracked = Tracked(self.lora_model, self.config)\n    self.trainer = None\n    self.callbacks = (\n        [cls(self.tracked) for cls in callback_cls]\n        if callback_cls is not None\n        else [Callbacks(self.tracked)]\n    )\n\n    gradient_accumulation_steps = config.batch_size // config.micro_batch_size\n    actual_lr = float(config.learning_rate)\n\n    self.args = transformers.TrainingArguments(\n        per_device_train_batch_size=self.config.micro_batch_size,\n        gradient_accumulation_steps=gradient_accumulation_steps,\n        warmup_steps=math.ceil(config.warmup_steps / gradient_accumulation_steps),\n        num_train_epochs=config.epochs,\n        learning_rate=actual_lr,\n        fp16=not (config.flags.use_cpu or config.flags.use_bf16),\n        bf16=config.flags.use_bf16,\n        optim=config.optimizer,\n        logging_steps=2 if config.stop_at_loss > 0 else 5,\n        eval_strategy=\"no\",\n        eval_steps=None,\n        save_strategy=\"no\",\n        output_dir=config.output_dir,\n        lr_scheduler_type=config.lr_scheduler,\n        load_best_model_at_end=False,\n        # TODO: Enable multi-device support\n        ddp_find_unused_parameters=None,\n        use_ipex=config.flags.use_ipex,\n        save_steps=config.save_steps,\n        use_cpu=config.flags.use_cpu,\n        # any of these two will set `torch_compile=True`\n        # torch_compile_backend=\"inductor\",\n        # torch_compile_mode=\"reduce-overhead\",\n    )\n
    "},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Trainer.interrupt","title":"interrupt","text":"
    interrupt() -> None\n

    Interrupt the training process.

    Source code in gerd/training/trainer.py
    def interrupt(self) -> None:\n    \"\"\"Interrupt the training process.\"\"\"\n    self.tracked.interrupted = True\n
    "},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Trainer.save","title":"save","text":"
    save() -> None\n

    Save the model and log files to the path set in the trainer configuration.

    When the zip_output flag is set, the output directory is zipped as well.

    Source code in gerd/training/trainer.py
    def save(self) -> None:\n    \"\"\"Save the model and log files to the path set in the trainer configuration.\n\n    When the `zip_output` flag is set, the output directory is zipped as well.\n    \"\"\"\n    if self.trainer is not None:\n        self.trainer.save_model(self.config.output_dir)\n        if self.config.zip_output:\n            shutil.make_archive(\n                base_name=self.config.output_dir.as_posix(),\n                root_dir=self.config.output_dir,\n                # base_dir=self.config.output_dir,\n                format=\"zip\",\n            )\n    else:\n        _LOGGER.warning(\"Trainer not initialized\")\n
    "},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Trainer.setup_training","title":"setup_training","text":"
    setup_training(train_data: Dataset, train_template: Dict, torch_compile: bool = False) -> None\n

    Setup the training process and initialize the transformer trainer.

    Parameters:

    Name Type Description Default Dataset

    The training data

    required Dict

    The training template

    required bool

    Whether to use torch compile

    False Source code in gerd/training/trainer.py
    def setup_training(\n    self,\n    train_data: Dataset,\n    train_template: Dict,\n    torch_compile: bool = False,\n) -> None:\n    \"\"\"Setup the training process and initialize the transformer trainer.\n\n    Parameters:\n        train_data: The training data\n        train_template: The training template\n        torch_compile: Whether to use torch compile\n    \"\"\"\n    _LOGGER.debug(\"Training parameter\\n============\\n %s\", self.args)\n    self.trainer = transformers.Trainer(\n        model=self.lora_model,\n        train_dataset=train_data,\n        eval_dataset=None,\n        args=self.args,\n        data_collator=transformers.DataCollatorForLanguageModeling(\n            self.config.tokenizer, mlm=False\n        ),\n        callbacks=self.callbacks,\n    )\n\n    # This must be done after Trainer init because otherwise\n    # the trainer cannot identify the relevant (as in 'trainable') parameters\n    # and will remove required information from the training data set.\n    # Whether it is useful to compile after reassignment.\n    if torch_compile and torch.__version__ >= \"2\" and sys.platform != \"win32\":\n        self.lora_model = torch.compile(self.lora_model)  # type: ignore[assignment]\n\n    # == Save parameters for reuse ==\n    with open(\n        f\"{self.config.output_dir}/training_parameters.json\", \"w\", encoding=\"utf-8\"\n    ) as file:\n        file.write(self.config.model_dump_json(indent=2))\n\n    # == Save training prompt ==\n    with open(\n        f\"{self.config.output_dir}/training_prompt.json\", \"w\", encoding=\"utf-8\"\n    ) as file:\n        json.dump(train_template, file, indent=2)\n
    "},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Trainer.setup_training(train_data)","title":"train_data","text":""},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Trainer.setup_training(train_template)","title":"train_template","text":""},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Trainer.setup_training(torch_compile)","title":"torch_compile","text":""},{"location":"reference/gerd/training/trainer/#gerd.training.trainer.Trainer.train","title":"train","text":"
    train() -> threading.Thread\n

    Start the training process.

    Returns:

    Type Description Thread

    The training thread

    Source code in gerd/training/trainer.py
    def train(self) -> threading.Thread:\n    \"\"\"Start the training process.\n\n    Returns:\n        The training thread\n    \"\"\"\n    model_type = type(self.base_model).__name__\n    model_id = MODEL_CLASSES[model_type]\n\n    projections_string = \", \".join(\n        [\n            projection.replace(\"_proj\", \"\")\n            for projection in self.config.modules.target_modules(self.base_model)\n        ]\n    )\n\n    _LOGGER.info(\n        \"Training '%s' model using (%s) projections\", model_id, projections_string\n    )\n    # == Main run and monitor loop ==\n\n    log = self.tracked.train_log\n    log.update({\"base_model_config\": self.config.model.name})\n    log.update({\"base_model_class\": self.base_model.__class__.__name__})\n    log.update(\n        {\n            \"base_loaded_in_4bit\": getattr(\n                self.lora_model, \"is_loaded_in_4bit\", False\n            )\n        }\n    )\n    log.update(\n        {\n            \"base_loaded_in_8bit\": getattr(\n                self.lora_model, \"is_loaded_in_8bit\", False\n            )\n        }\n    )\n    log.update({\"projections\": projections_string})\n\n    if self.config.stop_at_loss > 0:\n        _LOGGER.info(\n            \"Monitoring loss \\033[1;31;1m(Auto-Stop at: %f)\\033[0;37;0m\",\n            self.config.stop_at_loss,\n        )\n\n    def threaded_run() -> None:\n        if self.trainer is None:\n            msg = \"Trainer not initialized\"\n            raise RuntimeError(msg)\n        self.trainer.train()\n        # Note: save in the thread in case the gradio thread breaks\n        # (eg browser closed)\n        self.save()\n        self.tracked.did_save = True\n        # Save log\n        with open(\n            f\"{self.config.output_dir}/training_log.json\", \"w\", encoding=\"utf-8\"\n        ) as file:\n            json.dump(self.tracked.train_log, file, indent=2)\n\n    self.thread = threading.Thread(target=threaded_run)\n    self.thread.start()\n    return self.thread\n
    "},{"location":"reference/gerd/training/unstructured/","title":"gerd.training.unstructured","text":""},{"location":"reference/gerd/training/unstructured/#gerd.training.unstructured","title":"gerd.training.unstructured","text":"

    Training of LoRA models on unstructured text data.

    This module provides functions to train LoRA models to 'imitate' the style of a given text corpus.

    Functions:

    Name Description train_lora

    Train a LoRA model on unstructured text data.

    "},{"location":"reference/gerd/training/unstructured/#gerd.training.unstructured.train_lora","title":"train_lora","text":"
    train_lora(config: str | LoraTrainingConfig, texts: list[str] | None = None) -> Trainer\n

    Train a LoRA model on unstructured text data.

    Parameters:

    Name Type Description Default str | LoraTrainingConfig

    The configuration name or the configuration itself

    required list[str] | None

    The list of texts to train on, if None, the input_glob from the config is used

    None

    Returns:

    Type Description Trainer

    The trainer instance that is used for training

    Source code in gerd/training/unstructured.py
    def train_lora(\n    config: str | LoraTrainingConfig, texts: list[str] | None = None\n) -> Trainer:\n    \"\"\"Train a LoRA model on unstructured text data.\n\n    Parameters:\n        config: The configuration name or the configuration itself\n        texts: The list of texts to train on, if None,\n            the input_glob from the config is used\n\n    Returns:\n        The trainer instance that is used for training\n    \"\"\"\n    # Disable parallelism to avoid issues with transformers\n    os.environ[\"TOKENIZERS_PARALLELISM\"] = \"false\"\n\n    lora_config = load_training_config(config) if isinstance(config, str) else config\n\n    if Path(lora_config.output_dir).joinpath(\"adapter_model.safetensors\").exists():\n        if lora_config.override_existing:\n            # check that we do not delete anything vital\n            if lora_config.output_dir == Path(\"/\"):\n                msg = \"Cannot delete root directory.\"\n                raise RuntimeError(msg)\n\n            _LOGGER.warning(\n                \"Overriding existing LoRA adapter in %s ...\", lora_config.output_dir\n            )\n            shutil.rmtree(lora_config.output_dir)\n        else:\n            msg = (\n                f\"LoRA target directory {lora_config.output_dir}\"\n                \" must not contain another lora adapter.\"\n            )\n            raise AssertionError(msg)\n\n    _LOGGER.info(\"Tokenizing training data ...\")\n\n    if not texts:\n        texts = []\n        # currently, we expect the input glob to point to a local directory\n        # in the future, we might want to support other sources\n        glob_pattern = lora_config.input_glob.replace(\"file://\", \"\")\n        for file in Path().glob(glob_pattern):\n            with open(file, \"r\") as f:\n                texts.append(f.read())\n\n    training_tokens: list[list[int]] = []\n    for text in texts:\n        if len(text) == 0:\n            continue\n        training_tokens.extend(\n            split_chunks(\n                lora_config.tokenizer.encode(text),\n                lora_config.cutoff_len,\n                lora_config.cutoff_len - lora_config.overlap_len,\n            )\n        )\n    text_chunks = [lora_config.tokenizer.decode(x) for x in training_tokens]\n    train_data = Dataset.from_list(\n        [\n            tokenize(x, lora_config.tokenizer, lora_config.cutoff_len)\n            for x in text_chunks\n        ]\n    )\n    _LOGGER.info(\"Decoding sample data ...\")\n    decoded_entries = []\n    for i in range(min(10, len(train_data))):\n        decoded_text = lora_config.tokenizer.decode(train_data[i][\"input_ids\"])\n        decoded_entries.append({\"value\": decoded_text})\n\n    log_dir = lora_config.output_dir / \"logs\"\n    log_dir.mkdir(exist_ok=True, parents=True)\n    _LOGGER.info(\"Writing sample to %s ...\", log_dir)\n    with open(Path(f\"{log_dir}/train_dataset_sample.json\"), \"w\") as json_file:\n        json.dump(decoded_entries, json_file, indent=4)\n\n    trainer = Trainer(config=lora_config)\n    trainer.setup_training(\n        train_data=train_data, train_template={\"template_type\": \"raw_text\"}\n    )\n\n    trainer.train()\n    return trainer\n
    "},{"location":"reference/gerd/training/unstructured/#gerd.training.unstructured.train_lora(config)","title":"config","text":""},{"location":"reference/gerd/training/unstructured/#gerd.training.unstructured.train_lora(texts)","title":"texts","text":""},{"location":"reference/gerd/transport/","title":"gerd.transport","text":""},{"location":"reference/gerd/transport/#gerd.transport","title":"gerd.transport","text":"

    Module to define the transport protocol.

    The transport protocol is used to connect the backend and frontend services. Implemetations of the transport protocol can be found in the gerd.backends module.

    Classes:

    Name Description DocumentSource

    Dataclass to hold a document source.

    FileTypes

    Enum to hold all supported file types.

    GenResponse

    Dataclass to hold a response from the generation service.

    QAAnalyzeAnswer

    Dataclass to hold an answer from the predefined queries to the QA service.

    QAAnswer

    Dataclass to hold an answer from the QA service.

    QAFileUpload

    Dataclass to hold a file upload.

    QAModesEnum

    Enum to hold all supported QA modes.

    QAPromptConfig

    Prompt configuration for the QA service.

    QAQuestion

    Dataclass to hold a question for the QA service.

    Transport

    Transport protocol to connect backend and frontend services.

    "},{"location":"reference/gerd/transport/#gerd.transport.DocumentSource","title":"DocumentSource","text":"

    Bases: BaseModel

    Dataclass to hold a document source.

    Attributes:

    Name Type Description content str

    The content of the document.

    name str

    The name of the document.

    page int

    The page of the document.

    query str

    The query that was used to find the document.

    "},{"location":"reference/gerd/transport/#gerd.transport.DocumentSource.content","title":"content instance-attribute","text":"
    content: str\n

    The content of the document.

    "},{"location":"reference/gerd/transport/#gerd.transport.DocumentSource.name","title":"name instance-attribute","text":"
    name: str\n

    The name of the document.

    "},{"location":"reference/gerd/transport/#gerd.transport.DocumentSource.page","title":"page instance-attribute","text":"
    page: int\n

    The page of the document.

    "},{"location":"reference/gerd/transport/#gerd.transport.DocumentSource.query","title":"query instance-attribute","text":"
    query: str\n

    The query that was used to find the document.

    "},{"location":"reference/gerd/transport/#gerd.transport.FileTypes","title":"FileTypes","text":"

    Bases: Enum

    Enum to hold all supported file types.

    Attributes:

    Name Type Description PDF

    PDF file type.

    TEXT

    Text file type.

    "},{"location":"reference/gerd/transport/#gerd.transport.FileTypes.PDF","title":"PDF class-attribute instance-attribute","text":"
    PDF = 'pdf'\n

    PDF file type.

    "},{"location":"reference/gerd/transport/#gerd.transport.FileTypes.TEXT","title":"TEXT class-attribute instance-attribute","text":"
    TEXT = 'txt'\n

    Text file type.

    "},{"location":"reference/gerd/transport/#gerd.transport.GenResponse","title":"GenResponse","text":"

    Bases: BaseModel

    Dataclass to hold a response from the generation service.

    Attributes:

    Name Type Description error_msg str

    The error message if the status code is not 200.

    prompt str | None

    The custom prompt that was used to generate the text.

    status int

    The status code of the response.

    text str

    The generated text if the status code is 200.

    "},{"location":"reference/gerd/transport/#gerd.transport.GenResponse.error_msg","title":"error_msg class-attribute instance-attribute","text":"
    error_msg: str = ''\n

    The error message if the status code is not 200.

    "},{"location":"reference/gerd/transport/#gerd.transport.GenResponse.prompt","title":"prompt class-attribute instance-attribute","text":"
    prompt: str | None = None\n

    The custom prompt that was used to generate the text.

    "},{"location":"reference/gerd/transport/#gerd.transport.GenResponse.status","title":"status class-attribute instance-attribute","text":"
    status: int = 200\n

    The status code of the response.

    "},{"location":"reference/gerd/transport/#gerd.transport.GenResponse.text","title":"text class-attribute instance-attribute","text":"
    text: str = ''\n

    The generated text if the status code is 200.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAAnalyzeAnswer","title":"QAAnalyzeAnswer","text":"

    Bases: BaseModel

    Dataclass to hold an answer from the predefined queries to the QA service.

    Attributes:

    Name Type Description error_msg str

    The error message of the answer if the status code is not 200.

    status int

    The status code of the answer.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAAnalyzeAnswer.error_msg","title":"error_msg class-attribute instance-attribute","text":"
    error_msg: str = ''\n

    The error message of the answer if the status code is not 200.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAAnalyzeAnswer.status","title":"status class-attribute instance-attribute","text":"
    status: int = 200\n

    The status code of the answer.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAAnswer","title":"QAAnswer","text":"

    Bases: BaseModel

    Dataclass to hold an answer from the QA service.

    Attributes:

    Name Type Description error_msg str

    The error message of the answer if the status code is not 200.

    response str

    The response of the answer.

    sources List[DocumentSource]

    The sources of the answer.

    status int

    The status code of the answer.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAAnswer.error_msg","title":"error_msg class-attribute instance-attribute","text":"
    error_msg: str = ''\n

    The error message of the answer if the status code is not 200.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAAnswer.response","title":"response class-attribute instance-attribute","text":"
    response: str = ''\n

    The response of the answer.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAAnswer.sources","title":"sources class-attribute instance-attribute","text":"
    sources: List[DocumentSource] = []\n

    The sources of the answer.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAAnswer.status","title":"status class-attribute instance-attribute","text":"
    status: int = 200\n

    The status code of the answer.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAFileUpload","title":"QAFileUpload","text":"

    Bases: BaseModel

    Dataclass to hold a file upload.

    Attributes:

    Name Type Description data bytes

    The file data.

    name str

    The name of the file.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAFileUpload.data","title":"data instance-attribute","text":"
    data: bytes\n

    The file data.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAFileUpload.name","title":"name instance-attribute","text":"
    name: str\n

    The name of the file.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAModesEnum","title":"QAModesEnum","text":"

    Bases: Enum

    Enum to hold all supported QA modes.

    Attributes:

    Name Type Description ANALYZE

    Analyze mode.

    ANALYZE_MULT_PROMPTS

    Analyze multiple prompts mode.

    NONE

    No mode.

    SEARCH

    Search mode.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAModesEnum.ANALYZE","title":"ANALYZE class-attribute instance-attribute","text":"
    ANALYZE = 2\n

    Analyze mode.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAModesEnum.ANALYZE_MULT_PROMPTS","title":"ANALYZE_MULT_PROMPTS class-attribute instance-attribute","text":"
    ANALYZE_MULT_PROMPTS = 3\n

    Analyze multiple prompts mode.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAModesEnum.NONE","title":"NONE class-attribute instance-attribute","text":"
    NONE = 0\n

    No mode.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAModesEnum.SEARCH","title":"SEARCH class-attribute instance-attribute","text":"
    SEARCH = 1\n

    Search mode.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAPromptConfig","title":"QAPromptConfig","text":"

    Bases: BaseModel

    Prompt configuration for the QA service.

    Attributes:

    Name Type Description config PromptConfig

    The prompt configuration.

    mode QAModesEnum

    The mode to set the prompt configuration for.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAPromptConfig.config","title":"config instance-attribute","text":"
    config: PromptConfig\n

    The prompt configuration.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAPromptConfig.mode","title":"mode instance-attribute","text":"
    mode: QAModesEnum\n

    The mode to set the prompt configuration for.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAQuestion","title":"QAQuestion","text":"

    Bases: BaseModel

    Dataclass to hold a question for the QA service.

    Attributes:

    Name Type Description max_sources int

    The maximum number of sources to return.

    question str

    The question to ask the QA service.

    search_strategy str

    The search strategy to use.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAQuestion.max_sources","title":"max_sources class-attribute instance-attribute","text":"
    max_sources: int = 3\n

    The maximum number of sources to return.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAQuestion.question","title":"question instance-attribute","text":"
    question: str\n

    The question to ask the QA service.

    "},{"location":"reference/gerd/transport/#gerd.transport.QAQuestion.search_strategy","title":"search_strategy class-attribute instance-attribute","text":"
    search_strategy: str = 'similarity'\n

    The search strategy to use.

    "},{"location":"reference/gerd/transport/#gerd.transport.Transport","title":"Transport","text":"

    Bases: Protocol

    Transport protocol to connect backend and frontend services.

    Transport should be implemented by a class that provides the necessary methods to interact with the backend.

    Methods:

    Name Description add_file

    Add a file to the vector store.

    analyze_mult_prompts_query

    Queries the vector store with a set of predefined queries.

    analyze_query

    Queries the vector store with a predefined query.

    db_embedding

    Converts a question to an embedding.

    db_query

    Queries the vector store with a question.

    generate

    Generates text with the generation service.

    get_gen_prompt

    Gets the prompt configuration for the generation service.

    get_qa_prompt

    Gets the prompt configuration for a mode of the QA service.

    qa_query

    Query the QA service with a question.

    remove_file

    Remove a file from the vector store.

    set_gen_prompt

    Sets the prompt configuration for the generation service.

    set_qa_prompt

    Sets the prompt configuration for the QA service.

    "},{"location":"reference/gerd/transport/#gerd.transport.Transport.add_file","title":"add_file","text":"
    add_file(file: QAFileUpload) -> QAAnswer\n

    Add a file to the vector store.

    The returned answer has a status code of 200 if the file was added successfully. Parameters: file: The file to add to the vector store.

    Returns:

    Type Description QAAnswer

    The answer from the QA service

    Source code in gerd/transport.py
    def add_file(self, file: QAFileUpload) -> QAAnswer:\n    \"\"\"Add a file to the vector store.\n\n    The returned answer has a status code of 200 if the file was added successfully.\n    Parameters:\n        file: The file to add to the vector store.\n\n    Returns:\n        The answer from the QA service\n\n    \"\"\"\n    pass\n
    "},{"location":"reference/gerd/transport/#gerd.transport.Transport.analyze_mult_prompts_query","title":"analyze_mult_prompts_query","text":"
    analyze_mult_prompts_query() -> QAAnalyzeAnswer\n

    Queries the vector store with a set of predefined queries.

    In contrast to analyze_query, this method queries the vector store with multiple prompts.

    Returns:

    Type Description QAAnalyzeAnswer

    The answer from the QA service.

    Source code in gerd/transport.py
    def analyze_mult_prompts_query(self) -> QAAnalyzeAnswer:\n    \"\"\"Queries the vector store with a set of predefined queries.\n\n    In contrast to [`analyze_query`][gerd.transport.Transport.analyze_query],\n    this method queries the vector store with multiple prompts.\n\n    Returns:\n        The answer from the QA service.\n    \"\"\"\n    pass\n
    "},{"location":"reference/gerd/transport/#gerd.transport.Transport.analyze_query","title":"analyze_query","text":"
    analyze_query() -> QAAnalyzeAnswer\n

    Queries the vector store with a predefined query.

    The query should return vital information gathered from letters of discharge.

    Returns:

    Type Description QAAnalyzeAnswer

    The answer from the QA service.

    Source code in gerd/transport.py
    def analyze_query(self) -> QAAnalyzeAnswer:\n    \"\"\"Queries the vector store with a predefined query.\n\n    The query should return vital information gathered\n    from letters of discharge.\n\n    Returns:\n        The answer from the QA service.\n    \"\"\"\n    pass\n
    "},{"location":"reference/gerd/transport/#gerd.transport.Transport.db_embedding","title":"db_embedding","text":"
    db_embedding(question: QAQuestion) -> List[float]\n

    Converts a question to an embedding.

    The embedding is defined by the vector store.

    Parameters:

    Name Type Description Default QAQuestion

    The question to convert to an embedding.

    required

    Returns:

    Type Description List[float]

    The embedding of the question

    Source code in gerd/transport.py
    def db_embedding(self, question: QAQuestion) -> List[float]:\n    \"\"\"Converts a question to an embedding.\n\n    The embedding is defined by the vector store.\n\n    Parameters:\n        question: The question to convert to an embedding.\n\n    Returns:\n        The embedding of the question\n    \"\"\"\n    pass\n
    "},{"location":"reference/gerd/transport/#gerd.transport.Transport.db_embedding(question)","title":"question","text":""},{"location":"reference/gerd/transport/#gerd.transport.Transport.db_query","title":"db_query","text":"
    db_query(question: QAQuestion) -> List[DocumentSource]\n

    Queries the vector store with a question.

    Parameters:

    Name Type Description Default QAQuestion

    The question to query the vector store with.

    required

    Returns:

    Type Description List[DocumentSource]

    A list of document sources

    Source code in gerd/transport.py
    def db_query(self, question: QAQuestion) -> List[DocumentSource]:\n    \"\"\"Queries the vector store with a question.\n\n    Parameters:\n        question: The question to query the vector store with.\n\n    Returns:\n        A list of document sources\n    \"\"\"\n    pass\n
    "},{"location":"reference/gerd/transport/#gerd.transport.Transport.db_query(question)","title":"question","text":""},{"location":"reference/gerd/transport/#gerd.transport.Transport.generate","title":"generate","text":"
    generate(parameters: Dict[str, str]) -> GenResponse\n

    Generates text with the generation service.

    Parameters:

    Name Type Description Default Dict[str, str]

    The parameters to generate text with

    required

    Returns:

    Type Description GenResponse

    The generation result

    Source code in gerd/transport.py
    def generate(self, parameters: Dict[str, str]) -> GenResponse:\n    \"\"\"Generates text with the generation service.\n\n    Parameters:\n        parameters: The parameters to generate text with\n\n    Returns:\n        The generation result\n    \"\"\"\n    pass\n
    "},{"location":"reference/gerd/transport/#gerd.transport.Transport.generate(parameters)","title":"parameters","text":""},{"location":"reference/gerd/transport/#gerd.transport.Transport.get_gen_prompt","title":"get_gen_prompt","text":"
    get_gen_prompt() -> PromptConfig\n

    Gets the prompt configuration for the generation service.

    Returns:

    Type Description PromptConfig

    The current prompt configuration

    Source code in gerd/transport.py
    def get_gen_prompt(self) -> PromptConfig:\n    \"\"\"Gets the prompt configuration for the generation service.\n\n    Returns:\n        The current prompt configuration\n    \"\"\"\n    pass\n
    "},{"location":"reference/gerd/transport/#gerd.transport.Transport.get_qa_prompt","title":"get_qa_prompt","text":"
    get_qa_prompt(qa_mode: QAModesEnum) -> PromptConfig\n

    Gets the prompt configuration for a mode of the QA service.

    Parameters:

    Name Type Description Default QAModesEnum

    The mode to get the prompt configuration for

    required

    Returns:

    Type Description PromptConfig

    The prompt configuration for the QA service

    Source code in gerd/transport.py
    def get_qa_prompt(self, qa_mode: QAModesEnum) -> PromptConfig:\n    \"\"\"Gets the prompt configuration for a mode of the QA service.\n\n    Parameters:\n        qa_mode: The mode to get the prompt configuration for\n\n    Returns:\n        The prompt configuration for the QA service\n    \"\"\"\n    pass\n
    "},{"location":"reference/gerd/transport/#gerd.transport.Transport.get_qa_prompt(qa_mode)","title":"qa_mode","text":""},{"location":"reference/gerd/transport/#gerd.transport.Transport.qa_query","title":"qa_query","text":"
    qa_query(query: QAQuestion) -> QAAnswer\n

    Query the QA service with a question.

    Parameters:

    Name Type Description Default QAQuestion

    The question to query the QA service with.

    required

    Returns:

    Type Description QAAnswer

    The answer from the QA service.

    Source code in gerd/transport.py
    def qa_query(self, query: QAQuestion) -> QAAnswer:\n    \"\"\"Query the QA service with a question.\n\n    Parameters:\n        query: The question to query the QA service with.\n\n    Returns:\n       The answer from the QA service.\n    \"\"\"\n    pass\n
    "},{"location":"reference/gerd/transport/#gerd.transport.Transport.qa_query(query)","title":"query","text":""},{"location":"reference/gerd/transport/#gerd.transport.Transport.remove_file","title":"remove_file","text":"
    remove_file(file_name: str) -> QAAnswer\n

    Remove a file from the vector store.

    The returned answer has a status code of 200 if the file was removed successfully. Parameters: file_name: The name of the file to remove from the vector store.

    Returns:

    Type Description QAAnswer

    The answer from the QA service

    Source code in gerd/transport.py
    def remove_file(self, file_name: str) -> QAAnswer:\n    \"\"\"Remove a file from the vector store.\n\n    The returned answer has a status code of 200\n    if the file was removed successfully.\n    Parameters:\n        file_name: The name of the file to remove from the vector store.\n\n    Returns:\n        The answer from the QA service\n    \"\"\"\n    pass\n
    "},{"location":"reference/gerd/transport/#gerd.transport.Transport.set_gen_prompt","title":"set_gen_prompt","text":"
    set_gen_prompt(config: PromptConfig) -> PromptConfig\n

    Sets the prompt configuration for the generation service.

    The prompt configuration that is returned should in most cases be the same as the one that was set. Parameters: config: The prompt configuration to set

    Returns:

    Type Description PromptConfig

    The prompt configuration that was set

    Source code in gerd/transport.py
    def set_gen_prompt(self, config: PromptConfig) -> PromptConfig:\n    \"\"\"Sets the prompt configuration for the generation service.\n\n    The prompt configuration that is returned should in most cases\n    be the same as the one that was set.\n    Parameters:\n        config: The prompt configuration to set\n\n    Returns:\n        The prompt configuration that was set\n    \"\"\"\n    pass\n
    "},{"location":"reference/gerd/transport/#gerd.transport.Transport.set_qa_prompt","title":"set_qa_prompt","text":"
    set_qa_prompt(config: PromptConfig, qa_mode: QAModesEnum) -> QAAnswer\n

    Sets the prompt configuration for the QA service.

    Since the QA service uses multiple prompt configurations, the mode should be specified. For more details, see the documentation of QAService.set_prompt_config.

    Parameters:

    Name Type Description Default PromptConfig

    The prompt configuration to set

    required QAModesEnum

    The mode to set the prompt configuration for

    required

    Returns:

    Type Description QAAnswer

    The answer from the QA service

    Source code in gerd/transport.py
    def set_qa_prompt(self, config: PromptConfig, qa_mode: QAModesEnum) -> QAAnswer:\n    \"\"\"Sets the prompt configuration for the QA service.\n\n    Since the QA service uses multiple prompt configurations,\n    the mode should be specified. For more details, see the documentation\n    of [`QAService.set_prompt_config`][gerd.qa.QAService.set_prompt_config].\n\n    Parameters:\n        config: The prompt configuration to set\n        qa_mode: The mode to set the prompt configuration for\n\n    Returns:\n        The answer from the QA service\n    \"\"\"\n    pass\n
    "},{"location":"reference/gerd/transport/#gerd.transport.Transport.set_qa_prompt(config)","title":"config","text":""},{"location":"reference/gerd/transport/#gerd.transport.Transport.set_qa_prompt(qa_mode)","title":"qa_mode","text":""}]} \ No newline at end of file