From 2247e98988088f8700f710f43267be051997c034 Mon Sep 17 00:00:00 2001 From: Kyongsik Yun Date: Wed, 28 Aug 2024 09:43:45 -0700 Subject: [PATCH] proper error message and exit when OpenAI key is missing --- src/jpl/slim/cli.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/jpl/slim/cli.py b/src/jpl/slim/cli.py index 7314b75..ff1af25 100644 --- a/src/jpl/slim/cli.py +++ b/src/jpl/slim/cli.py @@ -306,9 +306,9 @@ def generate_with_azure(prompt: str, model_name: str) -> Optional[str]: def generate_with_openai(prompt: str, model_name: str) -> Optional[str]: from openai import OpenAI - client = OpenAI(api_key = os.getenv('OPENAI_API_KEY')) - - try: + load_dotenv() + try: + client = OpenAI(api_key = os.getenv('OPENAI_API_KEY')) response = client.chat.completions.create( model=model_name, messages=[{"role": "user", "content": prompt}], @@ -318,11 +318,9 @@ def generate_with_openai(prompt: str, model_name: str) -> Optional[str]: if chunk.choices[0].delta.content is not None: yield chunk.choices[0].delta.content except Exception as e: - print(f"An error occurred: {e}") + print(f"An error occurred running OpenAI model: {e}") yield None - #return str(response.choices[0].message.content) - def generate_with_ollama(prompt: str, model_name: str) -> Optional[str]: import ollama