Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -34,6 +34,12 @@ class ChatCompletionRequest(BaseModel):
|
|
34 |
messages: List[ChatMessage]
|
35 |
temperature: float = 0.7
|
36 |
stream: bool = False
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
def count_tokens(text: str) -> int:
|
39 |
# Используем тот же алгоритм, что и в прямом API
|
@@ -116,17 +122,43 @@ async def get_models():
|
|
116 |
except requests.RequestException as e:
|
117 |
raise HTTPException(status_code=500, detail=str(e))
|
118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
@app.post("/v1/chat/completions")
|
120 |
async def create_chat_completion(request: ChatCompletionRequest):
|
121 |
try:
|
122 |
-
#
|
123 |
-
|
124 |
-
|
125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
|
127 |
# Формируем запрос к Flowise
|
128 |
flowise_request = {
|
129 |
-
"question":
|
|
|
|
|
130 |
}
|
131 |
|
132 |
# Засекаем время начала запроса
|
@@ -136,7 +168,7 @@ async def create_chat_completion(request: ChatCompletionRequest):
|
|
136 |
response = requests.post(
|
137 |
f"{FLOWISE_API_BASE_URL}/prediction/{FLOWISE_CHATFLOW_ID}",
|
138 |
json=flowise_request,
|
139 |
-
timeout=10
|
140 |
)
|
141 |
response.raise_for_status()
|
142 |
|
@@ -152,7 +184,7 @@ async def create_chat_completion(request: ChatCompletionRequest):
|
|
152 |
)
|
153 |
|
154 |
# Подсчитываем токены
|
155 |
-
prompt_tokens = count_tokens(
|
156 |
completion_tokens = count_tokens(assistant_response)
|
157 |
|
158 |
response = JSONResponse({
|
|
|
34 |
messages: List[ChatMessage]
|
35 |
temperature: float = 0.7
|
36 |
stream: bool = False
|
37 |
+
frequency_penalty: float = 0.0
|
38 |
+
presence_penalty: float = 0.0
|
39 |
+
max_tokens: int = 512
|
40 |
+
seed: int = None
|
41 |
+
top_p: float = 1.0
|
42 |
+
tools: List[Any] = None
|
43 |
|
44 |
def count_tokens(text: str) -> int:
|
45 |
# Используем тот же алгоритм, что и в прямом API
|
|
|
122 |
except requests.RequestException as e:
|
123 |
raise HTTPException(status_code=500, detail=str(e))
|
124 |
|
125 |
+
def extract_system_prompt(messages: List[ChatMessage]) -> str:
|
126 |
+
"""Извлекает system prompt из сообщений"""
|
127 |
+
for msg in messages:
|
128 |
+
if msg.role == "system":
|
129 |
+
return msg.content
|
130 |
+
return ""
|
131 |
+
|
132 |
+
def get_conversation_history(messages: List[ChatMessage]) -> List[Dict[str, str]]:
|
133 |
+
"""Преобразует сообщения в формат для Flowise"""
|
134 |
+
history = []
|
135 |
+
for msg in messages:
|
136 |
+
if msg.role in ["user", "assistant"]:
|
137 |
+
history.append({
|
138 |
+
"role": msg.role,
|
139 |
+
"content": msg.content
|
140 |
+
})
|
141 |
+
return history
|
142 |
+
|
143 |
@app.post("/v1/chat/completions")
|
144 |
async def create_chat_completion(request: ChatCompletionRequest):
|
145 |
try:
|
146 |
+
# Извлекаем system prompt
|
147 |
+
system_prompt = extract_system_prompt(request.messages)
|
148 |
+
|
149 |
+
# Получаем историю диалога
|
150 |
+
conversation_history = get_conversation_history(request.messages)
|
151 |
+
|
152 |
+
# Получаем последнее сообщение пользователя
|
153 |
+
last_user_message = next((msg for msg in reversed(request.messages) if msg.role == "user"), None)
|
154 |
+
if not last_user_message:
|
155 |
+
raise HTTPException(status_code=400, detail="No user message found")
|
156 |
|
157 |
# Формируем запрос к Flowise
|
158 |
flowise_request = {
|
159 |
+
"question": last_user_message.content,
|
160 |
+
"system_prompt": system_prompt,
|
161 |
+
"conversation_history": conversation_history
|
162 |
}
|
163 |
|
164 |
# Засекаем время начала запроса
|
|
|
168 |
response = requests.post(
|
169 |
f"{FLOWISE_API_BASE_URL}/prediction/{FLOWISE_CHATFLOW_ID}",
|
170 |
json=flowise_request,
|
171 |
+
timeout=10
|
172 |
)
|
173 |
response.raise_for_status()
|
174 |
|
|
|
184 |
)
|
185 |
|
186 |
# Подсчитываем токены
|
187 |
+
prompt_tokens = count_tokens(last_user_message.content)
|
188 |
completion_tokens = count_tokens(assistant_response)
|
189 |
|
190 |
response = JSONResponse({
|