Skip to content

Commit

Permalink
Python: Fix missed assertion bug when loading from .env file (#2274)
Browse files Browse the repository at this point in the history
### Motivation and Context

<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->
When loading config from .env, the assertions like `assert api_key is
not None, "Azure OpenAI API key not found in .env file"` don't catch
anything. While `dotenv_values(".env")` does put None values for keys
with missing values, that's only if there's no equals sign after the
key. For example, this file:
```
OPENAI_API_KEY=""
OPENAI_ORG_ID="org_id"
AZURE_OPENAI_DEPLOYMENT_NAME=
AZURE_OPENAI_ENDPOINT
```
Would be parsed like this:
```
OrderedDict([('OPENAI_API_KEY', ''), ('OPENAI_ORG_ID', 'org_id'), ('AZURE_OPENAI_DEPLOYMENT_NAME', ''), ('AZURE_OPENAI_ENDPOINT', None)])
```

As you can see, OPENAI_API_KEY and AZURE_OPENAI_DEPLOYMENT_NAME are
loaded with empty strings, not None.
### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [ ] The code builds clean without any errors or warnings
- [ ] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [ ] All unit tests pass, and I have added new tests where possible
- [ ] I didn't break anyone 😄

---------

Co-authored-by: Shawn Callegari <36091529+shawncal@users.noreply.github.com>
  • Loading branch information
mkarle and shawncal committed Aug 3, 2023
1 parent 7d702dc commit 8552943
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions python/semantic_kernel/utils/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def openai_settings_from_dot_env() -> Tuple[str, Optional[str]]:
api_key = config.get("OPENAI_API_KEY", None)
org_id = config.get("OPENAI_ORG_ID", None)

assert api_key is not None, "OpenAI API key not found in .env file"
assert api_key, "OpenAI API key not found in .env file"

# It's okay if the org ID is not found (not required)
return api_key, org_id
Expand All @@ -40,12 +40,10 @@ def azure_openai_settings_from_dot_env(include_deployment=True) -> Tuple[str, st

# Azure requires the deployment name, the API key and the endpoint URL.
if include_deployment:
assert (
deployment is not None
), "Azure OpenAI deployment name not found in .env file"
assert deployment, "Azure OpenAI deployment name not found in .env file"

assert api_key is not None, "Azure OpenAI API key not found in .env file"
assert endpoint is not None, "Azure OpenAI endpoint not found in .env file"
assert api_key, "Azure OpenAI API key not found in .env file"
assert endpoint, "Azure OpenAI endpoint not found in .env file"

return deployment or "", api_key, endpoint

Expand All @@ -60,9 +58,7 @@ def postgres_settings_from_dot_env() -> str:
config = dotenv_values(".env")
connection_string = config.get("POSTGRES_CONNECTION_STRING", None)

assert (
connection_string is not None
), "Postgres connection string not found in .env file"
assert connection_string, "Postgres connection string not found in .env file"

return connection_string

Expand All @@ -79,7 +75,7 @@ def pinecone_settings_from_dot_env() -> Tuple[str, str]:
api_key = config.get("PINECONE_API_KEY", None)
environment = config.get("PINECONE_ENVIRONMENT", None)

assert api_key is not None, "Pinecone API key not found in .env file"
assert environment is not None, "Pinecone environment not found in .env file"
assert api_key, "Pinecone API key not found in .env file"
assert environment, "Pinecone environment not found in .env file"

return api_key, environment

0 comments on commit 8552943

Please sign in to comment.