socrates-core-api 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 Socrates 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,453 @@
1
+ Metadata-Version: 2.4
2
+ Name: socrates-core-api
3
+ Version: 0.1.0
4
+ Summary: REST API server for Socrates AI with GitHub-ready project generation, export, and publishing capabilities
5
+ Author: Socrates AI Contributors
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/Nireus79/Socrates
8
+ Project-URL: Repository, https://github.com/Nireus79/Socrates
9
+ Project-URL: Bug Tracker, https://github.com/Nireus79/Socrates/issues
10
+ Project-URL: Source Code, https://github.com/Nireus79/Socrates/tree/main/socrates-api
11
+ Keywords: api,rest,fastapi,socrates,ai,tutoring,github,export,git,ci-cd
12
+ Classifier: Development Status :: 5 - Production/Stable
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: License :: OSI Approved :: MIT License
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3.8
17
+ Classifier: Programming Language :: Python :: 3.9
18
+ Classifier: Programming Language :: Python :: 3.10
19
+ Classifier: Programming Language :: Python :: 3.11
20
+ Classifier: Programming Language :: Python :: 3.12
21
+ Classifier: Environment :: Web Environment
22
+ Classifier: Topic :: Education
23
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
24
+ Requires-Python: >=3.8
25
+ Description-Content-Type: text/markdown
26
+ License-File: LICENSE
27
+ Requires-Dist: socratic-core>=0.1.1
28
+ Requires-Dist: socrates-ai>=1.3.0
29
+ Requires-Dist: fastapi>=0.100.0
30
+ Requires-Dist: uvicorn[standard]>=0.23.0
31
+ Requires-Dist: pydantic>=2.0.0
32
+ Requires-Dist: python-dotenv>=1.0.0
33
+ Requires-Dist: python-multipart>=0.0.5
34
+ Provides-Extra: full
35
+ Requires-Dist: socratic-analyzer>=0.1.0; extra == "full"
36
+ Requires-Dist: socratic-knowledge>=0.1.0; extra == "full"
37
+ Provides-Extra: dev
38
+ Requires-Dist: pytest>=7.0; extra == "dev"
39
+ Requires-Dist: pytest-timeout>=2.1.0; extra == "dev"
40
+ Requires-Dist: pytest-asyncio>=0.21.0; extra == "dev"
41
+ Requires-Dist: pytest-cov>=4.0; extra == "dev"
42
+ Requires-Dist: httpx>=0.24.0; extra == "dev"
43
+ Requires-Dist: black>=23.0; extra == "dev"
44
+ Requires-Dist: ruff>=0.1.0; extra == "dev"
45
+ Dynamic: license-file
46
+
47
+ # socrates-api
48
+
49
+ REST API server for the Socrates AI platform. Provides a complete API for programmatic access to Socrates functionality.
50
+
51
+ ## Installation
52
+
53
+ ```bash
54
+ pip install socrates-api
55
+ ```
56
+
57
+ ## Quick Start
58
+
59
+ ### 1. Set Environment Variables
60
+
61
+ ```bash
62
+ export ANTHROPIC_API_KEY="your-api-key-here"
63
+ export SOCRATES_DATA_DIR="~/.socrates" # Optional
64
+ export SOCRATES_API_HOST="0.0.0.0" # Optional, default: 127.0.0.1
65
+ export SOCRATES_API_PORT="8000" # Optional, default: 8000
66
+ ```
67
+
68
+ ### 2. Run the Server
69
+
70
+ ```bash
71
+ socrates-api
72
+ ```
73
+
74
+ Or use Python directly:
75
+
76
+ ```bash
77
+ python -m socrates_api.main
78
+ ```
79
+
80
+ ### 3. Access the API
81
+
82
+ - **Swagger Documentation**: http://localhost:8000/docs
83
+ - **ReDoc Documentation**: http://localhost:8000/redoc
84
+ - **Health Check**: http://localhost:8000/health
85
+
86
+ ## API Endpoints
87
+
88
+ ### System Management
89
+
90
+ - `GET /health` - Health check
91
+ - `POST /initialize` - Initialize API with configuration
92
+ - `GET /info` - Get system information
93
+
94
+ ### Projects
95
+
96
+ - `POST /projects` - Create a new project
97
+ - `GET /projects` - List projects (optionally filtered by owner)
98
+
99
+ ### Socratic Questions
100
+
101
+ - `POST /projects/{project_id}/question` - Get a Socratic question
102
+ - `POST /projects/{project_id}/response` - Process user response
103
+
104
+ ### Code Generation
105
+
106
+ - `POST /code/generate` - Generate code for a project
107
+
108
+ ## Usage Examples
109
+
110
+ ### Initialize the API
111
+
112
+ ```bash
113
+ curl -X POST http://localhost:8000/initialize \
114
+ -H "Content-Type: application/json" \
115
+ -d '{"api_key": "sk-ant-..."}'
116
+ ```
117
+
118
+ ### Create a Project
119
+
120
+ ```bash
121
+ curl -X POST http://localhost:8000/projects \
122
+ -H "Content-Type: application/json" \
123
+ -d '{
124
+ "name": "Python API Development",
125
+ "owner": "alice",
126
+ "description": "Building REST APIs with FastAPI"
127
+ }'
128
+ ```
129
+
130
+ ### List Projects
131
+
132
+ ```bash
133
+ curl http://localhost:8000/projects
134
+ ```
135
+
136
+ ### Ask a Socratic Question
137
+
138
+ ```bash
139
+ curl -X POST http://localhost:8000/projects/proj_abc123/question \
140
+ -H "Content-Type: application/json" \
141
+ -d '{
142
+ "topic": "REST API design",
143
+ "difficulty_level": "intermediate"
144
+ }'
145
+ ```
146
+
147
+ ### Process a Response
148
+
149
+ ```bash
150
+ curl -X POST http://localhost:8000/projects/proj_abc123/response \
151
+ -H "Content-Type: application/json" \
152
+ -d '{
153
+ "question_id": "q_xyz789",
154
+ "user_response": "REST APIs should follow resource-oriented design principles...",
155
+ "project_id": "proj_abc123"
156
+ }'
157
+ ```
158
+
159
+ ### Generate Code
160
+
161
+ ```bash
162
+ curl -X POST http://localhost:8000/code/generate \
163
+ -H "Content-Type: application/json" \
164
+ -d '{
165
+ "project_id": "proj_abc123",
166
+ "specification": "Create a FastAPI endpoint for user registration",
167
+ "language": "python"
168
+ }'
169
+ ```
170
+
171
+ ## Python Client Example
172
+
173
+ ```python
174
+ import requests
175
+ import json
176
+
177
+ BASE_URL = "http://localhost:8000"
178
+
179
+ # Initialize
180
+ resp = requests.post(f"{BASE_URL}/initialize", json={
181
+ "api_key": "sk-ant-..."
182
+ })
183
+ print(resp.json())
184
+
185
+ # Create project
186
+ resp = requests.post(f"{BASE_URL}/projects", json={
187
+ "name": "My Project",
188
+ "owner": "alice",
189
+ "description": "Learning FastAPI"
190
+ })
191
+ project = resp.json()
192
+ project_id = project["project_id"]
193
+
194
+ # Get a question
195
+ resp = requests.post(f"{BASE_URL}/projects/{project_id}/question", json={
196
+ "topic": "FastAPI basics",
197
+ "difficulty_level": "beginner"
198
+ })
199
+ question = resp.json()
200
+ print(f"Question: {question['question']}")
201
+
202
+ # Process response
203
+ resp = requests.post(f"{BASE_URL}/projects/{project_id}/response", json={
204
+ "question_id": question["question_id"],
205
+ "user_response": "FastAPI is a modern Python web framework...",
206
+ "project_id": project_id
207
+ })
208
+ feedback = resp.json()
209
+ print(f"Feedback: {feedback['feedback']}")
210
+ ```
211
+
212
+ ## Async Integration Example
213
+
214
+ The API is built with FastAPI and uses asyncio internally. For high-throughput scenarios:
215
+
216
+ ```python
217
+ import asyncio
218
+ import httpx
219
+
220
+ async def main():
221
+ async with httpx.AsyncClient() as client:
222
+ # Initialize
223
+ resp = await client.post("http://localhost:8000/initialize", json={
224
+ "api_key": "sk-ant-..."
225
+ })
226
+ print(resp.json())
227
+
228
+ # Create multiple projects concurrently
229
+ tasks = [
230
+ client.post("http://localhost:8000/projects", json={
231
+ "name": f"Project {i}",
232
+ "owner": "alice"
233
+ })
234
+ for i in range(5)
235
+ ]
236
+
237
+ results = await asyncio.gather(*tasks)
238
+ for r in results:
239
+ print(r.json())
240
+
241
+ asyncio.run(main())
242
+ ```
243
+
244
+ ## Event Integration
245
+
246
+ The API automatically registers event listeners with the Socrates library. Events are logged and can be monitored via the logging system:
247
+
248
+ ```python
249
+ import logging
250
+
251
+ logging.basicConfig(level=logging.INFO)
252
+ logger = logging.getLogger("socrates_api.main")
253
+
254
+ # API will log events like:
255
+ # [Event] PROJECT_CREATED: {'project_id': 'proj_abc123', ...}
256
+ # [Event] CODE_GENERATED: {'lines': 150, ...}
257
+ # [Event] AGENT_ERROR: {'agent_name': 'code_generator', ...}
258
+ ```
259
+
260
+ ## Configuration
261
+
262
+ The API respects these environment variables:
263
+
264
+ | Variable | Default | Description |
265
+ |----------|---------|-------------|
266
+ | `ANTHROPIC_API_KEY` | None | Claude API key (required) |
267
+ | `SOCRATES_DATA_DIR` | `~/.socrates` | Directory for storing data |
268
+ | `SOCRATES_API_HOST` | `127.0.0.1` | Server host |
269
+ | `SOCRATES_API_PORT` | `8000` | Server port |
270
+ | `SOCRATES_API_RELOAD` | `false` | Enable auto-reload in development |
271
+
272
+ ## Error Handling
273
+
274
+ The API returns structured error responses:
275
+
276
+ ```json
277
+ {
278
+ "error": "ProjectNotFoundError",
279
+ "message": "Project 'proj_abc123' not found",
280
+ "error_code": "PROJECT_NOT_FOUND",
281
+ "details": {"project_id": "proj_abc123"}
282
+ }
283
+ ```
284
+
285
+ All Socrates library errors are caught and returned with appropriate HTTP status codes.
286
+
287
+ ## Deployment
288
+
289
+ ### Using Gunicorn (Production)
290
+
291
+ ```bash
292
+ pip install gunicorn
293
+ gunicorn -w 4 -k uvicorn.workers.UvicornWorker socrates_api.main:app
294
+ ```
295
+
296
+ ### Using Docker
297
+
298
+ ```dockerfile
299
+ FROM python:3.11-slim
300
+
301
+ WORKDIR /app
302
+
303
+ RUN pip install socrates-api
304
+
305
+ ENV SOCRATES_API_HOST=0.0.0.0
306
+
307
+ CMD ["socrates-api"]
308
+ ```
309
+
310
+ ```bash
311
+ docker build -t socrates-api .
312
+ docker run -e ANTHROPIC_API_KEY="sk-ant-..." -p 8000:8000 socrates-api
313
+ ```
314
+
315
+ ### Using Docker Compose
316
+
317
+ ```yaml
318
+ version: '3.8'
319
+
320
+ services:
321
+ socrates-api:
322
+ build: .
323
+ ports:
324
+ - "8000:8000"
325
+ environment:
326
+ ANTHROPIC_API_KEY: ${ANTHROPIC_API_KEY}
327
+ SOCRATES_DATA_DIR: /data
328
+ volumes:
329
+ - socrates_data:/data
330
+
331
+ volumes:
332
+ socrates_data:
333
+ ```
334
+
335
+ ## Development
336
+
337
+ ### Setup
338
+
339
+ ```bash
340
+ git clone https://github.com/Nireus79/Socrates
341
+ cd socrates-api
342
+ pip install -e ".[dev]"
343
+ ```
344
+
345
+ ### Run Tests
346
+
347
+ ```bash
348
+ pytest tests/ -v --cov=socrates_api
349
+ ```
350
+
351
+ ### Run in Development Mode
352
+
353
+ ```bash
354
+ export SOCRATES_API_RELOAD=true
355
+ socrates-api
356
+ ```
357
+
358
+ ## API Response Examples
359
+
360
+ ### Successful Project Creation
361
+
362
+ ```json
363
+ {
364
+ "project_id": "proj_abc123",
365
+ "name": "Python API Development",
366
+ "owner": "alice",
367
+ "description": "Building REST APIs with FastAPI",
368
+ "phase": "active",
369
+ "created_at": "2025-12-04T10:00:00Z",
370
+ "updated_at": "2025-12-04T10:30:00Z",
371
+ "is_archived": false
372
+ }
373
+ ```
374
+
375
+ ### Successful Question Generation
376
+
377
+ ```json
378
+ {
379
+ "question_id": "q_xyz789",
380
+ "question": "What are the main principles of RESTful API design?",
381
+ "context": "You are designing an API for a tutoring system",
382
+ "hints": [
383
+ "Think about resource-oriented design",
384
+ "Consider HTTP methods and status codes"
385
+ ]
386
+ }
387
+ ```
388
+
389
+ ### Successful Code Generation
390
+
391
+ ```json
392
+ {
393
+ "code": "@app.post('/api/users/register')\nasync def register_user(user: User):\n # Implementation here",
394
+ "explanation": "This endpoint handles user registration using FastAPI...",
395
+ "language": "python",
396
+ "token_usage": {
397
+ "input_tokens": 150,
398
+ "output_tokens": 200,
399
+ "total_tokens": 350
400
+ }
401
+ }
402
+ ```
403
+
404
+ ## Architecture
405
+
406
+ The API is built on three layers:
407
+
408
+ 1. **FastAPI Application** (`main.py`) - HTTP request handling, routing, and middleware
409
+ 2. **Pydantic Models** (`models.py`) - Request/response validation and serialization
410
+ 3. **Socrates Library** - Business logic via `socrates` package
411
+
412
+ Event flow:
413
+ ```
414
+ HTTP Request → FastAPI Route → Socrates Library → Event Emission → HTTP Response
415
+
416
+ Event Listeners (Logging)
417
+ ```
418
+
419
+ ## Monitoring
420
+
421
+ The API emits events for all significant operations. Monitor them via logging:
422
+
423
+ ```python
424
+ import logging
425
+ logging.basicConfig(level=logging.INFO)
426
+
427
+ # All events will be logged like:
428
+ # [Event] PROJECT_CREATED: {...}
429
+ # [Event] AGENT_COMPLETE: {...}
430
+ # [Event] TOKEN_USAGE: {...}
431
+ ```
432
+
433
+ Or set up custom event listeners by extending the API:
434
+
435
+ ```python
436
+ def setup_monitoring(orchestrator):
437
+ def on_token_usage(event_type, data):
438
+ # Send to monitoring system
439
+ send_metrics(data)
440
+
441
+ orchestrator.event_emitter.on(socrates.EventType.TOKEN_USAGE, on_token_usage)
442
+ ```
443
+
444
+ ## Support
445
+
446
+ For issues, feature requests, or contributions, visit:
447
+ - GitHub: https://github.com/Nireus79/Socrates
448
+ - Issues: https://github.com/Nireus79/Socrates/issues
449
+ - Documentation: https://socrates-ai.readthedocs.io
450
+
451
+ ## License
452
+
453
+ MIT