A small FastAPI service that turns structured property data (JSON) into SEO-optimized HTML listings in English, Portuguese or Spanish. It is an educational example of LLM generation, automatic evaluation with retries, tracing and prompt management.
Docs: https://fsystemweb.github.io/real-state-web-generator/ (source in docs/, run with docsify serve docs)
- Generates structured HTML: title, meta description, description, key features, neighborhood and call to action
- Multilingual:
en,pt,es - An LLM judge scores each result; the listing is regenerated up to 3 times while the score is below 3/5
- Optional tracing and prompt management with Comet Opik
- Evaluation datasets and fact-based metrics (see docs/evaluation.md)
flowchart LR
A[POST /generate-listing] --> B[Validate request]
B --> C[Generator: LLM stronger]
C --> D[Evaluator: LLM light]
D --> E{Score >= 3?}
E -- yes --> F[HTML response]
E -- "no, attempts left" --> C
E -- "no, 3 attempts used" --> G[Error response]
P[(Prompts: Opik or local files)] -.-> C
P -.-> D
C -.-> T[(Opik traces)]
D -.-> T
The generator uses the stronger model and the evaluator the light one. The defaults are in app/chains.py; override them with GENERATOR_LLM and EVALUATOR_LLM.
python3.12 -m venv venv
source venv/bin/activate # Windows: .\venv\Scripts\Activate.ps1
pip install -r requirements.txt
cp .env.example .env # then set OPENAI_API_KEY
python -m app.main # API on http://localhost:8000Optional demo page (in another terminal): python -m http.server 5500, then open http://localhost:5500/public/.
| Variable | Required | Description |
|---|---|---|
OPENAI_API_KEY |
yes | OpenAI API key |
GENERATOR_LLM |
no | Model that generates the listing (default gpt-6-luna) |
EVALUATOR_LLM |
no | Model that scores it (default gpt-5-nano) |
OPIK_URL_OVERRIDE |
no | Opik API URL, e.g. http://localhost:5173/api. Turns on tracing and Opik prompts |
OPIK_PROJECT_NAME |
no | Opik project for traces |
POST /generate-listing
{
"title": "T3 apartment in Lisbon",
"location": { "city": "Lisbon", "neighborhood": "Campo de Ourique" },
"features": {
"bedrooms": 3, "bathrooms": 2, "area_sqm": 120, "balcony": true,
"parking": false, "elevator": true, "floor": 2, "year_built": 2005
},
"price": 650000,
"listing_type": "sale",
"language": "en"
}Response:
{
"html": "<title>...</title>\n<meta ... > ...",
"evaluation": {
"structure_compliance": 5,
"language_fluency_seo": 4,
"multilingual_adaptability": 4,
"total_score": 4,
"missing_or_invalid_tags": []
},
"retries": 0,
"failed_criteria_log": []
}Only title, location.city, listing_type and language are required. If something fails, the API returns {"error": "..."}.
app/
main.py FastAPI app and the route
chains.py generator, evaluator and retry loop
prompt_loader.py prompts from Opik, local files as fallback
models.py request models
prompts/ prompt templates (examples and fallback)
evaluation/ datasets, metrics and the evaluation run
docs/ documentation site (Docsify)
public/index.html demo page
Everything here is off unless OPIK_URL_OVERRIDE is set.
- Start Opik locally (needs Docker):
bash scripts/opik.sh. The first run pulls several images. UI athttp://localhost:5173. Other commands:bash scripts/opik.sh down(stop, keep data),status,logs,reset(stop and delete data). For educational use only; for anything else follow the official guide. - Set
OPIK_URL_OVERRIDEin.envand restart the API. Each request shows up athttp://localhost:5173as one trace; each attempt is taggedattempt-N. - Prompts:
app/prompts/*.txtare examples and the fallback. On startup the API pushes them to Opik asreal-estate-generatorandreal-estate-evaluatorif they are missing (existing ones are never overwritten;python -m app.prompt_loaderpushes a new version by hand). The evaluation datasets are created the same way asreal-estate-en,-ptand-es. If the prompts exist in Opik, they are used instead (latest version, read at startup; the log says which source was used). Variables use Mustache with triple braces, e.g.{{{property_json}}}, so HTML is not escaped.
Datasets in evaluation/datasets/ are run through the generator and scored with fact precision and fact recall in an Opik experiment: python -m evaluation.run (paid API calls). Details in docs/evaluation.md.
MIT License – free to use and modify.