-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.py
106 lines (94 loc) · 6.56 KB
/
data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from datetime import datetime
from typing import List,Optional
from pydantic import BaseModel,Field
from PIL import Image
class ScreenCaptureData(BaseModel):
context_id: Optional[int] = Field(default=None,description="The id of the context table entry to which this screen capture belongs.Generated by DB.")
session_id: Optional[int] = Field(default=None,description="The id of the session table entry to which this screen capture belongs.Generated by DB.")
# image: Image.Image = Field(default=None,description="The image of the screen capture.") # remove cause we cannot serialize it
notes: Optional[List[str]] = Field(description="List of strings. User added notes",default=[])
resources: Optional[List[str]] = Field(description="List of Strings. URLs, paths, people/contacts, etc.",default=[])
created_at: Optional[datetime] = Field(description="The timestamp of the screen capture. Automatically generated by the system.",default_factory=datetime.now)
# LLM useful information
main_topic: str = Field(description="The main topic of the screen capture.")
summary: str = Field(description="A summary of the screen capture.")
is_learning_moment: bool = Field(description="Boolean. Whether the screen capture is a learning moment.")
learning_observations: Optional[List[str]] = Field(description="List of strings, representing observations about the screen capture that are useful for learning.",default=None)
def serialize(self) -> dict:
"""
Converts the model to a dict with all collection types (lists) converted to strings.
This is useful for database storage where we want to store collections as delimited strings.
"""
data = self.model_dump()
for key, value in data.items():
if isinstance(value, list):
data[key] = "\n".join(str(item) for item in value) if value else ""
elif isinstance(value, datetime):
data[key] = value.isoformat()
return data
class ContextData(BaseModel):
"""Note: This is a shared contract with the user interface , currently just raycast."""
id: Optional[int] = Field(description="The id of the context table entry.Generated by DB.")
name: str
color: str
description: str = Field(default="",description="A short description of the context.")
last_active: datetime = Field(default_factory=datetime.now)
class SessionSummary(BaseModel):
overview: str = Field(description="A high level overview of the session.")
key_topics: List[str] = Field(description="A list of key topics engaged with during the session.Include only the most important topics, not subtopics.")
learning_highlights: List[str] = Field(description="A list of learning highlights from the session.This should be a list of strings, each string capturing a single unique and pivotal aspect of the session.")
resources_used: List[str] = Field(description="A list of resources used during the session.Try to include urls, file paths, people/contacts, etc. Strictly do not hallucinate, if you are not sure about a resource, set it to empty list.")
conclusion: str = Field(description="A conclusion of the session based on the key topics and learning highlights.Try to include a list of the most important tasks that were accomplished and possible next steps.")
class SessionData(BaseModel):
session_id: int = Field(description="The id of the session table entry.Generated by DB.")
context_id: int = Field(description="The id of the context table entry to which this session belongs.Generated by DB.")
start_time: datetime = Field(description="The start time of the session.")
end_time: Optional[datetime] = Field(description="The end time of the session.")
overview: str = Field(description="A high level overview of the session.",default="")
key_topics: List[str] = Field(description="A list of key topics engaged with during the session.Include only the most important topics, not subtopics.",default=[])
learning_highlights: List[str] = Field(description="A list of learning highlights from the session.This should be a list of strings, each string capturing a single unique and pivotal aspect of the session.",default=[])
resources_used: List[str] = Field(description="A list of resources used during the session.Try to include urls, file paths, people/contacts, etc. Strictly do not hallucinate, if you are not sure about a resource, set it to empty list.",default=[])
conclusion: str = Field(description="A conclusion of the session based on the key topics and learning highlights.Try to include a list of the most important tasks that were accomplished and possible next steps.",default="")
def serialize(self) -> dict:
"""
Converts the model to a dict with all collection types (lists) converted to strings.
This is useful for database storage where we want to store collections as delimited strings.
"""
data = self.model_dump()
for key, value in data.items():
if isinstance(value, list):
data[key] = "\n".join(str(item) for item in value) if value else ""
elif isinstance(value, datetime):
data[key] = value.isoformat()
return data
def deserialize(self,data: dict) -> 'SessionData':
"""
Converts the dict back to a model instance.
"""
for key, value in data.items():
if isinstance(value, str) and value.strip():
if key in ["start_time", "end_time"]:
setattr(self, key, datetime.fromisoformat(value))
elif key in ["key_topics", "learning_highlights","resources_used"]:
setattr(self, key, value.split("\n"))
else:
setattr(self, key, value)
return self
@classmethod
def from_db_row(cls, row: tuple) -> 'SessionData':
"""Create a SessionData instance from a database row tuple"""
return cls(
session_id=row[0],
context_id=row[1],
start_time=datetime.fromisoformat(row[2]),
end_time=datetime.fromisoformat(row[3]),
overview=row[4],
key_topics=row[5].split("\n"),
learning_highlights=row[6].split("\n"),
resources_used=row[7].split("\n"),
conclusion=row[8]
)
class SessionMD(BaseModel):
session_id: int = Field(description="The id of the session table entry.Generated by DB.")
name: str = Field(description="The title of the session.")
markdown: str = Field(description="The markdown content of the session.")