Hugoifier
fix(config): raise EnvironmentError when API key is not set Closes #3
Commit
e36cfdb885d63b22b6b2ed1f7c624bd9fda28a0b30b825a62d60fb0a67081a5d
Parent
1f1280be7f5e7c2…
1 file changed
+6
+6
| --- src/config.py | ||
| +++ src/config.py | ||
| @@ -46,10 +46,12 @@ | ||
| 46 | 46 | "Set HUGOIFIER_BACKEND to 'anthropic', 'openai', or 'google'." |
| 47 | 47 | ) |
| 48 | 48 | |
| 49 | 49 | |
| 50 | 50 | def _call_anthropic(prompt: str, system: str) -> str: |
| 51 | + if not ANTHROPIC_API_KEY: | |
| 52 | + raise EnvironmentError("ANTHROPIC_API_KEY is not set") | |
| 51 | 53 | import anthropic |
| 52 | 54 | client = anthropic.Anthropic(api_key=ANTHROPIC_API_KEY) |
| 53 | 55 | message = client.messages.create( |
| 54 | 56 | model=ANTHROPIC_MODEL, |
| 55 | 57 | max_tokens=MAX_TOKENS, |
| @@ -58,10 +60,12 @@ | ||
| 58 | 60 | ) |
| 59 | 61 | return message.content[0].text |
| 60 | 62 | |
| 61 | 63 | |
| 62 | 64 | def _call_openai(prompt: str, system: str) -> str: |
| 65 | + if not OPENAI_API_KEY: | |
| 66 | + raise EnvironmentError("OPENAI_API_KEY is not set") | |
| 63 | 67 | from openai import OpenAI |
| 64 | 68 | client = OpenAI(api_key=OPENAI_API_KEY) |
| 65 | 69 | response = client.chat.completions.create( |
| 66 | 70 | model=OPENAI_MODEL, |
| 67 | 71 | messages=[ |
| @@ -72,13 +76,15 @@ | ||
| 72 | 76 | ) |
| 73 | 77 | return response.choices[0].message.content.strip() |
| 74 | 78 | |
| 75 | 79 | |
| 76 | 80 | def _call_google(prompt: str, system: str) -> str: |
| 81 | + if not GOOGLE_API_KEY: | |
| 82 | + raise EnvironmentError("GOOGLE_API_KEY is not set") | |
| 77 | 83 | import google.generativeai as genai |
| 78 | 84 | genai.configure(api_key=GOOGLE_API_KEY) |
| 79 | 85 | model = genai.GenerativeModel( |
| 80 | 86 | model_name=GOOGLE_MODEL, |
| 81 | 87 | system_instruction=system, |
| 82 | 88 | ) |
| 83 | 89 | response = model.generate_content(prompt) |
| 84 | 90 | return response.text |
| 85 | 91 |
| --- src/config.py | |
| +++ src/config.py | |
| @@ -46,10 +46,12 @@ | |
| 46 | "Set HUGOIFIER_BACKEND to 'anthropic', 'openai', or 'google'." |
| 47 | ) |
| 48 | |
| 49 | |
| 50 | def _call_anthropic(prompt: str, system: str) -> str: |
| 51 | import anthropic |
| 52 | client = anthropic.Anthropic(api_key=ANTHROPIC_API_KEY) |
| 53 | message = client.messages.create( |
| 54 | model=ANTHROPIC_MODEL, |
| 55 | max_tokens=MAX_TOKENS, |
| @@ -58,10 +60,12 @@ | |
| 58 | ) |
| 59 | return message.content[0].text |
| 60 | |
| 61 | |
| 62 | def _call_openai(prompt: str, system: str) -> str: |
| 63 | from openai import OpenAI |
| 64 | client = OpenAI(api_key=OPENAI_API_KEY) |
| 65 | response = client.chat.completions.create( |
| 66 | model=OPENAI_MODEL, |
| 67 | messages=[ |
| @@ -72,13 +76,15 @@ | |
| 72 | ) |
| 73 | return response.choices[0].message.content.strip() |
| 74 | |
| 75 | |
| 76 | def _call_google(prompt: str, system: str) -> str: |
| 77 | import google.generativeai as genai |
| 78 | genai.configure(api_key=GOOGLE_API_KEY) |
| 79 | model = genai.GenerativeModel( |
| 80 | model_name=GOOGLE_MODEL, |
| 81 | system_instruction=system, |
| 82 | ) |
| 83 | response = model.generate_content(prompt) |
| 84 | return response.text |
| 85 |
| --- src/config.py | |
| +++ src/config.py | |
| @@ -46,10 +46,12 @@ | |
| 46 | "Set HUGOIFIER_BACKEND to 'anthropic', 'openai', or 'google'." |
| 47 | ) |
| 48 | |
| 49 | |
| 50 | def _call_anthropic(prompt: str, system: str) -> str: |
| 51 | if not ANTHROPIC_API_KEY: |
| 52 | raise EnvironmentError("ANTHROPIC_API_KEY is not set") |
| 53 | import anthropic |
| 54 | client = anthropic.Anthropic(api_key=ANTHROPIC_API_KEY) |
| 55 | message = client.messages.create( |
| 56 | model=ANTHROPIC_MODEL, |
| 57 | max_tokens=MAX_TOKENS, |
| @@ -58,10 +60,12 @@ | |
| 60 | ) |
| 61 | return message.content[0].text |
| 62 | |
| 63 | |
| 64 | def _call_openai(prompt: str, system: str) -> str: |
| 65 | if not OPENAI_API_KEY: |
| 66 | raise EnvironmentError("OPENAI_API_KEY is not set") |
| 67 | from openai import OpenAI |
| 68 | client = OpenAI(api_key=OPENAI_API_KEY) |
| 69 | response = client.chat.completions.create( |
| 70 | model=OPENAI_MODEL, |
| 71 | messages=[ |
| @@ -72,13 +76,15 @@ | |
| 76 | ) |
| 77 | return response.choices[0].message.content.strip() |
| 78 | |
| 79 | |
| 80 | def _call_google(prompt: str, system: str) -> str: |
| 81 | if not GOOGLE_API_KEY: |
| 82 | raise EnvironmentError("GOOGLE_API_KEY is not set") |
| 83 | import google.generativeai as genai |
| 84 | genai.configure(api_key=GOOGLE_API_KEY) |
| 85 | model = genai.GenerativeModel( |
| 86 | model_name=GOOGLE_MODEL, |
| 87 | system_instruction=system, |
| 88 | ) |
| 89 | response = model.generate_content(prompt) |
| 90 | return response.text |
| 91 |