gaard-core 0.2.19__tar.gz → 0.2.21__tar.gz

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.
Files changed (62) hide show
  1. {gaard_core-0.2.19 → gaard_core-0.2.21}/PKG-INFO +2 -2
  2. {gaard_core-0.2.19 → gaard_core-0.2.21}/pyproject.toml +2 -2
  3. {gaard_core-0.2.19 → gaard_core-0.2.21}/src/gaard_core/prompt_compiler/result_interpretation_prompt.py +1 -1
  4. {gaard_core-0.2.19 → gaard_core-0.2.21}/src/gaard_core/prompt_compiler/sql_generation_prompt.py +2 -17
  5. {gaard_core-0.2.19 → gaard_core-0.2.21}/src/gaard_core/query_pipeline/models.py +2 -1
  6. {gaard_core-0.2.19 → gaard_core-0.2.21}/src/gaard_core/query_pipeline/pipeline.py +4 -1
  7. {gaard_core-0.2.19 → gaard_core-0.2.21}/src/gaard_core.egg-info/PKG-INFO +2 -2
  8. {gaard_core-0.2.19 → gaard_core-0.2.21}/src/gaard_core.egg-info/requires.txt +1 -1
  9. {gaard_core-0.2.19 → gaard_core-0.2.21}/tests/test_query_pipeline.py +21 -0
  10. {gaard_core-0.2.19 → gaard_core-0.2.21}/tests/test_sql_generation_prompt_compiler.py +0 -19
  11. {gaard_core-0.2.19 → gaard_core-0.2.21}/README.md +0 -0
  12. {gaard_core-0.2.19 → gaard_core-0.2.21}/setup.cfg +0 -0
  13. {gaard_core-0.2.19 → gaard_core-0.2.21}/src/gaard_core/__init__.py +0 -0
  14. {gaard_core-0.2.19 → gaard_core-0.2.21}/src/gaard_core/conversation_context/__init__.py +0 -0
  15. {gaard_core-0.2.19 → gaard_core-0.2.21}/src/gaard_core/conversation_context/llm_classifier.py +0 -0
  16. {gaard_core-0.2.19 → gaard_core-0.2.21}/src/gaard_core/conversation_context/mock_classifier.py +0 -0
  17. {gaard_core-0.2.19 → gaard_core-0.2.21}/src/gaard_core/errors.py +0 -0
  18. {gaard_core-0.2.19 → gaard_core-0.2.21}/src/gaard_core/execution/__init__.py +0 -0
  19. {gaard_core-0.2.19 → gaard_core-0.2.21}/src/gaard_core/execution/mock_executor.py +0 -0
  20. {gaard_core-0.2.19 → gaard_core-0.2.21}/src/gaard_core/json_utils.py +0 -0
  21. {gaard_core-0.2.19 → gaard_core-0.2.21}/src/gaard_core/llm_output.py +0 -0
  22. {gaard_core-0.2.19 → gaard_core-0.2.21}/src/gaard_core/prompt_compiler/__init__.py +0 -0
  23. {gaard_core-0.2.19 → gaard_core-0.2.21}/src/gaard_core/prompt_compiler/conversation_context_prompt.py +0 -0
  24. {gaard_core-0.2.19 → gaard_core-0.2.21}/src/gaard_core/prompt_compiler/intent_classification_prompt.py +0 -0
  25. {gaard_core-0.2.19 → gaard_core-0.2.21}/src/gaard_core/prompt_compiler/models.py +0 -0
  26. {gaard_core-0.2.19 → gaard_core-0.2.21}/src/gaard_core/prompt_compiler/result_classification_prompt.py +0 -0
  27. {gaard_core-0.2.19 → gaard_core-0.2.21}/src/gaard_core/prompt_compiler/schema_formatter.py +0 -0
  28. {gaard_core-0.2.19 → gaard_core-0.2.21}/src/gaard_core/py.typed +0 -0
  29. {gaard_core-0.2.19 → gaard_core-0.2.21}/src/gaard_core/query_intent/__init__.py +0 -0
  30. {gaard_core-0.2.19 → gaard_core-0.2.21}/src/gaard_core/query_intent/llm_classifier.py +0 -0
  31. {gaard_core-0.2.19 → gaard_core-0.2.21}/src/gaard_core/query_intent/mock_classifier.py +0 -0
  32. {gaard_core-0.2.19 → gaard_core-0.2.21}/src/gaard_core/query_pipeline/__init__.py +0 -0
  33. {gaard_core-0.2.19 → gaard_core-0.2.21}/src/gaard_core/query_pipeline/llm_sql_generator.py +0 -0
  34. {gaard_core-0.2.19 → gaard_core-0.2.21}/src/gaard_core/query_pipeline/mock_sql_generator.py +0 -0
  35. {gaard_core-0.2.19 → gaard_core-0.2.21}/src/gaard_core/result_classifier/__init__.py +0 -0
  36. {gaard_core-0.2.19 → gaard_core-0.2.21}/src/gaard_core/result_classifier/llm_classifier.py +0 -0
  37. {gaard_core-0.2.19 → gaard_core-0.2.21}/src/gaard_core/result_classifier/mock_classifier.py +0 -0
  38. {gaard_core-0.2.19 → gaard_core-0.2.21}/src/gaard_core/result_interpreter/__init__.py +0 -0
  39. {gaard_core-0.2.19 → gaard_core-0.2.21}/src/gaard_core/result_interpreter/llm_interpreter.py +0 -0
  40. {gaard_core-0.2.19 → gaard_core-0.2.21}/src/gaard_core/result_interpreter/mock_interpreter.py +0 -0
  41. {gaard_core-0.2.19 → gaard_core-0.2.21}/src/gaard_core/schema/__init__.py +0 -0
  42. {gaard_core-0.2.19 → gaard_core-0.2.21}/src/gaard_core/schema/cache.py +0 -0
  43. {gaard_core-0.2.19 → gaard_core-0.2.21}/src/gaard_core/schema/context.py +0 -0
  44. {gaard_core-0.2.19 → gaard_core-0.2.21}/src/gaard_core/schema/models.py +0 -0
  45. {gaard_core-0.2.19 → gaard_core-0.2.21}/src/gaard_core/sql_validator/__init__.py +0 -0
  46. {gaard_core-0.2.19 → gaard_core-0.2.21}/src/gaard_core/sql_validator/select_only.py +0 -0
  47. {gaard_core-0.2.19 → gaard_core-0.2.21}/src/gaard_core.egg-info/SOURCES.txt +0 -0
  48. {gaard_core-0.2.19 → gaard_core-0.2.21}/src/gaard_core.egg-info/dependency_links.txt +0 -0
  49. {gaard_core-0.2.19 → gaard_core-0.2.21}/src/gaard_core.egg-info/top_level.txt +0 -0
  50. {gaard_core-0.2.19 → gaard_core-0.2.21}/tests/test_conversation_context_classifier.py +0 -0
  51. {gaard_core-0.2.19 → gaard_core-0.2.21}/tests/test_json_utils.py +0 -0
  52. {gaard_core-0.2.19 → gaard_core-0.2.21}/tests/test_llm_output.py +0 -0
  53. {gaard_core-0.2.19 → gaard_core-0.2.21}/tests/test_llm_query_intent_classifier.py +0 -0
  54. {gaard_core-0.2.19 → gaard_core-0.2.21}/tests/test_llm_result_classifier.py +0 -0
  55. {gaard_core-0.2.19 → gaard_core-0.2.21}/tests/test_llm_result_interpreter.py +0 -0
  56. {gaard_core-0.2.19 → gaard_core-0.2.21}/tests/test_llm_sql_generator.py +0 -0
  57. {gaard_core-0.2.19 → gaard_core-0.2.21}/tests/test_result_classification_prompt_compiler.py +0 -0
  58. {gaard_core-0.2.19 → gaard_core-0.2.21}/tests/test_result_interpretation_prompt_compiler.py +0 -0
  59. {gaard_core-0.2.19 → gaard_core-0.2.21}/tests/test_schema_context_cache.py +0 -0
  60. {gaard_core-0.2.19 → gaard_core-0.2.21}/tests/test_schema_context_service.py +0 -0
  61. {gaard_core-0.2.19 → gaard_core-0.2.21}/tests/test_schema_prompt_formatter.py +0 -0
  62. {gaard_core-0.2.19 → gaard_core-0.2.21}/tests/test_sql_validator.py +0 -0
