Ask about the HPC compendium#

In this RAG example, we will use the HPC compendium which is licensed under CC BY 4.0 by TU Dresden ZIH.

Hint: Execute this notebook from the terminal using voila chat-with-hpc-compendium.ipynb.

from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from llama_index.core import Settings
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
from llama_index.llms.openai_like import OpenAILike
from llama_index.core import (
    VectorStoreIndex,
    SimpleDirectoryReader,
    StorageContext,
    load_index_from_storage,
)
import ipywidgets as widgets
from IPython.display import display
import markdown
import numpy as np
import os
from dotenv import load_dotenv
load_dotenv()
True
Settings.embed_model = HuggingFaceEmbedding(model_name="intfloat/multilingual-e5-large-instruct")
Settings.llm = OpenAILike(model="meta-llama/Llama-3.3-70B-Instruct", 
                          streaming=False,
                          request_timeout=120.0, 
                          context_window=128000,
                          max_tokens=1024,
                          api_base="https://llm.scads.ai/v1", 
                          api_key=os.environ.get('SCADSAI_API_KEY'))
class Chat:
    def __init__(self, persist_base_dir="./vector_stores"):
        self.former_folder = None
        self.persist_base_dir = persist_base_dir

    def _persist_dir(self, folder):
        safe_name = "data"
        return os.path.join(self.persist_base_dir, safe_name)

    def load(self, folder):
        if self.former_folder == folder:
            return self.count_documents()

        self.former_folder = folder
        persist_dir = self._persist_dir(folder)

        self._documents = SimpleDirectoryReader(
            folder,
            required_exts=[".md"],
            recursive=True,
        ).load_data()

        if os.path.exists(persist_dir):
            storage_context = StorageContext.from_defaults(persist_dir=persist_dir)
            index = load_index_from_storage(storage_context)
        else:
            index = VectorStoreIndex.from_documents(self._documents)
            index.storage_context.persist(persist_dir=persist_dir)

        self._query_engine = index.as_query_engine()
        return self.count_documents()

    def count_documents(self):
        return len(np.unique([d.metadata["file_name"] for d in self._documents]))

    def query(self, question):
        return self._query_engine.query(question)
# Create user interface
docs_input = widgets.Text(value="hpc-compendium/doc.zih.tu-dresden.de/docs/", placeholder="Enter a directory here")
load_button = widgets.Button(description="Load")

output_label = widgets.HTML(value="")

text_input = widgets.Text(placeholder="Enter a question here")
submit_button = widgets.Button(description="Submit")

chat = Chat()

def on_load(e=None):    
    number_of_documents = chat.load(docs_input.value)
    output_label.value = f"""
    <div style='text-align:left; color: darkgrey; font-size: 20px'>{number_of_documents} documents loaded.</div>
    """
    if number_of_documents > 0:
        submit_button.disabled = False

def on_submit(e):
    question = text_input.value
    if len(question) == 0:
        return
    text_input.value = ""

    # submit prompt to LLM
    answer = chat.query(question)

    # Append question and answer to the existing HTML content
    output_label.value += f"""
    <div style='text-align:right; color: blue; font-size: 20px'>{question}</div>
    <div style='text-align:left; color: darkgreen; font-size: 20px'>{answer}</div>
    """

# Attach the event handler to the text field and the button
docs_input.continuous_update = False
docs_input.observe(on_load)
load_button.on_click(on_load)

text_input.continuous_update = False
text_input.observe(on_submit)
submit_button.on_click(on_submit)
submit_button.disabled = True

on_load()

# Arrange the widgets for display
display(widgets.HBox([docs_input, load_button]), output_label, widgets.HBox([text_input, submit_button]))

Exercise#

Ask HPC-specific questions such as “How can one access the JupyterHub at ZIH TU Dresden?”.