agent-semantic-layer 0.0.1__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.
- agent_semantic_layer-0.0.1/LICENSE +21 -0
- agent_semantic_layer-0.0.1/PKG-INFO +201 -0
- agent_semantic_layer-0.0.1/README.zh-CN.md +490 -0
- agent_semantic_layer-0.0.1/README_PYPI.md +176 -0
- agent_semantic_layer-0.0.1/docs/architecture.md +282 -0
- agent_semantic_layer-0.0.1/docs/images/Designer.png +0 -0
- agent_semantic_layer-0.0.1/docs/semantic-view.md +289 -0
- agent_semantic_layer-0.0.1/examples/basic/sales.yml +19 -0
- agent_semantic_layer-0.0.1/examples/basic/sqlalchemy_sqlite.py +48 -0
- agent_semantic_layer-0.0.1/examples/basic/sqlite_backend.py +57 -0
- agent_semantic_layer-0.0.1/examples/enterprise/enterprise_sales.py +187 -0
- agent_semantic_layer-0.0.1/examples/enterprise/enterprise_sales.yml +146 -0
- agent_semantic_layer-0.0.1/pyproject.toml +63 -0
- agent_semantic_layer-0.0.1/pyproject.toml.orig +50 -0
- agent_semantic_layer-0.0.1/scripts/export_schema.py +20 -0
- agent_semantic_layer-0.0.1/skills/semantic-view/SKILL.md +81 -0
- agent_semantic_layer-0.0.1/skills/semantic-view/references/semantic-view-reference.md +152 -0
- agent_semantic_layer-0.0.1/src/agent_semantic_layer/__init__.py +63 -0
- agent_semantic_layer-0.0.1/src/agent_semantic_layer/backends/__init__.py +1 -0
- agent_semantic_layer-0.0.1/src/agent_semantic_layer/backends/sqlalchemy.py +38 -0
- agent_semantic_layer-0.0.1/src/agent_semantic_layer/backends/sqlalchemy_compiler.py +525 -0
- agent_semantic_layer-0.0.1/src/agent_semantic_layer/core/__init__.py +1 -0
- agent_semantic_layer-0.0.1/src/agent_semantic_layer/core/backend.py +23 -0
- agent_semantic_layer-0.0.1/src/agent_semantic_layer/core/context.py +197 -0
- agent_semantic_layer-0.0.1/src/agent_semantic_layer/core/errors.py +5 -0
- agent_semantic_layer-0.0.1/src/agent_semantic_layer/core/loader.py +77 -0
- agent_semantic_layer-0.0.1/src/agent_semantic_layer/core/model.py +347 -0
- agent_semantic_layer-0.0.1/src/agent_semantic_layer/core/prompt.py +153 -0
- agent_semantic_layer-0.0.1/src/agent_semantic_layer/core/query.py +131 -0
- agent_semantic_layer-0.0.1/src/agent_semantic_layer/integrations/__init__.py +1 -0
- agent_semantic_layer-0.0.1/src/agent_semantic_layer/integrations/langchain_middleware.py +85 -0
- agent_semantic_layer-0.0.1/src/agent_semantic_layer/integrations/langchain_tools.py +63 -0
- agent_semantic_layer-0.0.1/src/agent_semantic_layer/semantic-view.schema.json +1054 -0
- agent_semantic_layer-0.0.1/tests/test_schema.py +82 -0
- agent_semantic_layer-0.0.1/tests/test_semantic_layer.py +214 -0
- agent_semantic_layer-0.0.1/tests/test_sqlalchemy_backend.py +349 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 agent-semantic-layer contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: agent-semantic-layer
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: A semantic layer that connects business knowledge to structured, controlled data access for AI agents and applications.
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Requires-Dist: pydantic>=2.7,<3
|
|
8
|
+
Requires-Dist: pyyaml>=6,<7
|
|
9
|
+
Requires-Dist: sqlalchemy[asyncio]>=2.0,<3
|
|
10
|
+
Requires-Dist: langchain>=1.0,<2 ; extra == 'langchain'
|
|
11
|
+
Requires-Dist: pytest>=8,<10 ; extra == 'test'
|
|
12
|
+
Requires-Dist: jsonschema>=4,<5 ; extra == 'test'
|
|
13
|
+
Requires-Dist: aiosqlite>=0.20,<1 ; extra == 'test'
|
|
14
|
+
Requires-Dist: ruff>=0.14,<1 ; extra == 'test'
|
|
15
|
+
Requires-Dist: langchain>=1.0,<2 ; extra == 'test'
|
|
16
|
+
Requires-Python: >=3.10
|
|
17
|
+
Project-URL: Homepage, https://github.com/Carrgan/agent-semantic-layer
|
|
18
|
+
Project-URL: Repository, https://github.com/Carrgan/agent-semantic-layer
|
|
19
|
+
Project-URL: Documentation, https://github.com/Carrgan/agent-semantic-layer#readme
|
|
20
|
+
Project-URL: Issues, https://github.com/Carrgan/agent-semantic-layer/issues
|
|
21
|
+
Project-URL: Changelog, https://github.com/Carrgan/agent-semantic-layer/blob/main/CHANGELOG.md
|
|
22
|
+
Provides-Extra: langchain
|
|
23
|
+
Provides-Extra: test
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
|
|
26
|
+
# Agent Semantic Layer
|
|
27
|
+
|
|
28
|
+
**A semantic layer that connects business knowledge to structured, controlled data access for AI agents and applications.**
|
|
29
|
+
|
|
30
|
+
Agent Semantic Layer is a framework-independent Python library that enables
|
|
31
|
+
applications and AI agents to access structured data through business-defined
|
|
32
|
+
semantics rather than directly interacting with database schemas or generating
|
|
33
|
+
raw SQL. Developers define business concepts, metrics, dimensions, and data
|
|
34
|
+
relationships in a declarative **Semantic View**, while applications and agents
|
|
35
|
+
use structured **Semantic Queries** to request data within the boundaries of
|
|
36
|
+
that view. An extensible query backend resolves these requests against the
|
|
37
|
+
underlying data systems.
|
|
38
|
+
|
|
39
|
+
It provides a shared, version-controlled foundation for business definitions, separating what data means from how it is stored and queried. By establishing a clear boundary between AI agents and underlying data systems, it makes business knowledge reusable across applications and gives developers greater control over how agents discover, request, and access data.
|
|
40
|
+
|
|
41
|
+
## Get started
|
|
42
|
+
|
|
43
|
+
Install it directly into your
|
|
44
|
+
application and follow the steps below to run a local SQLite query.
|
|
45
|
+
|
|
46
|
+
### 1. Install
|
|
47
|
+
|
|
48
|
+
Run this (Or any other way you mangement your packgages) from your application's project root. For a new project,
|
|
49
|
+
run `uv init my-app` and `cd my-app` first:
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
uv add agent-semantic-layer
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
The SQLAlchemy backend also needs a driver for your chosen database. SQLite is
|
|
56
|
+
available through Python's standard library. To use the optional LangChain
|
|
57
|
+
middleware, install `uv add "agent-semantic-layer[langchain]"` and configure a
|
|
58
|
+
chat-model provider separately.
|
|
59
|
+
|
|
60
|
+
### 2. Define a Semantic View
|
|
61
|
+
|
|
62
|
+
Create a `sales.yml` file in your application and define the logical fields and
|
|
63
|
+
their mappings to your database:
|
|
64
|
+
|
|
65
|
+
```yaml
|
|
66
|
+
name: sales
|
|
67
|
+
description: Revenue by region.
|
|
68
|
+
tables:
|
|
69
|
+
- name: orders
|
|
70
|
+
base_table:
|
|
71
|
+
table: orders
|
|
72
|
+
dimensions:
|
|
73
|
+
- name: region
|
|
74
|
+
expr: region
|
|
75
|
+
description: Sales region recorded on each order.
|
|
76
|
+
facts:
|
|
77
|
+
- name: amount
|
|
78
|
+
expr: amount
|
|
79
|
+
metrics:
|
|
80
|
+
- name: revenue
|
|
81
|
+
expr: SUM(amount)
|
|
82
|
+
description: Sum of order amounts.
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Here `region` describes how to group data, `amount` represents a row-level
|
|
86
|
+
value, and `revenue` defines a reusable aggregation. The backend uses the
|
|
87
|
+
trusted SQL expressions in the view to query your physical database.
|
|
88
|
+
|
|
89
|
+
### 3. Run a query locally
|
|
90
|
+
|
|
91
|
+
This example creates an in-memory SQLite table, loads `sales.yml`, executes a
|
|
92
|
+
Semantic Query, and prints the rows. It needs no model provider, external
|
|
93
|
+
database, or credentials:
|
|
94
|
+
|
|
95
|
+
```python
|
|
96
|
+
import asyncio
|
|
97
|
+
|
|
98
|
+
from sqlalchemy import create_engine, text
|
|
99
|
+
from sqlalchemy.pool import StaticPool
|
|
100
|
+
|
|
101
|
+
from agent_semantic_layer import (
|
|
102
|
+
SQLAlchemyQueryBackend,
|
|
103
|
+
SemanticQuery,
|
|
104
|
+
load_semantic_view,
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
async def main():
|
|
109
|
+
engine = create_engine(
|
|
110
|
+
"sqlite://",
|
|
111
|
+
connect_args={"check_same_thread": False},
|
|
112
|
+
poolclass=StaticPool,
|
|
113
|
+
)
|
|
114
|
+
try:
|
|
115
|
+
with engine.begin() as connection:
|
|
116
|
+
connection.execute(text("CREATE TABLE orders (region TEXT, amount REAL)"))
|
|
117
|
+
connection.execute(
|
|
118
|
+
text("INSERT INTO orders VALUES "
|
|
119
|
+
"('North', 10), ('North', 20), ('South', 5)")
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
view = load_semantic_view("sales.yml")
|
|
123
|
+
query = SemanticQuery.model_validate({
|
|
124
|
+
"model": "sales",
|
|
125
|
+
"dimensions": [{"name": "orders.region"}],
|
|
126
|
+
"metrics": [{"name": "orders.revenue"}],
|
|
127
|
+
"order_by": [{"field": "orders.region"}],
|
|
128
|
+
})
|
|
129
|
+
rows = await SQLAlchemyQueryBackend(engine).execute(view, query)
|
|
130
|
+
print(rows)
|
|
131
|
+
finally:
|
|
132
|
+
engine.dispose()
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
asyncio.run(main())
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
Save this as `quickstart.py` next to `sales.yml`, then run
|
|
139
|
+
`uv run python quickstart.py`.
|
|
140
|
+
|
|
141
|
+
Expected output:
|
|
142
|
+
|
|
143
|
+
```text
|
|
144
|
+
[{'region': 'North', 'revenue': 30.0}, {'region': 'South', 'revenue': 5.0}]
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### 4. Connect an agent
|
|
148
|
+
|
|
149
|
+
The following example assumes a local `sales.db` SQLite database containing an
|
|
150
|
+
`orders` table with `region` and `amount` columns. Install `langchain-openai`
|
|
151
|
+
and configure `OPENAI_API_KEY`, or substitute a different tool-capable LangChain
|
|
152
|
+
chat model and its provider package.
|
|
153
|
+
|
|
154
|
+
```python
|
|
155
|
+
import asyncio
|
|
156
|
+
|
|
157
|
+
from langchain.agents import create_agent
|
|
158
|
+
from langchain_openai import ChatOpenAI
|
|
159
|
+
from sqlalchemy import create_engine
|
|
160
|
+
|
|
161
|
+
from agent_semantic_layer import (
|
|
162
|
+
SQLAlchemyQueryBackend,
|
|
163
|
+
load_semantic_view,
|
|
164
|
+
)
|
|
165
|
+
from agent_semantic_layer.integrations.langchain_middleware import (
|
|
166
|
+
SemanticLayerMiddleware,
|
|
167
|
+
)
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
async def main():
|
|
171
|
+
view = load_semantic_view("sales.yml")
|
|
172
|
+
engine = create_engine("sqlite:///sales.db")
|
|
173
|
+
try:
|
|
174
|
+
backend = SQLAlchemyQueryBackend(engine)
|
|
175
|
+
agent = create_agent(
|
|
176
|
+
model=ChatOpenAI(model="gpt-4o-mini"),
|
|
177
|
+
middleware=[SemanticLayerMiddleware(views=[view], backend=backend)],
|
|
178
|
+
)
|
|
179
|
+
result = await agent.ainvoke({
|
|
180
|
+
"messages": [{"role": "user", "content": "What is revenue by region?"}]
|
|
181
|
+
})
|
|
182
|
+
print(result["messages"][-1].content)
|
|
183
|
+
finally:
|
|
184
|
+
engine.dispose()
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
asyncio.run(main())
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
For another database, create an appropriate SQLAlchemy `Engine` or
|
|
191
|
+
`AsyncEngine` and install its driver. SQLAlchemy adapts the SQL it constructs,
|
|
192
|
+
but SQL expressions in your view must still be valid for the target database.
|
|
193
|
+
The application owns the engine and its lifecycle.
|
|
194
|
+
|
|
195
|
+
## Details
|
|
196
|
+
|
|
197
|
+
See [GITHUB](https://github.com/Carrgan/agent-semantic-layer)
|
|
198
|
+
|
|
199
|
+
## License
|
|
200
|
+
|
|
201
|
+
[MIT](LICENSE)
|