Skip to content

Commit

Permalink
WIP: openai: don't crash if OpenAI client fails to connect
Browse files Browse the repository at this point in the history
Maybe we should add a loop and try reconnecting?
  • Loading branch information
stintel committed Feb 22, 2024
1 parent 7e0ccca commit 628521f
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions app/internal/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 628521f

Please sign in to comment.