Skip to content

Commit

Permalink
Use datetime.now(timezone.utc) instead of datetime.utcnow(), cause py… (
Browse files Browse the repository at this point in the history
#790)

…dantic will not include post-fix Z with utcnow() func
  • Loading branch information
beastoin authored Sep 8, 2024
2 parents 6b65bf2 + eb614a0 commit baac75d
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 32 deletions.
6 changes: 3 additions & 3 deletions backend/database/chat.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import uuid
from datetime import datetime
from datetime import datetime, timezone
from typing import Optional

from google.cloud import firestore
Expand All @@ -19,7 +19,7 @@ def add_plugin_message(text: str, plugin_id: str, uid: str, memory_id: Optional[
ai_message = Message(
id=str(uuid.uuid4()),
text=text,
created_at=datetime.utcnow(),
created_at=datetime.now(timezone.utc),
sender='ai',
plugin_id=plugin_id,
from_external_integration=False,
Expand All @@ -34,7 +34,7 @@ def add_summary_message(text: str, uid: str) -> Message:
ai_message = Message(
id=str(uuid.uuid4()),
text=text,
created_at=datetime.utcnow(),
created_at=datetime.now(timezone.utc),
sender='ai',
plugin_id=None,
from_external_integration=False,
Expand Down
4 changes: 2 additions & 2 deletions backend/database/facts.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime
from datetime import datetime, timezone
from typing import List

from google.cloud import firestore
Expand Down Expand Up @@ -67,7 +67,7 @@ def edit_fact(uid: str, fact_id: str, value: str):
user_ref = db.collection('users').document(uid)
facts_ref = user_ref.collection('facts')
fact_ref = facts_ref.document(fact_id)
fact_ref.update({'content': value, 'edited': True, 'updated_at': datetime.utcnow()})
fact_ref.update({'content': value, 'edited': True, 'updated_at': datetime.now(timezone.utc)})


def delete_fact(uid: str, fact_id: str):
Expand Down
4 changes: 2 additions & 2 deletions backend/database/redis_db.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from datetime import datetime
from datetime import datetime, timezone
from typing import List

import redis
Expand Down Expand Up @@ -30,7 +30,7 @@ def set_plugin_review(plugin_id: str, uid: str, score: float, review: str = ''):
reviews = {}
else:
reviews = eval(reviews)
reviews[uid] = {'score': score, 'review': review, 'rated_at': datetime.utcnow().isoformat(), 'uid': uid}
reviews[uid] = {'score': score, 'review': review, 'rated_at': datetime.now(timezone.utc).isoformat(), 'uid': uid}
r.set(f'plugins:{plugin_id}:reviews', str(reviews))


Expand Down
4 changes: 2 additions & 2 deletions backend/database/vector_db.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from datetime import datetime
from datetime import datetime, timezone
from typing import List

from pinecone import Pinecone
Expand All @@ -21,7 +21,7 @@ def _get_data(uid: str, memory_id: str, vector: List[float]):
'metadata': {
'uid': uid,
'memory_id': memory_id,
'created_at': datetime.utcnow().timestamp() / 1000, # TODO: check this
'created_at': datetime.now(timezone.utc).timestamp() / 1000, # TODO: check this
}
}

Expand Down
6 changes: 3 additions & 3 deletions backend/models/facts.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime
from datetime import datetime, timezone
from enum import Enum
from typing import Optional, List

Expand Down Expand Up @@ -54,8 +54,8 @@ def from_fact(fact: Fact, uid: str, memory_id: str, memory_category: CategoryEnu
uid=uid,
content=fact.content,
category=fact.category,
created_at=datetime.utcnow(),
updated_at=datetime.utcnow(),
created_at=datetime.now(timezone.utc),
updated_at=datetime.now(timezone.utc),
memory_id=memory_id,
memory_category=memory_category,
)
8 changes: 4 additions & 4 deletions backend/routers/chat.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import uuid
from datetime import datetime
from datetime import datetime, timezone
from typing import List, Optional

from fastapi import APIRouter, Depends
Expand Down Expand Up @@ -29,7 +29,7 @@ def filter_messages(messages, plugin_id):
def send_message(
data: SendMessageRequest, plugin_id: Optional[str] = None, uid: str = Depends(auth.get_current_user_uid)
):
message = Message(id=str(uuid.uuid4()), text=data.text, created_at=datetime.utcnow(), sender='human', type='text')
message = Message(id=str(uuid.uuid4()), text=data.text, created_at=datetime.now(timezone.utc), sender='human', type='text')
chat_db.add_message(uid, message.dict())

plugin = get_plugin_by_id(plugin_id)
Expand All @@ -44,7 +44,7 @@ def send_message(
ai_message = Message(
id=str(uuid.uuid4()),
text=response,
created_at=datetime.utcnow(),
created_at=datetime.now(timezone.utc),
sender='ai',
plugin_id=plugin_id,
type='text',
Expand All @@ -63,7 +63,7 @@ def initial_message_util(uid: str, plugin_id: Optional[str] = None):
ai_message = Message(
id=str(uuid.uuid4()),
text=text,
created_at=datetime.utcnow(),
created_at=datetime.now(timezone.utc),
sender='ai',
plugin_id=plugin_id,
from_external_integration=False,
Expand Down
10 changes: 5 additions & 5 deletions backend/routers/screenpipe.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# import os
# import uuid
# from datetime import datetime
# from datetime import datetime, timezone
#
# from fastapi import APIRouter
# from fastapi import Request, HTTPException
Expand All @@ -21,17 +21,17 @@
# if data.source == 'screen':
# structured = summarize_screen_pipe(data.text)
# elif data.source == 'audio':
# structured = get_transcript_structure(data.text, datetime.utcnow(), 'en')
# structured = get_transcript_structure(data.text, datetime.now(timezone.utc), 'en')
# else:
# raise HTTPException(status_code=400, detail='Invalid memory source')
#
# memory = Memory(
# id=str(uuid.uuid4()),
# uid=uid,
# structured=structured,
# started_at=datetime.utcnow(),
# finished_at=datetime.utcnow(),
# created_at=datetime.utcnow(),
# started_at=datetime.now(timezone.utc),
# finished_at=datetime.now(timezone.utc),
# created_at=datetime.now(timezone.utc),
# discarded=False,
# deleted=False,
# source='screenpipe',
Expand Down
4 changes: 2 additions & 2 deletions backend/routers/transcribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import threading
import time
import uuid
from datetime import datetime
from datetime import datetime, timezone

from fastapi import APIRouter
from fastapi.websockets import (WebSocketDisconnect, WebSocket)
Expand Down Expand Up @@ -272,7 +272,7 @@ async def _create_processing_memory():
processing_memory = ProcessingMemory(
id=str(uuid.uuid4()),
session_id=session_id,
created_at=datetime.utcnow(),
created_at=datetime.now(timezone.utc),
timer_start=timer_start,
language=language,
)
Expand Down
6 changes: 3 additions & 3 deletions backend/routers/users.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import threading
import uuid
from datetime import datetime
from datetime import datetime, timezone
from typing import List

from fastapi import APIRouter, Depends, HTTPException
Expand Down Expand Up @@ -65,8 +65,8 @@ def create_new_person(data: CreatePerson, uid: str = Depends(auth.get_current_us
data = {
'id': str(uuid.uuid4()),
'name': data.name,
'created_at': datetime.utcnow(),
'updated_at': datetime.utcnow(),
'created_at': datetime.now(timezone.utc),
'updated_at': datetime.now(timezone.utc),
'deleted': False,
}
result = create_person(uid, data)
Expand Down
4 changes: 2 additions & 2 deletions backend/routers/workflow.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
from typing import Annotated, List
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from fastapi import APIRouter, Header
from fastapi import Request, HTTPException
import models.memory as memory_models
Expand All @@ -19,7 +19,7 @@ def create_memory(request: Request, uid: str, api_key: Annotated[str | None, Hea
raise HTTPException(status_code=401, detail="Invalid API Key")

# Time
started_at = create_memory.started_at if create_memory.started_at is not None else datetime.utcnow()
started_at = create_memory.started_at if create_memory.started_at is not None else datetime.now(timezone.utc)
finished_at = create_memory.finished_at if create_memory.finished_at is not None else started_at + \
timedelta(seconds=300) # 5 minutes
create_memory.started_at = started_at
Expand Down
4 changes: 2 additions & 2 deletions backend/scripts/rag/current.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import uuid
from datetime import datetime
from datetime import datetime, timezone
from typing import Dict

from _shared import *
Expand All @@ -8,7 +8,7 @@


def _get_mesage(text: str, sender: str):
return Message(id=str(uuid.uuid4()), text=text, created_at=datetime.utcnow(), sender=sender, type='text')
return Message(id=str(uuid.uuid4()), text=text, created_at=datetime.now(timezone.utc), sender=sender, type='text')


conversation = [
Expand Down
5 changes: 3 additions & 2 deletions backend/utils/memories/process_memory.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
from datetime import timezone
import random
import threading
import uuid
Expand Down Expand Up @@ -70,7 +71,7 @@ def _get_memory_obj(uid: str, structured: Structured, memory: Union[Memory, Crea
uid=uid,
structured=structured,
**memory.dict(),
created_at=datetime.utcnow(),
created_at=datetime.now(timezone.utc),
discarded=discarded,
deleted=False,
)
Expand All @@ -81,7 +82,7 @@ def _get_memory_obj(uid: str, structured: Structured, memory: Union[Memory, Crea
memory = Memory(
id=str(uuid.uuid4()),
**memory.dict(),
created_at=datetime.utcnow(),
created_at=datetime.now(timezone.utc),
deleted=False,
structured=structured,
discarded=discarded,
Expand Down

0 comments on commit baac75d

Please sign in to comment.