You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
### Motivation and Context
SK Python is tightly coupled to the use of a `.env` file to read all
secrets, keys, endpoints, and more. This doesn't scale well for users
who wish to be able to use environment variables with their SK
Applications. By introducing Pydantic Settings, it is possible to use
both environment variables as well as have a fall-back to a `.env` file
(via a `env_file_path` parameter), if desired.
By introducing Pydantic Settings, we are removing the requirement to
have to create Text/Embedding/Chat completion objects with an `api_key`
or other previously required information (in the case of
AzureChatCompletion that means an `endpoint`, an `api_key`, a
`deployment_name`, and an `api_version`). When the AI connector is
created, the Pydantic settings are loaded either via env vars or the
fall-back `.env` file, and that means the user can create a chat
completion object like:
```python
chat_completion = OpenAIChatCompletion(service_id="test")
```
or, to optionally override the `ai_model_id` env var:
```python
chat_completion = OpenAIChatCompletion(service_id="test", ai_model_id="gpt-4-1106")
```
Note: we have left the ability to specific an `api_key`/`org_id` for
`OpenAIChatCompletion` or a `deployment_name`, `endpoint`, `base_url`,
and `api_version` for `AzureChatCompletion` as before, but if your
settings are configured to use env vars/.env file then there is no need
to pass this information.
<!-- 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.
-->
### Description
The PR introduces the use of Pydantic settings and removes the use of
the python-dotenv library.
- Closesmicrosoft#1779
- Updates notebooks, samples, code and tests to remove the explicit
config of api_key or other previous .env files values.
- Adds new unit test config using monkeypatch to simulate env variables
for testing
- All unit and integration tests passing
<!-- 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: -->
- [X] The code builds clean without any errors or warnings
- [X] 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
- [X] All unit tests pass, and I have added new tests where possible
- [ ] I didn't break anyone 😄
Copy file name to clipboardExpand all lines: python/DEV_SETUP.md
+18-4
Original file line number
Diff line number
Diff line change
@@ -10,17 +10,31 @@ Make sure you have an
10
10
[OpenAI API Key](https://platform.openai.com) or
11
11
[Azure OpenAI service key](https://learn.microsoft.com/azure/cognitive-services/openai/quickstart?pivots=rest-api)
12
12
13
-
Copy those keys into a `.env` file (see the `.env.example` file):
13
+
There are two methods to manage keys, secrets, and endpoints:
14
14
15
-
```bash
15
+
1. Store them in environment variables. SK Python leverages pydantic settings to load keys, secrets, and endpoints. This means that there is a first attempt to load them from environment variables. The `.env` file naming applies to how the names should be stored as environment variables.
16
+
17
+
2. If you'd like to use the `.env` file, you will need to configure the `.env` file with the following keys into a `.env` file (see the `.env.example` file):
18
+
19
+
```
16
20
OPENAI_API_KEY=""
17
21
OPENAI_ORG_ID=""
18
-
AZURE_OPENAI_DEPLOYMENT_NAME=""
22
+
AZURE_OPENAI_CHAT_DEPLOYMENT_NAME=""
23
+
AZURE_OPENAI_TEXT_DEPLOYMENT_NAME=""
24
+
AZURE_OPENAI_EMBEDDING_DEPLOYMENT_NAME=""
19
25
AZURE_OPENAI_ENDPOINT=""
20
26
AZURE_OPENAI_API_KEY=""
21
27
```
22
28
23
-
We suggest adding a copy of the `.env` file under these folders:
29
+
You will then configure the Text/ChatCompletion class with the keyword argument `env_file_path`:
Copy file name to clipboardExpand all lines: python/README.md
+20-15
Original file line number
Diff line number
Diff line change
@@ -20,47 +20,52 @@ Make sure you have an
20
20
[OpenAI API Key](https://platform.openai.com) or
21
21
[Azure OpenAI service key](https://learn.microsoft.com/azure/cognitive-services/openai/quickstart?pivots=rest-api)
22
22
23
-
Copy those keys into a `.env` file (see the `.env.example` file):
23
+
There are two methods to manage keys, secrets, and endpoints:
24
+
25
+
1. Store them in environment variables. SK Python leverages pydantic settings to load keys, secrets, and endpoints. This means that there is a first attempt to load them from environment variables. The `.env` file naming applies to how the names should be stored as environment variables.
26
+
27
+
2. If you'd like to use the `.env` file, you will need to configure the `.env` file with the following keys in the file (see the `.env.example` file):
24
28
25
29
```
26
30
OPENAI_API_KEY=""
27
31
OPENAI_ORG_ID=""
28
-
AZURE_OPENAI_DEPLOYMENT_NAME=""
32
+
AZURE_OPENAI_CHAT_DEPLOYMENT_NAME=""
33
+
AZURE_OPENAI_TEXT_DEPLOYMENT_NAME=""
34
+
AZURE_OPENAI_EMBEDDING_DEPLOYMENT_NAME=""
29
35
AZURE_OPENAI_ENDPOINT=""
30
36
AZURE_OPENAI_API_KEY=""
31
37
```
32
38
39
+
You will then configure the Text/ChatCompletion class with the keyword argument `env_file_path`:
0 commit comments