66from abc import ABC , abstractmethod
77from collections .abc import AsyncGenerator
88from operator import add
9- from typing import Annotated , Any , Optional
9+ from typing import Annotated , Any
1010from uuid import uuid4
1111
1212import posthog
@@ -73,10 +73,10 @@ def __init__( # noqa: PLR0913
7373 checkpointer : BaseCheckpointSaver ,
7474 * ,
7575 name : str = "MIT Open Learning Chatbot" ,
76- model : Optional [ str ] = None ,
77- temperature : Optional [ float ] = None ,
78- instructions : Optional [ str ] = None ,
79- thread_id : Optional [ str ] = None ,
76+ model : str | None = None ,
77+ temperature : float | None = None ,
78+ instructions : str | None = None ,
79+ thread_id : str | None = None ,
8080 ):
8181 """Initialize the AI chat agent service"""
8282 self .bot_name = name
@@ -224,7 +224,7 @@ async def validate_and_clean_checkpoint(self) -> None:
224224 except Exception :
225225 log .exception ("Error while cleaning checkpoint" )
226226
227- async def _get_latest_checkpoint_id (self ) -> Optional [ str ] :
227+ async def _get_latest_checkpoint_id (self ) -> str | None :
228228 """Get the most recent assistant response checkpoint"""
229229 checkpoint = (
230230 await DjangoCheckpoint .objects .prefetch_related ("session" , "session__user" )
@@ -237,7 +237,7 @@ async def _get_latest_checkpoint_id(self) -> Optional[str]:
237237 return checkpoint .id if checkpoint else None
238238
239239 async def set_callbacks (
240- self , properties : Optional [ dict ] = None
240+ self , properties : dict | None = None
241241 ) -> list [CallbackHandler ]:
242242 """Set callbacks for the agent LLM"""
243243 if settings .POSTHOG_PROJECT_API_KEY and settings .POSTHOG_API_HOST :
@@ -282,7 +282,7 @@ async def get_completion(
282282 self ,
283283 message : str ,
284284 * ,
285- extra_state : Optional [ dict [str , Any ]] = None ,
285+ extra_state : dict [str , Any ] | None = None ,
286286 debug : bool = settings .AI_DEBUG ,
287287 ) -> AsyncGenerator [str , None ]:
288288 """
@@ -424,13 +424,13 @@ class ResourceRecommendationBot(TruncatingChatbot):
424424 def __init__ ( # noqa: PLR0913
425425 self ,
426426 user_id : str ,
427- checkpointer : Optional [ BaseCheckpointSaver ] = None ,
427+ checkpointer : BaseCheckpointSaver | None = None ,
428428 * ,
429429 name : str = "MIT Open Learning Chatbot" ,
430- model : Optional [ str ] = None ,
431- temperature : Optional [ float ] = None ,
432- instructions : Optional [ str ] = None ,
433- thread_id : Optional [ str ] = None ,
430+ model : str | None = None ,
431+ temperature : float | None = None ,
432+ instructions : str | None = None ,
433+ thread_id : str | None = None ,
434434 ):
435435 """Initialize the AI search agent service"""
436436 super ().__init__ (
@@ -466,7 +466,7 @@ class SyllabusAgentState(SummaryState):
466466 related_courses : Annotated [list [str ], add ]
467467 # str representation of a boolean value, because the
468468 # langgraph JsonPlusSerializer can't handle booleans
469- exclude_canvas : Annotated [Optional [ list [str ]] , add ]
469+ exclude_canvas : Annotated [list [str ] | None , add ]
470470
471471
472472class SyllabusBot (TruncatingChatbot ):
@@ -483,11 +483,11 @@ def __init__( # noqa: PLR0913
483483 checkpointer : BaseCheckpointSaver ,
484484 * ,
485485 name : str = "MIT Open Learning Syllabus Chatbot" ,
486- model : Optional [ str ] = None ,
487- temperature : Optional [ float ] = None ,
488- instructions : Optional [ str ] = None ,
489- thread_id : Optional [ str ] = None ,
490- enable_related_courses : Optional [ bool ] = False ,
486+ model : str | None = None ,
487+ temperature : float | None = None ,
488+ instructions : str | None = None ,
489+ thread_id : str | None = None ,
490+ enable_related_courses : bool | None = False ,
491491 ):
492492 self .enable_related_courses = enable_related_courses
493493 super ().__init__ (
@@ -546,16 +546,16 @@ class TutorBot(BaseChatbot):
546546 def __init__ ( # noqa: PLR0913
547547 self ,
548548 user_id : str ,
549- checkpointer : Optional [ BaseCheckpointSaver ] = BaseCheckpointSaver ,
549+ checkpointer : BaseCheckpointSaver | None = BaseCheckpointSaver ,
550550 * ,
551551 name : str = "MIT Open Learning Tutor Chatbot" ,
552- model : Optional [ str ] = None ,
553- temperature : Optional [ float ] = None ,
554- thread_id : Optional [ str ] = None ,
555- block_siblings : Optional [ list [str ]] = None ,
556- edx_module_id : Optional [ str ] = None ,
557- run_readable_id : Optional [ str ] = None ,
558- problem_set_title : Optional [ str ] = None ,
552+ model : str | None = None ,
553+ temperature : float | None = None ,
554+ thread_id : str | None = None ,
555+ block_siblings : list [str ] | None = None ,
556+ edx_module_id : str | None = None ,
557+ run_readable_id : str | None = None ,
558+ problem_set_title : str | None = None ,
559559 ):
560560 super ().__init__ (
561561 user_id ,
@@ -600,7 +600,7 @@ async def get_completion(
600600 self ,
601601 message : str ,
602602 * ,
603- extra_state : Optional [ dict [str , Any ]] = None , # noqa: ARG002
603+ extra_state : dict [str , Any ] | None = None , # noqa: ARG002
604604 debug : bool = settings .AI_DEBUG ,
605605 ) -> AsyncGenerator [str , None ]:
606606 """Call message_tutor with the user query and return the response"""
@@ -810,10 +810,10 @@ def __init__( # noqa: PLR0913
810810 checkpointer : BaseCheckpointSaver ,
811811 * ,
812812 name : str = "MIT Open Learning VideoGPT Chatbot" ,
813- model : Optional [ str ] = None ,
814- temperature : Optional [ float ] = None ,
815- instructions : Optional [ str ] = None ,
816- thread_id : Optional [ str ] = None ,
813+ model : str | None = None ,
814+ temperature : float | None = None ,
815+ instructions : str | None = None ,
816+ thread_id : str | None = None ,
817817 ):
818818 super ().__init__ (
819819 user_id ,
0 commit comments