huijio commited on
Commit
40a7838
·
verified ·
1 Parent(s): 174d03e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -24
app.py CHANGED
@@ -1,13 +1,24 @@
1
- from fastapi import FastAPI, HTTPException, Request
2
  from fastapi.responses import JSONResponse
3
  from pydantic import BaseModel
4
  import requests
5
  import uuid
6
  from datetime import datetime
 
7
 
8
- app = FastAPI()
 
9
 
10
- # OpenAI-compatible request model
 
 
 
 
 
 
 
 
 
11
  class ChatMessage(BaseModel):
12
  role: str
13
  content: str
@@ -15,13 +26,19 @@ class ChatMessage(BaseModel):
15
  class ChatCompletionRequest(BaseModel):
16
  model: str = "deepseek-ai/DeepSeek-V3"
17
  messages: list[ChatMessage]
18
- temperature: float = 0.5
19
  max_tokens: int = None
20
 
 
 
 
 
 
 
21
  @app.post("/v1/chat/completions")
22
  async def chat_completion(request: ChatCompletionRequest):
23
  try:
24
- # Prepare the MultiChatAI request
25
  multi_chat_body = {
26
  "chatSettings": {
27
  "model": request.model,
@@ -33,31 +50,32 @@ async def chat_completion(request: ChatCompletionRequest):
33
  "embeddingsProvider": "openai"
34
  },
35
  "messages": [
36
- {"role": "system", "content": f"Today is {datetime.now().strftime('%m/%d/%Y')}.\n\nYou are a helpful AI assistant."},
37
  *[{"role": msg.role, "content": msg.content} for msg in request.messages]
38
  ],
39
  "customModelId": ""
40
  }
41
 
42
  headers = {
43
- "accept": "*/*",
44
  "content-type": "application/json",
45
  }
46
 
47
- # Make request to MultiChatAI
48
  response = requests.post(
49
  "https://www.multichatai.com/api/chat/deepinfra",
50
  headers=headers,
51
- json=multi_chat_body
 
52
  )
53
 
54
- if response.status_code != 200:
55
  raise HTTPException(
56
  status_code=response.status_code,
57
  detail=f"MultiChatAI API error: {response.text}"
58
  )
59
 
60
- # Transform response to OpenAI format
61
  return JSONResponse({
62
  "id": f"chatcmpl-{uuid.uuid4()}",
63
  "object": "chat.completion",
@@ -72,24 +90,18 @@ async def chat_completion(request: ChatCompletionRequest):
72
  "finish_reason": "stop"
73
  }],
74
  "usage": {
75
- "prompt_tokens": 0,
76
  "completion_tokens": 0,
77
  "total_tokens": 0
78
  }
79
  })
80
 
 
 
81
  except Exception as e:
82
  raise HTTPException(status_code=500, detail=str(e))
83
 
84
- @app.get("/")
85
- async def health_check():
86
- return {"status": "healthy"}
87
-
88
- # Add CORS middleware if needed
89
- @app.middleware("http")
90
- async def add_cors_header(request: Request, call_next):
91
- response = await call_next(request)
92
- response.headers["Access-Control-Allow-Origin"] = "*"
93
- response.headers["Access-Control-Allow-Methods"] = "GET, POST, OPTIONS"
94
- response.headers["Access-Control-Allow-Headers"] = "Content-Type"
95
- return response
 
1
+ from fastapi import FastAPI, HTTPException
2
  from fastapi.responses import JSONResponse
3
  from pydantic import BaseModel
4
  import requests
5
  import uuid
6
  from datetime import datetime
7
+ from fastapi.middleware.cors import CORSMiddleware
8
 
9
+ # Initialize FastAPI app
10
+ app = FastAPI(title="MultiChatAI to OpenAI API Wrapper")
11
 
12
+ # Configure CORS - adjust these based on your needs
13
+ app.add_middleware(
14
+ CORSMiddleware,
15
+ allow_origins=["*"],
16
+ allow_credentials=True,
17
+ allow_methods=["*"],
18
+ allow_headers=["*"],
19
+ )
20
+
21
+ # Request models
22
  class ChatMessage(BaseModel):
23
  role: str
24
  content: str
 
26
  class ChatCompletionRequest(BaseModel):
27
  model: str = "deepseek-ai/DeepSeek-V3"
28
  messages: list[ChatMessage]
29
+ temperature: float = 0.7
30
  max_tokens: int = None
31
 
32
+ # Health check endpoint
33
+ @app.get("/")
34
+ async def health_check():
35
+ return {"status": "OK", "service": "MultiChatAI Proxy", "timestamp": datetime.now().isoformat()}
36
+
37
+ # Main API endpoint
38
  @app.post("/v1/chat/completions")
39
  async def chat_completion(request: ChatCompletionRequest):
40
  try:
41
+ # Prepare request for MultiChatAI
42
  multi_chat_body = {
43
  "chatSettings": {
44
  "model": request.model,
 
50
  "embeddingsProvider": "openai"
51
  },
52
  "messages": [
53
+ {"role": "system", "content": f"Today is {datetime.now().strftime('%m/%d/%Y')}.\nYou are a helpful AI assistant."},
54
  *[{"role": msg.role, "content": msg.content} for msg in request.messages]
55
  ],
56
  "customModelId": ""
57
  }
58
 
59
  headers = {
60
+ "accept": "application/json",
61
  "content-type": "application/json",
62
  }
63
 
64
+ # Call MultiChatAI API
65
  response = requests.post(
66
  "https://www.multichatai.com/api/chat/deepinfra",
67
  headers=headers,
68
+ json=multi_chat_body,
69
+ timeout=30 # Add timeout to prevent hanging
70
  )
71
 
72
+ if not response.ok:
73
  raise HTTPException(
74
  status_code=response.status_code,
75
  detail=f"MultiChatAI API error: {response.text}"
76
  )
77
 
78
+ # Format response in OpenAI style
79
  return JSONResponse({
80
  "id": f"chatcmpl-{uuid.uuid4()}",
81
  "object": "chat.completion",
 
90
  "finish_reason": "stop"
91
  }],
92
  "usage": {
93
+ "prompt_tokens": 0, # You can implement token counting later
94
  "completion_tokens": 0,
95
  "total_tokens": 0
96
  }
97
  })
98
 
99
+ except requests.Timeout:
100
+ raise HTTPException(status_code=504, detail="Upstream service timeout")
101
  except Exception as e:
102
  raise HTTPException(status_code=500, detail=str(e))
103
 
104
+ # Add this if you need to support OPTIONS requests
105
+ @app.options("/v1/chat/completions")
106
+ async def options_handler():
107
+ return JSONResponse(content={}, status_code=200)