Skip to content

Commit

Permalink
first version of chat
Browse files Browse the repository at this point in the history
  • Loading branch information
GuidoGrogger committed Sep 30, 2024
1 parent e6c74a2 commit 881c2cc
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 8 deletions.
22 changes: 19 additions & 3 deletions backend/app/ai_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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 "<agenda>" in assistant_response and "</agenda>" in assistant_response:
agenda_start = assistant_response.index("<agenda>") + len("<agenda>")
agenda_end = assistant_response.index("</agenda>")
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
Expand All @@ -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__":
Expand Down
3 changes: 2 additions & 1 deletion backend/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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():
Expand Down
31 changes: 29 additions & 2 deletions backend/app/routers/meetings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand All @@ -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
Expand Down
12 changes: 12 additions & 0 deletions backend/app/routers/test.py
Original file line number Diff line number Diff line change
@@ -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) ...
4 changes: 2 additions & 2 deletions backend/app/util/openai.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
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
conversation.chat_messages = []
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

0 comments on commit 881c2cc

Please sign in to comment.