Skip to content

Commit

Permalink
refactor instructions to instructions_url
Browse files Browse the repository at this point in the history
  • Loading branch information
lifeizhou-ap committed Oct 2, 2024
1 parent ba328b5 commit 329274e
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 14 deletions.
10 changes: 5 additions & 5 deletions src/exchange/providers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ def complete(


class MissingProviderEnvVariableError(Exception):
def __init__(self, env_variable: str, provider: str, instructions: Optional[str] = None) -> None:
def __init__(self, env_variable: str, provider: str, instructions_url: Optional[str] = None) -> None:
self.env_variable = env_variable
self.provider = provider
self.instructions = instructions
self.message = f"Missing environment variable: {env_variable} for provider {provider}"
if instructions:
self.message += f". {instructions}"
self.instructions_url = instructions_url
self.message = f"Missing environment variable: {env_variable} for provider {provider}."
if instructions_url:
self.message += f"\n Please see {instructions_url} for instructions"
super().__init__(self.message)
4 changes: 2 additions & 2 deletions src/exchange/providers/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ def __init__(self, client: httpx.Client) -> None:
@classmethod
def from_env(cls: Type["GoogleProvider"]) -> "GoogleProvider":
url = os.environ.get("GOOGLE_HOST", GOOGLE_HOST)
api_key_instructions = "see https://ai.google.dev/gemini-api/docs/api-key"
key = get_provider_env_value("GOOGLE_API_KEY", "google", api_key_instructions)
api_key_instructions_url = "see https://ai.google.dev/gemini-api/docs/api-key"
key = get_provider_env_value("GOOGLE_API_KEY", "google", api_key_instructions_url)
client = httpx.Client(
base_url=url,
headers={
Expand Down
4 changes: 2 additions & 2 deletions src/exchange/providers/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ def __init__(self, client: httpx.Client) -> None:
@classmethod
def from_env(cls: Type["OpenAiProvider"]) -> "OpenAiProvider":
url = os.environ.get("OPENAI_HOST", OPENAI_HOST)
api_key_instructions = "see https://platform.openai.com/docs/api-reference/api-keys"
key = get_provider_env_value("OPENAI_API_KEY", "openai", api_key_instructions)
api_key_instructions_url = "see https://platform.openai.com/docs/api-reference/api-keys"
key = get_provider_env_value("OPENAI_API_KEY", "openai", api_key_instructions_url)
client = httpx.Client(
base_url=url + "v1/",
auth=("Bearer", key),
Expand Down
4 changes: 2 additions & 2 deletions src/exchange/providers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,11 @@ def openai_single_message_context_length_exceeded(error_dict: dict) -> None:
raise InitialMessageTooLargeError(f"Input message too long. Message: {error_dict.get('message')}")


def get_provider_env_value(env_variable: str, provider: str, instructions: Optional[str] = None) -> str:
def get_provider_env_value(env_variable: str, provider: str, instructions_url: Optional[str] = None) -> str:
try:
return os.environ[env_variable]
except KeyError:
raise MissingProviderEnvVariableError(env_variable, provider, instructions)
raise MissingProviderEnvVariableError(env_variable, provider, instructions_url)


class InitialMessageTooLargeError(Exception):
Expand Down
2 changes: 1 addition & 1 deletion tests/providers/test_anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_from_env_throw_error_when_missing_api_key():
AnthropicProvider.from_env()
assert context.value.provider == "anthropic"
assert context.value.env_variable == "ANTHROPIC_API_KEY"
assert context.value.message == "Missing environment variable: ANTHROPIC_API_KEY for provider anthropic"
assert context.value.message == "Missing environment variable: ANTHROPIC_API_KEY for provider anthropic."


def test_anthropic_response_to_text_message() -> None:
Expand Down
2 changes: 1 addition & 1 deletion tests/providers/test_azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_from_env_throw_error_when_missing_env_var(env_var_name):
AzureProvider.from_env()
assert context.value.provider == "azure"
assert context.value.env_variable == env_var_name
assert context.value.message == f"Missing environment variable: {env_var_name} for provider azure"
assert context.value.message == f"Missing environment variable: {env_var_name} for provider azure."


@pytest.mark.vcr()
Expand Down
2 changes: 1 addition & 1 deletion tests/providers/test_bedrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_from_env_throw_error_when_missing_env_var(env_var_name):
BedrockProvider.from_env()
assert context.value.provider == "bedrock"
assert context.value.env_variable == env_var_name
assert context.value.message == f"Missing environment variable: {env_var_name} for provider bedrock"
assert context.value.message == f"Missing environment variable: {env_var_name} for provider bedrock."


@pytest.fixture
Expand Down
27 changes: 27 additions & 0 deletions tests/test_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from exchange.providers.base import MissingProviderEnvVariableError


def test_missing_provider_env_variable_error_without_instructions_url():
env_variable = "API_KEY"
provider = "TestProvider"
error = MissingProviderEnvVariableError(env_variable, provider)

assert error.env_variable == env_variable
assert error.provider == provider
assert error.instructions_url is None
assert error.message == "Missing environment variable: API_KEY for provider TestProvider."


def test_missing_provider_env_variable_error_with_instructions_url():
env_variable = "API_KEY"
provider = "TestProvider"
instructions_url = "http://example.com/instructions"
error = MissingProviderEnvVariableError(env_variable, provider, instructions_url)

assert error.env_variable == env_variable
assert error.provider == provider
assert error.instructions_url == instructions_url
assert error.message == (
"Missing environment variable: API_KEY for provider TestProvider.\n"
" Please see http://example.com/instructions for instructions"
)

0 comments on commit 329274e

Please sign in to comment.