huijio commited on
Commit
4105399
·
verified ·
1 Parent(s): e577a4d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -37
app.py CHANGED
@@ -1,8 +1,10 @@
1
  from fastapi import FastAPI, HTTPException
 
2
  from pydantic import BaseModel
3
  import requests
4
  import uuid
5
  from datetime import datetime
 
6
 
7
  app = FastAPI()
8
 
@@ -16,45 +18,31 @@ class ChatRequest(BaseModel):
16
  temperature: float = 0.7
17
 
18
  @app.get("/")
19
- def health_check():
20
- return {"status": "OK", "time": datetime.now().isoformat()}
21
 
22
  @app.post("/v1/chat/completions")
23
- def chat_completion(request: ChatRequest):
24
  try:
25
- # Prepare payload for MultiChatAI
26
- payload = {
27
- "chatSettings": {
28
- "model": request.model,
29
- "prompt": "You are a helpful assistant.",
30
- "temperature": request.temperature,
31
- "contextLength": 4096,
32
- "includeProfileContext": False,
33
- "includeWorkspaceInstructions": False,
34
- "embeddingsProvider": "openai"
35
- },
36
- "messages": [
37
- {"role": "system", "content": "You are a helpful assistant."},
38
- *[{"role": msg.role, "content": msg.content} for msg in request.messages]
39
- ]
40
- }
41
-
42
- # Call MultiChatAI API
43
  response = requests.post(
44
  "https://www.multichatai.com/api/chat/deepinfra",
45
- json=payload,
46
- headers={"Content-Type": "application/json"},
 
 
 
 
 
 
 
 
 
 
47
  timeout=10
48
  )
49
-
50
- if response.status_code != 200:
51
- raise HTTPException(
52
- status_code=502,
53
- detail=f"Upstream API error: {response.text}"
54
- )
55
-
56
- # Format response
57
- return {
58
  "id": str(uuid.uuid4()),
59
  "object": "chat.completion",
60
  "created": int(datetime.now().timestamp()),
@@ -65,9 +53,9 @@ def chat_completion(request: ChatRequest):
65
  "content": response.text.strip()
66
  }
67
  }]
68
- }
69
-
70
- except requests.Timeout:
71
- raise HTTPException(status_code=504, detail="Request timeout")
72
  except Exception as e:
73
- raise HTTPException(status_code=500, detail=str(e))
 
 
 
 
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
+ import uvicorn
8
 
9
  app = FastAPI()
10
 
 
18
  temperature: float = 0.7
19
 
20
  @app.get("/")
21
+ async def health_check():
22
+ return {"status": "running", "timestamp": datetime.now().isoformat()}
23
 
24
  @app.post("/v1/chat/completions")
25
+ async def chat_completion(request: ChatRequest):
26
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  response = requests.post(
28
  "https://www.multichatai.com/api/chat/deepinfra",
29
+ json={
30
+ "chatSettings": {
31
+ "model": request.model,
32
+ "prompt": "You are helpful.",
33
+ "temperature": request.temperature,
34
+ "contextLength": 4096
35
+ },
36
+ "messages": [
37
+ {"role": "system", "content": "You are helpful."},
38
+ *[{"role": m.role, "content": m.content} for m in request.messages]
39
+ ]
40
+ },
41
  timeout=10
42
  )
43
+ response.raise_for_status()
44
+
45
+ return JSONResponse({
 
 
 
 
 
 
46
  "id": str(uuid.uuid4()),
47
  "object": "chat.completion",
48
  "created": int(datetime.now().timestamp()),
 
53
  "content": response.text.strip()
54
  }
55
  }]
56
+ })
 
 
 
57
  except Exception as e:
58
+ raise HTTPException(status_code=500, detail=str(e))
59
+
60
+ if __name__ == "__main__":
61
+ uvicorn.run("app:app", host="0.0.0.0", port=7860, reload=False)