Spaces:
Paused
Paused
Commit ·
8e787c5
1
Parent(s): 2b656f3
fixed system role
Browse files
main.py
CHANGED
|
@@ -60,24 +60,25 @@ def build_notion_request(request_data: ChatCompletionRequest) -> NotionRequestBo
|
|
| 60 |
|
| 61 |
# --- Timestamp and User ID Logic ---
|
| 62 |
user_id = os.getenv("NOTION_ACTIVE_USER_HEADER")
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
| 66 |
|
| 67 |
-
if
|
| 68 |
# Get current time specifically in Pacific Time (America/Los_Angeles)
|
| 69 |
pacific_tz = ZoneInfo("America/Los_Angeles")
|
| 70 |
now_pacific = datetime.now(timezone.utc).astimezone(pacific_tz)
|
| 71 |
|
| 72 |
-
# Assign timestamp to the last
|
| 73 |
-
|
| 74 |
-
|
| 75 |
|
| 76 |
-
# Calculate timestamps for previous
|
| 77 |
current_timestamp = now_pacific
|
| 78 |
-
for i in range(
|
| 79 |
current_timestamp -= timedelta(minutes=random.randint(3, 20)) # Use random interval (3-20 mins)
|
| 80 |
-
|
| 81 |
|
| 82 |
# --- Build Transcript ---
|
| 83 |
# Get current time in Pacific timezone for context
|
|
@@ -121,14 +122,14 @@ def build_notion_request(request_data: ChatCompletionRequest) -> NotionRequestBo
|
|
| 121 |
|
| 122 |
for message in request_data.messages:
|
| 123 |
if message.role == "assistant":
|
| 124 |
-
# Assistant messages get
|
| 125 |
transcript.append(NotionTranscriptItem(
|
| 126 |
type="markdown-chat",
|
| 127 |
value=message.content,
|
| 128 |
traceId=str(uuid.uuid4()) # Generate unique traceId for assistant message
|
| 129 |
))
|
| 130 |
-
|
| 131 |
-
created_at_dt =
|
| 132 |
created_at_iso = None
|
| 133 |
if created_at_dt:
|
| 134 |
# Format timestamp exactly as YYYY-MM-DDTHH:MM:SS.fff-HH:MM
|
|
@@ -139,48 +140,29 @@ def build_notion_request(request_data: ChatCompletionRequest) -> NotionRequestBo
|
|
| 139 |
created_at_iso = f"{dt_str}.{ms}{formatted_tz}"
|
| 140 |
|
| 141 |
content = message.content
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
userId=user_id,
|
| 148 |
-
createdAt=created_at_iso
|
| 149 |
-
))
|
| 150 |
-
elif isinstance(content, list):
|
| 151 |
-
found_text_part = False
|
| 152 |
-
for part in content:
|
| 153 |
if isinstance(part, dict) and part.get("type") == "text":
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
))
|
| 172 |
-
print(f'Warning: No valid text parts found in user message list content: {message}')
|
| 173 |
-
else:
|
| 174 |
-
# Handle unexpected content types with default empty item
|
| 175 |
-
transcript.append(NotionTranscriptItem(
|
| 176 |
-
type="user",
|
| 177 |
-
value=[[""]],
|
| 178 |
-
userId=user_id,
|
| 179 |
-
createdAt=created_at_iso # Still add userId/timestamp
|
| 180 |
-
))
|
| 181 |
-
print(f'Warning: Unexpected content type in user message: {message}')
|
| 182 |
-
# System messages are currently ignored in the Notion transcript based on original logic
|
| 183 |
-
# else: # Handle 'system' role if needed in the future
|
| 184 |
|
| 185 |
# Use globally configured spaceId, set createThread=True
|
| 186 |
return NotionRequestBody(
|
|
|
|
| 60 |
|
| 61 |
# --- Timestamp and User ID Logic ---
|
| 62 |
user_id = os.getenv("NOTION_ACTIVE_USER_HEADER")
|
| 63 |
+
# Get all non-assistant messages to assign timestamps
|
| 64 |
+
non_assistant_messages = [msg for msg in request_data.messages if msg.role != "assistant"]
|
| 65 |
+
num_non_assistant_messages = len(non_assistant_messages)
|
| 66 |
+
message_timestamps = {} # Store timestamps keyed by message id
|
| 67 |
|
| 68 |
+
if num_non_assistant_messages > 0:
|
| 69 |
# Get current time specifically in Pacific Time (America/Los_Angeles)
|
| 70 |
pacific_tz = ZoneInfo("America/Los_Angeles")
|
| 71 |
now_pacific = datetime.now(timezone.utc).astimezone(pacific_tz)
|
| 72 |
|
| 73 |
+
# Assign timestamp to the last non-assistant message
|
| 74 |
+
last_msg_id = non_assistant_messages[-1].id
|
| 75 |
+
message_timestamps[last_msg_id] = now_pacific
|
| 76 |
|
| 77 |
+
# Calculate timestamps for previous non-assistant messages (random intervals earlier)
|
| 78 |
current_timestamp = now_pacific
|
| 79 |
+
for i in range(num_non_assistant_messages - 2, -1, -1): # Iterate backwards from second-to-last
|
| 80 |
current_timestamp -= timedelta(minutes=random.randint(3, 20)) # Use random interval (3-20 mins)
|
| 81 |
+
message_timestamps[non_assistant_messages[i].id] = current_timestamp
|
| 82 |
|
| 83 |
# --- Build Transcript ---
|
| 84 |
# Get current time in Pacific timezone for context
|
|
|
|
| 122 |
|
| 123 |
for message in request_data.messages:
|
| 124 |
if message.role == "assistant":
|
| 125 |
+
# Assistant messages get type="markdown-chat" and a traceId
|
| 126 |
transcript.append(NotionTranscriptItem(
|
| 127 |
type="markdown-chat",
|
| 128 |
value=message.content,
|
| 129 |
traceId=str(uuid.uuid4()) # Generate unique traceId for assistant message
|
| 130 |
))
|
| 131 |
+
else: # Treat all other roles (user, system, etc.) as "user" type
|
| 132 |
+
created_at_dt = message_timestamps.get(message.id) # Use the unified timestamp dict
|
| 133 |
created_at_iso = None
|
| 134 |
if created_at_dt:
|
| 135 |
# Format timestamp exactly as YYYY-MM-DDTHH:MM:SS.fff-HH:MM
|
|
|
|
| 140 |
created_at_iso = f"{dt_str}.{ms}{formatted_tz}"
|
| 141 |
|
| 142 |
content = message.content
|
| 143 |
+
# Ensure content is treated as a string for user/system messages
|
| 144 |
+
if isinstance(content, list):
|
| 145 |
+
# Attempt to extract text from list format, default to empty string
|
| 146 |
+
text_content = ""
|
| 147 |
+
for part in content:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
if isinstance(part, dict) and part.get("type") == "text":
|
| 149 |
+
text_part = part.get("text")
|
| 150 |
+
if isinstance(text_part, str):
|
| 151 |
+
text_content += text_part # Concatenate text parts if needed
|
| 152 |
+
content = text_content if text_content else "" # Use extracted text or empty string
|
| 153 |
+
elif not isinstance(content, str):
|
| 154 |
+
content = "" # Default to empty string if not list or string
|
| 155 |
+
|
| 156 |
+
# Format value as expected by Notion for user type: [[content_string]]
|
| 157 |
+
notion_value = [[content]] if content else [[""]]
|
| 158 |
+
|
| 159 |
+
transcript.append(NotionTranscriptItem(
|
| 160 |
+
type="user", # Set type to "user" for non-assistant roles
|
| 161 |
+
value=notion_value,
|
| 162 |
+
userId=user_id, # Assign userId
|
| 163 |
+
createdAt=created_at_iso # Assign timestamp
|
| 164 |
+
# No traceId for user/system messages
|
| 165 |
+
))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
|
| 167 |
# Use globally configured spaceId, set createThread=True
|
| 168 |
return NotionRequestBody(
|