TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/sdarionicolas-boop/AgroIA-RAG/llms.txt
Use this file to discover all available pages before exploring further.
src/rag/core.py module is the query layer of AgroIA. It connects to PostgreSQL (via psycopg2), generates embeddings with the nomic-embed-text model through Ollama, and produces natural-language answers with gemma3:4b. Five public functions and the BASE_PROMPT constant are exported; import them directly rather than duplicating the logic elsewhere in the codebase.
Both the embedding model (
nomic-embed-text) and the generation model (gemma3:4b) must be running in Ollama before calling any function in this module. Verify with ollama list.BASE_PROMPT
The system prompt passed to the LLM on every call toconsultar_agente. Import it to maintain consistency when building custom wrappers.
consultar_agente
Full RAG pipeline: retrieves context from the database and generates a natural-language response from the LLM.Parameters
The unique lot identifier as stored in the
informes_lotes table. Use listar_lotes() to enumerate all available IDs.The question to answer, in free-form text. The question is embedded and used for vector similarity search against
informes_lotes.Controls context depth. The consolidated report always occupies one slot; the remaining
top_k - 1 slots are filled with the most recent entries from lote_historial. Increase this value to give the LLM more historical campaigns.Return value
The LLM-generated answer as a plain string. If the lot is not found in the database, returns a warning string prefixed with
⚠️. On internal errors, returns a string prefixed with ❌.fetch_context
Returns the raw context string that would be passed to the LLM, without performing any generation. Use this to inspect retrieval quality or to build custom prompts.Parameters
Lot identifier to scope the retrieval. Only rows matching this
lote_id are considered.Query used to generate the embedding for vector similarity search against
informes_lotes.Total number of context fragments. One fragment is always the consolidated report;
top_k - 1 fragments come from lote_historial, ordered by year descending.Return value
A multi-paragraph string ready to embed in a prompt. Each fragment is separated by a blank line. If the lot does not exist, returns the string
"No hay datos registrados para el lote '<lote_id>'".listar_lotes
Returns all lot identifiers registered ininformes_lotes, sorted alphabetically.
Return value
A list of unique
lote_id strings from informes_lotes, ordered by lote_id ascending. Returns an empty list if the table has no rows.get_historial_lote_raw
Returns the full temporal series fromlote_historial as a list of dicts, ordered by year ascending.
Parameters
Lot identifier. Returns an empty list if no rows exist for this ID.
Return value
Alist[dict] where each dict represents one campaign year. All numeric fields default to 0 or 0.0 if the database value is NULL.
Campaign year (e.g.
2024).Crop key for this campaign. Defaults to
"N/D" if null.NDVI value at the critical month used to compute the score.
Accumulated heat-stress hours from NASA POWER for this year.
Overall AgroIA Score (0–100, integer) for this campaign.
Vigor component (0–40).
Stability component (0–30).
Cleanliness component (0–20).
Climate component (0–10).
True when this year’s NDVI passed validation and was included in score calculation. Defaults to True if null.True when spatial zoning (A/B/C) was applied for this campaign.Number of K-Means centroids classified as Zone C (low productivity) in this campaign.
get_datos_lote_raw
Returns the consolidated lot record frominformes_lotes as a dict, or None if the lot does not exist.
Parameters
Lot identifier to look up in
informes_lotes.Return value
None when the lot is not found. Otherwise a dict with the following keys:
Primary key of the row in
informes_lotes.Analysis date as stored in the database.
NDVI value stored in
ndvi_promedio. Defaults to 0 if null.Heat-stress hours stored in
gdd_acumulados. Defaults to 0 if null.Overall Score (0–100). Falls back to
metadata.score_total when the column is null.Spatial coefficient of variation. Falls back to
metadata.cv_espacial when null.Whether zone segmentation was active. Falls back to
metadata.zonificacion_activa.Number of Zone C points. Falls back to
metadata.puntos_zona_c.Crop key. Falls back to
metadata.cultivo, then "N/D".Field area in hectares. Falls back to
metadata.superficie_ha.The full
contenido_tecnico text from the database, as generated by construir_payload_v2.Breakdown of the score components from
metadata.score_desglose.The full
metadata JSONB object parsed as a Python dict.Usage example
Related pages
RAG engine concepts
Architecture of the retrieval-augmented generation system.
Score AgroIA concepts
Explanation of the four score components and how they are calculated.