diff --git a/app/api/endpoints/v1/conversation.py b/app/api/endpoints/v1/conversation.py index 06aaba4..eb6b4ea 100644 --- a/app/api/endpoints/v1/conversation.py +++ b/app/api/endpoints/v1/conversation.py @@ -2,15 +2,14 @@ from fastapi.responses import StreamingResponse from app.schemas import ChatRequest from app.utils.message_handler import MessageHandler -from app.utils.cache_manager import CacheManager +from app.utils.llm_manager import GeminiLLMManager from app.permissions import verify_api_key -from app.config import settings router = APIRouter() -cache_manager = CacheManager(settings.REDIS_URL) -message_handler = MessageHandler(cache_manager) +llm_manager = GeminiLLMManager() +message_handler = MessageHandler(llm_manager) @router.post("/{conversation_id}") diff --git a/app/api/endpoints/v1/conversation_delete.py b/app/api/endpoints/v1/conversation_delete.py index a0a3ff2..88c3fb1 100644 --- a/app/api/endpoints/v1/conversation_delete.py +++ b/app/api/endpoints/v1/conversation_delete.py @@ -1,6 +1,6 @@ from fastapi import APIRouter, HTTPException, Depends, Header, Response from app.utils.message_handler import MessageHandler -from app.utils.cache_manager import CacheManager +from app.utils.llm_manager import GeminiLLMManager from app.permissions import verify_api_key from app.config import settings @@ -13,8 +13,8 @@ async def config_update(conversation_id: str, api_key: str = Depends(verify_api_key), x_api_key: str = Header(None, alias='x-api-key') ): - cache_manager = CacheManager(settings.REDIS_URL) - message_handler = MessageHandler(cache_manager) + llm_manager = GeminiLLMManager() + message_handler = MessageHandler(llm_manager) try: await message_handler.flush_conversation_cache(conversation_id) diff --git a/app/utils/cache_manager.py b/app/utils/cache_manager.py deleted file mode 100644 index cc16ae0..0000000 --- a/app/utils/cache_manager.py +++ /dev/null @@ -1,35 +0,0 @@ -from redis import asyncio as aioredis -import json - - -class CacheManager: - def __init__(self, redis_url): - self.redis_url = redis_url - self.redis_client = None - - async def connect(self): - # Connect to redis - self.redis_client = await aioredis.from_url(self.redis_url) - - async def get_conversation_config(self, conversation_id): - # Get project configuration from redis - if not self.redis_client: - await self.connect() - project_config = await self.redis_client.get(conversation_id) - if project_config: - return json.loads(project_config) - else: - return None - - async def save_conversation_config(self, conversation_id, project_config, timeout=3600): - # Save project configuration to redis with timeout by default 1 hour(3600 seconds) - if not self.redis_client: - await self.connect() - project_config_json = json.dumps(project_config) - await self.redis_client.set(conversation_id, project_config_json, ex=timeout) - - async def flush_conversation_cache(self, conversation_id): - # Flush project cache - if not self.redis_client: - await self.connect() - await self.redis_client.delete(conversation_id) diff --git a/app/utils/message_handler.py b/app/utils/message_handler.py index 99c73d4..98c6c98 100644 --- a/app/utils/message_handler.py +++ b/app/utils/message_handler.py @@ -1,16 +1,15 @@ import logging -from typing import AsyncIterable, Dict +from typing import AsyncIterable from .llm_manager import GeminiLLMManager -from .cache_manager import CacheManager logger = logging.getLogger(__name__) class MessageHandler: - def __init__(self, cache_manager: CacheManager): - self.cache_manager = cache_manager + def __init__(self, llm_manager: GeminiLLMManager): + self.llm_manager = llm_manager async def send_message_async( self, message: str, @@ -19,11 +18,7 @@ async def send_message_async( image_url: str = None ) -> AsyncIterable[str]: try: - - # LLM configuration - llm_manager = GeminiLLMManager() - - async for token in llm_manager.generate_async_response(message, conversation_id, image, image_url): + async for token in self.llm_manager.generate_async_response(message, conversation_id, image, image_url): yield token except Exception as e: logger.error(f"Error in send_message: {e}") @@ -31,10 +26,5 @@ async def send_message_async( async def flush_conversation_cache(self, project_id: str): # LLM configuration - llm_manager = GeminiLLMManager() - history = llm_manager.create_or_get_memory(project_id) + history = self.llm_manager.create_or_get_memory(project_id) history.clear() - await self.cache_manager.flush_conversation_cache(project_id) - - async def save_conversation_config(self, project_id: str, config: Dict): - await self.cache_manager.save_conversation_config(project_id, config)