DEV Community

shashank ms
shashank ms

Posted on

Using LLM for Question Answering in Educational Applications

We are going to build a reading-comprehension tutor that takes a source text and a student question, then returns a guided, cited answer. It is useful for teachers who want to let students interrogate course material without leaving the classroom app, and for learners who need step-by-step explanations tied to specific sources.

What you'll need

Step 1: Initialize the Oxlo.ai client

Create a file named tutor.py. Import the SDK and point it at Oxlo.ai. I use llama-3.3-70b because it handles long context windows reliably and follows system instructions well, which matters when we inject full reading passages.

from openai import OpenAI
import os

client = OpenAI(
    base_url="https://api.oxlo.ai/v1",
    api_key=os.environ.get("OXLO_API_KEY")
)

MODEL = "llama-3.3-70b"

Step 2: Write the educational system prompt

Store the prompt in a constant. It forces the model to cite sentences from the provided context, explain reasoning in plain language, and refuse to guess when the answer is not present.

SYSTEM_PROMPT = """You are a reading-comprehension tutor. Your rules:
1. Use only the provided SOURCE TEXT to answer the QUESTION.
2. Cite the specific sentence or clause that supports your answer.
3. If the SOURCE TEXT does not contain the answer, say that the passage does not contain enough information.
4. Explain your reasoning in one short paragraph before giving the final answer.
5. Do not use outside knowledge."""

Step 3: Build the context-aware answer function

This function wraps the source text and the student question into a single user message. Because Oxlo.ai uses flat per-request pricing, injecting a long passage does not inflate the cost the way token-based billing would. That makes this pattern cheap to run even with full textbook chapters.

import json

def answer_question(source_text: str, question: str) -> dict:
    user_content = f"""SOURCE TEXT:
\"\"\"{source_text}\"\"\"

QUESTION:
{question}

Respond with valid JSON containing:
- reasoning: a short explanation
- citation: the exact quoted sentence from SOURCE TEXT that supports the answer
- answer: the final answer
- confidence: either "high" or "missing""""
    
    response = client.chat.completions.create(
        model=MODEL,
        messages=[
            {"role": "system", "content": SYSTEM_PROMPT},
            {"role": "user", "content": user_content},
        ],
        temperature=0.2,
        max_tokens=512,
        response_format={"type": "json_object"},
    )
    return json.loads(response.choices[0].message.content)

Step 4: Add guardrails for missing information

Before showing the answer to the student, verify that the cited string actually appears in the source text. If it does not, or if confidence is "missing", we return a safe fallback message instead of a hallucination.

def safe_answer(source_text: str, question: str) -> str:
    result = answer_question(source_text, question)
    
    citation = result.get("citation", "")
    confidence = result.get("confidence", "")
    
    if confidence == "missing" or citation.strip() not in source_text:
        return "The passage does not contain enough information to answer that."
    
    return f"Reasoning: {result['reasoning']}\n\nCitation: \"{citation}\"\n\nAnswer: {result['answer']}"

Step 5: Create the interactive test loop

Add a short passage about photosynthesis and a few questions so you can run the script immediately.

if __name__ == "__main__":
    passage = (
        "Photosynthesis is the process by which green plants and some other organisms use sunlight to synthesize "
        "foods with the help of chlorophyll pigments. During photosynthesis in green plants, light energy is captured "
        "and used to convert water, carbon dioxide, and minerals into oxygen and energy-rich organic compounds. "
        "The process occurs mainly in the leaves of plants."
    )
    
    questions = [
        "What pigments help capture light energy during photosynthesis?",
        "Where does photosynthesis mainly occur in plants?",
        "Who discovered the structure of DNA?",
    ]
    
    for q in questions:
        print(f"Q: {q}")
        print(safe_answer(passage, q))
        print("-" * 40)

Run it

Export your key and execute the script:

export OXLO_API_KEY="sk-..."
python tutor.py

You should see output similar to this:

Q: What pigments help capture light energy during photosynthesis?
Reasoning: The passage states that chlorophyll pigments help capture light energy.

Citation: "foods with the help of chlorophyll pigments"

Answer: Chlorophyll pigments.

----------------------------------------
Q: Where does photosynthesis mainly occur in plants?
Reasoning: The passage explicitly states where photosynthesis mainly occurs.

Citation: "The process occurs mainly in the leaves of plants."

Answer: Mainly in the leaves of plants.

----------------------------------------
Q: Who discovered the structure of DNA?
The passage does not contain enough information to answer that.
----------------------------------------

Next steps

Swap in kimi-k2.6 or deepseek-v3.2 from Oxlo.ai if you need stronger reasoning for advanced STEM material. To scale this beyond a single passage, pipe in retrieved chunks from a vector database and run each chunk through safe_answer in parallel. Oxlo.ai's per-request pricing keeps the cost predictable even when you batch many long-context calls.

Top comments (0)