contex-python 0.1.0__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.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Contex Team
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,277 @@
1
+ Metadata-Version: 2.4
2
+ Name: contex-python
3
+ Version: 0.1.0
4
+ Summary: Official Python SDK for Contex - Semantic context routing for AI agents
5
+ Author-email: Cahoots <admin@cahoots.cc>
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/cahoots-org/contex
8
+ Project-URL: Repository, https://github.com/cahoots-org/contex
9
+ Project-URL: Issues, https://github.com/cahoots-org/contex/issues
10
+ Keywords: ai,agents,context,semantic-search,machine-learning
11
+ Classifier: Development Status :: 4 - Beta
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: License :: OSI Approved :: MIT License
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.10
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
19
+ Requires-Python: >=3.10
20
+ Description-Content-Type: text/markdown
21
+ License-File: LICENSE
22
+ Requires-Dist: httpx>=0.25.0
23
+ Requires-Dist: pydantic>=2.0.0
24
+ Provides-Extra: dev
25
+ Requires-Dist: pytest>=7.4.0; extra == "dev"
26
+ Requires-Dist: pytest-asyncio>=0.21.0; extra == "dev"
27
+ Requires-Dist: pytest-mock>=3.12.0; extra == "dev"
28
+ Requires-Dist: black>=23.0.0; extra == "dev"
29
+ Requires-Dist: mypy>=1.5.0; extra == "dev"
30
+ Requires-Dist: ruff>=0.1.0; extra == "dev"
31
+ Dynamic: license-file
32
+
33
+ # Contex Python SDK
34
+
35
+ Official Python client for [Contex](https://github.com/cahoots-org/contex) - Semantic context routing for AI agents.
36
+
37
+ ## Installation
38
+
39
+ ```bash
40
+ pip install contex-python
41
+ ```
42
+
43
+ ## Quick Start
44
+
45
+ ### Async Client (Recommended)
46
+
47
+ ```python
48
+ from contex import ContexAsyncClient
49
+
50
+ async def main():
51
+ async with ContexAsyncClient(
52
+ url="http://localhost:8001",
53
+ api_key="ck_your_api_key_here"
54
+ ) as client:
55
+ # Publish data
56
+ await client.publish(
57
+ project_id="my-app",
58
+ data_key="coding_standards",
59
+ data={
60
+ "style": "PEP 8",
61
+ "max_line_length": 100,
62
+ "quotes": "double"
63
+ }
64
+ )
65
+
66
+ # Register agent
67
+ response = await client.register_agent(
68
+ agent_id="code-reviewer",
69
+ project_id="my-app",
70
+ data_needs=[
71
+ "coding standards and style guidelines",
72
+ "testing requirements and coverage goals"
73
+ ]
74
+ )
75
+
76
+ print(f"Matched {len(response.matched_data)} items")
77
+ for match in response.matched_data:
78
+ print(f" {match.data_key}: {match.similarity_score:.2f}")
79
+
80
+ # Query for data
81
+ results = await client.query(
82
+ project_id="my-app",
83
+ query="authentication configuration"
84
+ )
85
+
86
+ for result in results.results:
87
+ print(f"{result.data_key}: {result.data}")
88
+
89
+ import asyncio
90
+ asyncio.run(main())
91
+ ```
92
+
93
+ ### Sync Client
94
+
95
+ ```python
96
+ from contex import ContexClient
97
+
98
+ client = ContexClient(
99
+ url="http://localhost:8001",
100
+ api_key="ck_your_api_key_here"
101
+ )
102
+
103
+ # Publish data
104
+ client.publish(
105
+ project_id="my-app",
106
+ data_key="config",
107
+ data={"env": "prod", "debug": False}
108
+ )
109
+
110
+ # Register agent
111
+ response = client.register_agent(
112
+ agent_id="my-agent",
113
+ project_id="my-app",
114
+ data_needs=["configuration", "secrets"]
115
+ )
116
+ ```
117
+
118
+ ## Features
119
+
120
+ - ✅ **Async & Sync**: Both async and synchronous interfaces
121
+ - ✅ **Type Hints**: Full type annotations with Pydantic models
122
+ - ✅ **Error Handling**: Comprehensive exception hierarchy
123
+ - ✅ **Retry Logic**: Automatic retries with exponential backoff
124
+ - ✅ **Rate Limiting**: Built-in rate limit handling
125
+ - ✅ **Authentication**: API key authentication support
126
+
127
+ ## API Reference
128
+
129
+ ### Client Initialization
130
+
131
+ ```python
132
+ client = ContexAsyncClient(
133
+ url="http://localhost:8001", # Contex server URL
134
+ api_key="ck_...", # API key for authentication
135
+ timeout=30.0, # Request timeout in seconds
136
+ max_retries=3, # Maximum number of retries
137
+ )
138
+ ```
139
+
140
+ ### Publishing Data
141
+
142
+ ```python
143
+ await client.publish(
144
+ project_id="my-app", # Project identifier
145
+ data_key="unique-key", # Unique key for this data
146
+ data={"any": "json"}, # Data payload
147
+ data_format="json", # Format: json, yaml, toml, text
148
+ metadata={"tags": ["prod"]}, # Optional metadata
149
+ )
150
+ ```
151
+
152
+ ### Registering Agents
153
+
154
+ ```python
155
+ response = await client.register_agent(
156
+ agent_id="agent-1", # Unique agent ID
157
+ project_id="my-app", # Project ID
158
+ data_needs=["config", "secrets"], # Data needs (natural language)
159
+ notification_method="redis", # redis or webhook
160
+ webhook_url="https://...", # Optional webhook URL
161
+ webhook_secret="secret", # Optional webhook secret
162
+ last_seen_sequence="0", # Last seen sequence
163
+ )
164
+ ```
165
+
166
+ ### Querying Data
167
+
168
+ ```python
169
+ results = await client.query(
170
+ project_id="my-app",
171
+ query="authentication settings",
172
+ max_results=10,
173
+ )
174
+
175
+ for result in results.results:
176
+ print(f"{result.data_key}: {result.similarity_score}")
177
+ ```
178
+
179
+ ### API Key Management
180
+
181
+ ```python
182
+ # Create API key
183
+ key_response = await client.create_api_key(name="production-key")
184
+ print(f"API Key: {key_response.key}") # Store this securely!
185
+
186
+ # List keys
187
+ keys = await client.list_api_keys()
188
+
189
+ # Revoke key
190
+ await client.revoke_api_key(key_id="key-123")
191
+ ```
192
+
193
+ ### Health Checks
194
+
195
+ ```python
196
+ # Comprehensive health
197
+ health = await client.health()
198
+
199
+ # Readiness check
200
+ ready = await client.ready()
201
+
202
+ # Rate limit status
203
+ rate_limit = await client.rate_limit_status()
204
+ print(f"Remaining: {rate_limit.remaining}/{rate_limit.limit}")
205
+ ```
206
+
207
+ ## Exception Handling
208
+
209
+ ```python
210
+ from contex import (
211
+ ContexError,
212
+ AuthenticationError,
213
+ RateLimitError,
214
+ ValidationError,
215
+ NotFoundError,
216
+ ServerError,
217
+ )
218
+
219
+ try:
220
+ await client.publish(...)
221
+ except AuthenticationError:
222
+ print("Invalid API key")
223
+ except RateLimitError as e:
224
+ print(f"Rate limited. Retry after {e.retry_after} seconds")
225
+ except ValidationError as e:
226
+ print(f"Validation error: {e}")
227
+ except NotFoundError:
228
+ print("Resource not found")
229
+ except ServerError:
230
+ print("Server error")
231
+ except ContexError as e:
232
+ print(f"Contex error: {e}")
233
+ ```
234
+
235
+ ## Development
236
+
237
+ ### Setup
238
+
239
+ ```bash
240
+ cd sdk/python
241
+ pip install -e ".[dev]"
242
+ ```
243
+
244
+ ### Running Tests
245
+
246
+ ```bash
247
+ pytest
248
+ ```
249
+
250
+ ### Code Formatting
251
+
252
+ ```bash
253
+ black contex/
254
+ ruff check contex/
255
+ mypy contex/
256
+ ```
257
+
258
+ ## Examples
259
+
260
+ See the [examples](examples/) directory for more usage examples:
261
+
262
+ - `basic_usage.py` - Basic publish and query
263
+ - `agent_registration.py` - Agent registration and updates
264
+ - `webhook_agent.py` - Webhook-based agent
265
+ - `error_handling.py` - Error handling patterns
266
+ - `batch_operations.py` - Batch publishing
267
+
268
+ ## License
269
+
270
+ MIT License - see [LICENSE](LICENSE) for details.
271
+
272
+ ## Links
273
+
274
+ - [Documentation](https://contex.readthedocs.io)
275
+ - [GitHub](https://github.com/cahoots-org/contex)
276
+ - [PyPI](https://pypi.org/project/contex-python/)
277
+ - [Issues](https://github.com/cahoots-org/contex/issues)
@@ -0,0 +1,245 @@
1
+ # Contex Python SDK
2
+
3
+ Official Python client for [Contex](https://github.com/cahoots-org/contex) - Semantic context routing for AI agents.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pip install contex-python
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ### Async Client (Recommended)
14
+
15
+ ```python
16
+ from contex import ContexAsyncClient
17
+
18
+ async def main():
19
+ async with ContexAsyncClient(
20
+ url="http://localhost:8001",
21
+ api_key="ck_your_api_key_here"
22
+ ) as client:
23
+ # Publish data
24
+ await client.publish(
25
+ project_id="my-app",
26
+ data_key="coding_standards",
27
+ data={
28
+ "style": "PEP 8",
29
+ "max_line_length": 100,
30
+ "quotes": "double"
31
+ }
32
+ )
33
+
34
+ # Register agent
35
+ response = await client.register_agent(
36
+ agent_id="code-reviewer",
37
+ project_id="my-app",
38
+ data_needs=[
39
+ "coding standards and style guidelines",
40
+ "testing requirements and coverage goals"
41
+ ]
42
+ )
43
+
44
+ print(f"Matched {len(response.matched_data)} items")
45
+ for match in response.matched_data:
46
+ print(f" {match.data_key}: {match.similarity_score:.2f}")
47
+
48
+ # Query for data
49
+ results = await client.query(
50
+ project_id="my-app",
51
+ query="authentication configuration"
52
+ )
53
+
54
+ for result in results.results:
55
+ print(f"{result.data_key}: {result.data}")
56
+
57
+ import asyncio
58
+ asyncio.run(main())
59
+ ```
60
+
61
+ ### Sync Client
62
+
63
+ ```python
64
+ from contex import ContexClient
65
+
66
+ client = ContexClient(
67
+ url="http://localhost:8001",
68
+ api_key="ck_your_api_key_here"
69
+ )
70
+
71
+ # Publish data
72
+ client.publish(
73
+ project_id="my-app",
74
+ data_key="config",
75
+ data={"env": "prod", "debug": False}
76
+ )
77
+
78
+ # Register agent
79
+ response = client.register_agent(
80
+ agent_id="my-agent",
81
+ project_id="my-app",
82
+ data_needs=["configuration", "secrets"]
83
+ )
84
+ ```
85
+
86
+ ## Features
87
+
88
+ - ✅ **Async & Sync**: Both async and synchronous interfaces
89
+ - ✅ **Type Hints**: Full type annotations with Pydantic models
90
+ - ✅ **Error Handling**: Comprehensive exception hierarchy
91
+ - ✅ **Retry Logic**: Automatic retries with exponential backoff
92
+ - ✅ **Rate Limiting**: Built-in rate limit handling
93
+ - ✅ **Authentication**: API key authentication support
94
+
95
+ ## API Reference
96
+
97
+ ### Client Initialization
98
+
99
+ ```python
100
+ client = ContexAsyncClient(
101
+ url="http://localhost:8001", # Contex server URL
102
+ api_key="ck_...", # API key for authentication
103
+ timeout=30.0, # Request timeout in seconds
104
+ max_retries=3, # Maximum number of retries
105
+ )
106
+ ```
107
+
108
+ ### Publishing Data
109
+
110
+ ```python
111
+ await client.publish(
112
+ project_id="my-app", # Project identifier
113
+ data_key="unique-key", # Unique key for this data
114
+ data={"any": "json"}, # Data payload
115
+ data_format="json", # Format: json, yaml, toml, text
116
+ metadata={"tags": ["prod"]}, # Optional metadata
117
+ )
118
+ ```
119
+
120
+ ### Registering Agents
121
+
122
+ ```python
123
+ response = await client.register_agent(
124
+ agent_id="agent-1", # Unique agent ID
125
+ project_id="my-app", # Project ID
126
+ data_needs=["config", "secrets"], # Data needs (natural language)
127
+ notification_method="redis", # redis or webhook
128
+ webhook_url="https://...", # Optional webhook URL
129
+ webhook_secret="secret", # Optional webhook secret
130
+ last_seen_sequence="0", # Last seen sequence
131
+ )
132
+ ```
133
+
134
+ ### Querying Data
135
+
136
+ ```python
137
+ results = await client.query(
138
+ project_id="my-app",
139
+ query="authentication settings",
140
+ max_results=10,
141
+ )
142
+
143
+ for result in results.results:
144
+ print(f"{result.data_key}: {result.similarity_score}")
145
+ ```
146
+
147
+ ### API Key Management
148
+
149
+ ```python
150
+ # Create API key
151
+ key_response = await client.create_api_key(name="production-key")
152
+ print(f"API Key: {key_response.key}") # Store this securely!
153
+
154
+ # List keys
155
+ keys = await client.list_api_keys()
156
+
157
+ # Revoke key
158
+ await client.revoke_api_key(key_id="key-123")
159
+ ```
160
+
161
+ ### Health Checks
162
+
163
+ ```python
164
+ # Comprehensive health
165
+ health = await client.health()
166
+
167
+ # Readiness check
168
+ ready = await client.ready()
169
+
170
+ # Rate limit status
171
+ rate_limit = await client.rate_limit_status()
172
+ print(f"Remaining: {rate_limit.remaining}/{rate_limit.limit}")
173
+ ```
174
+
175
+ ## Exception Handling
176
+
177
+ ```python
178
+ from contex import (
179
+ ContexError,
180
+ AuthenticationError,
181
+ RateLimitError,
182
+ ValidationError,
183
+ NotFoundError,
184
+ ServerError,
185
+ )
186
+
187
+ try:
188
+ await client.publish(...)
189
+ except AuthenticationError:
190
+ print("Invalid API key")
191
+ except RateLimitError as e:
192
+ print(f"Rate limited. Retry after {e.retry_after} seconds")
193
+ except ValidationError as e:
194
+ print(f"Validation error: {e}")
195
+ except NotFoundError:
196
+ print("Resource not found")
197
+ except ServerError:
198
+ print("Server error")
199
+ except ContexError as e:
200
+ print(f"Contex error: {e}")
201
+ ```
202
+
203
+ ## Development
204
+
205
+ ### Setup
206
+
207
+ ```bash
208
+ cd sdk/python
209
+ pip install -e ".[dev]"
210
+ ```
211
+
212
+ ### Running Tests
213
+
214
+ ```bash
215
+ pytest
216
+ ```
217
+
218
+ ### Code Formatting
219
+
220
+ ```bash
221
+ black contex/
222
+ ruff check contex/
223
+ mypy contex/
224
+ ```
225
+
226
+ ## Examples
227
+
228
+ See the [examples](examples/) directory for more usage examples:
229
+
230
+ - `basic_usage.py` - Basic publish and query
231
+ - `agent_registration.py` - Agent registration and updates
232
+ - `webhook_agent.py` - Webhook-based agent
233
+ - `error_handling.py` - Error handling patterns
234
+ - `batch_operations.py` - Batch publishing
235
+
236
+ ## License
237
+
238
+ MIT License - see [LICENSE](LICENSE) for details.
239
+
240
+ ## Links
241
+
242
+ - [Documentation](https://contex.readthedocs.io)
243
+ - [GitHub](https://github.com/cahoots-org/contex)
244
+ - [PyPI](https://pypi.org/project/contex-python/)
245
+ - [Issues](https://github.com/cahoots-org/contex/issues)
@@ -0,0 +1,49 @@
1
+ """
2
+ Contex Python SDK
3
+ ~~~~~~~~~~~~~~~~~
4
+
5
+ Official Python client for Contex - Semantic context routing for AI agents.
6
+
7
+ Basic usage:
8
+
9
+ >>> from contex import ContexClient
10
+ >>> client = ContexClient(url="http://localhost:8001", api_key="ck_...")
11
+ >>> await client.publish(project_id="my-app", data_key="config", data={"env": "prod"})
12
+
13
+ Full documentation: https://github.com/cahoots-org/contex
14
+ """
15
+
16
+ __version__ = "0.2.0"
17
+ __author__ = "Contex Team"
18
+ __license__ = "MIT"
19
+
20
+ from .client import ContexClient, ContexAsyncClient
21
+ from .models import (
22
+ DataEvent,
23
+ AgentRegistration,
24
+ RegistrationResponse,
25
+ MatchedData,
26
+ )
27
+ from .exceptions import (
28
+ ContexError,
29
+ AuthenticationError,
30
+ RateLimitError,
31
+ ValidationError,
32
+ NotFoundError,
33
+ ServerError,
34
+ )
35
+
36
+ __all__ = [
37
+ "ContexClient",
38
+ "ContexAsyncClient",
39
+ "DataEvent",
40
+ "AgentRegistration",
41
+ "RegistrationResponse",
42
+ "MatchedData",
43
+ "ContexError",
44
+ "AuthenticationError",
45
+ "RateLimitError",
46
+ "ValidationError",
47
+ "NotFoundError",
48
+ "ServerError",
49
+ ]