prograph 1.3.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- prograph/__init__.py +36 -0
- prograph/__main__.py +5 -0
- prograph/_version.py +375 -0
- prograph/cli/__init__.py +1 -0
- prograph/cli/_iri_utils.py +124 -0
- prograph/cli/emit_genie_space.py +107 -0
- prograph/cli/generate_views.py +140 -0
- prograph/cli/main.py +420 -0
- prograph/cli/schema_inferrer.py +746 -0
- prograph/cli/ttl_loader.py +289 -0
- prograph/cli/upload_genie_space.py +84 -0
- prograph/config/__init__.py +27 -0
- prograph/config/yaml_config.py +848 -0
- prograph/demos/__init__.py +14 -0
- prograph/demos/turbofan/README.md +74 -0
- prograph/demos/turbofan/SCENARIO.md +209 -0
- prograph/demos/turbofan/__init__.py +75 -0
- prograph/demos/turbofan/create_tables.sql +206 -0
- prograph/demos/turbofan/data/engine-instances.ttl +1123 -0
- prograph/demos/turbofan/data/sensor-timeseries.csv +90 -0
- prograph/demos/turbofan/demo.py +162 -0
- prograph/demos/turbofan/genie_qa_notebook.py +434 -0
- prograph/demos/turbofan/graph.yaml +1098 -0
- prograph/demos/turbofan/instance_graph_demo.py +32 -0
- prograph/demos/turbofan/load.py +223 -0
- prograph/demos/turbofan/mapping.r2rml.ttl +991 -0
- prograph/demos/turbofan/ontology/engine-maintenance.ttl +606 -0
- prograph/demos/turbofan/payload-shapes.ttl +33 -0
- prograph/demos/turbofan/queries/example-queries.sparql +215 -0
- prograph/demos/turbofan/shapes.ttl +75 -0
- prograph/demos/turbofan/tables.py +106 -0
- prograph/demos/turbofan/turbofan_schema.py +393 -0
- prograph/demos/turbofan/validation_demo.py +153 -0
- prograph/describe.py +231 -0
- prograph/endpoint/__init__.py +12 -0
- prograph/endpoint/app.py +722 -0
- prograph/endpoint/cli.py +280 -0
- prograph/endpoint/credentials.py +240 -0
- prograph/endpoint/engines.py +418 -0
- prograph/endpoint/negotiate.py +101 -0
- prograph/endpoint/results.py +181 -0
- prograph/endpoint/service.py +31 -0
- prograph/engine.py +2718 -0
- prograph/execution.py +455 -0
- prograph/executors/__init__.py +6 -0
- prograph/executors/base.py +11 -0
- prograph/executors/databricks_sql_executor.py +51 -0
- prograph/executors/duckdb_executor.py +24 -0
- prograph/executors/mysql_executor.py +20 -0
- prograph/executors/oracle_executor.py +24 -0
- prograph/executors/pandas_executor.py +72 -0
- prograph/executors/postgresql_executor.py +35 -0
- prograph/executors/spark_executor.py +88 -0
- prograph/executors/sqlserver_executor.py +35 -0
- prograph/extras/__init__.py +1 -0
- prograph/extras/graphql/__init__.py +33 -0
- prograph/extras/graphql/execute.py +25 -0
- prograph/extras/graphql/schema.py +314 -0
- prograph/extras/instance_graph.py +559 -0
- prograph/genie/__init__.py +83 -0
- prograph/genie/databricks_uploader.py +306 -0
- prograph/genie/deploy.py +393 -0
- prograph/genie/formats.py +414 -0
- prograph/genie/instruction_generator.py +375 -0
- prograph/genie/sample_query_generator.py +256 -0
- prograph/genie/shacl_views.py +318 -0
- prograph/genie/space_emitter.py +329 -0
- prograph/genie/view_generator.py +628 -0
- prograph/genie/view_writer.py +172 -0
- prograph/graph/__init__.py +1 -0
- prograph/graph/ansi_session.py +348 -0
- prograph/graph/databricks_sql_graph.py +300 -0
- prograph/graph/drivers.py +143 -0
- prograph/graph/duckdb_graph.py +68 -0
- prograph/graph/mysql_graph.py +38 -0
- prograph/graph/oracle_graph.py +285 -0
- prograph/graph/pandas_graph.py +343 -0
- prograph/graph/postgresql_graph.py +644 -0
- prograph/graph/spark_graph.py +556 -0
- prograph/graph/sql_graph.py +83 -0
- prograph/graph/sqlserver_graph.py +72 -0
- prograph/hints.py +95 -0
- prograph/logging.py +550 -0
- prograph/mcp/__init__.py +29 -0
- prograph/mcp/__main__.py +180 -0
- prograph/mcp/bootstrap.py +98 -0
- prograph/mcp/server.py +601 -0
- prograph/mcp/sparql.py +397 -0
- prograph/mcp/tools.py +504 -0
- prograph/nlp/__init__.py +79 -0
- prograph/nlp/client.py +305 -0
- prograph/nlp/databricks_tools.py +369 -0
- prograph/nlp/graph_context.py +313 -0
- prograph/nlp/interface.py +652 -0
- prograph/nlp/prompts/__init__.py +19 -0
- prograph/nlp/prompts/examples.py +258 -0
- prograph/nlp/prompts/system.py +239 -0
- prograph/nlp/providers/__init__.py +14 -0
- prograph/nlp/providers/anthropic.py +274 -0
- prograph/nlp/providers/databricks.py +312 -0
- prograph/nlp/providers/openai.py +203 -0
- prograph/nlp/schema_context.py +78 -0
- prograph/ontology/__init__.py +53 -0
- prograph/ontology/alignment.py +542 -0
- prograph/ontology/hierarchy.py +310 -0
- prograph/ontology/imports.py +587 -0
- prograph/ontology/layer.py +991 -0
- prograph/ontology/skos.py +61 -0
- prograph/owl/__init__.py +27 -0
- prograph/owl/defined_classes.py +323 -0
- prograph/parameters.py +242 -0
- prograph/parser/__init__.py +1 -0
- prograph/parser/cypher/CypherLexer.py +836 -0
- prograph/parser/cypher/CypherListener.py +966 -0
- prograph/parser/cypher/CypherParser.py +10854 -0
- prograph/parser/cypher/CypherVisitor.py +543 -0
- prograph/parser/cypher/__init__.py +1 -0
- prograph/parser/sparql/SparqlLexer.py +801 -0
- prograph/parser/sparql/SparqlParser.py +10146 -0
- prograph/parser/sparql/SparqlParserListener.py +1056 -0
- prograph/parser/sparql/SparqlParserVisitor.py +593 -0
- prograph/parser/sparql/__init__.py +8 -0
- prograph/plan/__init__.py +3 -0
- prograph/plan/ir/__init__.py +1 -0
- prograph/plan/ir/casts.py +338 -0
- prograph/plan/ir/consistency.py +397 -0
- prograph/plan/ir/construct.py +90 -0
- prograph/plan/ir/cost.py +234 -0
- prograph/plan/ir/cypher_answer.py +1081 -0
- prograph/plan/ir/cypher_apoc.py +717 -0
- prograph/plan/ir/cypher_apoc_convert.py +505 -0
- prograph/plan/ir/cypher_apoc_date.py +845 -0
- prograph/plan/ir/cypher_apoc_map.py +496 -0
- prograph/plan/ir/cypher_apoc_meta.py +384 -0
- prograph/plan/ir/cypher_apoc_path.py +597 -0
- prograph/plan/ir/cypher_apoc_procedures.py +163 -0
- prograph/plan/ir/cypher_apoc_text.py +785 -0
- prograph/plan/ir/cypher_front.py +5474 -0
- prograph/plan/ir/cypher_functions.py +565 -0
- prograph/plan/ir/cypher_lists.py +935 -0
- prograph/plan/ir/cypher_maps.py +370 -0
- prograph/plan/ir/cypher_mixed.py +604 -0
- prograph/plan/ir/cypher_ontology.py +226 -0
- prograph/plan/ir/cypher_procedures.py +191 -0
- prograph/plan/ir/cypher_runtime_errors.py +107 -0
- prograph/plan/ir/cypher_temporal.py +1357 -0
- prograph/plan/ir/cypher_temporal_arith.py +339 -0
- prograph/plan/ir/cypher_values.py +546 -0
- prograph/plan/ir/cypher_write.py +612 -0
- prograph/plan/ir/dataset.py +643 -0
- prograph/plan/ir/dates.py +102 -0
- prograph/plan/ir/entry.py +992 -0
- prograph/plan/ir/expr.py +260 -0
- prograph/plan/ir/facts.py +1171 -0
- prograph/plan/ir/fresh.py +291 -0
- prograph/plan/ir/freshness.py +625 -0
- prograph/plan/ir/identity.py +797 -0
- prograph/plan/ir/java_datetime.py +265 -0
- prograph/plan/ir/java_regex.py +391 -0
- prograph/plan/ir/join_order.py +39 -0
- prograph/plan/ir/key_checks.py +433 -0
- prograph/plan/ir/keys.py +454 -0
- prograph/plan/ir/lower.py +5323 -0
- prograph/plan/ir/ops.py +619 -0
- prograph/plan/ir/owl_rl.py +1119 -0
- prograph/plan/ir/owl_rl_rules.py +725 -0
- prograph/plan/ir/profiles.py +694 -0
- prograph/plan/ir/ql.py +987 -0
- prograph/plan/ir/rdfs.py +342 -0
- prograph/plan/ir/rewrite.py +1782 -0
- prograph/plan/ir/saturate.py +339 -0
- prograph/plan/ir/semantics.py +103 -0
- prograph/plan/ir/slot.py +509 -0
- prograph/plan/ir/sparql_front.py +3418 -0
- prograph/plan/ir/strings.py +534 -0
- prograph/plan/ir/term_functions.py +274 -0
- prograph/plan/ir/terms.py +84 -0
- prograph/plan/ir/xpath_regex.py +264 -0
- prograph/plan/run_record.py +65 -0
- prograph/plan/sql/__init__.py +21 -0
- prograph/plan/sql/dialect.py +3726 -0
- prograph/plan/sql/mysql_serializer.py +103 -0
- prograph/plan/sql/names.py +65 -0
- prograph/plan/sql/oracle_serializer.py +306 -0
- prograph/plan/sql/pg_serializer.py +501 -0
- prograph/plan/sql/rel_algebra.py +781 -0
- prograph/plan/sql/sql_serializer.py +1419 -0
- prograph/plan/sql/sqlserver_serializer.py +603 -0
- prograph/provenance.py +222 -0
- prograph/r2rml/__init__.py +48 -0
- prograph/r2rml/catalog.py +259 -0
- prograph/r2rml/compile.py +372 -0
- prograph/r2rml/direct.py +140 -0
- prograph/r2rml/export.py +495 -0
- prograph/r2rml/model.py +180 -0
- prograph/r2rml/parser.py +546 -0
- prograph/r2rml/schema.py +120 -0
- prograph/read_only.py +122 -0
- prograph/results.py +157 -0
- prograph/rules/__init__.py +168 -0
- prograph/rules/__main__.py +32 -0
- prograph/schema/__init__.py +60 -0
- prograph/schema/change_set.py +50 -0
- prograph/schema/collations.py +198 -0
- prograph/schema/compile_schema.py +388 -0
- prograph/schema/context.py +845 -0
- prograph/schema/examples.py +234 -0
- prograph/schema/iri.py +817 -0
- prograph/schema/literals.py +94 -0
- prograph/schema/lpg_view.py +617 -0
- prograph/schema/mapping.py +1803 -0
- prograph/schema/maps_schema.py +40 -0
- prograph/schema/naming.py +77 -0
- prograph/schema/query_sources.py +59 -0
- prograph/schema/rows.py +215 -0
- prograph/schema/source_map.py +1103 -0
- prograph/schema/store_view.py +459 -0
- prograph/schema/types.py +569 -0
- prograph/schema/xsd.py +569 -0
- prograph/shacl/__init__.py +30 -0
- prograph/shacl/dqv.py +58 -0
- prograph/shacl/generate.py +255 -0
- prograph/shacl/report.py +138 -0
- prograph/shacl/validator.py +164 -0
- prograph/store.py +1737 -0
- prograph/utils/__init__.py +6 -0
- prograph/utils/exceptions.py +308 -0
- prograph/utils/helpers.py +126 -0
- prograph/warnings.py +46 -0
- prograph/writes.py +369 -0
- prograph-1.3.3.dist-info/METADATA +281 -0
- prograph-1.3.3.dist-info/RECORD +237 -0
- prograph-1.3.3.dist-info/WHEEL +5 -0
- prograph-1.3.3.dist-info/entry_points.txt +3 -0
- prograph-1.3.3.dist-info/licenses/LICENSE +201 -0
- prograph-1.3.3.dist-info/licenses/NOTICE +10 -0
- prograph-1.3.3.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
"""Schema-driven query examples for LLM few-shot prompting.
|
|
2
|
+
|
|
3
|
+
Two layers:
|
|
4
|
+
|
|
5
|
+
``QueryExample``, a typed example pair (question + query) with optional
|
|
6
|
+
tags and language. Schema authors can attach curated examples to a
|
|
7
|
+
``GraphSchema`` to surface real, meaningful patterns to LLMs.
|
|
8
|
+
|
|
9
|
+
``generate_examples``, given a ``SchemaDescriptor``, synthesize generic
|
|
10
|
+
examples (simple lookup, relationship, aggregation). Used as a fallback
|
|
11
|
+
when no curated examples are attached.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
import re
|
|
17
|
+
from dataclasses import asdict, dataclass, field
|
|
18
|
+
from typing import Any, Dict, List, Optional
|
|
19
|
+
|
|
20
|
+
from prograph.schema.context import SchemaDescriptor
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@dataclass(frozen=True)
|
|
24
|
+
class QueryExample:
|
|
25
|
+
"""A natural-language question paired with the query that answers it."""
|
|
26
|
+
|
|
27
|
+
question: str
|
|
28
|
+
query: str
|
|
29
|
+
language: str = "sparql"
|
|
30
|
+
tags: List[str] = field(default_factory=list)
|
|
31
|
+
|
|
32
|
+
def to_dict(self) -> Dict[str, Any]:
|
|
33
|
+
return asdict(self)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _first_non_id_property(node) -> Optional[str]:
|
|
37
|
+
for p in node.properties:
|
|
38
|
+
if p.name not in node.id_columns:
|
|
39
|
+
return p.name
|
|
40
|
+
return None
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def generate_examples(
|
|
44
|
+
desc: SchemaDescriptor,
|
|
45
|
+
*,
|
|
46
|
+
n: int = 3,
|
|
47
|
+
language: str = "sparql",
|
|
48
|
+
) -> List[QueryExample]:
|
|
49
|
+
"""Synthesize generic few-shot examples from a schema descriptor.
|
|
50
|
+
|
|
51
|
+
Produces up to ``n`` examples covering: simple node lookup, relationship
|
|
52
|
+
traversal, and aggregation. Returns an empty list if the schema has no
|
|
53
|
+
node descriptors.
|
|
54
|
+
"""
|
|
55
|
+
examples: List[QueryExample] = []
|
|
56
|
+
if not desc.nodes:
|
|
57
|
+
return examples
|
|
58
|
+
|
|
59
|
+
lang = language.lower()
|
|
60
|
+
if lang == "sparql":
|
|
61
|
+
return sparql_examples(desc, n=n)
|
|
62
|
+
first_node = desc.nodes[0]
|
|
63
|
+
label = first_node.label
|
|
64
|
+
|
|
65
|
+
# 1. Simple node query
|
|
66
|
+
prop = _first_non_id_property(first_node)
|
|
67
|
+
if prop:
|
|
68
|
+
query = f"MATCH (n:{label}) RETURN n.{prop} LIMIT 10"
|
|
69
|
+
else:
|
|
70
|
+
query = f"MATCH (n:{label}) RETURN n LIMIT 10"
|
|
71
|
+
examples.append(
|
|
72
|
+
QueryExample(
|
|
73
|
+
question=f"Show me the first 10 {label.lower()}s",
|
|
74
|
+
query=query,
|
|
75
|
+
language=lang,
|
|
76
|
+
tags=["lookup", "limit"],
|
|
77
|
+
)
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
# 2. Relationship traversal
|
|
81
|
+
if desc.relationships and len(examples) < n:
|
|
82
|
+
rel = desc.relationships[0]
|
|
83
|
+
query = (
|
|
84
|
+
f"MATCH (a:{rel.start_label})-[:{rel.rel_type}]->(b:{rel.end_label}) "
|
|
85
|
+
f"RETURN a, b LIMIT 10"
|
|
86
|
+
)
|
|
87
|
+
examples.append(
|
|
88
|
+
QueryExample(
|
|
89
|
+
question=(
|
|
90
|
+
f"Find {rel.start_label.lower()}s that have "
|
|
91
|
+
f"{rel.rel_type.lower().replace('_', ' ')} "
|
|
92
|
+
f"{rel.end_label.lower()}s"
|
|
93
|
+
),
|
|
94
|
+
query=query,
|
|
95
|
+
language=lang,
|
|
96
|
+
tags=["relationship", "traversal"],
|
|
97
|
+
)
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
# 3. Aggregation
|
|
101
|
+
if len(examples) < n:
|
|
102
|
+
query = f"MATCH (n:{label}) RETURN count(n) AS total"
|
|
103
|
+
examples.append(
|
|
104
|
+
QueryExample(
|
|
105
|
+
question=f"How many {label.lower()}s are there?",
|
|
106
|
+
query=query,
|
|
107
|
+
language=lang,
|
|
108
|
+
tags=["aggregation", "count"],
|
|
109
|
+
)
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
return examples[:n]
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def sparql_examples(desc: SchemaDescriptor, *, n: int = 5) -> List[QueryExample]:
|
|
116
|
+
"""SPARQL examples over the schema's own vocabulary (`describe_vocabulary`):
|
|
117
|
+
a lookup, a join over an object predicate, a count over a class that
|
|
118
|
+
others are within, a property path where a predicate's objects can be
|
|
119
|
+
its subjects again, and an OPTIONAL. Each starts with the PREFIX lines
|
|
120
|
+
`render_sparql` gives."""
|
|
121
|
+
from prograph.schema.context import _WELL_KNOWN_PREFIXES, curie, sparql_prefixes
|
|
122
|
+
|
|
123
|
+
_RDFS_LABEL = _WELL_KNOWN_PREFIXES["rdfs"] + "label"
|
|
124
|
+
vocab = desc.vocabulary
|
|
125
|
+
if vocab is None or not vocab.classes:
|
|
126
|
+
return []
|
|
127
|
+
prefixes = sparql_prefixes(vocab)
|
|
128
|
+
head = "\n".join(f"PREFIX {k}: <{v}>" for k, v in prefixes.items()) + "\n"
|
|
129
|
+
c = lambda iri: curie(iri, prefixes)
|
|
130
|
+
word = lambda iri: c(iri).split(":", 1)[-1].strip("<>")
|
|
131
|
+
top = [k for k in vocab.classes if not k.within]
|
|
132
|
+
literal = [t for t in vocab.predicates if t.kind == "datatype" and t.subject_classes]
|
|
133
|
+
linked = [t for t in vocab.predicates if t.kind == "object" and t.subject_classes and t.object_classes]
|
|
134
|
+
out: List[QueryExample] = []
|
|
135
|
+
|
|
136
|
+
def add(question: str, body: str, tags: List[str]) -> None:
|
|
137
|
+
out.append(QueryExample(question=question, query=head + body, language="sparql", tags=tags))
|
|
138
|
+
|
|
139
|
+
def var(iri: str) -> str:
|
|
140
|
+
# A variable named after what it holds (the prompt's rule).
|
|
141
|
+
name = re.sub(r"\W", "_", word(iri))
|
|
142
|
+
return "?" + (name[:1].lower() + name[1:] if name else "x")
|
|
143
|
+
|
|
144
|
+
def name_of(k: str):
|
|
145
|
+
"""The predicate naming a node of class k: rdfs:label, else one
|
|
146
|
+
whose local name ends in "name"; None where it has neither."""
|
|
147
|
+
own = [t for t in literal if k in t.subject_classes]
|
|
148
|
+
return (next((t for t in own if t.iri == _RDFS_LABEL), None)
|
|
149
|
+
or next((t for t in own if word(t.iri).lower().endswith("name")), None))
|
|
150
|
+
|
|
151
|
+
def named(k: str, v: str):
|
|
152
|
+
"""A node's name beside it: (the variables to select, the triple
|
|
153
|
+
that reads the name); the node alone where its class has none."""
|
|
154
|
+
t = name_of(k)
|
|
155
|
+
if t is None:
|
|
156
|
+
return v, ""
|
|
157
|
+
return f"{v} {v}Name", f" . {v} {c(t.iri)} {v}Name"
|
|
158
|
+
|
|
159
|
+
if literal:
|
|
160
|
+
k = literal[0].subject_classes[0]
|
|
161
|
+
t = next((u for u in literal if k in u.subject_classes and u is not name_of(k)), literal[0])
|
|
162
|
+
x = var(k)
|
|
163
|
+
shown, read = named(k, x)
|
|
164
|
+
add(f"Show ten {word(k)}s with their {word(t.iri)}",
|
|
165
|
+
f"SELECT {shown} {var(t.iri)} WHERE {{ {x} a {c(k)} ; {c(t.iri)} {var(t.iri)}{read} }} "
|
|
166
|
+
f"ORDER BY {x} LIMIT 10", ["lookup"])
|
|
167
|
+
if linked:
|
|
168
|
+
t = linked[0]
|
|
169
|
+
a, b = t.subject_classes[0], t.object_classes[0]
|
|
170
|
+
x, y = var(a), var(b)
|
|
171
|
+
if x == y:
|
|
172
|
+
y = x + "2"
|
|
173
|
+
shown_x, read_x = named(a, x)
|
|
174
|
+
shown_y, read_y = named(b, y)
|
|
175
|
+
add(f"Which {word(b)} is each {word(a)} {word(t.iri)}?",
|
|
176
|
+
f"SELECT {shown_x} {shown_y} WHERE {{ {x} a {c(a)} ; {c(t.iri)} {y}{read_x} . "
|
|
177
|
+
f"{y} a {c(b)}{read_y} }}", ["join"])
|
|
178
|
+
parent = next((k for k in top if any(k.iri in s.within for s in vocab.classes)), None)
|
|
179
|
+
if parent is not None:
|
|
180
|
+
add(f"How many {word(parent.iri)}s are there of each kind?",
|
|
181
|
+
f"SELECT ?kind (COUNT(?x) AS ?n) WHERE {{ ?x a {c(parent.iri)} ; a ?kind }} GROUP BY ?kind",
|
|
182
|
+
["aggregation", "hierarchy"])
|
|
183
|
+
chained = next((t for t in linked if set(t.object_classes) & set(t.subject_classes)), None)
|
|
184
|
+
if chained is not None:
|
|
185
|
+
end = next((o for o in chained.object_classes if o not in chained.subject_classes),
|
|
186
|
+
chained.object_classes[0])
|
|
187
|
+
add(f"What does each {word(chained.subject_classes[0])} reach by {word(chained.iri)} "
|
|
188
|
+
f"one or more times, as far as a {word(end)}?",
|
|
189
|
+
f"SELECT ?x ?y WHERE {{ ?x {c(chained.iri)}+ ?y . ?y a {c(end)} }}", ["path"])
|
|
190
|
+
if literal and len(out) < n:
|
|
191
|
+
t = literal[-1]
|
|
192
|
+
k = t.subject_classes[0]
|
|
193
|
+
add(f"List every {word(k)} and its {word(t.iri)} where it has one",
|
|
194
|
+
f"SELECT ?x ?value WHERE {{ ?x a {c(k)} OPTIONAL {{ ?x {c(t.iri)} ?value }} }}", ["optional"])
|
|
195
|
+
if linked:
|
|
196
|
+
t = linked[0]
|
|
197
|
+
a, b = t.subject_classes[0], t.object_classes[0]
|
|
198
|
+
x, y = var(a), var(b)
|
|
199
|
+
if x == y:
|
|
200
|
+
y = x + "2"
|
|
201
|
+
shown_y, read_y = named(b, y)
|
|
202
|
+
# The most of a kind: grouped by the node and its name, a tie broken
|
|
203
|
+
# by the name (else the node), the top three.
|
|
204
|
+
order = f"{y}Name" if name_of(b) is not None else y
|
|
205
|
+
add(f"Which 3 {word(b)}s have the most {word(a)}s?",
|
|
206
|
+
f"SELECT {shown_y} (COUNT({x}) AS ?count) WHERE {{ {x} a {c(a)} ; {c(t.iri)} {y}{read_y} }} "
|
|
207
|
+
f"GROUP BY {shown_y} ORDER BY DESC(?count) {order} LIMIT 3", ["top", "aggregation"])
|
|
208
|
+
return out[:n]
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
def render_examples(examples: List[QueryExample]) -> str:
|
|
212
|
+
"""Format examples for inclusion in a system prompt.
|
|
213
|
+
|
|
214
|
+
Matches the layout of the legacy ``format_examples_for_prompt`` so prompts
|
|
215
|
+
stay stable across the refactor.
|
|
216
|
+
"""
|
|
217
|
+
if not examples:
|
|
218
|
+
return ""
|
|
219
|
+
|
|
220
|
+
lines = ["## Examples", ""]
|
|
221
|
+
for i, ex in enumerate(examples, 1):
|
|
222
|
+
lines.append(f"**Question {i}:** {ex.question}")
|
|
223
|
+
lines.append("```")
|
|
224
|
+
lines.append(ex.query)
|
|
225
|
+
lines.append("```")
|
|
226
|
+
lines.append("")
|
|
227
|
+
return "\n".join(lines)
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
__all__ = [
|
|
231
|
+
"QueryExample",
|
|
232
|
+
"generate_examples",
|
|
233
|
+
"render_examples",
|
|
234
|
+
]
|