From 628521f630204adaf667d8455a1ebf5ae2cf9283 Mon Sep 17 00:00:00 2001 From: Stijn Tintel Date: Thu, 22 Feb 2024 20:59:06 +0200 Subject: [PATCH] WIP: openai: don't crash if OpenAI client fails to connect Maybe we should add a loop and try reconnecting? --- app/internal/openai.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/app/internal/openai.py b/app/internal/openai.py index d995cb1..6253676 100644 --- a/app/internal/openai.py +++ b/app/internal/openai.py @@ -11,13 +11,16 @@ if settings.openai_api_key is not None: log.info("Initializing OpenAI Client") import openai - openai_client = openai.OpenAI( - api_key=settings.openai_api_key, base_url=settings.openai_base_url) - models = openai_client.models.list() - if len(models.data) == 1: - force_openai_model = models.data[0].id - log.info( - f"Only one model on OpenAI endpoint - forcing model '{force_openai_model}'") + try: + openai_client = openai.OpenAI( + api_key=settings.openai_api_key, base_url=settings.openai_base_url) + models = openai_client.models.list() + if len(models.data) == 1: + force_openai_model = models.data[0].id + log.info( + f"Only one model on OpenAI endpoint - forcing model '{force_openai_model}'") + except Exception as e: + log.error(f"failed to initialize OpenAI client: {e}") else: openai_client = None