Samuraiog commited on
Commit
a8ee16c
·
verified ·
1 Parent(s): 4531f3d

Upload 5 files

Browse files
Files changed (5) hide show
  1. .dockerignore +20 -0
  2. Dockerfile +14 -0
  3. config.py +29 -0
  4. main.py +399 -0
  5. requirements.txt +4 -0
.dockerignore ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ __pycache__
2
+ *.pyc
3
+ *.pyo
4
+ *.pyd
5
+ .Python
6
+ *.so
7
+ *.egg
8
+ *.egg-info
9
+ dist
10
+ build
11
+ .env
12
+ .venv
13
+ venv/
14
+ ENV/
15
+ env/
16
+ .git
17
+ .gitignore
18
+ README.md
19
+ test_api.py
20
+ run.bat
Dockerfile ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.11-slim
2
+
3
+ WORKDIR /app
4
+
5
+ COPY requirements.txt .
6
+
7
+ RUN pip install --no-cache-dir -r requirements.txt
8
+
9
+ COPY main.py .
10
+ COPY config.py .
11
+
12
+ EXPOSE 7860
13
+
14
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
config.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Model Mapping Configuration
2
+ # Add your custom model IDs here and map them to the backend model
3
+
4
+ MODEL_MAPPING = {
5
+ # Claude Models
6
+ "claude-haiku-4.5": "claude-haiku-4.5",
7
+ "claude-sonnet-4.5-max": "claude-haiku-4.5",
8
+ "claude-3-5-sonnet-20241022": "claude-haiku-4.5",
9
+ "claude-3-5-haiku-20241022": "claude-haiku-4.5",
10
+
11
+ # OpenAI GPT-4 Models
12
+ "gpt-4": "claude-haiku-4.5",
13
+ "gpt-4-turbo": "claude-haiku-4.5",
14
+ "gpt-4-turbo-preview": "claude-haiku-4.5",
15
+ "gpt-4o": "claude-haiku-4.5",
16
+ "gpt-4o-mini": "claude-haiku-4.5",
17
+
18
+ # OpenAI GPT-3.5 Models
19
+ "gpt-3.5-turbo": "claude-haiku-4.5",
20
+ "gpt-3.5-turbo-16k": "claude-haiku-4.5",
21
+
22
+ # Custom aliases (add your own here)
23
+ "my-custom-model": "claude-haiku-4.5",
24
+ "fast-model": "claude-haiku-4.5",
25
+ "smart-model": "claude-haiku-4.5",
26
+ }
27
+
28
+ # You can add more models by editing this dictionary
29
+ # Format: "custom-model-id": "backend-model-id"
main.py ADDED
@@ -0,0 +1,399 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from fastapi.responses import StreamingResponse
3
+ from fastapi.middleware.cors import CORSMiddleware
4
+ from pydantic import BaseModel, ConfigDict
5
+ from typing import List, Optional, Dict, Any, Union
6
+ import httpx
7
+ import json
8
+ import uuid
9
+ import time
10
+ import logging
11
+ from config import MODEL_MAPPING
12
+
13
+ logging.basicConfig(level=logging.INFO)
14
+ logger = logging.getLogger(__name__)
15
+
16
+ app = FastAPI(title="Smithery AI Reverse API", version="1.0.0")
17
+
18
+ app.add_middleware(
19
+ CORSMiddleware,
20
+ allow_origins=["*"],
21
+ allow_credentials=True,
22
+ allow_methods=["*"],
23
+ allow_headers=["*"],
24
+ )
25
+
26
+ SMITHERY_API_URL = "https://smithery.ai/api/chat"
27
+
28
+ def get_backend_model(requested_model: str) -> str:
29
+ return MODEL_MAPPING.get(requested_model, "claude-haiku-4.5")
30
+ SMITHERY_HEADERS = {
31
+ "accept": "*/*",
32
+ "accept-language": "en-US,en;q=0.5",
33
+ "content-type": "application/json",
34
+ "origin": "https://smithery.ai",
35
+ "priority": "u=1, i",
36
+ "referer": "https://smithery.ai/chat?mcp=@LinkupPlatform/linkup-mcp-server",
37
+ "sec-ch-ua": '"Chromium";v="142", "Brave";v="142", "Not_A Brand";v="99"',
38
+ "sec-ch-ua-mobile": "?0",
39
+ "sec-ch-ua-platform": '"Windows"',
40
+ "sec-fetch-dest": "empty",
41
+ "sec-fetch-mode": "cors",
42
+ "sec-fetch-site": "same-origin",
43
+ "sec-gpc": "1",
44
+ "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36",
45
+ "Cookie": 'sb-spjawbfpwezjfmicopsl-auth-token.0=base64-eyJhY2Nlc3NfdG9rZW4iOiJleUpoYkdjaU9pSklVekkxTmlJc0ltd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2tlbiI6InlhMjkuYTBBVGk2SzJ0NnpLV1dqRDdQdi1HNDU3TlpFWElja2Y1WHN4SFlheUR6cms5TzZiSHVRY0xwT3dvZFpzWE9pSXEyMC1NdzViRm9CNE9RcGJ4LUV3eHhMM3BfT1kxZ2liQ3dTU3ExU2FsYndmbW8zN3ZlUjVDSTJiQVhlM3E3d1NnX3NCVnp5a1hoc3Z5blFnNHdKWVJqOXQ0NUZEQ1NEYmFNQmZkTVpmeWhITjJTYVByNkFzLUJjMUFuQXU1NEs2a1lQRWlvMGRNYUNnWUtBV2tTQVJVU0ZRSEdYMk1pVnM5a2N2NHBfZmxTcGthOE1rRkNLUTAyMDYifQ; ph_phc_WiMP1Rj0YvrdwYVYdE0AdRBNmB8MTdbsWY8oalxSrts_posthog=%7B%22distinct_id%22%3A%2271b95f80-2e0f-4882-8a53-b8effa9a1b65%22%2C%22%24sesid%22%3A%5B1762170188623%2C%22019a4981-a7e1-7778-9297-f569f82af9bb%22%2C1762169825241%5D%2C%22%24epp%22%3Atrue%2C%22%24initial_person_info%22%3A%7B%22r%22%3A%22https%3A%2F%2Fsearch.brave.com%2F%22%2C%22u%22%3A%22https%3A%2F%2Fsmithery.ai%2F%22%7D%7D'
46
+ }
47
+
48
+ class Message(BaseModel):
49
+ model_config = ConfigDict(extra="allow")
50
+
51
+ role: str
52
+ content: Union[str, List[Dict[str, Any]]]
53
+ name: Optional[str] = None
54
+
55
+ class ChatCompletionRequest(BaseModel):
56
+ model_config = ConfigDict(extra="allow")
57
+
58
+ model: Optional[str] = "claude-haiku-4.5"
59
+ messages: List[Message]
60
+ temperature: Optional[float] = None
61
+ max_tokens: Optional[int] = None
62
+ top_p: Optional[float] = None
63
+ n: Optional[int] = None
64
+ stream: Optional[bool] = False
65
+ stop: Optional[Union[str, List[str]]] = None
66
+ presence_penalty: Optional[float] = None
67
+ frequency_penalty: Optional[float] = None
68
+ logit_bias: Optional[Dict[str, float]] = None
69
+ user: Optional[str] = None
70
+ tools: Optional[List[Dict[str, Any]]] = None
71
+ tool_choice: Optional[Union[str, Dict[str, Any]]] = None
72
+ response_format: Optional[Dict[str, Any]] = None
73
+
74
+ class Choice(BaseModel):
75
+ index: int
76
+ message: Message
77
+ finish_reason: str
78
+
79
+ class Usage(BaseModel):
80
+ prompt_tokens: int
81
+ completion_tokens: int
82
+ total_tokens: int
83
+
84
+ class ChatCompletionResponse(BaseModel):
85
+ id: str
86
+ object: str = "chat.completion"
87
+ created: int
88
+ model: str
89
+ choices: List[Choice]
90
+ usage: Usage
91
+
92
+ def convert_to_smithery_format(messages: List[Message]) -> List[Dict]:
93
+ smithery_messages = []
94
+ for msg in messages:
95
+ role = "user" if msg.role == "user" else "assistant"
96
+
97
+ if isinstance(msg.content, str):
98
+ content = msg.content
99
+ elif isinstance(msg.content, list):
100
+ content = " ".join([part.get("text", "") for part in msg.content if isinstance(part, dict) and part.get("type") == "text"])
101
+ else:
102
+ content = str(msg.content)
103
+
104
+ smithery_msg = {
105
+ "parts": [{"type": "text", "text": content}],
106
+ "id": str(uuid.uuid4()).replace("-", "")[:16],
107
+ "role": role
108
+ }
109
+ smithery_messages.append(smithery_msg)
110
+ return smithery_messages
111
+
112
+ def convert_from_smithery_format(smithery_response: str) -> str:
113
+ return smithery_response
114
+
115
+ async def stream_smithery_response(messages: List[Message], model: str, tools: Optional[List] = None):
116
+ smithery_messages = convert_to_smithery_format(messages)
117
+ backend_model = get_backend_model(model)
118
+
119
+ payload = {
120
+ "messages": smithery_messages,
121
+ "tools": [],
122
+ "model": backend_model,
123
+ "systemPrompt": "You are a helpful assistant.",
124
+ "chatId": "default"
125
+ }
126
+
127
+ chat_id = f"chatcmpl-{uuid.uuid4()}"
128
+ created = int(time.time())
129
+ first_chunk = True
130
+
131
+ logger.info(f"Streaming request for model: {model} -> {backend_model}")
132
+
133
+ try:
134
+ timeout = httpx.Timeout(120.0, connect=30.0)
135
+ async with httpx.AsyncClient(timeout=timeout, follow_redirects=True) as client:
136
+ async with client.stream(
137
+ "POST",
138
+ SMITHERY_API_URL,
139
+ headers=SMITHERY_HEADERS,
140
+ json=payload
141
+ ) as response:
142
+ logger.info(f"Backend response status: {response.status_code}")
143
+
144
+ if response.status_code != 200:
145
+ error_text = await response.aread()
146
+ error_msg = error_text.decode()
147
+ logger.error(f"Backend error: {error_msg}")
148
+
149
+ error_chunk = {
150
+ "id": chat_id,
151
+ "object": "chat.completion.chunk",
152
+ "created": created,
153
+ "model": model,
154
+ "choices": [{
155
+ "index": 0,
156
+ "delta": {"role": "assistant", "content": f"Error: {error_msg}"},
157
+ "finish_reason": None
158
+ }]
159
+ }
160
+ yield f"data: {json.dumps(error_chunk)}\n\n"
161
+ yield "data: [DONE]\n\n"
162
+ return
163
+
164
+ chunk_count = 0
165
+ async for line in response.aiter_lines():
166
+ if line.startswith("data: "):
167
+ data_str = line[6:]
168
+
169
+ if data_str == "[DONE]":
170
+ logger.info(f"Stream completed, sent {chunk_count} chunks")
171
+ final_chunk = {
172
+ "id": chat_id,
173
+ "object": "chat.completion.chunk",
174
+ "created": created,
175
+ "model": model,
176
+ "choices": [{
177
+ "index": 0,
178
+ "delta": {},
179
+ "finish_reason": "stop"
180
+ }]
181
+ }
182
+ yield f"data: {json.dumps(final_chunk)}\n\n"
183
+ yield "data: [DONE]\n\n"
184
+ break
185
+
186
+ try:
187
+ data = json.loads(data_str)
188
+
189
+ if data.get("type") == "text-delta":
190
+ delta_text = data.get("delta", "")
191
+ chunk_count += 1
192
+
193
+ if first_chunk:
194
+ chunk_data = {
195
+ "id": chat_id,
196
+ "object": "chat.completion.chunk",
197
+ "created": created,
198
+ "model": model,
199
+ "choices": [{
200
+ "index": 0,
201
+ "delta": {"role": "assistant", "content": delta_text},
202
+ "finish_reason": None
203
+ }]
204
+ }
205
+ first_chunk = False
206
+ else:
207
+ chunk_data = {
208
+ "id": chat_id,
209
+ "object": "chat.completion.chunk",
210
+ "created": created,
211
+ "model": model,
212
+ "choices": [{
213
+ "index": 0,
214
+ "delta": {"content": delta_text},
215
+ "finish_reason": None
216
+ }]
217
+ }
218
+ yield f"data: {json.dumps(chunk_data)}\n\n"
219
+ except Exception as parse_error:
220
+ logger.warning(f"Failed to parse chunk: {parse_error}")
221
+ pass
222
+
223
+ except httpx.TimeoutException as e:
224
+ logger.error(f"Timeout error: {str(e)}")
225
+ error_chunk = {
226
+ "id": chat_id,
227
+ "object": "chat.completion.chunk",
228
+ "created": created,
229
+ "model": model,
230
+ "choices": [{
231
+ "index": 0,
232
+ "delta": {"role": "assistant", "content": "Error: Request timeout. The backend took too long to respond."},
233
+ "finish_reason": "stop"
234
+ }]
235
+ }
236
+ yield f"data: {json.dumps(error_chunk)}\n\n"
237
+ yield "data: [DONE]\n\n"
238
+ except Exception as e:
239
+ logger.error(f"Stream error: {str(e)}")
240
+ error_chunk = {
241
+ "id": chat_id,
242
+ "object": "chat.completion.chunk",
243
+ "created": created,
244
+ "model": model,
245
+ "choices": [{
246
+ "index": 0,
247
+ "delta": {"role": "assistant", "content": f"Error: {str(e)}"},
248
+ "finish_reason": "stop"
249
+ }]
250
+ }
251
+ yield f"data: {json.dumps(error_chunk)}\n\n"
252
+ yield "data: [DONE]\n\n"
253
+
254
+ async def get_smithery_response(messages: List[Message], model: str, tools: Optional[List] = None) -> str:
255
+ smithery_messages = convert_to_smithery_format(messages)
256
+ backend_model = get_backend_model(model)
257
+
258
+ payload = {
259
+ "messages": smithery_messages,
260
+ "tools": [],
261
+ "model": backend_model,
262
+ "systemPrompt": "You are a helpful assistant.",
263
+ "chatId": "default"
264
+ }
265
+
266
+ try:
267
+ async with httpx.AsyncClient(timeout=60.0, follow_redirects=True) as client:
268
+ response = await client.post(
269
+ SMITHERY_API_URL,
270
+ headers=SMITHERY_HEADERS,
271
+ json=payload
272
+ )
273
+ if response.status_code != 200:
274
+ error_detail = f"Smithery API returned {response.status_code}: {response.text}"
275
+ return error_detail
276
+ return response.text
277
+ except Exception as e:
278
+ return f"Error calling Smithery API: {str(e)}"
279
+
280
+ @app.post("/v1/chat/completions")
281
+ async def chat_completions(request: ChatCompletionRequest):
282
+ try:
283
+ logger.info(f"Chat request: model={request.model}, stream={request.stream}, messages={len(request.messages)}")
284
+
285
+ if request.stream:
286
+ return StreamingResponse(
287
+ stream_smithery_response(request.messages, request.model, request.tools),
288
+ media_type="text/event-stream",
289
+ headers={
290
+ "Cache-Control": "no-cache",
291
+ "Connection": "keep-alive",
292
+ "X-Accel-Buffering": "no"
293
+ }
294
+ )
295
+ else:
296
+ content = await get_smithery_response(request.messages, request.model, request.tools)
297
+
298
+ response = ChatCompletionResponse(
299
+ id=f"chatcmpl-{uuid.uuid4()}",
300
+ created=int(time.time()),
301
+ model=request.model,
302
+ choices=[
303
+ Choice(
304
+ index=0,
305
+ message=Message(role="assistant", content=content),
306
+ finish_reason="stop"
307
+ )
308
+ ],
309
+ usage=Usage(
310
+ prompt_tokens=len(str(request.messages)),
311
+ completion_tokens=len(content),
312
+ total_tokens=len(str(request.messages)) + len(content)
313
+ )
314
+ )
315
+ logger.info(f"Non-streaming response completed, content length: {len(content)}")
316
+ return response
317
+
318
+ except httpx.TimeoutException as e:
319
+ logger.error(f"Timeout in chat_completions: {str(e)}")
320
+ raise HTTPException(status_code=504, detail="Gateway timeout - backend took too long to respond")
321
+ except httpx.HTTPStatusError as e:
322
+ logger.error(f"HTTP error in chat_completions: {e.response.status_code}")
323
+ raise HTTPException(status_code=e.response.status_code, detail=str(e))
324
+ except Exception as e:
325
+ logger.error(f"Error in chat_completions: {str(e)}")
326
+ raise HTTPException(status_code=500, detail=str(e))
327
+
328
+ @app.get("/v1/models")
329
+ async def list_models():
330
+ models_list = []
331
+ for model_id in MODEL_MAPPING.keys():
332
+ models_list.append({
333
+ "id": model_id,
334
+ "object": "model",
335
+ "created": int(time.time()),
336
+ "owned_by": "smithery-ai"
337
+ })
338
+
339
+ return {
340
+ "object": "list",
341
+ "data": models_list
342
+ }
343
+
344
+ @app.get("/")
345
+ async def root():
346
+ return {
347
+ "message": "Smithery AI Reverse API",
348
+ "version": "1.0.0",
349
+ "status": "online",
350
+ "endpoints": {
351
+ "chat_completions": "/v1/chat/completions",
352
+ "models": "/v1/models"
353
+ },
354
+ "docs": "/docs"
355
+ }
356
+
357
+ @app.get("/health")
358
+ async def health():
359
+ return {"status": "healthy", "service": "smithery-ai-reverse-api"}
360
+
361
+ @app.get("/test-backend")
362
+ async def test_backend():
363
+ """Test if backend is accessible"""
364
+ try:
365
+ test_messages = [{"parts": [{"type": "text", "text": "hi"}], "id": "test123", "role": "user"}]
366
+ payload = {
367
+ "messages": test_messages,
368
+ "tools": [],
369
+ "model": "claude-haiku-4.5",
370
+ "systemPrompt": "You are a helpful assistant.",
371
+ "chatId": "test"
372
+ }
373
+
374
+ timeout = httpx.Timeout(30.0, connect=10.0)
375
+ async with httpx.AsyncClient(timeout=timeout, follow_redirects=True) as client:
376
+ response = await client.post(
377
+ SMITHERY_API_URL,
378
+ headers=SMITHERY_HEADERS,
379
+ json=payload
380
+ )
381
+ return {
382
+ "backend_accessible": True,
383
+ "status_code": response.status_code,
384
+ "response_length": len(response.text),
385
+ "smithery_url": SMITHERY_API_URL
386
+ }
387
+ except Exception as e:
388
+ logger.error(f"Backend test failed: {str(e)}")
389
+ return {
390
+ "backend_accessible": False,
391
+ "error": str(e),
392
+ "smithery_url": SMITHERY_API_URL
393
+ }
394
+
395
+ if __name__ == "__main__":
396
+ import uvicorn
397
+ import os
398
+ port = int(os.getenv("PORT", 7860))
399
+ uvicorn.run(app, host="0.0.0.0", port=port)
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ httpx
4
+ pydantic