Skip to content

Commit ea0e4b4

Browse files
committed
document cli compiles
1 parent f53c5c8 commit ea0e4b4

File tree

5 files changed

+37
-28
lines changed

5 files changed

+37
-28
lines changed

server/cli.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
increment_response_count,
1111
thread_emails_to_openai_messages,
1212
)
13+
from server.models.document import Document
1314
from server.models.email import Email
1415
from server.models.response import Response
1516
from server.models.thread import Thread
@@ -28,8 +29,9 @@ def email():
2829
thread = Thread()
2930
db.session.add(thread)
3031
db.session.commit()
32+
3133
email = Email(
32-
date=datetime.datetime.now(datetime.timezone.utc),
34+
date=datetime.datetime.now(datetime.timezone.utc), # type: ignore
3335
sender="azliu@mit.edu",
3436
subject=subject,
3537
body=body,
@@ -46,14 +48,26 @@ def email():
4648
questions, document_ids, document_confidences = document_data(documents)
4749
db.session.add(email)
4850
db.session.commit()
51+
52+
documents = []
53+
for i, doc_ids_question in enumerate(document_ids):
54+
documents.append([])
55+
for doc_id in doc_ids_question:
56+
document = db.session.execute(
57+
db.select(Document).where(Document.id == doc_id)
58+
).scalar()
59+
if document:
60+
documents[i].append(document)
61+
4962
r = Response(
5063
openai_res,
5164
questions,
52-
document_ids,
65+
documents,
5366
document_confidences,
5467
confidence,
5568
email.id,
5669
)
70+
5771
db.session.add(r)
5872
thread.last_email = email.id
5973
thread.resolved = False

server/controllers/emails.py

+17-22
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import os
77
import re
88
from datetime import datetime, timezone
9+
from typing import List
910

1011
import boto3
1112
from apiflask import APIBlueprint
@@ -33,17 +34,13 @@
3334
emails = APIBlueprint("emails", __name__, url_prefix="/emails", tag="Emails")
3435

3536

36-
def thread_emails_to_openai_messages(thread_emails: list[Email]) -> list[OpenAIMessage]:
37+
def thread_emails_to_openai_messages(thread_emails: List[Email]) -> List[OpenAIMessage]:
3738
"""Converts list of email to openai messages.
3839
39-
Parameters:
40-
----------
41-
thread_email_ids : :obj:`list` of :obj:`Email`
42-
list of email ids
40+
Args:
41+
thread_emails: list of emails
4342
4443
Returns:
45-
-------
46-
:obj:`list` of :obj:`OpenAIMessage`
4744
list of openai messages
4845
"""
4946
openai_messages = []
@@ -54,11 +51,11 @@ def thread_emails_to_openai_messages(thread_emails: list[Email]) -> list[OpenAIM
5451

5552

5653
def document_data(
57-
documents: dict[str, list[RedisDocument]],
58-
) -> tuple[list[str], list[list[int]], list[list[float]]]:
54+
documents: dict[str, List[RedisDocument]],
55+
) -> tuple[List[str], List[List[int]], List[List[float]]]:
5956
"""Process raw openai document output.
6057
61-
Parameters
58+
Args:
6259
----------
6360
documents : :obj:`list` of :obj:`dict`
6461
raw openai document output
@@ -88,14 +85,13 @@ def document_data(
8885
return questions, doc_ids, doc_confidences
8986

9087

91-
def increment_response_count(document_ids: list[list[int]]):
88+
def increment_response_count(document_ids: List[List[int]]):
9289
"""Increment response count for documents.
9390
94-
Parameters
95-
----------
96-
document_ids : :obj:`list` of :obj:`list` of :obj:`int`
97-
list of document ids. each element in the list is a list of document ids used to
98-
answer a specific question
91+
Args:
92+
document_ids : :obj:`list` of :obj:`list` of :obj:`int`
93+
list of document ids. each element in the list is a list of document ids
94+
used to answer a specific question
9995
"""
10096
for doc_ids_question in document_ids:
10197
for doc_id in doc_ids_question:
@@ -105,14 +101,13 @@ def increment_response_count(document_ids: list[list[int]]):
105101
db.session.commit()
106102

107103

108-
def decrement_response_count(document_ids: list[list[int]]):
104+
def decrement_response_count(document_ids: List[List[int]]):
109105
"""Decrement response count for documents.
110106
111-
Parameters
112-
----------
113-
document_ids : :obj:`list` of :obj:`list` of :obj:`int`
114-
list of document ids. each element in the list is a list of document ids used to
115-
answer a specific question
107+
Args:
108+
document_ids : :obj:`list` of :obj:`list` of :obj:`int`
109+
list of document ids. each element in the list is a list of document ids
110+
used to answer a specific question
116111
"""
117112
for doc_ids_question in document_ids:
118113
for doc_id in doc_ids_question:

server/models/email.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ class Email(db.Model):
4444

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

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

5050
def map(self):
5151
"""Map the email to a dictionary."""

server/models/response.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class Response(db.Model):
4444

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

47-
email_id: Mapped[int] = mapped_column(ForeignKey("Emails.id", ondelete="CASCADE"))
47+
email_id: Mapped[str] = mapped_column(ForeignKey("Emails.id", ondelete="CASCADE"))
4848
email: Mapped[Email] = relationship(
4949
back_populates="response", init=False, single_parent=True
5050
)

server/models/thread.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Thread(db.Model):
2626
__tablename__ = "Threads"
2727

2828
id: Mapped[str] = mapped_column(primary_key=True, init=False)
29-
last_email: Mapped[Optional[str]] = mapped_column(nullable=True)
29+
last_email: Mapped[Optional[str]] = mapped_column(nullable=True, init=False)
3030
resolved: Mapped[bool] = mapped_column(nullable=False, default=False)
3131

3232
emails: Mapped[List[Email]] = relationship(

0 commit comments

Comments
 (0)