agent-semantic-layer 0.0.1__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.
@@ -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)
@@ -0,0 +1,20 @@
1
+ agent_semantic_layer/__init__.py,sha256=dAmc6OUz9LtCLteNfb8T7t4SQ-V8b9NreBnO_K9Mpb8,1383
2
+ agent_semantic_layer/backends/__init__.py,sha256=Vo0iidA8wQUf-DOfkXRm7o3eUjP3afuh11YUi-wMGSo,55
3
+ agent_semantic_layer/backends/sqlalchemy.py,sha256=7F6whlqqFyKvhQ2jMm9WSao29qZ25ORfWAkgs6fiJNI,1326
4
+ agent_semantic_layer/backends/sqlalchemy_compiler.py,sha256=Qz5qdFEy9TiujVbwA9N_Cd5JID7hQGoI0sBelOyTL2M,21141
5
+ agent_semantic_layer/core/__init__.py,sha256=hh4aoco6TUrT6lwSDggKmMlTfRAFt907wB0TxYhsohg,66
6
+ agent_semantic_layer/core/backend.py,sha256=e7fKzWC6gUoCbB8YpUDVtFlSn16hPITxAAf45-OfXRs,737
7
+ agent_semantic_layer/core/context.py,sha256=ZHxZxhUAHNDzfq3TkeMOeZAutbK4NUe9EAQUUB0Hf0g,6777
8
+ agent_semantic_layer/core/errors.py,sha256=9W4pz3duSXUYpVR5_kARA5_VjiKORWwlBjja-hH2fzo,174
9
+ agent_semantic_layer/core/loader.py,sha256=uEuZ68AHBJmKqRT-6NcofFd5D7TJ3RnwhueCahGIx-U,2478
10
+ agent_semantic_layer/core/model.py,sha256=cs5fVwZ2BsEQaXNOO33e--l_QAuTCp5YKu_lkC6AjbA,13300
11
+ agent_semantic_layer/core/prompt.py,sha256=svGsvUYdQkGdxOQFF9hFa-MDIgUV2U70moyxRtRAH4Q,5986
12
+ agent_semantic_layer/core/query.py,sha256=7PSosqvj7cR-FPxoBxZFI97hp2SiPB_CbYUh5YQPYWA,4401
13
+ agent_semantic_layer/integrations/__init__.py,sha256=yC0DWrV2XAkyc-hFISVFojumv0rid_-6SC0BnHBgiSY,40
14
+ agent_semantic_layer/integrations/langchain_middleware.py,sha256=KihhaPpRwsyrgzDbOb8lpz1pn1U5B3PuzCZIj2xDWl8,3293
15
+ agent_semantic_layer/integrations/langchain_tools.py,sha256=gXF-TbiftlYL1pRJW_JRpd7JhPKBMd_R8rpKDezum0U,2261
16
+ agent_semantic_layer/semantic-view.schema.json,sha256=umIoJ5JGyw2nNyp6ihGE5c9mqj56vryYlEJS9Ht4bcs,25955
17
+ agent_semantic_layer-0.0.1.dist-info/licenses/LICENSE,sha256=B5ectCgkACdr2mlAjLcmpevJpsYeDCM2Qiq1w_mH8go,1111
18
+ agent_semantic_layer-0.0.1.dist-info/WHEEL,sha256=3iQx15CyXwGi4dbygedzA4M5T1eqO_SjwdPfUmqt2Cw,81
19
+ agent_semantic_layer-0.0.1.dist-info/METADATA,sha256=p8eSg2LYJu2lE4bYYcmMazYCmT3yQVIwS8npdAgvf_I,6903
20
+ agent_semantic_layer-0.0.1.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: uv 0.12.24
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -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.