🐛 Bug: Fix the bug where request information cannot be stored in the database.
Browse files
main.py
CHANGED
|
@@ -261,29 +261,6 @@ if not DISABLE_DATABASE:
|
|
| 261 |
from starlette.types import Scope, Receive, Send
|
| 262 |
from starlette.responses import Response
|
| 263 |
|
| 264 |
-
from decimal import Decimal, getcontext
|
| 265 |
-
|
| 266 |
-
# 设置全局精度
|
| 267 |
-
getcontext().prec = 17 # 设置为17是为了确保15位小数的精度
|
| 268 |
-
|
| 269 |
-
def calculate_cost(model: str, input_tokens: int, output_tokens: int) -> Decimal:
|
| 270 |
-
costs = {
|
| 271 |
-
"gpt-4": {"input": Decimal('5.0') / Decimal('1000000'), "output": Decimal('15.0') / Decimal('1000000')},
|
| 272 |
-
"claude-3-sonnet": {"input": Decimal('3.0') / Decimal('1000000'), "output": Decimal('15.0') / Decimal('1000000')}
|
| 273 |
-
}
|
| 274 |
-
|
| 275 |
-
if model not in costs:
|
| 276 |
-
logger.error(f"Unknown model: {model}")
|
| 277 |
-
return 0
|
| 278 |
-
|
| 279 |
-
model_costs = costs[model]
|
| 280 |
-
input_cost = Decimal(input_tokens) * model_costs["input"]
|
| 281 |
-
output_cost = Decimal(output_tokens) * model_costs["output"]
|
| 282 |
-
total_cost = input_cost + output_cost
|
| 283 |
-
|
| 284 |
-
# 返回精确到15位小数的结果
|
| 285 |
-
return total_cost.quantize(Decimal('0.000000000000001'))
|
| 286 |
-
|
| 287 |
from asyncio import Semaphore
|
| 288 |
|
| 289 |
# 创建一个信号量来控制数据库访问
|
|
@@ -383,9 +360,9 @@ class LoggingStreamingResponse(Response):
|
|
| 383 |
await self.body_iterator.aclose()
|
| 384 |
self._closed = True
|
| 385 |
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
|
| 390 |
async def _logging_iterator(self):
|
| 391 |
try:
|
|
|
|
| 261 |
from starlette.types import Scope, Receive, Send
|
| 262 |
from starlette.responses import Response
|
| 263 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
from asyncio import Semaphore
|
| 265 |
|
| 266 |
# 创建一个信号量来控制数据库访问
|
|
|
|
| 360 |
await self.body_iterator.aclose()
|
| 361 |
self._closed = True
|
| 362 |
|
| 363 |
+
process_time = time() - self.current_info["start_time"]
|
| 364 |
+
self.current_info["process_time"] = process_time
|
| 365 |
+
await update_stats(self.current_info)
|
| 366 |
|
| 367 |
async def _logging_iterator(self):
|
| 368 |
try:
|