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,203 @@
|
|
|
1
|
+
"""
|
|
2
|
+
OpenAI API LLM client.
|
|
3
|
+
|
|
4
|
+
Direct integration with OpenAI API for non-Databricks environments.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import os
|
|
8
|
+
from typing import Any, Dict, List, Optional
|
|
9
|
+
|
|
10
|
+
from prograph.nlp.client import (
|
|
11
|
+
CompletionResponse,
|
|
12
|
+
LLMAPIError,
|
|
13
|
+
LLMAuthenticationError,
|
|
14
|
+
LLMClient,
|
|
15
|
+
LLMError,
|
|
16
|
+
LLMRateLimitError,
|
|
17
|
+
Message,
|
|
18
|
+
Tool,
|
|
19
|
+
ToolCall,
|
|
20
|
+
answer_text,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
# Check for OpenAI SDK availability
|
|
24
|
+
try:
|
|
25
|
+
from openai import OpenAI, APIError, AuthenticationError, RateLimitError
|
|
26
|
+
|
|
27
|
+
OPENAI_SDK_AVAILABLE = True
|
|
28
|
+
except ImportError:
|
|
29
|
+
OPENAI_SDK_AVAILABLE = False
|
|
30
|
+
OpenAI = None # type: ignore
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class OpenAILLMClient(LLMClient):
|
|
34
|
+
"""
|
|
35
|
+
LLM client using OpenAI API directly.
|
|
36
|
+
|
|
37
|
+
For use in non-Databricks environments or when you want to use
|
|
38
|
+
OpenAI models directly.
|
|
39
|
+
|
|
40
|
+
Example:
|
|
41
|
+
>>> # From environment variable
|
|
42
|
+
>>> client = OpenAILLMClient()
|
|
43
|
+
|
|
44
|
+
>>> # Explicit API key
|
|
45
|
+
>>> client = OpenAILLMClient(
|
|
46
|
+
... model="gpt-4o",
|
|
47
|
+
... api_key="sk-..."
|
|
48
|
+
... )
|
|
49
|
+
|
|
50
|
+
>>> messages = [Message.user("Explain graph databases")]
|
|
51
|
+
>>> response = client.complete(messages)
|
|
52
|
+
"""
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def __init__(
|
|
56
|
+
self,
|
|
57
|
+
model: str = "gpt-4o",
|
|
58
|
+
api_key: Optional[str] = None,
|
|
59
|
+
organization: Optional[str] = None,
|
|
60
|
+
base_url: Optional[str] = None,
|
|
61
|
+
timeout: float = 120.0,
|
|
62
|
+
):
|
|
63
|
+
"""
|
|
64
|
+
Initialize OpenAI LLM client.
|
|
65
|
+
|
|
66
|
+
Args:
|
|
67
|
+
model: Model name (e.g., "gpt-4o", "gpt-4-turbo").
|
|
68
|
+
api_key: OpenAI API key. Uses OPENAI_API_KEY env var if not provided.
|
|
69
|
+
organization: Optional OpenAI organization ID.
|
|
70
|
+
base_url: Optional custom base URL (for Azure OpenAI or proxies).
|
|
71
|
+
timeout: Request timeout in seconds.
|
|
72
|
+
|
|
73
|
+
Raises:
|
|
74
|
+
LLMError: If OpenAI SDK is not installed.
|
|
75
|
+
LLMAuthenticationError: If API key is not found.
|
|
76
|
+
"""
|
|
77
|
+
if not OPENAI_SDK_AVAILABLE:
|
|
78
|
+
raise LLMError(
|
|
79
|
+
"OpenAI SDK is required. Install with: pip install 'prograph[nlp]' (the mcp extra includes it)"
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
self._model = model
|
|
83
|
+
self._timeout = timeout
|
|
84
|
+
|
|
85
|
+
# Get API key from environment if not provided
|
|
86
|
+
if api_key is None:
|
|
87
|
+
api_key = os.environ.get("OPENAI_API_KEY")
|
|
88
|
+
if api_key is None:
|
|
89
|
+
raise LLMAuthenticationError(
|
|
90
|
+
"OpenAI API key not found. "
|
|
91
|
+
"Set OPENAI_API_KEY environment variable or pass api_key parameter."
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
# Build client kwargs
|
|
95
|
+
client_kwargs: Dict[str, Any] = {
|
|
96
|
+
"api_key": api_key,
|
|
97
|
+
"timeout": timeout,
|
|
98
|
+
}
|
|
99
|
+
if organization:
|
|
100
|
+
client_kwargs["organization"] = organization
|
|
101
|
+
if base_url:
|
|
102
|
+
client_kwargs["base_url"] = base_url
|
|
103
|
+
|
|
104
|
+
self._client = OpenAI(**client_kwargs)
|
|
105
|
+
|
|
106
|
+
def complete(
|
|
107
|
+
self,
|
|
108
|
+
messages: List[Message],
|
|
109
|
+
tools: Optional[List[Tool]] = None,
|
|
110
|
+
tool_choice: str = "auto",
|
|
111
|
+
temperature: Optional[float] = 0.0,
|
|
112
|
+
max_tokens: Optional[int] = None,
|
|
113
|
+
) -> CompletionResponse:
|
|
114
|
+
"""
|
|
115
|
+
Generate a completion using OpenAI API.
|
|
116
|
+
|
|
117
|
+
Args:
|
|
118
|
+
messages: Conversation messages.
|
|
119
|
+
tools: Optional tools for function calling.
|
|
120
|
+
tool_choice: Tool selection strategy.
|
|
121
|
+
temperature: Sampling temperature; None sends none, and a
|
|
122
|
+
model that refuses it is asked again without it.
|
|
123
|
+
max_tokens: Maximum response tokens.
|
|
124
|
+
|
|
125
|
+
Returns:
|
|
126
|
+
CompletionResponse with the model's response.
|
|
127
|
+
|
|
128
|
+
Raises:
|
|
129
|
+
LLMAuthenticationError: If authentication fails.
|
|
130
|
+
LLMRateLimitError: If rate limit is exceeded.
|
|
131
|
+
LLMAPIError: For other API errors.
|
|
132
|
+
"""
|
|
133
|
+
try:
|
|
134
|
+
# Build request
|
|
135
|
+
request_kwargs: Dict[str, Any] = {
|
|
136
|
+
"model": self._model,
|
|
137
|
+
"messages": [m.to_dict() for m in messages],
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if max_tokens is not None:
|
|
141
|
+
request_kwargs["max_tokens"] = max_tokens
|
|
142
|
+
|
|
143
|
+
if tools is not None and len(tools) > 0:
|
|
144
|
+
request_kwargs["tools"] = [t.to_dict() for t in tools]
|
|
145
|
+
request_kwargs["tool_choice"] = tool_choice
|
|
146
|
+
|
|
147
|
+
# Make API call
|
|
148
|
+
response = self._send_with_temperature(
|
|
149
|
+
lambda request: self._client.chat.completions.create(**request),
|
|
150
|
+
request_kwargs,
|
|
151
|
+
temperature,
|
|
152
|
+
)
|
|
153
|
+
|
|
154
|
+
# Parse response
|
|
155
|
+
choice = response.choices[0]
|
|
156
|
+
message = choice.message
|
|
157
|
+
|
|
158
|
+
# Parse tool calls if present
|
|
159
|
+
tool_calls = None
|
|
160
|
+
if message.tool_calls:
|
|
161
|
+
tool_calls = [
|
|
162
|
+
ToolCall(
|
|
163
|
+
id=tc.id,
|
|
164
|
+
name=tc.function.name,
|
|
165
|
+
arguments=tc.function.arguments,
|
|
166
|
+
)
|
|
167
|
+
for tc in message.tool_calls
|
|
168
|
+
]
|
|
169
|
+
|
|
170
|
+
# Build usage dict
|
|
171
|
+
usage = None
|
|
172
|
+
if response.usage:
|
|
173
|
+
usage = {
|
|
174
|
+
"prompt_tokens": response.usage.prompt_tokens,
|
|
175
|
+
"completion_tokens": response.usage.completion_tokens,
|
|
176
|
+
"total_tokens": response.usage.total_tokens,
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
return CompletionResponse(
|
|
180
|
+
content=answer_text(message.content),
|
|
181
|
+
tool_calls=tool_calls,
|
|
182
|
+
usage=usage,
|
|
183
|
+
finish_reason=choice.finish_reason,
|
|
184
|
+
model=response.model,
|
|
185
|
+
)
|
|
186
|
+
|
|
187
|
+
except AuthenticationError as e:
|
|
188
|
+
raise LLMAuthenticationError(f"OpenAI authentication failed: {e}")
|
|
189
|
+
except RateLimitError as e:
|
|
190
|
+
raise LLMRateLimitError(f"OpenAI rate limit exceeded: {e}")
|
|
191
|
+
except APIError as e:
|
|
192
|
+
raise LLMAPIError(f"OpenAI API error: {e}", status_code=getattr(e, "status_code", None))
|
|
193
|
+
except Exception as e:
|
|
194
|
+
raise LLMError(f"Unexpected error calling OpenAI: {e}")
|
|
195
|
+
|
|
196
|
+
def supports_function_calling(self) -> bool:
|
|
197
|
+
"""OpenAI models support function calling."""
|
|
198
|
+
return True
|
|
199
|
+
|
|
200
|
+
@property
|
|
201
|
+
def model_name(self) -> str:
|
|
202
|
+
"""Get the model name."""
|
|
203
|
+
return self._model
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"""Schema context generation for LLM prompts.
|
|
2
|
+
|
|
3
|
+
This module is a compatibility shim. The schema-description machinery now
|
|
4
|
+
lives in ``prograph.schema.context`` (descriptor, renderers) and
|
|
5
|
+
``prograph.schema.examples`` (curated and generated examples), where it is
|
|
6
|
+
useful to MCP servers, tool-using agents, and any consumer that doesn't
|
|
7
|
+
want to depend on the NLI stack.
|
|
8
|
+
|
|
9
|
+
New code should prefer:
|
|
10
|
+
|
|
11
|
+
>>> desc = engine.schema.describe()
|
|
12
|
+
>>> from prograph.schema import render_markdown, render_prompt
|
|
13
|
+
>>> render_markdown(desc)
|
|
14
|
+
>>> render_prompt(desc, role="agent")
|
|
15
|
+
|
|
16
|
+
``SchemaContextBuilder`` is retained for backward compatibility with code
|
|
17
|
+
that imports it (notably ``NaturalLanguageInterface`` and the Databricks
|
|
18
|
+
tools wrappers).
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple
|
|
22
|
+
|
|
23
|
+
from prograph.schema.context import (
|
|
24
|
+
describe_schema,
|
|
25
|
+
render_markdown,
|
|
26
|
+
render_prompt,
|
|
27
|
+
)
|
|
28
|
+
from prograph.schema.examples import (
|
|
29
|
+
QueryExample,
|
|
30
|
+
generate_examples,
|
|
31
|
+
render_examples,
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
if TYPE_CHECKING:
|
|
35
|
+
from prograph import ProGraph
|
|
36
|
+
from prograph.schema.mapping import GraphSchema
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class SchemaContextBuilder:
|
|
40
|
+
"""Backward-compatible wrapper around the new schema-description API.
|
|
41
|
+
|
|
42
|
+
Delegates to ``prograph.schema.context`` and ``prograph.schema.examples``.
|
|
43
|
+
Existing call sites continue to work; new code should use the underlying
|
|
44
|
+
functions directly.
|
|
45
|
+
"""
|
|
46
|
+
|
|
47
|
+
def __init__(self, engine: "ProGraph"):
|
|
48
|
+
self.engine = engine
|
|
49
|
+
|
|
50
|
+
@property
|
|
51
|
+
def schema(self) -> Optional["GraphSchema"]:
|
|
52
|
+
return getattr(self.engine, "_schema", None)
|
|
53
|
+
|
|
54
|
+
# ----- accessors used by tests and external callers ------------------
|
|
55
|
+
|
|
56
|
+
# ----- string forms --------------------------------------------------
|
|
57
|
+
|
|
58
|
+
def build_schema_description(self) -> str:
|
|
59
|
+
if self.schema is None:
|
|
60
|
+
return "No schema available."
|
|
61
|
+
return render_markdown(describe_schema(self.schema))
|
|
62
|
+
|
|
63
|
+
def build_few_shot_examples(
|
|
64
|
+
self, language: str = "sparql", n: int = 3
|
|
65
|
+
) -> List[Dict[str, str]]:
|
|
66
|
+
if self.schema is None:
|
|
67
|
+
return []
|
|
68
|
+
descs = generate_examples(describe_schema(self.schema), n=n, language=language)
|
|
69
|
+
# Legacy callers expect plain dicts with "question" and "query".
|
|
70
|
+
return [{"question": e.question, "query": e.query} for e in descs]
|
|
71
|
+
|
|
72
|
+
def describe_schema_empty():
|
|
73
|
+
"""Return an empty SchemaDescriptor for ``build_system_prompt`` with no schema."""
|
|
74
|
+
from prograph.schema.context import SchemaDescriptor
|
|
75
|
+
return SchemaDescriptor(nodes=[], relationships=[])
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
__all__ = ["SchemaContextBuilder"]
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"""Ontology module for subclass-based query expansion and alignment.
|
|
2
|
+
|
|
3
|
+
This module loads OWL/RDFS ontologies and uses their declared class and
|
|
4
|
+
property hierarchies to perform *syntactic* query rewriting at compile
|
|
5
|
+
time, subclass expansion, equivalent-property expansion, symmetric
|
|
6
|
+
and transitive pattern rewriting.
|
|
7
|
+
|
|
8
|
+
This is label and pattern rewriting, not OWL reasoning. Full OWL 2 RL+
|
|
9
|
+
entailment (property chains, defined classes, triple materialization)
|
|
10
|
+
is delivered by ``prograph.owl`` in phase 2 of the W3C roadmap.
|
|
11
|
+
|
|
12
|
+
Key capabilities:
|
|
13
|
+
|
|
14
|
+
- Subclass expansion (``MATCH (p:Person)`` → includes Employee, Manager)
|
|
15
|
+
- Equivalent-property expansion
|
|
16
|
+
- Symmetric / transitive pattern rewriting
|
|
17
|
+
- Automatic ``owl:imports`` resolution (GIST, FIBO, etc.)
|
|
18
|
+
- Well-known ontology registry with caching
|
|
19
|
+
- Cross-ontology alignment via ``owl:equivalentClass`` / ``owl:equivalentProperty``
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
from prograph.ontology.hierarchy import ClassHierarchy
|
|
23
|
+
from prograph.ontology.layer import OntologyLayer
|
|
24
|
+
from prograph.ontology.imports import (
|
|
25
|
+
ImportResolver,
|
|
26
|
+
OntologyCache,
|
|
27
|
+
ImportResolutionError,
|
|
28
|
+
WELL_KNOWN_ONTOLOGIES,
|
|
29
|
+
list_well_known_ontologies,
|
|
30
|
+
get_well_known_ontology_info,
|
|
31
|
+
)
|
|
32
|
+
from prograph.ontology.alignment import (
|
|
33
|
+
AlignmentLayer,
|
|
34
|
+
ClassAlignment,
|
|
35
|
+
PropertyAlignment,
|
|
36
|
+
)
|
|
37
|
+
from prograph.ontology.skos import SKOS_CORE, with_skos
|
|
38
|
+
|
|
39
|
+
__all__ = [
|
|
40
|
+
'ClassHierarchy',
|
|
41
|
+
'OntologyLayer',
|
|
42
|
+
'SKOS_CORE',
|
|
43
|
+
'with_skos',
|
|
44
|
+
'ImportResolver',
|
|
45
|
+
'OntologyCache',
|
|
46
|
+
'ImportResolutionError',
|
|
47
|
+
'WELL_KNOWN_ONTOLOGIES',
|
|
48
|
+
'list_well_known_ontologies',
|
|
49
|
+
'get_well_known_ontology_info',
|
|
50
|
+
'AlignmentLayer',
|
|
51
|
+
'ClassAlignment',
|
|
52
|
+
'PropertyAlignment',
|
|
53
|
+
]
|