Natural Language β Matrix DSL β Verifiable LaTeX
CSE291P course project Β· LLM + DSL for matrix operations
Live demo:
http://47.76.240.140:32000/
TexLM is an endβtoβend system that converts naturalβlanguage matrix instructions into:
- A grammarβconstrained matrix DSL
- A NumPyβevaluated numerical result
- A verifiable LaTeX snippet using a constrained LaTeX core pattern
The pipeline is fully modular and supports:
- LLMβbased decomposition β DSL generation
- LLM verifier ensuring DSL matches original user intent
- AST execution with NumPy
- Constraintβguided LaTeX rendering
- Streamlit web UI with feedback mailer
- Docker + Kubernetes deployment
Originally developed as a course project for UCSD CSE291P.
The system parses the userβs natural language request into:
dsl: core matrix operation expressionformatting: requested output stylereasoning: chainβofβthought describing how DSL was produced
Implementations:
dsl/generator.py, prompts in config/prompts.py
Ensures:
- DSL matches the original user request, not a paraphrase
- No hallucinated operations
- Dimensions or matrix counts not invented
Returns:
{
"is_valid": true/false,
"explanation": "why"
}File: dsl/verify.py
Supported operations:
add(A, B)multiply(A, B)transpose(A)inverse(A)- Nested compositions (arbitrary depth)
Execution flow:
- Parse DSL β AST
- Validate matrix shapes
- Run via NumPy
- Return result or error
File: dsl/evaluate.py
The numerical matrix is converted into a LaTeX "core pattern", defining:
- Exact brackets
- Exact row/column structure
- Allowed float formatting patterns
File: constraints/generate_constraint.py
The LaTeX core is used as a hard constraint.
LLM must produce a LaTeX snippet consistent with the core.
File: renderers/latex.py
Features:
- Live chat
- Expandable reasoning trace
- Pretty LaTeX output
- Feedback button that emails full chat log
File: app.py
utils/mailer.py sends feedback emails with:
- User message
- DSL
- Trace
- LaTeX
- Full UI session transcript (attachment)
- Dockerfile builds Streamlit app container
- Kubernetes manifest (
k8s/deployment.yaml) used for cloud hosting - Supports environment variables + Kubernetes secrets
TexLM/
βββ app.py # Streamlit web UI
βββ main.py # Core end-to-end pipeline
βββ config/
β βββ config.py # API key/env loader
β βββ prompts.py # All LLM prompts
βββ constraints/
β βββ generate_constraint.py
βββ dsl/
β βββ grammar.py # DSL grammar definition
β βββ generator.py # NL β DSL
β βββ verify.py # DSL verifier
β βββ evaluate.py # AST execution
βββ renderers/
β βββ decompose.py
β βββ latex.py # LaTeX rendering with constraints
βββ utils/
β βββ mailer.py # Email sender
β βββ ...
βββ k8s/
β βββ deployment.yaml
βββ Dockerfile
βββ requirements.txt
βββ README.md # This file
git clone https://github.com/Ydz0616/TexLM
cd TexLM
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txtOPENAI_API_KEY=your-key
OPENAI_BASE_URL=https://your-relay/v1
EMAIL_USER=...
EMAIL_PASS=...
streamlit run app.py
Visit:
http://localhost:8501/
from main import run_demo
msg = "give me a latex table of inverse of transpose of matrix ([1,2],[3,4])"
res = run_demo(msg)
print(res["dsl"])
print(res["final_latex"])docker build -t texlm .
docker run -p 8501:8501 \
-e OPENAI_API_KEY=... \
texlm
k8s/deployment.yaml includes:
- deployment
- env var injection
- image
yz743/texlm:v0.2.1
Apply:
kubectl apply -f k8s/deployment.yaml
- Update
dsl/grammar.py - Implement logic in
dsl/evaluate.py - Update prompts
- Add custom table styles
- pmatrix / bmatrix switching
- Align environments
- Enforce stricter dimension checks
- Validate matrix counts / shapes
- Natural language is ambiguous β system may need rephrasing
- DSL grammar currently supports a limited set of operations
- Numerical precision may affect LaTeX formatting
- Requires an LLM backend for DSL + rendering
This project is released under the MIT License.
Copyright Β© 2025 Yuandong Zhang and Matthew Thomas.
If you use TexLM or build upon its ideas in coursework, research, or publications,
please cite the project:
Y. Zhang, M. Thomas. TexLM: Synthesizing Reliable Latex Matrix from Natural Language Input, 2025.
https://github.com/Ydz0616/TexLM