@@ -1,10 +1,10 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: gaard-core
3
- Version: 0.2.19
3
+ Version: 0.2.21
4
4
  Summary: Core GAARD query pipeline, prompt compiler, policies and SQL validation
5
5
  Requires-Python: >=3.11
6
6
  Description-Content-Type: text/markdown
7
- Requires-Dist: gaard-plugin-api<0.3.0,>=0.2.17
7
+ Requires-Dist: gaard-plugin-api<0.3.0,>=0.2.21
8
8
  Requires-Dist: pydantic>=2.7.0
9
9
  Requires-Dist: sqlglot>=25.0.0
10
10
  Provides-Extra: dev
@@ -4,12 +4,12 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "gaard-core"
7
- version = "0.2.19"
7
+ version = "0.2.21"
8
8
  description = "Core GAARD query pipeline, prompt compiler, policies and SQL validation"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.11"
11
11
  dependencies = [
12
- "gaard-plugin-api>=0.2.17,<0.3.0",
12
+ "gaard-plugin-api>=0.2.21,<0.3.0",
13
13
  "pydantic>=2.7.0",
14
14
  "sqlglot>=25.0.0",
15
15
  ]
@@ -35,7 +35,7 @@ class ResultInterpretationPromptCompiler:
35
35
  Your task is to explain SQL query results to the user.
