Update main.py
Browse files
main.py
CHANGED
@@ -586,7 +586,7 @@ async def chat_completions(request: ChatRequest, req: Request, api_key: str = De
|
|
586 |
async def generate():
|
587 |
try:
|
588 |
assistant_content = ""
|
589 |
-
prompt_tokens =
|
590 |
completion_tokens = 0
|
591 |
|
592 |
async for chunk in async_generator:
|
@@ -599,6 +599,7 @@ async def chat_completions(request: ChatRequest, req: Request, api_key: str = De
|
|
599 |
else:
|
600 |
# Assuming 'chunk' is a string of text
|
601 |
assistant_content += chunk
|
|
|
602 |
response_chunk = {
|
603 |
"id": f"chatcmpl-{uuid.uuid4()}",
|
604 |
"object": "chat.completion.chunk",
|
@@ -616,8 +617,6 @@ async def chat_completions(request: ChatRequest, req: Request, api_key: str = De
|
|
616 |
yield f"data: {json.dumps(response_chunk)}\n\n"
|
617 |
|
618 |
# After all chunks are sent, calculate tokens and estimated cost
|
619 |
-
prompt_tokens = sum(len(msg['content'].split()) for msg in request.messages)
|
620 |
-
completion_tokens = len(assistant_content.split())
|
621 |
total_tokens = prompt_tokens + completion_tokens
|
622 |
estimated_cost = calculate_estimated_cost(prompt_tokens, completion_tokens)
|
623 |
|
|
|
586 |
async def generate():
|
587 |
try:
|
588 |
assistant_content = ""
|
589 |
+
prompt_tokens = sum(len(msg.content.split()) for msg in request.messages)
|
590 |
completion_tokens = 0
|
591 |
|
592 |
async for chunk in async_generator:
|
|
|
599 |
else:
|
600 |
# Assuming 'chunk' is a string of text
|
601 |
assistant_content += chunk
|
602 |
+
completion_tokens += len(chunk.split())
|
603 |
response_chunk = {
|
604 |
"id": f"chatcmpl-{uuid.uuid4()}",
|
605 |
"object": "chat.completion.chunk",
|
|
|
617 |
yield f"data: {json.dumps(response_chunk)}\n\n"
|
618 |
|
619 |
# After all chunks are sent, calculate tokens and estimated cost
|
|
|
|
|
620 |
total_tokens = prompt_tokens + completion_tokens
|
621 |
estimated_cost = calculate_estimated_cost(prompt_tokens, completion_tokens)
|
622 |
|