Skip to content

Commit

Permalink
Merge pull request #13 from shamspias/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
shamspias authored Dec 26, 2023
2 parents eb189a5 + 68d8e0c commit f4e4d59
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 57 deletions.
7 changes: 3 additions & 4 deletions app/api/endpoints/v1/conversation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
6 changes: 3 additions & 3 deletions app/api/endpoints/v1/conversation_delete.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down
35 changes: 0 additions & 35 deletions app/utils/cache_manager.py

This file was deleted.

20 changes: 5 additions & 15 deletions app/utils/message_handler.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -19,22 +18,13 @@ 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}")
raise

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)

0 comments on commit f4e4d59

Please sign in to comment.