@@ -93,9 +93,9 @@ async def lifespan(app: GuardrailsApp):
9393
9494 # If there is a `config.yml` in the root `app.rails_config_path`, then
9595 # that means we are in single config mode.
96- if os .path .exists (
97- os .path .join (app .rails_config_path , "config.yml " )
98- ) or os . path . exists ( os . path . join ( app . rails_config_path , "config.yaml" )) :
96+ if os .path .exists (os . path . join ( app . rails_config_path , "config.yml" )) or os . path . exists (
97+ os .path .join (app .rails_config_path , "config.yaml " )
98+ ):
9999 app .single_config_mode = True
100100 app .single_config_id = os .path .basename (app .rails_config_path )
101101 else :
@@ -281,15 +281,11 @@ def ensure_config_id(cls, data: Any) -> Any:
281281 if data .get ("model" ) is not None and data .get ("config_id" ) is None :
282282 data ["config_id" ] = data ["model" ]
283283 if data .get ("config_id" ) is not None and data .get ("config_ids" ) is not None :
284- raise ValueError (
285- "Only one of config_id or config_ids should be specified"
286- )
284+ raise ValueError ("Only one of config_id or config_ids should be specified" )
287285 if data .get ("config_id" ) is None and data .get ("config_ids" ) is not None :
288286 data ["config_id" ] = None
289287 if data .get ("config_id" ) is None and data .get ("config_ids" ) is None :
290- warnings .warn (
291- "No config_id or config_ids provided, using default config_id"
292- )
288+ warnings .warn ("No config_id or config_ids provided, using default config_id" )
293289 return data
294290
295291 @validator ("config_ids" , pre = True , always = True )
@@ -461,9 +457,7 @@ def _get_rails(config_ids: List[str]) -> LLMRails:
461457 llm_rails_instances [configs_cache_key ] = llm_rails
462458
463459 # If we have a cache for the events, we restore it
464- llm_rails .events_history_cache = llm_rails_events_history_cache .get (
465- configs_cache_key , {}
466- )
460+ llm_rails .events_history_cache = llm_rails_events_history_cache .get (configs_cache_key , {})
467461
468462 return llm_rails
469463
@@ -547,9 +541,7 @@ async def chat_completion(body: RequestBody, request: Request):
547541 """
548542 log .info ("Got request for config %s" , body .config_id )
549543 for logger in registered_loggers :
550- asyncio .get_event_loop ().create_task (
551- logger ({"endpoint" : "/v1/chat/completions" , "body" : body .json ()})
552- )
544+ asyncio .get_event_loop ().create_task (logger ({"endpoint" : "/v1/chat/completions" , "body" : body .json ()}))
553545
554546 # Save the request headers in a context variable.
555547 api_request_headers .set (request .headers )
@@ -562,9 +554,7 @@ async def chat_completion(body: RequestBody, request: Request):
562554 if app .default_config_id :
563555 config_ids = [app .default_config_id ]
564556 else :
565- raise GuardrailsConfigurationError (
566- "No request config_ids provided and server has no default configuration"
567- )
557+ raise GuardrailsConfigurationError ("No request config_ids provided and server has no default configuration" )
568558
569559 try :
570560 llm_rails = _get_rails (config_ids )
@@ -652,11 +642,7 @@ async def chat_completion(body: RequestBody, request: Request):
652642 generation_options .llm_params ["presence_penalty" ] = body .presence_penalty
653643 if body .frequency_penalty is not None :
654644 generation_options .llm_params ["frequency_penalty" ] = body .frequency_penalty
655- if (
656- body .stream
657- and llm_rails .config .streaming_supported
658- and llm_rails .main_llm_supports_streaming
659- ):
645+ if body .stream and llm_rails .config .streaming_supported and llm_rails .main_llm_supports_streaming :
660646 # Create the streaming handler instance
661647 streaming_handler = StreamingHandler ()
662648
@@ -671,15 +657,11 @@ async def chat_completion(body: RequestBody, request: Request):
671657 )
672658
673659 return StreamingResponse (
674- _format_streaming_response (
675- streaming_handler , model_name = main_model_name
676- ),
660+ _format_streaming_response (streaming_handler , model_name = main_model_name ),
677661 media_type = "text/event-stream" ,
678662 )
679663 else :
680- res = await llm_rails .generate_async (
681- messages = messages , options = generation_options , state = body .state
682- )
664+ res = await llm_rails .generate_async (messages = messages , options = generation_options , state = body .state )
683665
684666 if isinstance (res , GenerationResponse ):
685667 bot_message_content = res .response [0 ]
@@ -793,9 +775,7 @@ def on_any_event(self, event):
793775 return None
794776
795777 elif event .event_type == "created" or event .event_type == "modified" :
796- log .info (
797- f"Watchdog received { event .event_type } event for file { event .src_path } "
798- )
778+ log .info (f"Watchdog received { event .event_type } event for file { event .src_path } " )
799779
800780 # Compute the relative path
801781 src_path_str = str (event .src_path )
@@ -819,9 +799,7 @@ def on_any_event(self, event):
819799 # We save the events history cache, to restore it on the new instance
820800 llm_rails_events_history_cache [config_id ] = val
821801
822- log .info (
823- f"Configuration { config_id } has changed. Clearing cache."
824- )
802+ log .info (f"Configuration { config_id } has changed. Clearing cache." )
825803
826804 observer = Observer ()
827805 event_handler = Handler ()
@@ -836,9 +814,7 @@ def on_any_event(self, event):
836814
837815 except ImportError :
838816 # Since this is running in a separate thread, we just print the error.
839- print (
840- "The auto-reload feature requires `watchdog`. Please install using `pip install watchdog`."
841- )
817+ print ("The auto-reload feature requires `watchdog`. Please install using `pip install watchdog`." )
842818 # Force close everything.
843819 os ._exit (- 1 )
844820
0 commit comments