aita_core.rag — RAG Pipeline

The RAG (Retrieval-Augmented Generation) pipeline handles the complete flow from user query to LLM response.

Pipeline Functions

aita_core.rag.chat(user_query, chat_history=None, current_week=15)[source]

Full RAG pipeline: retrieve context, build prompt, generate response. Returns (assistant_message, sources).

aita_core.rag.retrieve(query, k=None, current_week=15)[source]

Retrieve top-k relevant chunks, filtered to only topics covered by current_week.

aita_core.rag.build_messages(chat_history, user_query, context_chunks, current_week)[source]

Build the message list for the OpenAI chat completion.

aita_core.rag.build_system_prompt(current_week, has_context=True)[source]

Build system prompt with week-awareness and exam scope.

Homework Injection

aita_core.rag._inject_current_hw(query, context_chunks, current_week)[source]

If the query mentions homework, ensure the current HW is in retrieved chunks.

When a student asks about “homework”, “hw”, or “assignment”, the system checks if the current week’s homework is already in the retrieved chunks. If not, it scans the full chunk index and injects the first matching chunk at the top of the context. This ensures the LLM always has the relevant homework content when answering homework-related questions.

Week-Aware System Prompt

The system prompt is dynamically built based on the current week:

aita_core.rag.FUTURE_TOPIC_INSTRUCTION = '\nIMPORTANT WEEK-AWARE INSTRUCTION:\nThe class is currently in Week {current_week}.\n{current_hw_line}\nTopics covered so far: {covered_topics}\n\nTopics NOT yet covered: {future_topics}\n\nSTRICT RULE FUTURE TOPICS:\nIf the student asks about a topic that has NOT been covered yet, you MUST:\n1. Say: "That\'s a great question! We\'ll cover that topic later in the course."\n2. Give AT MOST a one-sentence definition no formulas, no numbers, no calculations.\n3. Do NOT provide specific values (like rates, speeds, constants) for future topics.\n4. Redirect: "For now, let\'s focus on the current material. Is there anything about [current topic] I can help with?"\nThis is an absolute rule. Even if you know the answer, do NOT provide detailed information about future topics. Giving incorrect or premature information is worse than redirecting the student.\n\nIf the student asks about "this week\'s homework" or "the current homework", refer to {current_hw_ref}.\nIf the student says "problem 1" or "problem 2" etc. without specifying which homework, assume they mean {current_hw_ref} and use the retrieved context from that homework.\n'

str(object=’’) -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.__str__() (if defined) or repr(object). encoding defaults to ‘utf-8’. errors defaults to ‘strict’.

The prompt includes:

  • Current week number

  • Current homework assignment name

  • List of topics covered so far

  • List of topics not yet covered

  • Instructions to redirect questions about future topics

Internal Functions

aita_core.rag._get_client()[source]
aita_core.rag._load_index()[source]