Inyección de glosarios y ontologías de dominio
Incorpore terminología y conocimientos específicos del dominio a los prompts del sistema.
Inyección de glosarios y ontologías de dominio es una lección gratuita de AI Prompt Engineering en CoddyKit. Esta es la lección 4 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de AI Prompt Engineering, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de AI Prompt Engineering incluye 4 lecciones en total.
El problema de la desambiguación
El lenguaje especializado está lleno de ambigüedades. «Rendimiento» significa rendimiento de un bono en finanzas y rendimiento de un cultivo en agricultura. «Resolución» significa resolución de pantalla en UI y resolución de incidencias en soporte. Sin contexto del dominio, los modelos recurren por defecto al significado más común del lenguaje general, que es incorrecto en dominios especializados.
Patrón de inyección de glosarios
Introduzca un glosario del dominio directamente en el prompt del sistema. Esto sustituye el vocabulario predeterminado del modelo y garantiza que los términos específicos del dominio se interpreten correctamente durante toda la sesión.
FINANCE_GLOSSARY = '''
DOMAIN GLOSSARY (these definitions override general language meaning):
- yield: bond yield (annual return as percentage of bond price), NOT crop or harvest
- duration: interest rate sensitivity measure (modified duration), NOT time length
- spread: yield spread between two bonds, NOT physical spreading
- convexity: second-order price sensitivity to interest rate changes, NOT geometry
- tenor: remaining time to maturity of a financial instrument, NOT musical pitch
- floor: minimum interest rate in a rate agreement, NOT building floor
- cap: maximum interest rate, NOT a hat or market capitalization
- swap: exchange of cash flows between counterparties, NOT physical exchange
- basis: difference between spot and futures price, NOT foundation
'''
FINANCE_SYSTEM_PROMPT = (
'You are a fixed income analyst.\n\n'
+ FINANCE_GLOSSARY +
'\nAlways use these domain definitions when answering questions.'
)
import anthropic
client = anthropic.Anthropic(api_key='YOUR_API_KEY')
response = client.messages.create(
model='claude-opus-4-5', max_tokens=500,
system=FINANCE_SYSTEM_PROMPT,
messages=[{'role': 'user', 'content': 'What is the yield of a 10-year bond?'}]
)
print(response.content[0].text)Creación de un archivo de glosario del dominio
Almacene los glosarios como archivos YAML estructurados para poder versionarlos, compartirlos entre prompts y actualizarlos sin modificar el código de los prompts.
# glossaries/fixed_income.yaml
glossary:
yield:
domain_meaning: Annual return on a bond as a percentage of its current market price
general_meaning: Crop or harvest output
use_domain: true
examples:
- 'The 10-year Treasury yield rose to 4.5%'
- 'Current yield = annual coupon / market price'
duration:
domain_meaning: |
Measure of a bond's price sensitivity to interest rate changes.
Modified duration = -dP/P / dr
general_meaning: Length of time
use_domain: true
basis:
domain_meaning: Difference between spot price and futures price of the same instrument
general_meaning: Foundation or base
use_domain: true
# glossaries/load.py
import yaml
def load_glossary(domain):
with open(f'glossaries/{domain}.yaml') as f:
data = yaml.safe_load(f)
lines = ['DOMAIN GLOSSARY:']
for term, info in data['glossary'].items():
lines.append(f'- {term}: {info["domain_meaning"].strip()}')
return '\n'.join(lines)Inyección de ontologías para dominios complejos
Una ontología va más allá de un glosario: define relaciones entre conceptos, como jerarquías, restricciones y reglas. Inyectar una ontología ayuda al modelo a comprender qué conceptos pertenecen a cada categoría y cómo se relacionan.
MEDICAL_ONTOLOGY_SNIPPET = '''
CLINICAL ONTOLOGY (use these relationships in all analysis):
Diagnosis Hierarchy:
- Condition > Category > Specific Diagnosis
- "Hypertension" is a specific diagnosis under "Cardiovascular Conditions"
- "Type 2 Diabetes" is under "Endocrine / Metabolic Conditions"
Medication Classes:
- ACE inhibitors (e.g., lisinopril) -> used for: hypertension, heart failure, CKD
- Beta-blockers (e.g., metoprolol) -> used for: hypertension, angina, heart failure
- Statins (e.g., atorvastatin) -> used for: hyperlipidemia, cardiovascular risk
Measurement Rules:
- "BP" means Blood Pressure, format: systolic/diastolic (e.g., 130/85 mmHg)
- "A1c" means glycated hemoglobin; > 6.5% is diagnostic for Type 2 Diabetes
- "eGFR" means estimated Glomerular Filtration Rate; < 60 mL/min/1.73m2 = CKD
Always use ICD-10 categories when classifying diagnoses.
'''
print(MEDICAL_ONTOLOGY_SNIPPET[:300])Generación dinámica de glosarios
Para bases de conocimiento grandes, genere dinámicamente un glosario enfocado: extraiga de un glosario maestro solo los términos más relevantes para la tarea actual y mantenga reducida la ventana de contexto.
import json
# master_glossary.json — full domain glossary
MASTER_GLOSSARY = {
'yield': 'Bond yield: annual return as percentage of current market price',
'duration': 'Modified duration: bond price sensitivity to rate changes',
'convexity': 'Second-order rate sensitivity measure',
'swap': 'Exchange of fixed and floating cash flows',
'option': 'Contract giving right (not obligation) to buy/sell an asset',
'beta': 'Stock volatility relative to market index',
'alpha': 'Excess return over benchmark after adjusting for risk',
# ... hundreds more
}
def focused_glossary(user_query, master_glossary, max_terms=10):
'''Select glossary terms most relevant to the user query.'''
query_lower = user_query.lower()
relevant = {}
for term, definition in master_glossary.items():
if term.lower() in query_lower or any(
word in query_lower for word in definition.lower().split()[:5]
):
relevant[term] = definition
if len(relevant) >= max_terms:
break
lines = ['RELEVANT DOMAIN TERMS:']
for t, d in relevant.items():
lines.append(f'- {t}: {d}')
return '\n'.join(lines)
query = 'What is the duration and convexity of this bond portfolio?'
print(focused_glossary(query, MASTER_GLOSSARY))Desambiguación entre varios dominios
Algunas consultas abarcan varios dominios. Inyecte el contexto de todos los dominios relevantes e indique al modelo que desambigüe basándose en el contexto de la conversación.
MULTI_DOMAIN_SYSTEM = '''
This system serves both agricultural and financial users.
The domain is determined by context cues in the user message.
Domain disambiguation rules:
- If the user mentions "crops", "harvest", "acres", "soil", "planting":
Use AGRICULTURAL definitions: yield = crop output, spread = physical spreading
- If the user mentions "bonds", "portfolio", "maturity", "coupon", "treasuries":
Use FINANCIAL definitions: yield = bond yield, spread = yield spread
- If the domain is ambiguous:
Ask the user to clarify: "Are you asking about agricultural or financial yields?"
AGRICULTURAL GLOSSARY:
- yield: crop output per unit area (e.g., bushels per acre)
- basis: difference between local cash price and futures price for a commodity
FINANCIAL GLOSSARY:
- yield: annual bond return as percentage of current price
- basis: yield spread between two financial instruments
'''
print('Multi-domain system prompt loaded.')
print('The model will ask for clarification when domain is ambiguous.')Salida restringida por ontología
La inyección de una ontología puede restringir la salida del modelo para que utilice únicamente categorías predefinidas, evitando una categorización libre que interrumpa el procesamiento posterior.
SUPPORT_ONTOLOGY_SYSTEM = '''
You are a support ticket classifier for a B2B SaaS company.
TICKET CATEGORY ONTOLOGY (use ONLY these exact category names):
Level 1 Categories:
- Billing > Sub-categories: Invoice Error, Subscription Change, Refund Request, Payment Failure
- Technical > Sub-categories: Bug Report, Performance Issue, Integration Error, Feature Not Working
- Account > Sub-categories: Access Request, User Management, Security Concern, Password Reset
- Feature Request > Sub-categories: New Feature, Enhancement, UI/UX Improvement
CLASSIFICATION RULES:
1. Always return exactly one Level 1 category and one Sub-category.
2. If ticket spans multiple categories, choose the PRIMARY issue.
3. If uncertain, use the category that would route to the most qualified team.
4. Return format: {"category": "Technical", "subcategory": "Bug Report", "confidence": "HIGH"}
Confidence: HIGH (clear), MEDIUM (likely), LOW (ambiguous)
'''
def classify_ticket(ticket_text):
import json
response = client.messages.create(
model='claude-opus-4-5', max_tokens=100,
system=SUPPORT_ONTOLOGY_SYSTEM,
messages=[{'role': 'user', 'content': f'Classify: {ticket_text}'}]
)
return json.loads(response.content[0].text)Inyección de ontologías jurídicas
Las ontologías del ámbito jurídico definen jerarquías de cláusulas contractuales, relaciones entre las partes y tipos de obligaciones. Inyectarlas garantiza una clasificación coherente en todas las tareas de análisis de contratos.
LEGAL_ONTOLOGY = '''
CONTRACT CLAUSE ONTOLOGY:
Obligation Types:
- SHALL: mandatory obligation (enforceable duty)
- MAY: permissive right (optional action)
- SHALL NOT: mandatory prohibition
- WILL: future intention (weaker than SHALL)
Clause Risk Hierarchy:
- CRITICAL: financial exposure > $1M or termination rights
- HIGH: material business impact, IP rights, indemnification
- MEDIUM: operational restrictions, notice requirements
- LOW: administrative provisions, definitions
Party References (standardize to these canonical forms):
- "the Company", "we", "us" -> VENDOR
- "Customer", "Client", "you" -> CUSTOMER
- "third party", "subcontractor" -> THIRD_PARTY
Always use these canonical party names in your analysis.
Do not use the actual company names — replace with canonical form.
'''
print('Legal ontology loaded. Party names will be canonicalized in all analysis.')Comprobador de coherencia terminológica
Después de recibir la salida del modelo, verifique que los términos del dominio se utilicen de forma coherente y no vuelvan a adoptar significados del lenguaje general. Una comprobación posterior al procesamiento detecta la deriva terminológica.
PROHIBITED_GENERAL_MEANINGS = {
# In fixed income context: these general meanings should not appear
'yield': ['harvest', 'crop', 'produce', 'give way', 'surrender'],
'duration': ['how long', 'length of time', 'period of time'],
'floor': ['ground floor', 'building floor', 'floor plan'],
'cap': ['hat', 'market cap', 'bottle cap'],
}
def check_terminology_consistency(text, domain_term):
text_lower = text.lower()
prohibited = PROHIBITED_GENERAL_MEANINGS.get(domain_term, [])
violations = []
for general_phrase in prohibited:
if general_phrase in text_lower:
# Find context window around the violation
idx = text_lower.index(general_phrase)
context = text[max(0, idx-50):idx+80]
violations.append({'phrase': general_phrase, 'context': context})
return violations
# Usage after LLM call
output = 'The yield of the bond is 4.5% per annum based on current market price.'
violations = check_terminology_consistency(output, 'yield')
if violations:
print('Terminology violation detected:', violations)
else:
print('Terminology consistency: PASS')Gestión de versiones de glosarios
Los glosarios del dominio deben versionarse junto con los prompts. Un cambio terminológico, como una nueva definición normativa o una norma clínica actualizada, requiere reevaluar todos los prompts que utilicen los términos afectados.
# Glossary versioning with impact tracking
GLOSSARY_VERSIONS = {
'1.0.0': {
'yield': 'Bond yield: annual coupon / face value (current yield)',
'duration': 'Macaulay duration'
},
'2.0.0': {
'yield': 'Bond yield: annual return as % of current market price (yield to maturity)',
'duration': 'Modified duration (more precise for risk management)',
'convexity': 'Second-order rate sensitivity (new in v2)' # new term
}
}
def get_affected_prompts(old_version, new_version, prompt_registry):
'''Find prompts that use terms changed between glossary versions.'''
old_terms = set(GLOSSARY_VERSIONS[old_version].keys())
new_terms = set(GLOSSARY_VERSIONS[new_version].keys())
changed_terms = old_terms ^ new_terms # symmetric difference
affected = []
for prompt_id, artifact in prompt_registry.items():
if any(term in artifact['template'] for term in changed_terms):
affected.append(prompt_id)
return affected
print('Prompts affected by glossary v1.0.0 -> v2.0.0 update:', ['rate-analysis-v1', 'bond-report'])Ontología jerárquica con relaciones padre-hijo
Las ontologías completas definen jerarquías de conceptos padre-hijo. Utilizar una jerarquía en el prompt permite al modelo razonar con el nivel adecuado de especificidad, sin ser demasiado general ni demasiado específico.
PRODUCT_ONTOLOGY = '''
PRODUCT CATEGORY ONTOLOGY (use for all product classification tasks):
Electronics
Computing
Laptops
Gaming Laptops
Ultrabooks
Workstations
Desktops
Tablets
Consumer Electronics
Smartphones
Smart Speakers
Wearables
Smartwatches
Fitness Trackers
CLASSIFICATION RULES:
1. Always classify to the most specific level where evidence exists.
2. If a product matches multiple branches, use the primary use case.
3. Use exact taxonomy names from above — do not invent new categories.
4. If a product does not fit, use the nearest parent category and
add "[NON-STANDARD: <reason>]" after the category name.
'''
print('Product ontology ready. 4-level hierarchy loaded.')Comprobación rápida
Se ha implementado un modelo para analizar carteras de bonos. Sin una inyección de glosario, el modelo interpreta «¿Cuál es el rendimiento de este instrumento?» describiendo la producción agrícola. ¿Cuál es la causa raíz y cuál es la solución?
Resumen de la inyección de glosarios y ontologías
La inyección de glosarios y ontologías del dominio resuelve la ambigüedad terminológica en el nivel del sistema:
- Inyección de glosarios: defina los significados específicos del dominio en el prompt del sistema para los términos ambiguos
- Inyección de ontologías: proporcione jerarquías de conceptos, reglas de relación y restricciones de clasificación
- Glosario dinámico: seleccione únicamente los términos relevantes de un glosario maestro para mantener reducidas las ventanas de contexto
- Desambiguación entre varios dominios: inyecte reglas para detectar el dominio según el contexto
- Versionado: los glosarios deben versionarse y los prompts deben reevaluarse cuando cambien los términos
- Comprobación de coherencia: procese posteriormente las salidas para detectar la deriva terminológica
Preguntas frecuentes
¿La lección «Inyección de glosarios y ontologías de dominio» es gratis?
Sí — el texto completo de «Inyección de glosarios y ontologías de dominio» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de AI Prompt Engineering, actualiza a CoddyKit PRO. El curso de AI Prompt Engineering incluye 4 lecciones en total.
¿Qué aprenderé en «Inyección de glosarios y ontologías de dominio»?
Incorpore terminología y conocimientos específicos del dominio a los prompts del sistema. Practicas AI Prompt Engineering con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar AI Prompt Engineering?
No se requiere experiencia previa. AI Prompt Engineering en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 4 de 4.
¿Cuánto tiempo toma la lección «Inyección de glosarios y ontologías de dominio»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de AI Prompt Engineering?
Sí. Cada lección de AI Prompt Engineering incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Patrones de prompts para el ámbito jurídico
- Prompts médicos y clínicos
- Prompts financieros y cuantitativos
- Inyección de glosarios y ontologías de dominio