nitrox commited on
Commit
0e91dea
·
verified ·
1 Parent(s): ce08ada

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -14
app.py CHANGED
@@ -1,12 +1,13 @@
1
  from fastapi import FastAPI, HTTPException
2
  from fastapi.middleware.cors import CORSMiddleware
3
- from fastapi.responses import StreamingResponse, JSONResponse
4
  import os
5
  from dotenv import load_dotenv
6
  import requests
7
  from typing import Dict, Any, List
8
  from pydantic import BaseModel
9
  import time
 
10
 
11
  load_dotenv()
12
 
@@ -43,9 +44,21 @@ def clean_assistant_response(text: str) -> str:
43
  text = text[:-3].strip()
44
  return text
45
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  @app.get("/")
47
  async def root():
48
- return {"status": "FastFlowWrapper is running"}
49
 
50
  @app.get("/v1/models")
51
  async def get_models():
@@ -61,15 +74,15 @@ async def get_models():
61
  models.append({
62
  "id": chatflow.get("id"),
63
  "object": "model",
64
- "created": int(time.time()), # Текущий timestamp
65
  "owned_by": "flowise",
66
  "permission": [],
67
  "root": "flowise",
68
  "parent": None,
69
- "system_fingerprint": "phi4-r1" # Добавляем system_fingerprint
70
  })
71
 
72
- return {"object": "list", "data": models}
73
  except requests.RequestException as e:
74
  raise HTTPException(status_code=500, detail=str(e))
75
 
@@ -92,10 +105,11 @@ async def create_chat_completion(request: ChatCompletionRequest):
92
  # Засекаем время начала запроса
93
  start_time = time.time()
94
 
95
- # Отправляем запрос к Flowise
96
  response = requests.post(
97
  f"{FLOWISE_API_BASE_URL}/prediction/{FLOWISE_CHATFLOW_ID}",
98
- json=flowise_request
 
99
  )
100
  response.raise_for_status()
101
 
@@ -106,15 +120,15 @@ async def create_chat_completion(request: ChatCompletionRequest):
106
  # Подсчитываем токены ответа
107
  completion_tokens = count_tokens(assistant_response)
108
 
109
- return {
110
  "id": "chatcmpl-" + os.urandom(12).hex(),
111
  "object": "chat.completion",
112
- "created": int(start_time), # Используем время начала запроса
113
- "model": "phi4-r1", # Используем тот же model_id что и в прямом API
114
  "choices": [
115
  {
116
  "index": 0,
117
- "logprobs": None, # Добавляем поле logprobs
118
  "finish_reason": "stop",
119
  "message": {
120
  "role": "assistant",
@@ -127,8 +141,8 @@ async def create_chat_completion(request: ChatCompletionRequest):
127
  "completion_tokens": completion_tokens,
128
  "total_tokens": prompt_tokens + completion_tokens
129
  },
130
- "stats": {}, # Добавляем пустой объект stats
131
- "system_fingerprint": "phi4-r1" # Добавляем system_fingerprint
132
- }
133
  except requests.RequestException as e:
134
  raise HTTPException(status_code=500, detail=str(e))
 
1
  from fastapi import FastAPI, HTTPException
2
  from fastapi.middleware.cors import CORSMiddleware
3
+ from fastapi.responses import JSONResponse
4
  import os
5
  from dotenv import load_dotenv
6
  import requests
7
  from typing import Dict, Any, List
8
  from pydantic import BaseModel
9
  import time
10
+ import json
11
 
12
  load_dotenv()
13
 
 
44
  text = text[:-3].strip()
45
  return text
46
 
47
+ class CustomJSONResponse(JSONResponse):
48
+ media_type = "application/json; charset=utf-8"
49
+
50
+ def render(self, content: Any) -> bytes:
51
+ return json.dumps(
52
+ content,
53
+ ensure_ascii=False,
54
+ allow_nan=False,
55
+ indent=None,
56
+ separators=(',', ':')
57
+ ).encode('utf-8')
58
+
59
  @app.get("/")
60
  async def root():
61
+ return CustomJSONResponse({"status": "FastFlowWrapper is running"})
62
 
63
  @app.get("/v1/models")
64
  async def get_models():
 
74
  models.append({
75
  "id": chatflow.get("id"),
76
  "object": "model",
77
+ "created": int(time.time()),
78
  "owned_by": "flowise",
79
  "permission": [],
80
  "root": "flowise",
81
  "parent": None,
82
+ "system_fingerprint": "phi4-r1"
83
  })
84
 
85
+ return CustomJSONResponse({"object": "list", "data": models})
86
  except requests.RequestException as e:
87
  raise HTTPException(status_code=500, detail=str(e))
88
 
 
105
  # Засекаем время начала запроса
106
  start_time = time.time()
107
 
108
+ # Отправляем запрос к Flowise с таймаутом
109
  response = requests.post(
110
  f"{FLOWISE_API_BASE_URL}/prediction/{FLOWISE_CHATFLOW_ID}",
111
+ json=flowise_request,
112
+ timeout=10 # Уменьшаем таймаут до 10 секунд
113
  )
114
  response.raise_for_status()
115
 
 
120
  # Подсчитываем токены ответа
121
  completion_tokens = count_tokens(assistant_response)
122
 
123
+ return CustomJSONResponse({
124
  "id": "chatcmpl-" + os.urandom(12).hex(),
125
  "object": "chat.completion",
126
+ "created": int(start_time),
127
+ "model": "phi4-r1",
128
  "choices": [
129
  {
130
  "index": 0,
131
+ "logprobs": None,
132
  "finish_reason": "stop",
133
  "message": {
134
  "role": "assistant",
 
141
  "completion_tokens": completion_tokens,
142
  "total_tokens": prompt_tokens + completion_tokens
143
  },
144
+ "stats": {},
145
+ "system_fingerprint": "phi4-r1"
146
+ })
147
  except requests.RequestException as e:
148
  raise HTTPException(status_code=500, detail=str(e))