From ca133a82a34809c1eb6f0cb6f3b39a080ef0626e Mon Sep 17 00:00:00 2001 From: David Quinney Date: Sun, 11 May 2025 08:34:51 +0000 Subject: [PATCH] Fix handling of empty OpenAI API key --- app/services/llm_service.py | 118 ++++++++++++++++++++++++++---------- 1 file changed, 86 insertions(+), 32 deletions(-) diff --git a/app/services/llm_service.py b/app/services/llm_service.py index 8243a1c..80ce228 100644 --- a/app/services/llm_service.py +++ b/app/services/llm_service.py @@ -7,6 +7,7 @@ import json import logging +import os import re from typing import List @@ -49,17 +50,35 @@ def get_artist_recommendations(self, prompt: str, artists: List[Artist], model: Do not add any explanations or other text - just the JSON object. Select 10-15 artists that match the mood/theme, only from the provided list.""" - response = completion( - model=model, - messages=[ - {"role": "system", "content": system_prompt}, - { - "role": "user", - "content": f"Context: {artist_context}\n\nCreate a playlist for: {prompt}", - }, - ], - temperature=0.7, - ) + # Check if OpenAI API key has a value and use it, otherwise fall back to Anthropic + if ("gpt" in model or "openai" in model) and os.getenv("OPENAI_API_KEY"): + response = completion( + model=model, + messages=[ + {"role": "system", "content": system_prompt}, + { + "role": "user", + "content": f"Context: {artist_context}\n\nCreate a playlist for: {prompt}", + }, + ], + temperature=0.7, + ) + else: + if "gpt" in model or "openai" in model: + logger.warning("OpenAI API key is empty or not set, falling back to Anthropic") + model = "claude-3-opus-20240229" + + response = completion( + model=model, + messages=[ + {"role": "system", "content": system_prompt}, + { + "role": "user", + "content": f"Context: {artist_context}\n\nCreate a playlist for: {prompt}", + }, + ], + temperature=0.7, + ) content = clean_llm_response(response.choices[0].message.content) logger.debug("Raw LLM response: %s", content) @@ -111,19 +130,39 @@ def get_track_recommendations( Select between {min_tracks} and {max_tracks} tracks total. Do not add any explanations or additional text.""" - response = completion( - model=model, - messages=[ - {"role": "system", "content": system_prompt}, - { - "role": "user", - "content": f"""Context: {albums_context}\n\n - Create a playlist with {min_tracks}-{max_tracks} tracks for: {prompt} - """, - }, - ], - temperature=0.7, - ) + # Check if OpenAI API key has a value and use it, otherwise fall back to Anthropic + if ("gpt" in model or "openai" in model) and os.getenv("OPENAI_API_KEY"): + response = completion( + model=model, + messages=[ + {"role": "system", "content": system_prompt}, + { + "role": "user", + "content": f"""Context: {albums_context}\n\n + Create a playlist with {min_tracks}-{max_tracks} tracks for: {prompt} + """, + }, + ], + temperature=0.7, + ) + else: + if "gpt" in model or "openai" in model: + logger.warning("OpenAI API key is empty or not set, falling back to Anthropic") + model = "claude-3-opus-20240229" + + response = completion( + model=model, + messages=[ + {"role": "system", "content": system_prompt}, + { + "role": "user", + "content": f"""Context: {albums_context}\n\n + Create a playlist with {min_tracks}-{max_tracks} tracks for: {prompt} + """, + }, + ], + temperature=0.7, + ) content = clean_llm_response(response.choices[0].message.content) result = json.loads(content) @@ -147,14 +186,29 @@ def generate_playlist_name(self, prompt: str, model: str = "gpt-4") -> str: Generate a SINGLE catchy and relevant playlist name based on the following prompt. Do not wrap in quotes. """ - response = completion( - model=model, - messages=[ - {"role": "system", "content": system_prompt}, - {"role": "user", "content": prompt}, - ], - temperature=0.7, - ) + # Check if OpenAI API key has a value and use it, otherwise fall back to Anthropic + if ("gpt" in model or "openai" in model) and os.getenv("OPENAI_API_KEY"): + response = completion( + model=model, + messages=[ + {"role": "system", "content": system_prompt}, + {"role": "user", "content": prompt}, + ], + temperature=0.7, + ) + else: + if "gpt" in model or "openai" in model: + logger.warning("OpenAI API key is empty or not set, falling back to Anthropic") + model = "claude-3-opus-20240229" + + response = completion( + model=model, + messages=[ + {"role": "system", "content": system_prompt}, + {"role": "user", "content": prompt}, + ], + temperature=0.7, + ) name = response.choices[0].message.content.strip() logger.info("Generated playlist name: %s", name)