|
3 | 3 | from dataclasses import dataclass, field |
4 | 4 | from typing import Any |
5 | 5 |
|
| 6 | +import yaml |
| 7 | + |
6 | 8 | from ..sources import Source |
7 | 9 | from .config import SOURCE_TABLE_SEPARATOR |
8 | 10 | from .utils import ( |
@@ -40,48 +42,52 @@ class VectorMetaset: |
40 | 42 |
|
41 | 43 | def _generate_context(self, include_columns: bool = False, truncate: bool = False) -> str: |
42 | 44 | """ |
43 | | - Generate formatted text representation of the context. |
| 45 | + Generate YAML formatted representation of the context. |
44 | 46 |
|
45 | 47 | Args: |
46 | 48 | include_columns: Whether to include column details in the context |
47 | 49 | truncate: Whether to truncate strings and columns for brevity |
48 | 50 | """ |
49 | | - context = "" |
| 51 | + tables_data = {} |
| 52 | + |
50 | 53 | for table_slug, vector_metadata in self.vector_metadata_map.items(): |
51 | 54 | base_sql = truncate_string(vector_metadata.base_sql, max_length=200) if truncate else vector_metadata.base_sql |
52 | | - context += f"{table_slug!r} (access this table with: {base_sql})\n" |
| 55 | + |
| 56 | + table_data = {'read_with': base_sql} |
53 | 57 |
|
54 | 58 | if vector_metadata.description: |
55 | 59 | desc = truncate_string(vector_metadata.description, max_length=100) if truncate else vector_metadata.description |
56 | | - context += f"Info: {desc}\n" |
| 60 | + table_data['info'] = desc |
57 | 61 |
|
58 | 62 | # Only include columns if explicitly requested |
59 | 63 | if include_columns and vector_metadata.columns: |
60 | 64 | max_length = 20 |
61 | 65 | cols_to_show = vector_metadata.columns |
62 | 66 |
|
63 | 67 | show_ellipsis = False |
64 | | - original_indices = [] |
65 | 68 | if truncate: |
66 | 69 | cols_to_show, original_indices, show_ellipsis = truncate_iterable(cols_to_show, max_length) |
67 | 70 | else: |
68 | 71 | cols_to_show = list(cols_to_show) |
69 | | - original_indices = list(range(len(cols_to_show))) |
70 | 72 |
|
71 | | - for i, (col, orig_idx) in enumerate(zip(cols_to_show, original_indices, strict=False)): |
| 73 | + columns_data = {} |
| 74 | + for i, col in enumerate(cols_to_show): |
72 | 75 | if show_ellipsis and i == len(cols_to_show) // 2: |
73 | | - context += "...\n" |
74 | | - |
75 | | - if i == 0: |
76 | | - context += "Cols:\n" |
| 76 | + columns_data['...'] = '...' |
77 | 77 |
|
78 | 78 | col_name = truncate_string(col.name) if truncate else col.name |
79 | | - context += f"{orig_idx}. {col_name!r}" |
80 | 79 | if col.description: |
81 | 80 | col_desc = truncate_string(col.description, max_length=100) if truncate else col.description |
82 | | - context += f": {col_desc}" |
83 | | - context += "\n" |
84 | | - return context |
| 81 | + columns_data[col_name] = col_desc |
| 82 | + else: |
| 83 | + columns_data[col_name] = None |
| 84 | + |
| 85 | + if columns_data: |
| 86 | + table_data['columns'] = columns_data |
| 87 | + |
| 88 | + tables_data[table_slug] = table_data |
| 89 | + |
| 90 | + return yaml.dump(tables_data, default_flow_style=False, allow_unicode=True, sort_keys=False) |
85 | 91 |
|
86 | 92 | @property |
87 | 93 | def table_context(self) -> str: |
@@ -120,63 +126,76 @@ class SQLMetaset: |
120 | 126 |
|
121 | 127 | def _generate_context(self, include_columns: bool = False, truncate: bool = False) -> str: |
122 | 128 | """ |
123 | | - Generate formatted context with both vector and SQL data. |
| 129 | + Generate YAML formatted context with both vector and SQL data. |
124 | 130 |
|
125 | 131 | Args: |
126 | 132 | include_columns: Whether to include column details in the context |
127 | 133 | truncate: Whether to truncate strings and columns for brevity |
128 | 134 |
|
129 | 135 | Returns: |
130 | | - Formatted context string |
| 136 | + YAML formatted context string |
131 | 137 | """ |
132 | | - context = "" |
| 138 | + tables_data = {} |
133 | 139 |
|
134 | 140 | for table_slug in self.sql_metadata_map.keys(): |
135 | 141 | vector_metadata = self.vector_metaset.vector_metadata_map.get(table_slug) |
136 | 142 | if not vector_metadata: |
137 | 143 | continue |
138 | 144 |
|
139 | 145 | base_sql = truncate_string(vector_metadata.base_sql, max_length=200) if truncate else vector_metadata.base_sql |
140 | | - context += f"\n{table_slug!r} (access this table with: {base_sql})\n" |
| 146 | + |
| 147 | + table_data = {'read_with': base_sql} |
141 | 148 |
|
142 | 149 | if vector_metadata.description: |
143 | 150 | desc = truncate_string(vector_metadata.description, max_length=100) if truncate else vector_metadata.description |
144 | | - context += f"Info: {desc}\n" |
| 151 | + table_data['info'] = desc |
145 | 152 |
|
146 | 153 | sql_data: SQLMetadata = self.sql_metadata_map.get(table_slug) |
147 | 154 | if sql_data: |
148 | 155 | # Get the count from schema |
149 | 156 | if sql_data.schema.get("__len__"): |
150 | | - context += f"Row count: {len(sql_data.schema)}\n" |
| 157 | + table_data['row_count'] = len(sql_data.schema) |
151 | 158 |
|
152 | 159 | # Only include columns if explicitly requested |
153 | 160 | if include_columns and vector_metadata.columns: |
154 | 161 | cols_to_show = vector_metadata.columns |
155 | | - context += "Columns:" |
| 162 | + columns_data = {} |
| 163 | + |
156 | 164 | for col in cols_to_show: |
157 | 165 | schema_data = None |
158 | 166 | if sql_data and col.name in sql_data.schema: |
159 | 167 | schema_data = sql_data.schema[col.name] |
160 | 168 | if truncate and schema_data == "<null>": |
161 | 169 | continue |
162 | 170 |
|
163 | | - # Get column name |
164 | | - context += f"\n- {col.name}" |
| 171 | + col_info = {} |
165 | 172 |
|
166 | 173 | # Get column description with optional truncation |
167 | 174 | if col.description: |
168 | 175 | col_desc = truncate_string(col.description, max_length=100) if truncate else col.description |
169 | | - context += f": {col_desc}" |
170 | | - else: |
171 | | - context += ": " |
| 176 | + col_info['description'] = col_desc |
172 | 177 |
|
173 | 178 | # Add schema info for the column if available |
174 | | - if schema_data: |
175 | | - if truncate and schema_data.get('type') == 'enum': |
176 | | - schema_data = truncate_string(str(schema_data), max_length=50) |
177 | | - context += f" `{schema_data}`" |
178 | | - context += "\n" |
179 | | - return context.replace("'type': 'str', ", "") # Remove type info for lower token |
| 179 | + if schema_data and schema_data != "<null>": |
| 180 | + if isinstance(schema_data, dict): |
| 181 | + # Remove 'type': 'str' for token efficiency |
| 182 | + schema_copy = {k: v for k, v in schema_data.items() if not (k == 'type' and v == 'str')} |
| 183 | + if truncate and schema_copy.get('type') == 'enum': |
| 184 | + schema_str = str(schema_copy) |
| 185 | + if len(schema_str) > 50: |
| 186 | + schema_copy = truncate_string(schema_str, max_length=50) |
| 187 | + col_info.update(schema_copy) |
| 188 | + else: |
| 189 | + col_info['value'] = schema_data |
| 190 | + |
| 191 | + columns_data[col.name] = col_info if col_info else None |
| 192 | + |
| 193 | + if columns_data: |
| 194 | + table_data['columns'] = columns_data |
| 195 | + |
| 196 | + tables_data[table_slug] = table_data |
| 197 | + |
| 198 | + return yaml.dump(tables_data, default_flow_style=False, allow_unicode=True, sort_keys=False) |
180 | 199 |
|
181 | 200 | @property |
182 | 201 | def table_context(self) -> str: |
|
0 commit comments