Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,6 +6,7 @@ from dotenv import load_dotenv
|
|
6 |
import requests
|
7 |
from typing import Dict, Any, List
|
8 |
from pydantic import BaseModel
|
|
|
9 |
|
10 |
load_dotenv()
|
11 |
|
@@ -31,6 +32,17 @@ class ChatCompletionRequest(BaseModel):
|
|
31 |
messages: List[ChatMessage]
|
32 |
temperature: float = 0.7
|
33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
@app.get("/")
|
35 |
async def root():
|
36 |
return {"status": "FastFlowWrapper is running"}
|
@@ -49,7 +61,7 @@ async def get_models():
|
|
49 |
models.append({
|
50 |
"id": chatflow.get("id"),
|
51 |
"object": "model",
|
52 |
-
"created":
|
53 |
"owned_by": "flowise",
|
54 |
"permission": [],
|
55 |
"root": "flowise",
|
@@ -68,11 +80,17 @@ async def create_chat_completion(request: ChatCompletionRequest):
|
|
68 |
if last_message.role != "user":
|
69 |
raise HTTPException(status_code=400, detail="Last message must be from user")
|
70 |
|
|
|
|
|
|
|
71 |
# Формируем запрос к Flowise
|
72 |
flowise_request = {
|
73 |
"question": last_message.content
|
74 |
}
|
75 |
|
|
|
|
|
|
|
76 |
# Отправляем запрос к Flowise
|
77 |
response = requests.post(
|
78 |
f"{FLOWISE_API_BASE_URL}/prediction/{FLOWISE_CHATFLOW_ID}",
|
@@ -80,27 +98,32 @@ async def create_chat_completion(request: ChatCompletionRequest):
|
|
80 |
)
|
81 |
response.raise_for_status()
|
82 |
|
83 |
-
#
|
84 |
flowise_response = response.json()
|
|
|
|
|
|
|
|
|
|
|
85 |
return {
|
86 |
"id": "chatcmpl-" + os.urandom(12).hex(),
|
87 |
"object": "chat.completion",
|
88 |
-
"created": int(
|
89 |
"model": request.model,
|
90 |
"choices": [
|
91 |
{
|
92 |
"index": 0,
|
93 |
"message": {
|
94 |
"role": "assistant",
|
95 |
-
"content":
|
96 |
},
|
97 |
"finish_reason": "stop"
|
98 |
}
|
99 |
],
|
100 |
"usage": {
|
101 |
-
"prompt_tokens":
|
102 |
-
"completion_tokens":
|
103 |
-
"total_tokens":
|
104 |
}
|
105 |
}
|
106 |
except requests.RequestException as e:
|
|
|
6 |
import requests
|
7 |
from typing import Dict, Any, List
|
8 |
from pydantic import BaseModel
|
9 |
+
import time
|
10 |
|
11 |
load_dotenv()
|
12 |
|
|
|
32 |
messages: List[ChatMessage]
|
33 |
temperature: float = 0.7
|
34 |
|
35 |
+
def count_tokens(text: str) -> int:
|
36 |
+
# Простой подсчет токенов (слова + знаки препинания)
|
37 |
+
return len(text.split()) + len([c for c in text if c in ".,!?;:()[]{}"])
|
38 |
+
|
39 |
+
def clean_assistant_response(text: str) -> str:
|
40 |
+
# Удаляем лишние маркеры кода и форматирования
|
41 |
+
text = text.strip()
|
42 |
+
if text.endswith("```"):
|
43 |
+
text = text[:-3].strip()
|
44 |
+
return text
|
45 |
+
|
46 |
@app.get("/")
|
47 |
async def root():
|
48 |
return {"status": "FastFlowWrapper is running"}
|
|
|
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",
|
|
|
80 |
if last_message.role != "user":
|
81 |
raise HTTPException(status_code=400, detail="Last message must be from user")
|
82 |
|
83 |
+
# Подсчитываем токены запроса
|
84 |
+
prompt_tokens = count_tokens(last_message.content)
|
85 |
+
|
86 |
# Формируем запрос к Flowise
|
87 |
flowise_request = {
|
88 |
"question": last_message.content
|
89 |
}
|
90 |
|
91 |
+
# Засекаем время начала запроса
|
92 |
+
start_time = time.time()
|
93 |
+
|
94 |
# Отправляем запрос к Flowise
|
95 |
response = requests.post(
|
96 |
f"{FLOWISE_API_BASE_URL}/prediction/{FLOWISE_CHATFLOW_ID}",
|
|
|
98 |
)
|
99 |
response.raise_for_status()
|
100 |
|
101 |
+
# Получаем и очищаем ответ
|
102 |
flowise_response = response.json()
|
103 |
+
assistant_response = clean_assistant_response(flowise_response.get("text", ""))
|
104 |
+
|
105 |
+
# Подсчитываем токены ответа
|
106 |
+
completion_tokens = count_tokens(assistant_response)
|
107 |
+
|
108 |
return {
|
109 |
"id": "chatcmpl-" + os.urandom(12).hex(),
|
110 |
"object": "chat.completion",
|
111 |
+
"created": int(start_time), # Используем время начала запроса
|
112 |
"model": request.model,
|
113 |
"choices": [
|
114 |
{
|
115 |
"index": 0,
|
116 |
"message": {
|
117 |
"role": "assistant",
|
118 |
+
"content": assistant_response
|
119 |
},
|
120 |
"finish_reason": "stop"
|
121 |
}
|
122 |
],
|
123 |
"usage": {
|
124 |
+
"prompt_tokens": prompt_tokens,
|
125 |
+
"completion_tokens": completion_tokens,
|
126 |
+
"total_tokens": prompt_tokens + completion_tokens
|
127 |
}
|
128 |
}
|
129 |
except requests.RequestException as e:
|