print statement replaced with logger

This commit is contained in:
Aditya Vaish 2025-10-16 10:45:46 +05:30
parent 92c0474df4
commit 215a401475
5 changed files with 84 additions and 29 deletions

View file

@ -305,19 +305,18 @@ class AirtableConnector:
) )
# Create Airtable formula for date filtering # Create Airtable formula for date filtering
# filter_formula = ( filter_formula = (
# f"AND(" f"AND("
# f"IS_AFTER({{date_field}}, '{start_date}'), " f"IS_AFTER({{{date_field}}}, '{start_date}'), "
# f"IS_BEFORE({{date_field}}, '{end_date}')" f"IS_BEFORE({{{date_field}}}, '{end_date}')"
# f")" f")"
# ).replace("{date_field}", date_field) )
# TODO: Investigate how to properly use filter formula
return self.get_all_records( return self.get_all_records(
base_id=base_id, base_id=base_id,
table_id=table_id, table_id=table_id,
max_records=max_records, max_records=max_records,
# filter_by_formula=filter_formula, filter_by_formula=filter_formula,
) )
except Exception as e: except Exception as e:

View file

@ -54,8 +54,6 @@ async def handle_chat_data(
request_data.get("document_ids_to_add_in_context") request_data.get("document_ids_to_add_in_context")
) )
search_mode_str = validate_search_mode(request_data.get("search_mode")) search_mode_str = validate_search_mode(request_data.get("search_mode"))
# print("RESQUEST DATA:", request_data)
# print("SELECTED CONNECTORS:", selected_connectors)
# Check if the search space belongs to the current user # Check if the search space belongs to the current user
try: try:
@ -76,7 +74,6 @@ async def handle_chat_data(
) )
) )
user_preference = language_result.scalars().first() user_preference = language_result.scalars().first()
# print("UserSearchSpacePreference:", user_preference)
language = None language = None
if ( if (

View file

@ -1,5 +1,6 @@
# Force asyncio to use standard event loop before unstructured imports # Force asyncio to use standard event loop before unstructured imports
import asyncio import asyncio
import logging
from fastapi import APIRouter, BackgroundTasks, Depends, Form, HTTPException, UploadFile from fastapi import APIRouter, BackgroundTasks, Depends, Form, HTTPException, UploadFile
from litellm import atranscription from litellm import atranscription
@ -40,7 +41,7 @@ from app.utils.check_ownership import check_ownership
try: try:
asyncio.set_event_loop_policy(asyncio.DefaultEventLoopPolicy()) asyncio.set_event_loop_policy(asyncio.DefaultEventLoopPolicy())
except RuntimeError as e: except RuntimeError as e:
print("Error setting event loop policy", e) logging.warning(f"Error setting event loop policy: {e}")
pass pass
import os import os
@ -48,6 +49,7 @@ import os
os.environ["UNSTRUCTURED_HAS_PATCHED_LOOP"] = "1" os.environ["UNSTRUCTURED_HAS_PATCHED_LOOP"] = "1"
logger = logging.getLogger(__name__)
router = APIRouter() router = APIRouter()
@ -740,7 +742,7 @@ async def process_file_in_background(
try: try:
os.unlink(file_path) os.unlink(file_path)
except Exception as e: except Exception as e:
print("Error deleting temp file", e) logger.warning(f"Error deleting temp file {file_path}: {e}")
pass pass
await task_logger.log_task_progress( await task_logger.log_task_progress(
@ -865,7 +867,7 @@ async def process_file_in_background(
try: try:
os.unlink(file_path) os.unlink(file_path)
except Exception as e: except Exception as e:
print("Error deleting temp file", e) logger.warning(f"Error deleting temp file {file_path}: {e}")
pass pass
# Process transcription as markdown document # Process transcription as markdown document
@ -931,7 +933,7 @@ async def process_file_in_background(
try: try:
os.unlink(file_path) os.unlink(file_path)
except Exception as e: except Exception as e:
print("Error deleting temp file", e) logger.warning(f"Error deleting temp file {file_path}: {e}")
pass pass
# Pass the documents to the existing background task # Pass the documents to the existing background task
@ -993,7 +995,7 @@ async def process_file_in_background(
try: try:
os.unlink(file_path) os.unlink(file_path)
except Exception as e: except Exception as e:
print("Error deleting temp file", e) logger.warning(f"Error deleting temp file {file_path}: {e}")
pass pass
# Get markdown documents from the result # Get markdown documents from the result
@ -1071,7 +1073,7 @@ async def process_file_in_background(
try: try:
os.unlink(file_path) os.unlink(file_path)
except Exception as e: except Exception as e:
print("Error deleting temp file", e) logger.warning(f"Error deleting temp file {file_path}: {e}")
pass pass
await task_logger.log_task_progress( await task_logger.log_task_progress(
@ -1121,7 +1123,5 @@ async def process_file_in_background(
str(e), str(e),
{"error_type": type(e).__name__, "filename": filename}, {"error_type": type(e).__name__, "filename": filename},
) )
import logging logger.error(f"Error processing file in background: {e!s}")
logging.error(f"Error processing file in background: {e!s}")
raise # Re-raise so the wrapper can also handle it raise # Re-raise so the wrapper can also handle it

View file

@ -11,8 +11,57 @@ from app.services.task_logging_service import TaskLoggingService
async def generate_document_podcast( async def generate_document_podcast(
session: AsyncSession, document_id: int, search_space_id: int, user_id: int session: AsyncSession, document_id: int, search_space_id: int, user_id: int
): ):
# TODO: Need to fetch the document chunks, then concatenate them and pass them to the podcast generation model """
pass Generate a podcast from a document by fetching its chunks and concatenating them.
Args:
session: Database session
document_id: ID of the document to convert to podcast
search_space_id: Search space ID
user_id: User ID
Returns:
Podcast object or raises exception
"""
task_logger = TaskLoggingService(session, search_space_id)
# Log task start
log_entry = await task_logger.log_task_start(
task_name="generate_document_podcast",
source="podcast_task",
message=f"Starting podcast generation for document {document_id}",
metadata={
"document_id": document_id,
"search_space_id": search_space_id,
"user_id": str(user_id),
},
)
try:
# TODO: Implement document chunk fetching and podcast generation
# This would involve:
# 1. Fetching the document and its chunks
# 2. Concatenating the chunks into readable content
# 3. Passing the content to the podcast generation model
# 4. Creating and saving the podcast
await task_logger.log_task_failure(
log_entry,
"Document podcast generation not yet implemented",
"NotImplementedError",
{"error_type": "NotImplementedError"},
)
raise NotImplementedError("Document podcast generation is not yet implemented")
except Exception as e:
await session.rollback()
await task_logger.log_task_failure(
log_entry,
f"Error during document podcast generation for document {document_id}",
str(e),
{"error_type": type(e).__name__},
)
raise
async def generate_chat_podcast( async def generate_chat_podcast(

View file

@ -1,7 +1,10 @@
import hashlib import hashlib
import logging
from litellm import get_model_info, token_counter from litellm import get_model_info, token_counter
logger = logging.getLogger(__name__)
from app.config import config from app.config import config
from app.db import Chunk, DocumentType from app.db import Chunk, DocumentType
from app.prompts import SUMMARY_PROMPT_TEMPLATE from app.prompts import SUMMARY_PROMPT_TEMPLATE
@ -14,8 +17,8 @@ def get_model_context_window(model_name: str) -> int:
context_window = model_info.get("max_input_tokens", 4096) # Default fallback context_window = model_info.get("max_input_tokens", 4096) # Default fallback
return context_window return context_window
except Exception as e: except Exception as e:
print( logger.warning(
f"Warning: Could not get model info for {model_name}, using default 4096 tokens. Error: {e}" f"Could not get model info for {model_name}, using default 4096 tokens. Error: {e}"
) )
return 4096 # Conservative fallback return 4096 # Conservative fallback
@ -41,9 +44,16 @@ def optimize_content_for_context_window(
context_window = get_model_context_window(model_name) context_window = get_model_context_window(model_name)
# Reserve tokens for: system prompt, metadata, template overhead, and output # Reserve tokens for: system prompt, metadata, template overhead, and output
# Conservative estimate: 2000 tokens for prompt + metadata + output buffer # Calculate actual system prompt token count
# TODO: Calculate Summary System Prompt Token Count Here try:
reserved_tokens = 2000 system_prompt_tokens = token_counter(
messages=[{"role": "system", "content": SUMMARY_PROMPT_TEMPLATE}],
model=model_name
)
reserved_tokens = system_prompt_tokens + 1000 # 1000 tokens for output buffer and overhead
except Exception as e:
logger.warning(f"Could not calculate system prompt tokens, using default: {e}")
reserved_tokens = 2000 # Conservative fallback
# Add metadata token cost if present # Add metadata token cost if present
if document_metadata: if document_metadata:
@ -58,7 +68,7 @@ def optimize_content_for_context_window(
available_tokens = context_window - reserved_tokens available_tokens = context_window - reserved_tokens
if available_tokens <= 100: # Minimum viable content if available_tokens <= 100: # Minimum viable content
print(f"Warning: Very limited tokens available for content: {available_tokens}") logger.warning(f"Very limited tokens available for content: {available_tokens}")
return content[:500] # Fallback to first 500 chars return content[:500] # Fallback to first 500 chars
# Binary search to find optimal content length # Binary search to find optimal content length
@ -86,7 +96,7 @@ def optimize_content_for_context_window(
) )
if optimal_length < len(content): if optimal_length < len(content):
print( logger.info(
f"Content optimized: {len(content)} -> {optimal_length} chars " f"Content optimized: {len(content)} -> {optimal_length} chars "
f"to fit in {available_tokens} available tokens" f"to fit in {available_tokens} available tokens"
) )