DEV Community

shashank ms
shashank ms

Posted on

Using LLMs for Physics Research and Education

I built a command-line derivation auditor to catch algebraic and conceptual errors in physics proofs before they reach a journal reviewer or a teaching assistant. It takes a raw .txt file, sends it to an LLM with a strict physics-review persona, and returns structured JSON feedback. Wiring it to Oxlo.ai keeps costs flat per request, which matters when you are pasting in multi-page LaTeX derivations.

What you'll need

Step 1: Scaffold the client

First I instantiate the OpenAI-compatible client pointing at Oxlo.ai and verify the endpoint with a quick health-check message.

from openai import OpenAI

client = OpenAI(base_url="https://api.oxlo.ai/v1", api_key="YOUR_OXLO_API_KEY")

response = client.chat.completions.create(
    model="llama-3.3-70b",
    messages=[
        {"role": "user", "content": "Say 'Physics engine ready' and nothing else."},
    ],
)
print(response.choices[0].message.content)

Step 2: The system prompt

The reviewer personality lives in the system prompt. It enforces JSON output and instructs the model to quote exact problematic lines rather than vague summaries.

SYSTEM_PROMPT = """You are a senior physics derivation reviewer. Audit the user's derivation for algebraic, conceptual, and dimensional errors.

Rules:
1. Quote the exact line that contains an error.
2. Explain why it is wrong using physical reasoning.
3. Provide a corrected replacement line.
4. Flag missing assumptions, unjustified approximations, or inconsistent units.
5. Respond in valid JSON with exactly these keys: summary (string), errors (list of objects with keys line, issue, correction), recommendations (list of strings).
6. If the derivation is fully correct, set errors to an empty list and briefly confirm correctness.

Be rigorous but concise."""

Step 3: Build the auditor function

We wrap the API call in a function that accepts a raw derivation string, injects the system prompt, and parses the JSON response.

import json

def audit_derivation(derivation_text: str) -> dict:
    response = client.chat.completions.create(
        model="llama-3.3-70b",
        messages=[
            {"role": "system", "content": SYSTEM_PROMPT},
            {"role": "user", "content": derivation_text},
        ],
        response_format={"type": "json_object"},
    )
    raw = response.choices[0].message.content
    return json.loads(raw)

Step 4: Add a CLI wrapper

Finally we add a small argparse interface so we can pipe derivation files directly into the tool from the terminal.

import argparse

def main():
    parser = argparse.ArgumentParser(description="Audit a physics derivation via Oxlo.ai")
    parser.add_argument("file", help="Path to a .txt file containing the derivation")
    args = parser.parse_args()

    with open(args.file, "r", encoding="utf-8") as f:
        derivation = f.read()

    result = audit_derivation(derivation)
    print(json.dumps(result, indent=2))

if __name__ == "__main__":
    main()

Run it

Save the following flawed derivation to sho_error.txt. It contains a classic sign mistake in the second derivative of a sine ansatz for the simple harmonic oscillator.

Problem: Find the angular frequency omega for a mass-spring system with mass m and spring constant k.

Derivation:
From Newton's second law, F = m * x'' = -k * x.
Dividing by m gives x'' = -(k/m) * x.

We propose the ansatz x = A * sin(omega * t).
First derivative: x' = A * omega * cos(omega * t).
Second derivative: x'' = A * omega^2 * sin(omega * t).

Substitute x'' back into the equation:
A * omega^2 * sin(omega * t) = -(k/m) * A * sin(omega * t).

Cancel A and sin(omega * t):
omega^2 = -k/m.

Therefore omega = i * sqrt(k/m) and the period is T = 2*pi*i*sqrt(m/k).

Execute the script:

python auditor.py sho_error.txt

You should see output similar to this:

{
  "summary": "The derivation contains a sign error in the second derivative of the sinusoidal ansatz, leading to an unphysical imaginary angular frequency.",
  "errors": [
    {
      "line": "Second derivative: x'' = A * omega^2 * sin(omega * t).",
      "issue": "The second derivative of sin(omega*t) is -omega^2 * sin(omega*t), not +omega^2 * sin(omega*t). The chain rule introduces a negative sign.",
      "correction": "Second derivative: x'' = -A * omega^2 * sin(omega * t)."
    },
    {
      "line": "Therefore omega = i * sqrt(k/m) and the period is T = 2*pi*i*sqrt(m/k).",
      "issue": "Because omega^2 was incorrectly derived as negative, the solver produces an imaginary frequency. With the corrected sign, omega is real and positive.",
      "correction": "Therefore omega = sqrt(k/m) and the period is T = 2*pi*sqrt(m/k)."
    }
  ],
  "recommendations": [
    "Double-check trigonometric derivatives when proposing ansatzes.",
    "Verify that physical quantities like frequency remain real in undamped oscillator problems."
  ]
}

Wrap-up

You now have a working derivation auditor backed by Oxlo.ai. Because Oxlo.ai charges per request rather than per token, running this against a ten-page LaTeX proof costs the same as a one-line sanity check. Two concrete next steps: integrate the auditor into a Git pre-commit hook to review committed .tex files automatically, or extend the system prompt to cross-check symbolic steps with a Python SymPy validator for guaranteed algebraic consistency.

Top comments (0)