Skip to content

Commit

Permalink
document cli compiles
Browse files Browse the repository at this point in the history
  • Loading branch information
azliu0 committed Apr 15, 2024
1 parent f53c5c8 commit ea0e4b4
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 28 deletions.
18 changes: 16 additions & 2 deletions server/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
increment_response_count,
thread_emails_to_openai_messages,
)
from server.models.document import Document
from server.models.email import Email
from server.models.response import Response
from server.models.thread import Thread
Expand All @@ -28,8 +29,9 @@ def email():
thread = Thread()
db.session.add(thread)
db.session.commit()

email = Email(
date=datetime.datetime.now(datetime.timezone.utc),
date=datetime.datetime.now(datetime.timezone.utc), # type: ignore
sender="[email protected]",
subject=subject,
body=body,
Expand All @@ -46,14 +48,26 @@ def email():
questions, document_ids, document_confidences = document_data(documents)
db.session.add(email)
db.session.commit()

documents = []
for i, doc_ids_question in enumerate(document_ids):
documents.append([])
for doc_id in doc_ids_question:
document = db.session.execute(
db.select(Document).where(Document.id == doc_id)
).scalar()
if document:
documents[i].append(document)

r = Response(
openai_res,
questions,
document_ids,
documents,
document_confidences,
confidence,
email.id,
)

db.session.add(r)
thread.last_email = email.id
thread.resolved = False
Expand Down
39 changes: 17 additions & 22 deletions server/controllers/emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import os
import re
from datetime import datetime, timezone
from typing import List

import boto3
from apiflask import APIBlueprint
Expand Down Expand Up @@ -33,17 +34,13 @@
emails = APIBlueprint("emails", __name__, url_prefix="/emails", tag="Emails")


def thread_emails_to_openai_messages(thread_emails: list[Email]) -> list[OpenAIMessage]:
def thread_emails_to_openai_messages(thread_emails: List[Email]) -> List[OpenAIMessage]:
"""Converts list of email to openai messages.
Parameters:
----------
thread_email_ids : :obj:`list` of :obj:`Email`
list of email ids
Args:
thread_emails: list of emails
Returns:
-------
:obj:`list` of :obj:`OpenAIMessage`
list of openai messages
"""
openai_messages = []
Expand All @@ -54,11 +51,11 @@ def thread_emails_to_openai_messages(thread_emails: list[Email]) -> list[OpenAIM


def document_data(
documents: dict[str, list[RedisDocument]],
) -> tuple[list[str], list[list[int]], list[list[float]]]:
documents: dict[str, List[RedisDocument]],
) -> tuple[List[str], List[List[int]], List[List[float]]]:
"""Process raw openai document output.
Parameters
Args:
----------
documents : :obj:`list` of :obj:`dict`
raw openai document output
Expand Down Expand Up @@ -88,14 +85,13 @@ def document_data(
return questions, doc_ids, doc_confidences


def increment_response_count(document_ids: list[list[int]]):
def increment_response_count(document_ids: List[List[int]]):
"""Increment response count for documents.
Parameters
----------
document_ids : :obj:`list` of :obj:`list` of :obj:`int`
list of document ids. each element in the list is a list of document ids used to
answer a specific question
Args:
document_ids : :obj:`list` of :obj:`list` of :obj:`int`
list of document ids. each element in the list is a list of document ids
used to answer a specific question
"""
for doc_ids_question in document_ids:
for doc_id in doc_ids_question:
Expand All @@ -105,14 +101,13 @@ def increment_response_count(document_ids: list[list[int]]):
db.session.commit()


def decrement_response_count(document_ids: list[list[int]]):
def decrement_response_count(document_ids: List[List[int]]):
"""Decrement response count for documents.
Parameters
----------
document_ids : :obj:`list` of :obj:`list` of :obj:`int`
list of document ids. each element in the list is a list of document ids used to
answer a specific question
Args:
document_ids : :obj:`list` of :obj:`list` of :obj:`int`
list of document ids. each element in the list is a list of document ids
used to answer a specific question
"""
for doc_ids_question in document_ids:
for doc_id in doc_ids_question:
Expand Down
4 changes: 2 additions & 2 deletions server/models/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ class Email(db.Model):

is_reply: Mapped[bool] = mapped_column(nullable=False)

thread_id: Mapped[int] = mapped_column(ForeignKey("Threads.id"), nullable=False)
thread: Mapped[Thread] = relationship(back_populates="emails")
thread_id: Mapped[str] = mapped_column(ForeignKey("Threads.id"), nullable=False)
thread: Mapped[Thread] = relationship(back_populates="emails", init=False)

def map(self):
"""Map the email to a dictionary."""
Expand Down
2 changes: 1 addition & 1 deletion server/models/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Response(db.Model):

confidence: Mapped[float] = mapped_column(nullable=False)

email_id: Mapped[int] = mapped_column(ForeignKey("Emails.id", ondelete="CASCADE"))
email_id: Mapped[str] = mapped_column(ForeignKey("Emails.id", ondelete="CASCADE"))
email: Mapped[Email] = relationship(
back_populates="response", init=False, single_parent=True
)
Expand Down
2 changes: 1 addition & 1 deletion server/models/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Thread(db.Model):
__tablename__ = "Threads"

id: Mapped[str] = mapped_column(primary_key=True, init=False)
last_email: Mapped[Optional[str]] = mapped_column(nullable=True)
last_email: Mapped[Optional[str]] = mapped_column(nullable=True, init=False)
resolved: Mapped[bool] = mapped_column(nullable=False, default=False)

emails: Mapped[List[Email]] = relationship(
Expand Down

0 comments on commit ea0e4b4

Please sign in to comment.