Spaces:
No application file
No application file
Delete neuro-router-2/neuro_router
Browse files- neuro-router-2/neuro_router/.dockerignore +0 -10
- neuro-router-2/neuro_router/.env.example +0 -15
- neuro-router-2/neuro_router/.gitignore +0 -44
- neuro-router-2/neuro_router/DockerFile +0 -20
- neuro-router-2/neuro_router/HOW_TO_RUN.txt +0 -105
- neuro-router-2/neuro_router/README.md +0 -331
- neuro-router-2/neuro_router/app/__init__.py +0 -1
- neuro-router-2/neuro_router/app/auth.py +0 -57
- neuro-router-2/neuro_router/app/config.py +0 -33
- neuro-router-2/neuro_router/app/main.py +0 -44
- neuro-router-2/neuro_router/app/proxy.py +0 -79
- neuro-router-2/neuro_router/app/routers/__init__.py +0 -1
- neuro-router-2/neuro_router/app/routers/openai_proxy.py +0 -95
- neuro-router-2/neuro_router/client_example.py +0 -50
- neuro-router-2/neuro_router/quick_test.py +0 -56
- neuro-router-2/neuro_router/requirements.txt +0 -8
- neuro-router-2/neuro_router/run.py +0 -12
- neuro-router-2/neuro_router/space.yaml +0 -3
- neuro-router-2/neuro_router/test_hf_endpoint.py +0 -134
- neuro-router-2/neuro_router/test_proxy.py +0 -138
neuro-router-2/neuro_router/.dockerignore
DELETED
|
@@ -1,10 +0,0 @@
|
|
| 1 |
-
__pycache__/
|
| 2 |
-
*.pyc
|
| 3 |
-
*.pyo
|
| 4 |
-
*.log
|
| 5 |
-
.env
|
| 6 |
-
venv/
|
| 7 |
-
.git/
|
| 8 |
-
node_modules/
|
| 9 |
-
dist/
|
| 10 |
-
build/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
neuro-router-2/neuro_router/.env.example
DELETED
|
@@ -1,15 +0,0 @@
|
|
| 1 |
-
# Your Groq API key from console.groq.com
|
| 2 |
-
GROQ_API_KEY=gsk_your_groq_api_key_here
|
| 3 |
-
|
| 4 |
-
# Groq base URL (OpenAI-compatible endpoint)
|
| 5 |
-
GROQ_BASE_URL=https://api.groq.com/openai/v1
|
| 6 |
-
|
| 7 |
-
# Your custom API keys for clients (neurostack_XXXXXXXXXXXXX format)
|
| 8 |
-
# Add multiple keys separated by comma
|
| 9 |
-
VALID_API_KEYS=neurostack_a1b2c3d4e5f6g,neurostack_h7i8j9k0l1m2n
|
| 10 |
-
|
| 11 |
-
# Model to use (llama-3.3-70b-versatile is Maverick)
|
| 12 |
-
DEFAULT_MODEL=llama-3.3-70b-versatile
|
| 13 |
-
|
| 14 |
-
# CORS settings
|
| 15 |
-
CORS_ALLOW_ORIGINS=*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
neuro-router-2/neuro_router/.gitignore
DELETED
|
@@ -1,44 +0,0 @@
|
|
| 1 |
-
# Python
|
| 2 |
-
__pycache__/
|
| 3 |
-
*.py[cod]
|
| 4 |
-
*$py.class
|
| 5 |
-
*.so
|
| 6 |
-
.Python
|
| 7 |
-
build/
|
| 8 |
-
develop-eggs/
|
| 9 |
-
dist/
|
| 10 |
-
downloads/
|
| 11 |
-
eggs/
|
| 12 |
-
.eggs/
|
| 13 |
-
lib/
|
| 14 |
-
lib64/
|
| 15 |
-
parts/
|
| 16 |
-
sdist/
|
| 17 |
-
var/
|
| 18 |
-
wheels/
|
| 19 |
-
*.egg-info/
|
| 20 |
-
.installed.cfg
|
| 21 |
-
*.egg
|
| 22 |
-
|
| 23 |
-
# Virtual Environment
|
| 24 |
-
venv/
|
| 25 |
-
env/
|
| 26 |
-
ENV/
|
| 27 |
-
.venv
|
| 28 |
-
|
| 29 |
-
# Environment variables
|
| 30 |
-
.env
|
| 31 |
-
|
| 32 |
-
# IDE
|
| 33 |
-
.vscode/
|
| 34 |
-
.idea/
|
| 35 |
-
*.swp
|
| 36 |
-
*.swo
|
| 37 |
-
*~
|
| 38 |
-
|
| 39 |
-
# OS
|
| 40 |
-
.DS_Store
|
| 41 |
-
Thumbs.db
|
| 42 |
-
|
| 43 |
-
# Logs
|
| 44 |
-
*.log
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
neuro-router-2/neuro_router/DockerFile
DELETED
|
@@ -1,20 +0,0 @@
|
|
| 1 |
-
FROM python:3.11-slim
|
| 2 |
-
|
| 3 |
-
# (optional) build tools for some pip packages
|
| 4 |
-
RUN apt-get update && apt-get install -y --no-install-recommends build-essential && rm -rf /var/lib/apt/lists/*
|
| 5 |
-
|
| 6 |
-
WORKDIR /app
|
| 7 |
-
|
| 8 |
-
# copy deps first for layer caching
|
| 9 |
-
COPY requirements.txt .
|
| 10 |
-
|
| 11 |
-
RUN pip install --no-cache-dir -r requirements.txt
|
| 12 |
-
|
| 13 |
-
# copy the rest of your code
|
| 14 |
-
COPY . .
|
| 15 |
-
|
| 16 |
-
# app listens on 7860 (change if yours uses a different port)
|
| 17 |
-
EXPOSE 7860
|
| 18 |
-
|
| 19 |
-
# start the app; keep this if you have run.py in project root
|
| 20 |
-
CMD ["python", "run.py"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
neuro-router-2/neuro_router/HOW_TO_RUN.txt
DELETED
|
@@ -1,105 +0,0 @@
|
|
| 1 |
-
========================================
|
| 2 |
-
HOW TO RUN NEUROSTACK PROXY
|
| 3 |
-
========================================
|
| 4 |
-
|
| 5 |
-
You need TWO terminals open at the same time:
|
| 6 |
-
|
| 7 |
-
TERMINAL 1 (Server):
|
| 8 |
-
--------------------
|
| 9 |
-
1. Open CMD or PowerShell
|
| 10 |
-
2. Navigate to project:
|
| 11 |
-
cd D:\Clients\neuro_router
|
| 12 |
-
|
| 13 |
-
3. Activate conda:
|
| 14 |
-
conda activate mcc-project
|
| 15 |
-
|
| 16 |
-
4. Start the server:
|
| 17 |
-
python run.py
|
| 18 |
-
|
| 19 |
-
5. You should see:
|
| 20 |
-
INFO: Uvicorn running on http://0.0.0.0:7860
|
| 21 |
-
INFO: Application startup complete.
|
| 22 |
-
|
| 23 |
-
6. KEEP THIS TERMINAL OPEN! Don't close it.
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
TERMINAL 2 (Client/Testing):
|
| 27 |
-
-----------------------------
|
| 28 |
-
1. Open a NEW CMD or PowerShell window
|
| 29 |
-
2. Navigate to project:
|
| 30 |
-
cd D:\Clients\neuro_router
|
| 31 |
-
|
| 32 |
-
3. Activate conda:
|
| 33 |
-
conda activate mcc-project
|
| 34 |
-
|
| 35 |
-
4. Run quick test:
|
| 36 |
-
python quick_test.py
|
| 37 |
-
|
| 38 |
-
OR run the client example:
|
| 39 |
-
python client_example.py
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
TROUBLESHOOTING:
|
| 43 |
-
----------------
|
| 44 |
-
Q: Nothing happens when I run client_example.py?
|
| 45 |
-
A: Make sure Terminal 1 is running the server!
|
| 46 |
-
|
| 47 |
-
Q: I get "Connection refused" error?
|
| 48 |
-
A: Server is not running. Start it in Terminal 1.
|
| 49 |
-
|
| 50 |
-
Q: Server starts but no response from Groq?
|
| 51 |
-
A: Check your Groq API key in .env file is valid.
|
| 52 |
-
|
| 53 |
-
Q: How do I know server is working?
|
| 54 |
-
A: Visit http://localhost:7860 in your browser.
|
| 55 |
-
You should see JSON response with service info.
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
TESTING IN BROWSER:
|
| 59 |
-
-------------------
|
| 60 |
-
While server is running, open browser:
|
| 61 |
-
http://localhost:7860
|
| 62 |
-
|
| 63 |
-
You should see:
|
| 64 |
-
{
|
| 65 |
-
"service": "NeuroStack OpenAI Proxy",
|
| 66 |
-
"status": "online",
|
| 67 |
-
"endpoints": {...}
|
| 68 |
-
}
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
YOUR API ENDPOINTS:
|
| 72 |
-
-------------------
|
| 73 |
-
Base URL: http://localhost:7860/v1
|
| 74 |
-
|
| 75 |
-
Available:
|
| 76 |
-
- POST /v1/chat/completions
|
| 77 |
-
- POST /v1/completions
|
| 78 |
-
- GET /v1/models
|
| 79 |
-
|
| 80 |
-
API Key: neurostack_a1b2c3d4e5f6g
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
TEST WITH CURL (while server running):
|
| 84 |
-
---------------------------------------
|
| 85 |
-
curl -X POST http://localhost:7860/v1/chat/completions ^
|
| 86 |
-
-H "Authorization: Bearer neurostack_a1b2c3d4e5f6g" ^
|
| 87 |
-
-H "Content-Type: application/json" ^
|
| 88 |
-
-d "{\"model\":\"gpt-4\",\"messages\":[{\"role\":\"user\",\"content\":\"Hi\"}],\"max_tokens\":20}"
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
CHECKING GROQ DASHBOARD:
|
| 92 |
-
-------------------------
|
| 93 |
-
1. Make sure server is running (Terminal 1)
|
| 94 |
-
2. Make request from Terminal 2
|
| 95 |
-
3. Check Groq dashboard: https://console.groq.com
|
| 96 |
-
4. You should see API calls appear there
|
| 97 |
-
(May take a minute to show up)
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
NEED HELP?
|
| 101 |
-
----------
|
| 102 |
-
1. Make sure .env file has valid GROQ_API_KEY
|
| 103 |
-
2. Check both terminals are in correct conda env
|
| 104 |
-
3. Ensure port 7860 is not used by another app
|
| 105 |
-
4. Try restarting the server
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
neuro-router-2/neuro_router/README.md
DELETED
|
@@ -1,331 +0,0 @@
|
|
| 1 |
-
# NeuroStack OpenAI Proxy
|
| 2 |
-
|
| 3 |
-
A FastAPI-based proxy server that provides OpenAI-compatible API endpoints while routing all requests to Groq's Llama Maverick (llama-3.3-70b-versatile) model under the hood.
|
| 4 |
-
|
| 5 |
-
## 🎯 What This Does
|
| 6 |
-
|
| 7 |
-
- **Accepts**: OpenAI-compatible API requests (any model name: gpt-4, gpt-4o-mini, etc.)
|
| 8 |
-
- **Processes**: Custom API key authentication (neurostack_XXXXXXXXXXXXX format)
|
| 9 |
-
- **Routes**: All requests to Groq's Llama Maverick model
|
| 10 |
-
- **Returns**: Responses in OpenAI-compatible format
|
| 11 |
-
|
| 12 |
-
Your clients use familiar OpenAI SDK and endpoints, but you control the backend model and costs!
|
| 13 |
-
|
| 14 |
-
## 🔑 API Key Format
|
| 15 |
-
|
| 16 |
-
- Format: `neurostack_` followed by exactly 13 alphanumeric characters
|
| 17 |
-
- Example: `neurostack_a1b2c3d4e5f6g`
|
| 18 |
-
|
| 19 |
-
## 🚀 Quick Start
|
| 20 |
-
|
| 21 |
-
### Prerequisites
|
| 22 |
-
```bash
|
| 23 |
-
# Conda environment (or Python 3.11+)
|
| 24 |
-
conda create -n mcc-project python=3.11
|
| 25 |
-
conda activate mcc-project
|
| 26 |
-
```
|
| 27 |
-
|
| 28 |
-
### Installation
|
| 29 |
-
|
| 30 |
-
1. **Clone/Copy the project**
|
| 31 |
-
```bash
|
| 32 |
-
cd D:\Clients\neuro_router
|
| 33 |
-
```
|
| 34 |
-
|
| 35 |
-
2. **Install dependencies**
|
| 36 |
-
```bash
|
| 37 |
-
pip install -r requirements.txt
|
| 38 |
-
```
|
| 39 |
-
|
| 40 |
-
3. **Configure environment**
|
| 41 |
-
```bash
|
| 42 |
-
# Copy .env.example to .env
|
| 43 |
-
copy .env.example .env
|
| 44 |
-
|
| 45 |
-
# Edit .env and add your keys:
|
| 46 |
-
# GROQ_API_KEY=gsk_your_groq_api_key_here
|
| 47 |
-
# VALID_API_KEYS=neurostack_a1b2c3d4e5f6g,neurostack_h7i8j9k0l1m2n
|
| 48 |
-
```
|
| 49 |
-
|
| 50 |
-
4. **Run the server**
|
| 51 |
-
```bash
|
| 52 |
-
python run.py
|
| 53 |
-
```
|
| 54 |
-
|
| 55 |
-
Server runs on: `http://localhost:7860`
|
| 56 |
-
|
| 57 |
-
## 📡 API Endpoints
|
| 58 |
-
|
| 59 |
-
### Base URL
|
| 60 |
-
```
|
| 61 |
-
http://localhost:7860/v1
|
| 62 |
-
```
|
| 63 |
-
|
| 64 |
-
### Available Endpoints
|
| 65 |
-
|
| 66 |
-
| Endpoint | Method | Description |
|
| 67 |
-
|----------|--------|-------------|
|
| 68 |
-
| `/v1/chat/completions` | POST | Chat completions (OpenAI compatible) |
|
| 69 |
-
| `/v1/completions` | POST | Text completions (OpenAI compatible) |
|
| 70 |
-
| `/v1/models` | GET | List available models |
|
| 71 |
-
| `/healthz` | GET | Health check |
|
| 72 |
-
| `/` | GET | Service info |
|
| 73 |
-
|
| 74 |
-
## 🔐 Authentication
|
| 75 |
-
|
| 76 |
-
All requests require Bearer token authentication:
|
| 77 |
-
|
| 78 |
-
```bash
|
| 79 |
-
Authorization: Bearer neurostack_XXXXXXXXXXXXX
|
| 80 |
-
```
|
| 81 |
-
|
| 82 |
-
## 💻 Usage Examples
|
| 83 |
-
|
| 84 |
-
### Python (OpenAI SDK)
|
| 85 |
-
|
| 86 |
-
```python
|
| 87 |
-
from openai import OpenAI
|
| 88 |
-
|
| 89 |
-
client = OpenAI(
|
| 90 |
-
base_url="http://localhost:7860/v1",
|
| 91 |
-
api_key="neurostack_a1b2c3d4e5f6g"
|
| 92 |
-
)
|
| 93 |
-
|
| 94 |
-
# Simple chat
|
| 95 |
-
response = client.chat.completions.create(
|
| 96 |
-
model="gpt-4o-mini", # Any model name works!
|
| 97 |
-
messages=[
|
| 98 |
-
{"role": "user", "content": "Hello!"}
|
| 99 |
-
]
|
| 100 |
-
)
|
| 101 |
-
print(response.choices[0].message.content)
|
| 102 |
-
|
| 103 |
-
# Streaming
|
| 104 |
-
stream = client.chat.completions.create(
|
| 105 |
-
model="gpt-4o-mini",
|
| 106 |
-
messages=[{"role": "user", "content": "Count to 5"}],
|
| 107 |
-
stream=True
|
| 108 |
-
)
|
| 109 |
-
for chunk in stream:
|
| 110 |
-
if chunk.choices[0].delta.content:
|
| 111 |
-
print(chunk.choices[0].delta.content, end="")
|
| 112 |
-
```
|
| 113 |
-
|
| 114 |
-
### cURL
|
| 115 |
-
|
| 116 |
-
```bash
|
| 117 |
-
curl -X POST http://localhost:7860/v1/chat/completions \
|
| 118 |
-
-H "Authorization: Bearer neurostack_a1b2c3d4e5f6g" \
|
| 119 |
-
-H "Content-Type: application/json" \
|
| 120 |
-
-d '{
|
| 121 |
-
"model": "gpt-4",
|
| 122 |
-
"messages": [{"role": "user", "content": "Hi"}],
|
| 123 |
-
"max_tokens": 50
|
| 124 |
-
}'
|
| 125 |
-
```
|
| 126 |
-
|
| 127 |
-
### JavaScript/TypeScript
|
| 128 |
-
|
| 129 |
-
```javascript
|
| 130 |
-
const OpenAI = require('openai');
|
| 131 |
-
|
| 132 |
-
const client = new OpenAI({
|
| 133 |
-
baseURL: 'http://localhost:7860/v1',
|
| 134 |
-
apiKey: 'neurostack_a1b2c3d4e5f6g'
|
| 135 |
-
});
|
| 136 |
-
|
| 137 |
-
async function main() {
|
| 138 |
-
const response = await client.chat.completions.create({
|
| 139 |
-
model: 'gpt-4o-mini',
|
| 140 |
-
messages: [{ role: 'user', content: 'Hello!' }]
|
| 141 |
-
});
|
| 142 |
-
console.log(response.choices[0].message.content);
|
| 143 |
-
}
|
| 144 |
-
|
| 145 |
-
main();
|
| 146 |
-
```
|
| 147 |
-
|
| 148 |
-
## 🧪 Testing
|
| 149 |
-
|
| 150 |
-
### Quick Test
|
| 151 |
-
```bash
|
| 152 |
-
conda activate mcc-project
|
| 153 |
-
python quick_test.py
|
| 154 |
-
```
|
| 155 |
-
|
| 156 |
-
### Full Test Suite
|
| 157 |
-
```bash
|
| 158 |
-
python test_proxy.py
|
| 159 |
-
```
|
| 160 |
-
|
| 161 |
-
### Client Examples
|
| 162 |
-
```bash
|
| 163 |
-
python client_example.py
|
| 164 |
-
```
|
| 165 |
-
|
| 166 |
-
## 🏗️ Project Structure
|
| 167 |
-
|
| 168 |
-
```
|
| 169 |
-
neuro_router/
|
| 170 |
-
├── app/
|
| 171 |
-
│ ├── __init__.py
|
| 172 |
-
│ ├── config.py # Configuration & settings
|
| 173 |
-
│ ├── auth.py # API key validation
|
| 174 |
-
│ ├── proxy.py # Groq forwarding logic
|
| 175 |
-
│ ├── main.py # FastAPI application
|
| 176 |
-
│ └── routers/
|
| 177 |
-
│ ├── __init__.py
|
| 178 |
-
│ └── openai_proxy.py # OpenAI-compatible endpoints
|
| 179 |
-
├── requirements.txt # Python dependencies
|
| 180 |
-
├── .env # Environment variables (DO NOT COMMIT)
|
| 181 |
-
├── .env.example # Environment template
|
| 182 |
-
├── run.py # Server startup script
|
| 183 |
-
├── test_proxy.py # Test suite
|
| 184 |
-
├── client_example.py # Usage examples
|
| 185 |
-
├── quick_test.py # Quick verification
|
| 186 |
-
└── README.md # This file
|
| 187 |
-
```
|
| 188 |
-
|
| 189 |
-
## ⚙️ Configuration
|
| 190 |
-
|
| 191 |
-
Environment variables (`.env` file):
|
| 192 |
-
|
| 193 |
-
```env
|
| 194 |
-
# Required: Your Groq API key
|
| 195 |
-
GROQ_API_KEY=gsk_your_groq_api_key_here
|
| 196 |
-
|
| 197 |
-
# Groq base URL (OpenAI-compatible endpoint)
|
| 198 |
-
GROQ_BASE_URL=https://api.groq.com/openai/v1
|
| 199 |
-
|
| 200 |
-
# Your custom API keys (comma-separated)
|
| 201 |
-
VALID_API_KEYS=neurostack_a1b2c3d4e5f6g,neurostack_h7i8j9k0l1m2n
|
| 202 |
-
|
| 203 |
-
# Model to use (Llama Maverick)
|
| 204 |
-
DEFAULT_MODEL=llama-3.3-70b-versatile
|
| 205 |
-
|
| 206 |
-
# CORS settings
|
| 207 |
-
CORS_ALLOW_ORIGINS=*
|
| 208 |
-
```
|
| 209 |
-
|
| 210 |
-
## 🔄 How It Works
|
| 211 |
-
|
| 212 |
-
1. **Client sends request** → with any OpenAI model name
|
| 213 |
-
2. **Proxy authenticates** → validates neurostack API key
|
| 214 |
-
3. **Model conversion** → changes to `llama-3.3-70b-versatile`
|
| 215 |
-
4. **Forward to Groq** → sends request to Groq Cloud
|
| 216 |
-
5. **Return response** → sends back in OpenAI format
|
| 217 |
-
|
| 218 |
-
The client never knows they're using Groq - it's completely transparent!
|
| 219 |
-
|
| 220 |
-
## 🌐 Deployment
|
| 221 |
-
|
| 222 |
-
### Hugging Face Spaces
|
| 223 |
-
|
| 224 |
-
1. Create new Space (Docker SDK)
|
| 225 |
-
2. Add files to Space repo
|
| 226 |
-
3. Create `Dockerfile`:
|
| 227 |
-
|
| 228 |
-
```dockerfile
|
| 229 |
-
FROM python:3.11-slim
|
| 230 |
-
|
| 231 |
-
WORKDIR /app
|
| 232 |
-
|
| 233 |
-
COPY requirements.txt .
|
| 234 |
-
RUN pip install --no-cache-dir -r requirements.txt
|
| 235 |
-
|
| 236 |
-
COPY app ./app
|
| 237 |
-
COPY run.py .
|
| 238 |
-
|
| 239 |
-
EXPOSE 7860
|
| 240 |
-
|
| 241 |
-
CMD ["python", "run.py"]
|
| 242 |
-
```
|
| 243 |
-
|
| 244 |
-
4. Add secrets in Space settings:
|
| 245 |
-
- `GROQ_API_KEY`
|
| 246 |
-
- `VALID_API_KEYS`
|
| 247 |
-
- `DEFAULT_MODEL`
|
| 248 |
-
|
| 249 |
-
5. Push and deploy!
|
| 250 |
-
|
| 251 |
-
### Production Considerations
|
| 252 |
-
|
| 253 |
-
- Use environment variables for all secrets
|
| 254 |
-
- Enable HTTPS/TLS in production
|
| 255 |
-
- Implement rate limiting
|
| 256 |
-
- Add request logging
|
| 257 |
-
- Set up monitoring
|
| 258 |
-
- Lock down CORS (`CORS_ALLOW_ORIGINS`)
|
| 259 |
-
|
| 260 |
-
## 🛠️ Development
|
| 261 |
-
|
| 262 |
-
### Run in Development Mode
|
| 263 |
-
```bash
|
| 264 |
-
conda activate mcc-project
|
| 265 |
-
python run.py
|
| 266 |
-
```
|
| 267 |
-
|
| 268 |
-
Server auto-reloads on code changes.
|
| 269 |
-
|
| 270 |
-
### Adding New API Keys
|
| 271 |
-
|
| 272 |
-
Edit `.env`:
|
| 273 |
-
```env
|
| 274 |
-
VALID_API_KEYS=neurostack_key1,neurostack_key2,neurostack_key3
|
| 275 |
-
```
|
| 276 |
-
|
| 277 |
-
Restart server to apply changes.
|
| 278 |
-
|
| 279 |
-
## 📊 Monitoring
|
| 280 |
-
|
| 281 |
-
- Check Groq dashboard: https://console.groq.com
|
| 282 |
-
- Server logs show all requests
|
| 283 |
-
- Health endpoint: `http://localhost:7860/healthz`
|
| 284 |
-
|
| 285 |
-
## ❓ Troubleshooting
|
| 286 |
-
|
| 287 |
-
### Server won't start
|
| 288 |
-
- Check `.env` file exists with valid `GROQ_API_KEY`
|
| 289 |
-
- Ensure port 7860 is not in use
|
| 290 |
-
- Verify conda environment is activated
|
| 291 |
-
|
| 292 |
-
### No response from API
|
| 293 |
-
- Ensure server is running in separate terminal
|
| 294 |
-
- Check API key format is correct
|
| 295 |
-
- Verify Groq API key is valid
|
| 296 |
-
|
| 297 |
-
### Groq dashboard shows 0 calls
|
| 298 |
-
- Dashboard may have delay (2-3 minutes)
|
| 299 |
-
- Check date filter in dashboard
|
| 300 |
-
- Verify correct Groq project selected
|
| 301 |
-
|
| 302 |
-
## 📝 License
|
| 303 |
-
|
| 304 |
-
Private/Proprietary
|
| 305 |
-
|
| 306 |
-
## 🤝 Support
|
| 307 |
-
|
| 308 |
-
For issues or questions:
|
| 309 |
-
1. Check `HOW_TO_RUN.txt` for detailed instructions
|
| 310 |
-
2. Verify `.env` configuration
|
| 311 |
-
3. Test with `quick_test.py`
|
| 312 |
-
|
| 313 |
-
---
|
| 314 |
-
|
| 315 |
-
## 🎯 For Your Clients
|
| 316 |
-
|
| 317 |
-
Share this with your clients:
|
| 318 |
-
|
| 319 |
-
**Endpoint**: `http://your-server:7860/v1`
|
| 320 |
-
**API Key**: `neurostack_XXXXXXXXXXXXX`
|
| 321 |
-
**Usage**: Exactly like OpenAI API
|
| 322 |
-
|
| 323 |
-
```python
|
| 324 |
-
from openai import OpenAI
|
| 325 |
-
client = OpenAI(
|
| 326 |
-
base_url="http://your-server:7860/v1",
|
| 327 |
-
api_key="neurostack_a1b2c3d4e5f6g"
|
| 328 |
-
)
|
| 329 |
-
```
|
| 330 |
-
|
| 331 |
-
It's that simple! 🚀
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
neuro-router-2/neuro_router/app/__init__.py
DELETED
|
@@ -1 +0,0 @@
|
|
| 1 |
-
# FastAPI Proxy App
|
|
|
|
|
|
neuro-router-2/neuro_router/app/auth.py
DELETED
|
@@ -1,57 +0,0 @@
|
|
| 1 |
-
import re
|
| 2 |
-
from typing import Optional
|
| 3 |
-
from fastapi import HTTPException, Header
|
| 4 |
-
|
| 5 |
-
from .config import settings
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
def validate_api_key_format(api_key: str) -> bool:
|
| 9 |
-
"""
|
| 10 |
-
Validate API key format: neurostack_ followed by exactly 13 alphanumeric characters
|
| 11 |
-
Example: neurostack_a1b2c3d4e5f6g
|
| 12 |
-
"""
|
| 13 |
-
pattern = r'^neurostack_[a-zA-Z0-9]{13}$'
|
| 14 |
-
return bool(re.match(pattern, api_key))
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
def verify_api_key(authorization: Optional[str] = Header(default=None)) -> str:
|
| 18 |
-
"""
|
| 19 |
-
Verify the API key from Authorization header.
|
| 20 |
-
Expects: Authorization: Bearer neurostack_XXXXXXXXXXXXX
|
| 21 |
-
"""
|
| 22 |
-
if not authorization:
|
| 23 |
-
raise HTTPException(
|
| 24 |
-
status_code=401,
|
| 25 |
-
detail="Missing Authorization header"
|
| 26 |
-
)
|
| 27 |
-
|
| 28 |
-
parts = authorization.split(" ", 1)
|
| 29 |
-
if len(parts) != 2:
|
| 30 |
-
raise HTTPException(
|
| 31 |
-
status_code=401,
|
| 32 |
-
detail="Invalid Authorization header format. Use: Bearer neurostack_XXXXXXXXXXXXX"
|
| 33 |
-
)
|
| 34 |
-
|
| 35 |
-
scheme, token = parts[0], parts[1]
|
| 36 |
-
if scheme.lower() != "bearer":
|
| 37 |
-
raise HTTPException(
|
| 38 |
-
status_code=401,
|
| 39 |
-
detail="Invalid authentication scheme. Use Bearer"
|
| 40 |
-
)
|
| 41 |
-
|
| 42 |
-
# Validate format
|
| 43 |
-
if not validate_api_key_format(token):
|
| 44 |
-
raise HTTPException(
|
| 45 |
-
status_code=401,
|
| 46 |
-
detail="Invalid API key format. Expected: neurostack_ followed by 13 alphanumeric characters"
|
| 47 |
-
)
|
| 48 |
-
|
| 49 |
-
# Check if key is in valid keys list
|
| 50 |
-
valid_keys = settings.get_valid_keys()
|
| 51 |
-
if token not in valid_keys:
|
| 52 |
-
raise HTTPException(
|
| 53 |
-
status_code=401,
|
| 54 |
-
detail="Invalid API key"
|
| 55 |
-
)
|
| 56 |
-
|
| 57 |
-
return token
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
neuro-router-2/neuro_router/app/config.py
DELETED
|
@@ -1,33 +0,0 @@
|
|
| 1 |
-
from pydantic_settings import BaseSettings
|
| 2 |
-
from pydantic import Field
|
| 3 |
-
from typing import List
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
class Settings(BaseSettings):
|
| 7 |
-
# Groq API key (required to talk to Groq Cloud)
|
| 8 |
-
groq_api_key: str = Field(..., env="GROQ_API_KEY")
|
| 9 |
-
|
| 10 |
-
# Groq's OpenAI-compatible base URL
|
| 11 |
-
groq_base_url: str = Field(
|
| 12 |
-
"https://api.groq.com/openai/v1", env="GROQ_BASE_URL"
|
| 13 |
-
)
|
| 14 |
-
|
| 15 |
-
# Valid API keys for your clients (comma-separated)
|
| 16 |
-
valid_api_keys: str = Field(..., env="VALID_API_KEYS")
|
| 17 |
-
|
| 18 |
-
# Default model (Llama Maverick)
|
| 19 |
-
default_model: str = Field("llama-3.3-70b-versatile", env="DEFAULT_MODEL")
|
| 20 |
-
|
| 21 |
-
# CORS
|
| 22 |
-
cors_allow_origins: str = Field("*", env="CORS_ALLOW_ORIGINS")
|
| 23 |
-
|
| 24 |
-
class Config:
|
| 25 |
-
env_file = ".env"
|
| 26 |
-
case_sensitive = False
|
| 27 |
-
|
| 28 |
-
def get_valid_keys(self) -> List[str]:
|
| 29 |
-
"""Parse valid API keys from comma-separated string"""
|
| 30 |
-
return [key.strip() for key in self.valid_api_keys.split(",") if key.strip()]
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
settings = Settings()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
neuro-router-2/neuro_router/app/main.py
DELETED
|
@@ -1,44 +0,0 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
| 2 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
-
|
| 4 |
-
from .config import settings
|
| 5 |
-
from .routers.openai_proxy import router as openai_router
|
| 6 |
-
|
| 7 |
-
app = FastAPI(
|
| 8 |
-
title="NeuroStack OpenAI-Compatible Proxy",
|
| 9 |
-
description="OpenAI-compatible API that routes to Groq's Llama Maverick model",
|
| 10 |
-
version="1.0.0",
|
| 11 |
-
docs_url="/docs",
|
| 12 |
-
redoc_url="/redoc",
|
| 13 |
-
)
|
| 14 |
-
|
| 15 |
-
# CORS
|
| 16 |
-
app.add_middleware(
|
| 17 |
-
CORSMiddleware,
|
| 18 |
-
allow_origins=[o.strip() for o in settings.cors_allow_origins.split(",") if o.strip()],
|
| 19 |
-
allow_credentials=True,
|
| 20 |
-
allow_methods=["*"],
|
| 21 |
-
allow_headers=["*"],
|
| 22 |
-
)
|
| 23 |
-
|
| 24 |
-
# Include routers
|
| 25 |
-
app.include_router(openai_router)
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
@app.get("/")
|
| 29 |
-
async def root():
|
| 30 |
-
return {
|
| 31 |
-
"service": "Neurorouter",
|
| 32 |
-
"status": "online",
|
| 33 |
-
"endpoints": {
|
| 34 |
-
"chat_completions": "/v1/chat/completions",
|
| 35 |
-
"completions": "/v1/completions",
|
| 36 |
-
"models": "/v1/models"
|
| 37 |
-
},
|
| 38 |
-
"auth": "Required - Bearer neurostack_XXXXXXXXXXXXX (13 alphanumeric chars)"
|
| 39 |
-
}
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
@app.get("/healthz")
|
| 43 |
-
async def healthz():
|
| 44 |
-
return {"status": "ok"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
neuro-router-2/neuro_router/app/proxy.py
DELETED
|
@@ -1,79 +0,0 @@
|
|
| 1 |
-
from typing import AsyncIterator, Dict, Any
|
| 2 |
-
import httpx
|
| 3 |
-
from fastapi import HTTPException
|
| 4 |
-
|
| 5 |
-
from .config import settings
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
def groq_headers() -> Dict[str, str]:
|
| 9 |
-
"""Headers for Groq API requests"""
|
| 10 |
-
return {
|
| 11 |
-
"Authorization": f"Bearer {settings.groq_api_key}",
|
| 12 |
-
"Content-Type": "application/json",
|
| 13 |
-
}
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
def build_groq_payload(payload: Dict[str, Any]) -> Dict[str, Any]:
|
| 17 |
-
"""
|
| 18 |
-
Build the payload for Groq, using Llama Maverick model
|
| 19 |
-
"""
|
| 20 |
-
groq_payload = dict(payload)
|
| 21 |
-
# Force the model to Llama Maverick (llama-3.3-70b-versatile)
|
| 22 |
-
groq_payload["model"] = settings.default_model
|
| 23 |
-
return groq_payload
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
async def stream_sse(resp: httpx.Response) -> AsyncIterator[bytes]:
|
| 27 |
-
"""
|
| 28 |
-
Stream Server-Sent Events from Groq to client
|
| 29 |
-
"""
|
| 30 |
-
async for line in resp.aiter_lines():
|
| 31 |
-
yield (line + "\n").encode("utf-8")
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
async def forward_to_groq(
|
| 35 |
-
client: httpx.AsyncClient,
|
| 36 |
-
endpoint: str,
|
| 37 |
-
body: Dict[str, Any],
|
| 38 |
-
stream: bool = False
|
| 39 |
-
):
|
| 40 |
-
"""
|
| 41 |
-
Forward request to Groq Cloud
|
| 42 |
-
"""
|
| 43 |
-
url = f"{settings.groq_base_url.rstrip('/')}/{endpoint.lstrip('/')}"
|
| 44 |
-
groq_payload = build_groq_payload(body)
|
| 45 |
-
|
| 46 |
-
if stream:
|
| 47 |
-
# Streaming response
|
| 48 |
-
resp = await client.stream(
|
| 49 |
-
"POST",
|
| 50 |
-
url,
|
| 51 |
-
headers=groq_headers(),
|
| 52 |
-
json=groq_payload,
|
| 53 |
-
timeout=None
|
| 54 |
-
)
|
| 55 |
-
if resp.status_code >= 400:
|
| 56 |
-
text = await resp.aread()
|
| 57 |
-
raise HTTPException(
|
| 58 |
-
status_code=resp.status_code,
|
| 59 |
-
detail=text.decode()
|
| 60 |
-
)
|
| 61 |
-
return resp
|
| 62 |
-
else:
|
| 63 |
-
# Non-streaming response
|
| 64 |
-
resp = await client.post(
|
| 65 |
-
url,
|
| 66 |
-
headers=groq_headers(),
|
| 67 |
-
json=groq_payload,
|
| 68 |
-
timeout=None
|
| 69 |
-
)
|
| 70 |
-
if resp.status_code >= 400:
|
| 71 |
-
try:
|
| 72 |
-
err = resp.json()
|
| 73 |
-
except Exception:
|
| 74 |
-
err = {"error": {"message": resp.text}}
|
| 75 |
-
raise HTTPException(
|
| 76 |
-
status_code=resp.status_code,
|
| 77 |
-
detail=err
|
| 78 |
-
)
|
| 79 |
-
return resp.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
neuro-router-2/neuro_router/app/routers/__init__.py
DELETED
|
@@ -1 +0,0 @@
|
|
| 1 |
-
# Routers
|
|
|
|
|
|
neuro-router-2/neuro_router/app/routers/openai_proxy.py
DELETED
|
@@ -1,95 +0,0 @@
|
|
| 1 |
-
from typing import Any, Dict
|
| 2 |
-
from fastapi import APIRouter, Request, Depends, HTTPException
|
| 3 |
-
from fastapi.responses import JSONResponse, StreamingResponse
|
| 4 |
-
import httpx
|
| 5 |
-
|
| 6 |
-
from ..auth import verify_api_key
|
| 7 |
-
from ..proxy import forward_to_groq, stream_sse
|
| 8 |
-
from ..config import settings
|
| 9 |
-
|
| 10 |
-
router = APIRouter(prefix="/v1", tags=["OpenAI-compatible"])
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
@router.post("/chat/completions")
|
| 14 |
-
async def chat_completions(
|
| 15 |
-
request: Request,
|
| 16 |
-
api_key: str = Depends(verify_api_key),
|
| 17 |
-
):
|
| 18 |
-
"""
|
| 19 |
-
OpenAI-compatible chat completions endpoint
|
| 20 |
-
Routes to Groq's Llama Maverick model
|
| 21 |
-
"""
|
| 22 |
-
body: Dict[str, Any] = await request.json()
|
| 23 |
-
stream = bool(body.get("stream", False))
|
| 24 |
-
|
| 25 |
-
async with httpx.AsyncClient(timeout=None) as client:
|
| 26 |
-
if stream:
|
| 27 |
-
# Streaming response
|
| 28 |
-
upstream = await forward_to_groq(
|
| 29 |
-
client, "chat/completions", body, stream=True
|
| 30 |
-
)
|
| 31 |
-
return StreamingResponse(
|
| 32 |
-
stream_sse(upstream),
|
| 33 |
-
media_type="text/event-stream",
|
| 34 |
-
headers={
|
| 35 |
-
"Cache-Control": "no-cache",
|
| 36 |
-
"Connection": "keep-alive",
|
| 37 |
-
},
|
| 38 |
-
)
|
| 39 |
-
else:
|
| 40 |
-
# Non-streaming response
|
| 41 |
-
data = await forward_to_groq(
|
| 42 |
-
client, "chat/completions", body, stream=False
|
| 43 |
-
)
|
| 44 |
-
return JSONResponse(status_code=200, content=data)
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
@router.post("/completions")
|
| 48 |
-
async def completions(
|
| 49 |
-
request: Request,
|
| 50 |
-
api_key: str = Depends(verify_api_key),
|
| 51 |
-
):
|
| 52 |
-
"""
|
| 53 |
-
OpenAI-compatible completions endpoint
|
| 54 |
-
Routes to Groq's Llama Maverick model
|
| 55 |
-
"""
|
| 56 |
-
body: Dict[str, Any] = await request.json()
|
| 57 |
-
stream = bool(body.get("stream", False))
|
| 58 |
-
|
| 59 |
-
async with httpx.AsyncClient(timeout=None) as client:
|
| 60 |
-
if stream:
|
| 61 |
-
upstream = await forward_to_groq(
|
| 62 |
-
client, "completions", body, stream=True
|
| 63 |
-
)
|
| 64 |
-
return StreamingResponse(
|
| 65 |
-
stream_sse(upstream),
|
| 66 |
-
media_type="text/event-stream",
|
| 67 |
-
headers={
|
| 68 |
-
"Cache-Control": "no-cache",
|
| 69 |
-
"Connection": "keep-alive",
|
| 70 |
-
},
|
| 71 |
-
)
|
| 72 |
-
else:
|
| 73 |
-
data = await forward_to_groq(
|
| 74 |
-
client, "completions", body, stream=False
|
| 75 |
-
)
|
| 76 |
-
return JSONResponse(status_code=200, content=data)
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
@router.get("/models")
|
| 80 |
-
async def list_models(api_key: str = Depends(verify_api_key)):
|
| 81 |
-
"""
|
| 82 |
-
List available models (returns Groq's models)
|
| 83 |
-
"""
|
| 84 |
-
groq_models_url = f"{settings.groq_base_url.rstrip('/')}/models"
|
| 85 |
-
async with httpx.AsyncClient(timeout=None) as client:
|
| 86 |
-
resp = await client.get(
|
| 87 |
-
groq_models_url,
|
| 88 |
-
headers={"Authorization": f"Bearer {settings.groq_api_key}"},
|
| 89 |
-
)
|
| 90 |
-
try:
|
| 91 |
-
data = resp.json()
|
| 92 |
-
except Exception:
|
| 93 |
-
raise HTTPException(status_code=resp.status_code, detail=resp.text)
|
| 94 |
-
|
| 95 |
-
return JSONResponse(status_code=resp.status_code, content=data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
neuro-router-2/neuro_router/client_example.py
DELETED
|
@@ -1,50 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Example client usage for NeuroStack OpenAI Proxy
|
| 3 |
-
"""
|
| 4 |
-
from openai import OpenAI
|
| 5 |
-
|
| 6 |
-
# Initialize the OpenAI client pointing to your proxy
|
| 7 |
-
client = OpenAI(
|
| 8 |
-
base_url="http://localhost:7860/v1", # Your proxy URL
|
| 9 |
-
api_key="neurostack_a1b2c3d4e5f6g" # Your custom API key
|
| 10 |
-
)
|
| 11 |
-
|
| 12 |
-
# Example 1: Simple chat completion
|
| 13 |
-
print("=== Example 1: Simple Chat ===")
|
| 14 |
-
response = client.chat.completions.create(
|
| 15 |
-
model="gpt-4o-mini", # Model name doesn't matter, will use Llama Maverick
|
| 16 |
-
messages=[
|
| 17 |
-
{"role": "user", "content": "Hello! Who are you?"}
|
| 18 |
-
],
|
| 19 |
-
max_tokens=100
|
| 20 |
-
)
|
| 21 |
-
print(response.choices[0].message.content)
|
| 22 |
-
print()
|
| 23 |
-
|
| 24 |
-
# Example 2: Streaming response
|
| 25 |
-
print("=== Example 2: Streaming Chat ===")
|
| 26 |
-
stream = client.chat.completions.create(
|
| 27 |
-
model="gpt-4o-mini",
|
| 28 |
-
messages=[
|
| 29 |
-
{"role": "user", "content": "Count from 1 to 10"}
|
| 30 |
-
],
|
| 31 |
-
stream=True,
|
| 32 |
-
max_tokens=100
|
| 33 |
-
)
|
| 34 |
-
|
| 35 |
-
for chunk in stream:
|
| 36 |
-
if chunk.choices[0].delta.content:
|
| 37 |
-
print(chunk.choices[0].delta.content, end="", flush=True)
|
| 38 |
-
print("\n")
|
| 39 |
-
|
| 40 |
-
# Example 3: With system message
|
| 41 |
-
print("=== Example 3: With System Message ===")
|
| 42 |
-
response = client.chat.completions.create(
|
| 43 |
-
model="gpt-4o-mini",
|
| 44 |
-
messages=[
|
| 45 |
-
{"role": "system", "content": "You are a helpful assistant that speaks like a pirate."},
|
| 46 |
-
{"role": "user", "content": "Tell me about the weather today"}
|
| 47 |
-
],
|
| 48 |
-
max_tokens=100
|
| 49 |
-
)
|
| 50 |
-
print(response.choices[0].message.content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
neuro-router-2/neuro_router/quick_test.py
DELETED
|
@@ -1,56 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Quick test to check if proxy is responding
|
| 3 |
-
"""
|
| 4 |
-
import requests
|
| 5 |
-
|
| 6 |
-
print("Testing proxy server...")
|
| 7 |
-
|
| 8 |
-
try:
|
| 9 |
-
# Test 1: Health check
|
| 10 |
-
print("\n1. Testing health endpoint...")
|
| 11 |
-
response = requests.get("http://localhost:7860/healthz", timeout=5)
|
| 12 |
-
print(f" Status: {response.status_code}")
|
| 13 |
-
print(f" Response: {response.json()}")
|
| 14 |
-
|
| 15 |
-
# Test 2: Root endpoint
|
| 16 |
-
print("\n2. Testing root endpoint...")
|
| 17 |
-
response = requests.get("http://localhost:7860/", timeout=5)
|
| 18 |
-
print(f" Status: {response.status_code}")
|
| 19 |
-
print(f" Response: {response.json()}")
|
| 20 |
-
|
| 21 |
-
# Test 3: Simple API call
|
| 22 |
-
print("\n3. Testing chat endpoint...")
|
| 23 |
-
headers = {
|
| 24 |
-
"Authorization": "Bearer neurostack_a1b2c3d4e5f6g",
|
| 25 |
-
"Content-Type": "application/json"
|
| 26 |
-
}
|
| 27 |
-
payload = {
|
| 28 |
-
"model": "gpt-4o-mini",
|
| 29 |
-
"messages": [{"role": "user", "content": "Say hi"}],
|
| 30 |
-
"max_tokens": 10
|
| 31 |
-
}
|
| 32 |
-
response = requests.post(
|
| 33 |
-
"http://localhost:7860/v1/chat/completions",
|
| 34 |
-
headers=headers,
|
| 35 |
-
json=payload,
|
| 36 |
-
timeout=30
|
| 37 |
-
)
|
| 38 |
-
print(f" Status: {response.status_code}")
|
| 39 |
-
if response.status_code == 200:
|
| 40 |
-
data = response.json()
|
| 41 |
-
print(f" Response: {data['choices'][0]['message']['content']}")
|
| 42 |
-
else:
|
| 43 |
-
print(f" Error: {response.text}")
|
| 44 |
-
|
| 45 |
-
print("\n✅ Proxy is working!")
|
| 46 |
-
|
| 47 |
-
except requests.exceptions.ConnectionError:
|
| 48 |
-
print("\n❌ ERROR: Cannot connect to server!")
|
| 49 |
-
print(" Make sure the server is running:")
|
| 50 |
-
print(" conda activate mcc-project")
|
| 51 |
-
print(" python run.py")
|
| 52 |
-
except requests.exceptions.Timeout:
|
| 53 |
-
print("\n❌ ERROR: Request timed out!")
|
| 54 |
-
print(" The server might be slow or hanging.")
|
| 55 |
-
except Exception as e:
|
| 56 |
-
print(f"\n❌ ERROR: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
neuro-router-2/neuro_router/requirements.txt
DELETED
|
@@ -1,8 +0,0 @@
|
|
| 1 |
-
fastapi==0.115.0
|
| 2 |
-
uvicorn[standard]==0.30.6
|
| 3 |
-
httpx==0.27.2
|
| 4 |
-
pydantic==2.10.3
|
| 5 |
-
pydantic-settings==2.6.1
|
| 6 |
-
python-dotenv==1.0.1
|
| 7 |
-
openai>=1.0.0
|
| 8 |
-
requests>=2.31.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
neuro-router-2/neuro_router/run.py
DELETED
|
@@ -1,12 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Run script for NeuroStack OpenAI Proxy
|
| 3 |
-
"""
|
| 4 |
-
import uvicorn
|
| 5 |
-
|
| 6 |
-
if __name__ == "__main__":
|
| 7 |
-
uvicorn.run(
|
| 8 |
-
"app.main:app",
|
| 9 |
-
host="0.0.0.0",
|
| 10 |
-
port=7860, # Hugging Face Spaces default port
|
| 11 |
-
reload=True
|
| 12 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
neuro-router-2/neuro_router/space.yaml
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
sdk: docker
|
| 2 |
-
app_port: 7860
|
| 3 |
-
entrypoint: uvicorn app.main:app --host 0.0.0.0 --port 7860
|
|
|
|
|
|
|
|
|
|
|
|
neuro-router-2/neuro_router/test_hf_endpoint.py
DELETED
|
@@ -1,134 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Test script for NeuroStack API on Hugging Face
|
| 3 |
-
"""
|
| 4 |
-
from openai import OpenAI
|
| 5 |
-
import sys
|
| 6 |
-
|
| 7 |
-
# NeuroStack API Configuration
|
| 8 |
-
NEUROSTACK_BASE_URL = "https://kiranparthiban-neuro-router.hf.space/v1"
|
| 9 |
-
NEUROSTACK_API_KEY = "neurostack_a1b2c3d4e5f6g" # Replace with your actual key
|
| 10 |
-
|
| 11 |
-
def test_chat_completion():
|
| 12 |
-
"""Test basic chat completion"""
|
| 13 |
-
print("Testing NeuroStack Chat Completion...")
|
| 14 |
-
|
| 15 |
-
client = OpenAI(
|
| 16 |
-
base_url=NEUROSTACK_BASE_URL,
|
| 17 |
-
api_key=NEUROSTACK_API_KEY
|
| 18 |
-
)
|
| 19 |
-
|
| 20 |
-
try:
|
| 21 |
-
response = client.chat.completions.create(
|
| 22 |
-
model="gpt-4o-mini",
|
| 23 |
-
messages=[
|
| 24 |
-
{"role": "user", "content": "Say 'Hello from NeuroStack API!'"}
|
| 25 |
-
]
|
| 26 |
-
)
|
| 27 |
-
print("✅ Success!")
|
| 28 |
-
print(f"Response: {response.choices[0].message.content}\n")
|
| 29 |
-
return True
|
| 30 |
-
except Exception as e:
|
| 31 |
-
print(f"❌ Failed: {e}\n")
|
| 32 |
-
return False
|
| 33 |
-
|
| 34 |
-
def test_streaming():
|
| 35 |
-
"""Test streaming responses"""
|
| 36 |
-
print("Testing NeuroStack Streaming...")
|
| 37 |
-
|
| 38 |
-
client = OpenAI(
|
| 39 |
-
base_url=NEUROSTACK_BASE_URL,
|
| 40 |
-
api_key=NEUROSTACK_API_KEY
|
| 41 |
-
)
|
| 42 |
-
|
| 43 |
-
try:
|
| 44 |
-
stream = client.chat.completions.create(
|
| 45 |
-
model="gpt-4o-mini",
|
| 46 |
-
messages=[
|
| 47 |
-
{"role": "user", "content": "Count from 1 to 5"}
|
| 48 |
-
],
|
| 49 |
-
stream=True
|
| 50 |
-
)
|
| 51 |
-
|
| 52 |
-
print("✅ Streaming response: ", end="")
|
| 53 |
-
for chunk in stream:
|
| 54 |
-
if chunk.choices[0].delta.content:
|
| 55 |
-
print(chunk.choices[0].delta.content, end="", flush=True)
|
| 56 |
-
print("\n")
|
| 57 |
-
return True
|
| 58 |
-
except Exception as e:
|
| 59 |
-
print(f"❌ Failed: {e}\n")
|
| 60 |
-
return False
|
| 61 |
-
|
| 62 |
-
def test_models_endpoint():
|
| 63 |
-
"""Test models listing"""
|
| 64 |
-
print("Testing NeuroStack Models Endpoint...")
|
| 65 |
-
|
| 66 |
-
client = OpenAI(
|
| 67 |
-
base_url=NEUROSTACK_BASE_URL,
|
| 68 |
-
api_key=NEUROSTACK_API_KEY
|
| 69 |
-
)
|
| 70 |
-
|
| 71 |
-
try:
|
| 72 |
-
models = client.models.list()
|
| 73 |
-
print("✅ Available models:")
|
| 74 |
-
for model in models.data[:5]: # Show first 5
|
| 75 |
-
print(f" - {model.id}")
|
| 76 |
-
print()
|
| 77 |
-
return True
|
| 78 |
-
except Exception as e:
|
| 79 |
-
print(f"❌ Failed: {e}\n")
|
| 80 |
-
return False
|
| 81 |
-
|
| 82 |
-
def test_invalid_key():
|
| 83 |
-
"""Test with invalid API key"""
|
| 84 |
-
print("Testing Invalid API Key (should fail)...")
|
| 85 |
-
|
| 86 |
-
client = OpenAI(
|
| 87 |
-
base_url=NEUROSTACK_BASE_URL,
|
| 88 |
-
api_key="invalid_key"
|
| 89 |
-
)
|
| 90 |
-
|
| 91 |
-
try:
|
| 92 |
-
response = client.chat.completions.create(
|
| 93 |
-
model="gpt-4o-mini",
|
| 94 |
-
messages=[{"role": "user", "content": "Hello"}]
|
| 95 |
-
)
|
| 96 |
-
print("❌ Should have failed but didn't!\n")
|
| 97 |
-
return False
|
| 98 |
-
except Exception as e:
|
| 99 |
-
print(f"✅ Correctly rejected: {str(e)[:50]}...\n")
|
| 100 |
-
return True
|
| 101 |
-
|
| 102 |
-
if __name__ == "__main__":
|
| 103 |
-
print("=" * 60)
|
| 104 |
-
print("NeuroStack API Test Suite")
|
| 105 |
-
print("=" * 60)
|
| 106 |
-
print(f"Endpoint: {NEUROSTACK_BASE_URL}")
|
| 107 |
-
print("=" * 60)
|
| 108 |
-
print()
|
| 109 |
-
|
| 110 |
-
results = []
|
| 111 |
-
|
| 112 |
-
# Run tests
|
| 113 |
-
results.append(("Chat Completion", test_chat_completion()))
|
| 114 |
-
results.append(("Streaming", test_streaming()))
|
| 115 |
-
results.append(("Models List", test_models_endpoint()))
|
| 116 |
-
results.append(("Invalid Key", test_invalid_key()))
|
| 117 |
-
|
| 118 |
-
# Summary
|
| 119 |
-
print("=" * 60)
|
| 120 |
-
print("Test Results Summary")
|
| 121 |
-
print("=" * 60)
|
| 122 |
-
|
| 123 |
-
passed = sum(1 for _, result in results if result)
|
| 124 |
-
total = len(results)
|
| 125 |
-
|
| 126 |
-
for name, result in results:
|
| 127 |
-
status = "✅ PASS" if result else "❌ FAIL"
|
| 128 |
-
print(f"{status} - {name}")
|
| 129 |
-
|
| 130 |
-
print("=" * 60)
|
| 131 |
-
print(f"Passed: {passed}/{total}")
|
| 132 |
-
print("=" * 60)
|
| 133 |
-
|
| 134 |
-
sys.exit(0 if passed == total else 1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
neuro-router-2/neuro_router/test_proxy.py
DELETED
|
@@ -1,138 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Test script for NeuroStack OpenAI Proxy
|
| 3 |
-
"""
|
| 4 |
-
import requests
|
| 5 |
-
import json
|
| 6 |
-
|
| 7 |
-
# Configuration
|
| 8 |
-
BASE_URL = "http://localhost:7860"
|
| 9 |
-
API_KEY = "neurostack_a1b2c3d4e5f6g" # Replace with your actual API key
|
| 10 |
-
|
| 11 |
-
headers = {
|
| 12 |
-
"Authorization": f"Bearer {API_KEY}",
|
| 13 |
-
"Content-Type": "application/json"
|
| 14 |
-
}
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
def test_health():
|
| 18 |
-
"""Test health endpoint"""
|
| 19 |
-
print("\n=== Testing Health Endpoint ===")
|
| 20 |
-
response = requests.get(f"{BASE_URL}/healthz")
|
| 21 |
-
print(f"Status: {response.status_code}")
|
| 22 |
-
print(f"Response: {response.json()}")
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
def test_root():
|
| 26 |
-
"""Test root endpoint"""
|
| 27 |
-
print("\n=== Testing Root Endpoint ===")
|
| 28 |
-
response = requests.get(f"{BASE_URL}/")
|
| 29 |
-
print(f"Status: {response.status_code}")
|
| 30 |
-
print(f"Response: {json.dumps(response.json(), indent=2)}")
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
def test_chat_completion():
|
| 34 |
-
"""Test chat completions endpoint"""
|
| 35 |
-
print("\n=== Testing Chat Completions (Non-streaming) ===")
|
| 36 |
-
|
| 37 |
-
payload = {
|
| 38 |
-
"model": "gpt-4o-mini", # Will be mapped to Llama Maverick
|
| 39 |
-
"messages": [
|
| 40 |
-
{"role": "user", "content": "Say hello in one sentence!"}
|
| 41 |
-
],
|
| 42 |
-
"max_tokens": 50
|
| 43 |
-
}
|
| 44 |
-
|
| 45 |
-
response = requests.post(
|
| 46 |
-
f"{BASE_URL}/v1/chat/completions",
|
| 47 |
-
headers=headers,
|
| 48 |
-
json=payload
|
| 49 |
-
)
|
| 50 |
-
|
| 51 |
-
print(f"Status: {response.status_code}")
|
| 52 |
-
if response.status_code == 200:
|
| 53 |
-
data = response.json()
|
| 54 |
-
print(f"Model: {data.get('model')}")
|
| 55 |
-
print(f"Response: {data['choices'][0]['message']['content']}")
|
| 56 |
-
else:
|
| 57 |
-
print(f"Error: {response.text}")
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
def test_chat_completion_streaming():
|
| 61 |
-
"""Test chat completions with streaming"""
|
| 62 |
-
print("\n=== Testing Chat Completions (Streaming) ===")
|
| 63 |
-
|
| 64 |
-
payload = {
|
| 65 |
-
"model": "gpt-4o-mini",
|
| 66 |
-
"messages": [
|
| 67 |
-
{"role": "user", "content": "Count from 1 to 5"}
|
| 68 |
-
],
|
| 69 |
-
"stream": True,
|
| 70 |
-
"max_tokens": 50
|
| 71 |
-
}
|
| 72 |
-
|
| 73 |
-
response = requests.post(
|
| 74 |
-
f"{BASE_URL}/v1/chat/completions",
|
| 75 |
-
headers=headers,
|
| 76 |
-
json=payload,
|
| 77 |
-
stream=True
|
| 78 |
-
)
|
| 79 |
-
|
| 80 |
-
print(f"Status: {response.status_code}")
|
| 81 |
-
if response.status_code == 200:
|
| 82 |
-
print("Streaming response:")
|
| 83 |
-
for line in response.iter_lines():
|
| 84 |
-
if line:
|
| 85 |
-
line_str = line.decode('utf-8')
|
| 86 |
-
if line_str.startswith('data: ') and line_str != 'data: [DONE]':
|
| 87 |
-
try:
|
| 88 |
-
data = json.loads(line_str[6:])
|
| 89 |
-
if data['choices'][0]['delta'].get('content'):
|
| 90 |
-
print(data['choices'][0]['delta']['content'], end='', flush=True)
|
| 91 |
-
except:
|
| 92 |
-
pass
|
| 93 |
-
print()
|
| 94 |
-
else:
|
| 95 |
-
print(f"Error: {response.text}")
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
def test_invalid_key():
|
| 99 |
-
"""Test with invalid API key"""
|
| 100 |
-
print("\n=== Testing Invalid API Key ===")
|
| 101 |
-
|
| 102 |
-
invalid_headers = {
|
| 103 |
-
"Authorization": "Bearer neurostack_invalid123",
|
| 104 |
-
"Content-Type": "application/json"
|
| 105 |
-
}
|
| 106 |
-
|
| 107 |
-
payload = {
|
| 108 |
-
"model": "gpt-4o-mini",
|
| 109 |
-
"messages": [{"role": "user", "content": "Hello"}]
|
| 110 |
-
}
|
| 111 |
-
|
| 112 |
-
response = requests.post(
|
| 113 |
-
f"{BASE_URL}/v1/chat/completions",
|
| 114 |
-
headers=invalid_headers,
|
| 115 |
-
json=payload
|
| 116 |
-
)
|
| 117 |
-
|
| 118 |
-
print(f"Status: {response.status_code}")
|
| 119 |
-
print(f"Response: {response.json()}")
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
if __name__ == "__main__":
|
| 123 |
-
print("=" * 50)
|
| 124 |
-
print("NeuroStack OpenAI Proxy - Test Suite")
|
| 125 |
-
print("=" * 50)
|
| 126 |
-
|
| 127 |
-
try:
|
| 128 |
-
test_health()
|
| 129 |
-
test_root()
|
| 130 |
-
test_chat_completion()
|
| 131 |
-
test_chat_completion_streaming()
|
| 132 |
-
test_invalid_key()
|
| 133 |
-
|
| 134 |
-
print("\n" + "=" * 50)
|
| 135 |
-
print("All tests completed!")
|
| 136 |
-
print("=" * 50)
|
| 137 |
-
except Exception as e:
|
| 138 |
-
print(f"\nError during testing: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|