Update main.py
Browse files
main.py
CHANGED
@@ -582,6 +582,24 @@ async def analyze_image(image_data_uri: str) -> str:
|
|
582 |
logger.error(f"Failed to analyze image: {e}")
|
583 |
raise HTTPException(status_code=400, detail="Failed to process the provided image.")
|
584 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
585 |
# Endpoint: POST /v1/chat/completions
|
586 |
@app.post("/v1/chat/completions", dependencies=[Depends(rate_limiter_per_ip)])
|
587 |
async def chat_completions(request: ChatRequest, req: Request, api_key: str = Depends(get_api_key)):
|
@@ -613,8 +631,8 @@ async def chat_completions(request: ChatRequest, req: Request, api_key: str = De
|
|
613 |
# Example response content
|
614 |
assistant_content += "Based on the image you provided, here are the insights..."
|
615 |
|
616 |
-
# Calculate token usage
|
617 |
-
prompt_tokens =
|
618 |
completion_tokens = count_tokens(assistant_content)
|
619 |
total_tokens = prompt_tokens + completion_tokens
|
620 |
estimated_cost = calculate_estimated_cost(prompt_tokens, completion_tokens)
|
|
|
582 |
logger.error(f"Failed to analyze image: {e}")
|
583 |
raise HTTPException(status_code=400, detail="Failed to process the provided image.")
|
584 |
|
585 |
+
# Helper Function for Token Counting
|
586 |
+
def count_prompt_tokens(request: ChatRequest) -> int:
|
587 |
+
"""
|
588 |
+
Counts the number of tokens in the prompt (input messages).
|
589 |
+
Handles both string and list types for the 'content' field.
|
590 |
+
"""
|
591 |
+
total = 0
|
592 |
+
for msg in request.messages:
|
593 |
+
if isinstance(msg.content, str):
|
594 |
+
total += count_tokens(msg.content)
|
595 |
+
elif isinstance(msg.content, list):
|
596 |
+
for item in msg.content:
|
597 |
+
if isinstance(item, TextContent):
|
598 |
+
total += count_tokens(item.text)
|
599 |
+
elif isinstance(item, ImageContent):
|
600 |
+
total += count_tokens(item.image_url['url'])
|
601 |
+
return total
|
602 |
+
|
603 |
# Endpoint: POST /v1/chat/completions
|
604 |
@app.post("/v1/chat/completions", dependencies=[Depends(rate_limiter_per_ip)])
|
605 |
async def chat_completions(request: ChatRequest, req: Request, api_key: str = Depends(get_api_key)):
|
|
|
631 |
# Example response content
|
632 |
assistant_content += "Based on the image you provided, here are the insights..."
|
633 |
|
634 |
+
# Calculate token usage using the helper function
|
635 |
+
prompt_tokens = count_prompt_tokens(request)
|
636 |
completion_tokens = count_tokens(assistant_content)
|
637 |
total_tokens = prompt_tokens + completion_tokens
|
638 |
estimated_cost = calculate_estimated_cost(prompt_tokens, completion_tokens)
|