From 881c2cce0285fe01a72035743e21d23366cc7c84 Mon Sep 17 00:00:00 2001 From: GuidoGrogger Date: Mon, 30 Sep 2024 21:44:33 +0200 Subject: [PATCH] first version of chat --- backend/app/ai_manager.py | 22 +++++++++++++++++++--- backend/app/main.py | 3 ++- backend/app/routers/meetings.py | 31 +++++++++++++++++++++++++++++-- backend/app/routers/test.py | 12 ++++++++++++ backend/app/util/openai.py | 4 ++-- 5 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 backend/app/routers/test.py diff --git a/backend/app/ai_manager.py b/backend/app/ai_manager.py index 70b6697..9f83960 100644 --- a/backend/app/ai_manager.py +++ b/backend/app/ai_manager.py @@ -38,6 +38,9 @@ def process_user_message(system_message: str, messages: List[str]) -> Dict[str, Process the user's message and return the assistant's response along with the agenda if present. """ try: + # Debug: Print input messages + print("Debug: Input messages:", messages) + # Call the OpenAI API with the system message and messages list response = client.chat.completions.create( model="gpt-4o", @@ -50,12 +53,19 @@ def process_user_message(system_message: str, messages: List[str]) -> Dict[str, ) assistant_response = response.choices[0].message.content + # Debug: Print raw assistant response + print("Debug: Raw assistant response:", assistant_response) + # Parse agenda if present agenda = None if "" in assistant_response and "" in assistant_response: agenda_start = assistant_response.index("") + len("") agenda_end = assistant_response.index("") agenda_str = assistant_response[agenda_start:agenda_end].strip() + + # Debug: Print agenda string before parsing + print("Debug: Agenda string before parsing:", agenda_str) + agenda = json.loads(f"[{agenda_str}]") # Remove the agenda from the assistant's response @@ -64,17 +74,23 @@ def process_user_message(system_message: str, messages: List[str]) -> Dict[str, # Remove #EOC# marker from the response assistant_response = assistant_response.replace("#EOC#", "").strip() + # Debug: Print final processed response and agenda + print("Debug: Processed response:", assistant_response) + print("Debug: Processed agenda:", agenda) + return { "response": assistant_response, "agenda": agenda, - "conversation_ended": "#EOC#" in response.choices[0].message.content # Check for #EOC# in the original response + "conversation_ended": "#EOC#" in response.choices[0].message.content } except Exception as e: - print(f"Error in creating chat completion: {e}") + # Debug: Print full exception details + import traceback + print(f"Debug: Full exception details:\n{traceback.format_exc()}") return { "response": "I apologize, but I encountered an error while processing your request.", "agenda": None, - "conversation_ended": False # Add this line + "conversation_ended": False } if __name__ == "__main__": diff --git a/backend/app/main.py b/backend/app/main.py index dc1de32..239b66d 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -6,7 +6,7 @@ from fastapi.middleware.cors import CORSMiddleware from .database import engine from . import models -from .routers import meetings, users +from .routers import meetings, users, test import os models.Base.metadata.create_all(bind=engine) @@ -34,6 +34,7 @@ # Include the router with the /api prefix app.include_router(meetings.router, tags=["meetings"]) app.include_router(users.router, tags=["users"]) +app.include_router(test.router, tags=["test"]) @app.get("/", include_in_schema=False) async def serve_index(): diff --git a/backend/app/routers/meetings.py b/backend/app/routers/meetings.py index a5b497e..363b677 100644 --- a/backend/app/routers/meetings.py +++ b/backend/app/routers/meetings.py @@ -4,9 +4,10 @@ from typing import List from app.routers.users import get_user +from app.ai_manager import generate_initial_prompt, process_user_message from .. import schemas from ..database import get_db -from ..models import Conversation, Meeting, ChatMessage +from ..models import Conversation, Meeting, ChatMessage, MeetingAgenda from ..util.openai import init_conversation, generate_response router = APIRouter() @@ -23,6 +24,12 @@ def get_conversation(db: Session, meeting_id: int, user_id: int) -> Conversation def create_new_conversation(db: Session, meeting_id: int, user_id: int): db_conversation = Conversation(meeting_id=meeting_id, user_id=user_id, system_prompt="") + + prompt = generate_initial_prompt("test", "Guido") + db_conversation.system_prompt = prompt + initial_message = process_user_message(prompt, []) + + db_conversation.chat_messages = [ChatMessage(message=initial_message["response"], author="assistant", timestamp=datetime.now())] db.add(db_conversation) db.commit() db.refresh(db_conversation) @@ -41,7 +48,27 @@ def add_message(db: Session, meeting_id: int, user_id: int, message: schemas.Cha if db_conversation is None: raise HTTPException(status_code=404, detail="Conversation not found") db_conversation.chat_messages.append(message) - db_conversation = generate_response(db_conversation) + + # Convert chat_messages to a list of strings + chat_history = [msg.message for msg in db_conversation.chat_messages] + + assistant_response = process_user_message(db_conversation.system_prompt, chat_history) + + # Add the assistant's response as a new ChatMessage + db_conversation.chat_messages.append(ChatMessage( + message=assistant_response["response"], + author="assistant", + timestamp=datetime.now() + )) + + # Create MeetingAgenda objects only if agenda exists and is not empty + if "agenda" in assistant_response and assistant_response["agenda"]: + for agenda_item in assistant_response["agenda"]: + db_conversation.meeting_agenda.append(MeetingAgenda( + agenda_item=agenda_item, + completed=False + )) + db.commit() db.refresh(db_conversation) return db_conversation diff --git a/backend/app/routers/test.py b/backend/app/routers/test.py new file mode 100644 index 0000000..dd049a8 --- /dev/null +++ b/backend/app/routers/test.py @@ -0,0 +1,12 @@ +from fastapi import APIRouter + +router = APIRouter() + +@router.get("/test") +async def test_endpoint(): + return { + "meeting-description": "dummy", + "chat": ["message1", "message2"] + } + +# ... existing code (if any) ... diff --git a/backend/app/util/openai.py b/backend/app/util/openai.py index 8ed6e83..bf8daef 100644 --- a/backend/app/util/openai.py +++ b/backend/app/util/openai.py @@ -1,5 +1,5 @@ from openai import OpenAI -from app.schemas import ChatMessage, Conversation +from app.models import Conversation from datetime import datetime, timezone def init_conversation(conversation: Conversation) -> Conversation: #TODO: Implement this function @@ -7,6 +7,6 @@ def init_conversation(conversation: Conversation) -> Conversation: return conversation # Evaluates if it's part of the conversation or should be added as a meeting agenda -def generate_response(conversation: Conversation, meeting_type: str) -> Conversation: +def generate_response(conversation: Conversation) -> Conversation: #TODO: Implement this function return conversation \ No newline at end of file