36
36
 
37
37
  Rules:
38
- - Answer in the same language as the user's question.
38
+ - Answer entirely in the same language as the user's question. Infer it only from the question, regardless of the language of SQL, column names, or returned values.
39
39
  - Pay attention to correct user's language grammar and plural forms.
40
40
  - Use only the data provided in the result.
41
41
  - Do not invent facts.
@@ -3,21 +3,6 @@ from gaard_core.prompt_compiler.models import CompiledPrompt, SqlGenerationPromp
3
3
  from gaard_core.prompt_compiler.schema_formatter import SchemaPromptFormatter
4
4
 
5
5
 
6
- def sql_row_limit_instruction(dialect: str, max_rows: int) -> str:
7
- normalized = dialect.strip().lower()
8
- if normalized in {"tsql", "mssql", "sqlserver"}:
9
- syntax = f"SELECT TOP ({max_rows}); never use LIMIT in T-SQL"
10
- elif normalized in {"oracle", "db2"}:
11
- syntax = f"FETCH FIRST {max_rows} ROWS ONLY; never use LIMIT"
12
- elif normalized == "teradata":
13
- syntax = f"SELECT TOP {max_rows}"
14
- elif normalized in {"sqlite", "postgres", "postgresql", "mysql", "duckdb"}:
15
- syntax = f"LIMIT {max_rows}"
16
- else:
17
- syntax = f"the native row-limiting syntax of the {dialect} dialect"
18
- return f"When the query may return many rows, return at most {max_rows} rows using {syntax}."
19
-
20
-
21
6
  class SqlGenerationPromptCompiler:
22
7
  def __init__(self, schema_formatter: SchemaPromptFormatter | None = None) -> None:
23
8
  self.schema_formatter = schema_formatter or SchemaPromptFormatter()
@@ -90,8 +75,8 @@ Query construction rules:
90
75
  3. If the user asks for both a total and a breakdown, prefer one SELECT statement that returns grouped rows or conditional aggregate columns.
91
76
  4. Do not solve one user question by generating multiple separate SELECT statements.
92
77
  5. Prefer explicit column names over SELECT *.
93
- 6. {sql_row_limit_instruction(dialect, max_rows)}
94
- 7. Do not add a row limit to pure aggregate queries that return a single row.
78
+ 6. Add LIMIT {max_rows} when the query may return many rows.
79
+ 7. Do not add LIMIT to pure aggregate queries that return a single row, unless it is already useful for the dialect or safety.
95
80
  8. Use clear aliases for computed expressions.
96
81
  9. When the query uses more than one table, every table must have a short, stable alias.
97
82
  10. When the query uses more than one table, every column reference must be qualified with the correct table alias in SELECT, JOIN, WHERE, GROUP BY, HAVING and ORDER BY.
@@ -1,7 +1,7 @@
1
1
  from enum import StrEnum
2
2
  from typing import Any
3
3
 
4
- from pydantic import BaseModel, Field
4
+ from pydantic import BaseModel, Field, PrivateAttr
5
5
 
6
6
 
7
7
  class OutputClassification(StrEnum):
@@ -56,6 +56,7 @@ class QueryRequest(BaseModel):
56
56
  interpret: bool = True
57
57
  conversation_id: str | None = None
58
58
  context_mode: ContextMode = ContextMode.AUTO
59
+ _original_question: str | None = PrivateAttr(default=None)
59
60
 
