nitrox commited on
Commit
610d6eb
·
verified ·
1 Parent(s): 893627d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -13
app.py CHANGED
@@ -34,12 +34,11 @@ class ChatCompletionRequest(BaseModel):
34
  temperature: float = 0.7
35
 
36
  def count_tokens(text: str) -> int:
37
- # Более точный подсчет токенов
38
- # Считаем слова, знаки препинания и специальные символы
39
  words = text.split()
40
  punctuation = sum(1 for c in text if c in ".,!?;:()[]{}")
41
- special_chars = sum(1 for c in text if c in "—«»…")
42
- return len(words) + punctuation + special_chars
43
 
44
  def clean_assistant_response(text: str) -> str:
45
  # Удаляем лишние маркеры кода и форматирования
@@ -90,9 +89,6 @@ async def create_chat_completion(request: ChatCompletionRequest):
90
  if last_message.role != "user":
91
  raise HTTPException(status_code=400, detail="Last message must be from user")
92
 
93
- # Подсчитываем токены запроса
94
- prompt_tokens = count_tokens(last_message.content)
95
-
96
  # Формируем запрос к Flowise
97
  flowise_request = {
98
  "question": last_message.content
@@ -113,9 +109,6 @@ async def create_chat_completion(request: ChatCompletionRequest):
113
  flowise_response = response.json()
114
  assistant_response = clean_assistant_response(flowise_response.get("text", ""))
115
 
116
- # Подсчитываем токены ответа
117
- completion_tokens = count_tokens(assistant_response)
118
-
119
  response = JSONResponse({
120
  "id": "chatcmpl-" + os.urandom(12).hex(),
121
  "object": "chat.completion",
@@ -133,9 +126,8 @@ async def create_chat_completion(request: ChatCompletionRequest):
133
  }
134
  ],
135
  "usage": {
136
- "prompt_tokens": prompt_tokens,
137
- "completion_tokens": completion_tokens,
138
- "total_tokens": prompt_tokens + completion_tokens
139
  },
140
  "stats": {},
141
  "system_fingerprint": "phi4-r1"
 
34
  temperature: float = 0.7
35
 
36
  def count_tokens(text: str) -> int:
37
+ # Используем тот же алгоритм, что и в прямом API
38
+ # Считаем слова и знаки препинания
39
  words = text.split()
40
  punctuation = sum(1 for c in text if c in ".,!?;:()[]{}")
41
+ return len(words) + punctuation
 
42
 
43
  def clean_assistant_response(text: str) -> str:
44
  # Удаляем лишние маркеры кода и форматирования
 
89
  if last_message.role != "user":
90
  raise HTTPException(status_code=400, detail="Last message must be from user")
91
 
 
 
 
92
  # Формируем запрос к Flowise
93
  flowise_request = {
94
  "question": last_message.content
 
109
  flowise_response = response.json()
110
  assistant_response = clean_assistant_response(flowise_response.get("text", ""))
111
 
 
 
 
112
  response = JSONResponse({
113
  "id": "chatcmpl-" + os.urandom(12).hex(),
114
  "object": "chat.completion",
 
126
  }
127
  ],
128
  "usage": {
129
+ "completion_tokens": 0, # Устанавливаем 0, так как это не важно
130
+ "total_tokens": 0 # Устанавливаем 0, так как это не важно
 
131
  },
132
  "stats": {},
133
  "system_fingerprint": "phi4-r1"