|
| 1 | +"""Schema context building and formatting for Data Fabric entities. |
| 2 | +
|
| 3 | +Converts raw Entity SDK objects into structured Pydantic models (SQLContext), |
| 4 | +then formats them as text for system prompt injection. |
| 5 | +
|
| 6 | +Note: This module will go through refinements as we better understand |
| 7 | +the tool's performance characteristics and scoring in production. |
| 8 | +""" |
| 9 | + |
| 10 | +import logging |
| 11 | + |
| 12 | +from uipath.platform.entities import Entity |
| 13 | + |
| 14 | +from .datafabric_prompts import SQL_CONSTRAINTS, SQL_EXPERT_SYSTEM_PROMPT |
| 15 | +from .models import ( |
| 16 | + EntitySchema, |
| 17 | + EntitySQLContext, |
| 18 | + FieldSchema, |
| 19 | + QueryPattern, |
| 20 | + SQLContext, |
| 21 | +) |
| 22 | + |
| 23 | +logger = logging.getLogger(__name__) |
| 24 | + |
| 25 | + |
| 26 | +def build_entity_context(entity: Entity) -> EntitySQLContext: |
| 27 | + """Convert an Entity SDK object to schema + derived query patterns.""" |
| 28 | + field_schemas: list[FieldSchema] = [] |
| 29 | + numeric_field: str | None = None |
| 30 | + text_field: str | None = None |
| 31 | + |
| 32 | + for field in entity.fields or []: |
| 33 | + if field.is_hidden_field or field.is_system_field: |
| 34 | + continue |
| 35 | + type_name = field.sql_type.name if field.sql_type else "unknown" |
| 36 | + fs = FieldSchema( |
| 37 | + name=field.name, |
| 38 | + display_name=field.display_name, |
| 39 | + type=type_name, |
| 40 | + description=field.description, |
| 41 | + is_foreign_key=field.is_foreign_key, |
| 42 | + is_required=field.is_required, |
| 43 | + is_unique=field.is_unique, |
| 44 | + nullable=not field.is_required, |
| 45 | + ) |
| 46 | + field_schemas.append(fs) |
| 47 | + |
| 48 | + if not numeric_field and fs.is_numeric: |
| 49 | + numeric_field = fs.name |
| 50 | + if not text_field and fs.is_text: |
| 51 | + text_field = fs.name |
| 52 | + |
| 53 | + field_names = [f.name for f in field_schemas] |
| 54 | + table = entity.name |
| 55 | + |
| 56 | + group_field = text_field or (field_names[0] if field_names else "Category") |
| 57 | + agg_field = numeric_field or (field_names[1] if len(field_names) > 1 else "Amount") |
| 58 | + filter_field = text_field or (field_names[0] if field_names else "Name") |
| 59 | + fields_sample = ", ".join(field_names[:5]) if field_names else "*" |
| 60 | + count_col = field_names[0] if field_names else "id" |
| 61 | + |
| 62 | + query_patterns = [ |
| 63 | + QueryPattern( |
| 64 | + intent="Show all", |
| 65 | + sql=f"SELECT {fields_sample} FROM {table} LIMIT 100", |
| 66 | + ), |
| 67 | + QueryPattern( |
| 68 | + intent="Find by X", |
| 69 | + sql=f"SELECT {fields_sample} FROM {table} WHERE {filter_field} = 'value' LIMIT 100", |
| 70 | + ), |
| 71 | + QueryPattern( |
| 72 | + intent="Top N by Y", |
| 73 | + sql=f"SELECT {fields_sample} FROM {table} ORDER BY {agg_field} DESC LIMIT N", |
| 74 | + ), |
| 75 | + QueryPattern( |
| 76 | + intent="Count by X", |
| 77 | + sql=f"SELECT {group_field}, COUNT({count_col}) as count FROM {table} GROUP BY {group_field}", |
| 78 | + ), |
| 79 | + QueryPattern( |
| 80 | + intent="Top N segments", |
| 81 | + sql=f"SELECT {group_field}, COUNT({count_col}) as count FROM {table} GROUP BY {group_field} ORDER BY count DESC LIMIT N", |
| 82 | + ), |
| 83 | + QueryPattern( |
| 84 | + intent="Sum/Avg of Y", |
| 85 | + sql=f"SELECT SUM({agg_field}) as total FROM {table}", |
| 86 | + ), |
| 87 | + ] |
| 88 | + |
| 89 | + schema = EntitySchema( |
| 90 | + id=entity.id, |
| 91 | + entity_name=entity.name, |
| 92 | + display_name=entity.display_name or entity.name, |
| 93 | + description=entity.description, |
| 94 | + record_count=entity.record_count, |
| 95 | + fields=field_schemas, |
| 96 | + ) |
| 97 | + return EntitySQLContext(entity_schema=schema, query_patterns=query_patterns) |
| 98 | + |
| 99 | + |
| 100 | +def build_sql_context( |
| 101 | + entities: list[Entity], |
| 102 | + resource_description: str = "", |
| 103 | + base_system_prompt: str = "", |
| 104 | +) -> SQLContext: |
| 105 | + """Build the full SQL context from entities, prompts, and constraints.""" |
| 106 | + return SQLContext( |
| 107 | + base_system_prompt=base_system_prompt or None, |
| 108 | + resource_description=resource_description or None, |
| 109 | + sql_expert_system_prompt=SQL_EXPERT_SYSTEM_PROMPT, |
| 110 | + constraints=SQL_CONSTRAINTS, |
| 111 | + entity_contexts=[build_entity_context(e) for e in entities], |
| 112 | + ) |
| 113 | + |
| 114 | + |
| 115 | +def format_sql_context(ctx: SQLContext) -> str: |
| 116 | + """Format a SQLContext as text for system prompt injection.""" |
| 117 | + lines: list[str] = [] |
| 118 | + |
| 119 | + if ctx.base_system_prompt: |
| 120 | + lines.append("## Agent Instructions") |
| 121 | + lines.append("") |
| 122 | + lines.append(ctx.base_system_prompt) |
| 123 | + lines.append("") |
| 124 | + |
| 125 | + if ctx.sql_expert_system_prompt: |
| 126 | + lines.append("## SQL Query Generation Guidelines") |
| 127 | + lines.append("") |
| 128 | + lines.append(ctx.sql_expert_system_prompt) |
| 129 | + lines.append("") |
| 130 | + |
| 131 | + if ctx.constraints: |
| 132 | + lines.append("## SQL Constraints") |
| 133 | + lines.append("") |
| 134 | + lines.append(ctx.constraints) |
| 135 | + lines.append("") |
| 136 | + |
| 137 | + if ctx.resource_description: |
| 138 | + lines.append("## Entity set description") |
| 139 | + lines.append("") |
| 140 | + lines.append(ctx.resource_description) |
| 141 | + lines.append("") |
| 142 | + |
| 143 | + lines.append("## All available Data Fabric Entities") |
| 144 | + lines.append("") |
| 145 | + |
| 146 | + for entity_ctx in ctx.entity_contexts: |
| 147 | + entity = entity_ctx.entity_schema |
| 148 | + lines.append( |
| 149 | + f"### Entity: {entity.display_name} (SQL table: `{entity.entity_name}`)" |
| 150 | + ) |
| 151 | + if entity.description: |
| 152 | + lines.append(f"_{entity.description}_") |
| 153 | + lines.append("") |
| 154 | + lines.append("| Field | Type |") |
| 155 | + lines.append("|-------|------|") |
| 156 | + |
| 157 | + for field in entity.fields: |
| 158 | + lines.append(f"| {field.name} | {field.display_type} |") |
| 159 | + |
| 160 | + lines.append("") |
| 161 | + |
| 162 | + lines.append(f"**Query Patterns for {entity.entity_name}:**") |
| 163 | + lines.append("") |
| 164 | + lines.append("| User Intent | SQL Pattern |") |
| 165 | + lines.append("|-------------|-------------|") |
| 166 | + for p in entity_ctx.query_patterns: |
| 167 | + lines.append(f"| '{p.intent}' | `{p.sql}` |") |
| 168 | + lines.append("") |
| 169 | + |
| 170 | + return "\n".join(lines) |
| 171 | + |
| 172 | + |
| 173 | +def build( |
| 174 | + entities: list[Entity], |
| 175 | + resource_description: str = "", |
| 176 | + base_system_prompt: str = "", |
| 177 | +) -> str: |
| 178 | + """Build the full SQL prompt text for the inner sub-graph LLM. |
| 179 | +
|
| 180 | + Combines agent system prompt, resource description, SQL guidelines, |
| 181 | + constraints, entity schemas, and query patterns into a single prompt string. |
| 182 | +
|
| 183 | + Args: |
| 184 | + entities: List of Entity objects with schema information. |
| 185 | + resource_description: Optional description of the resource/entity set. |
| 186 | + base_system_prompt: Optional system prompt from the outer agent. |
| 187 | +
|
| 188 | + Returns: |
| 189 | + Formatted prompt string for the inner LLM system message. |
| 190 | + """ |
| 191 | + if not entities: |
| 192 | + return "" |
| 193 | + |
| 194 | + ctx = build_sql_context(entities, resource_description, base_system_prompt) |
| 195 | + return format_sql_context(ctx) |
0 commit comments