60
61
 
61
62
  class GeneratedSql(BaseModel):
@@ -123,9 +123,12 @@ class QueryPipeline:
123
123
  metadata=metadata,
124
124
  )
125
125
 
126
+ answer_request = request
127
+ if request._original_question is not None:
128
+ answer_request = request.model_copy(update={"question": request._original_question})
126
129
  try:
127
130
  answer = self.interpreter.interpret(
128
- request=request,
131
+ request=answer_request,
129
132
  result=result,
130
133
  sql=generated_sql.sql,
131
134
  )
@@ -1,10 +1,10 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: gaard-core
3
- Version: 0.2.19
3
+ Version: 0.2.21
4
4
  Summary: Core GAARD query pipeline, prompt compiler, policies and SQL validation
5
5
  Requires-Python: >=3.11
6
6
  Description-Content-Type: text/markdown
7
- Requires-Dist: gaard-plugin-api<0.3.0,>=0.2.17
7
+ Requires-Dist: gaard-plugin-api<0.3.0,>=0.2.21
8
8
  Requires-Dist: pydantic>=2.7.0
9
9
  Requires-Dist: sqlglot>=25.0.0
10
10
  Provides-Extra: dev
@@ -1,4 +1,4 @@
1
- gaard-plugin-api<0.3.0,>=0.2.17
1
+ gaard-plugin-api<0.3.0,>=0.2.21
2
2
  pydantic>=2.7.0
3
3
  sqlglot>=25.0.0
4
4
 
@@ -98,3 +98,24 @@ def test_query_pipeline_can_return_raw_sql_output_without_interpretation() -> No
98
98
  assert response.metadata["output_classification_mode"] == "none"
99
99
  assert response.metadata["output_classification"] == "unknown"
100
100
  assert response.metadata["raw_sql_output"] is True
101
+
102
+
103
+ def test_interpreter_receives_original_question_after_sql_rewrite() -> None:
104
+ class CheckingGenerator:
105
+ def generate(self, request: QueryRequest) -> GeneratedSql:
106
+ assert request.question == "How many rows?"
107
+ return GeneratedSql(sql="SELECT 1 AS value")
108
+
109
+ class CheckingInterpreter:
110
+ def interpret(self, request: QueryRequest, result: QueryResult, sql: str = "") -> str:
111
+ assert request.question == "Ile jest wierszy?"
112
+ return "Jeden wiersz."
113
+
114
+ request = QueryRequest(question="How many rows?")
115
+ request._original_question = "Ile jest wierszy?"
116
+ pipeline = QueryPipeline(
117
+ sql_generator=CheckingGenerator(),
118
+ executor=StaticExecutor(),
119
+ interpreter=CheckingInterpreter(),
120
+ )
121
+ assert pipeline.handle(request).answer == "Jeden wiersz."
@@ -1,5 +1,3 @@
1
- import pytest
2
-
3
1
  from gaard_core.prompt_compiler.models import SqlGenerationPromptRequest
4
2
  from gaard_core.prompt_compiler.sql_generation_prompt import SqlGenerationPromptCompiler
5
3
  from gaard_core.schema.models import ColumnInfo, DatabaseSchema, TableInfo
@@ -43,20 +41,3 @@ def test_sql_generation_prompt_compiler_builds_prompt_with_rules_schema_and_ques
43
41
  assert compiled.metadata["dialect"] == "sqlite"
44
42
  assert compiled.metadata["max_rows"] == 100
45
43
  assert compiled.metadata["tables_count"] == 1
46
-
47
-
48
- @pytest.mark.parametrize(
49
- ("dialect", "row_limit"),
50
- [("tsql", "TOP (100)"), ("oracle", "FETCH FIRST 100 ROWS ONLY"),
51
- ("db2", "FETCH FIRST 100 ROWS ONLY"), ("postgres", "LIMIT 100")],
52
- )
53
- def test_row_limit_instruction_uses_the_requested_dialect(dialect: str, row_limit: str) -> None:
54
- compiled = SqlGenerationPromptCompiler().compile(
55
- SqlGenerationPromptRequest(
56
- question="What is in abc?", formatted_schema="Table: abc", dialect=dialect,
57
- max_rows=100,
58
- )
59
- )
60
- assert row_limit in compiled.system_prompt
61
- if dialect in {"tsql", "oracle", "db2"}:
62
- assert "LIMIT 100" not in compiled.system_prompt
File without changes
File without changes