Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python: Introduce Pydantic settings #6193

Merged
merged 29 commits into from
May 16, 2024
Merged

Conversation

moonbox3
Copy link
Contributor

@moonbox3 moonbox3 commented May 11, 2024

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:

chat_completion = OpenAIChatCompletion(service_id="test")

or, to optionally override the ai_model_id env var:

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.

Description

The PR introduces the use of Pydantic settings and removes the use of the python-dotenv library.

  • Closes Python: Remove dependencies to .env file #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

Contribution Checklist

@moonbox3 moonbox3 requested a review from a team as a code owner May 11, 2024 14:59
@markwallace-microsoft markwallace-microsoft added python Pull requests for the Python Semantic Kernel documentation memory labels May 11, 2024
Copy link
Member

@eavanvalkenburg eavanvalkenburg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 big points, I think it is nicer to parent the settings classes in the same folder as the services, for both AI and memory. and I wonder if allowing manually setting the same settings would make sense, and break less?

@markwallace-microsoft
Copy link
Member

markwallace-microsoft commented May 13, 2024

Py3.10 Test Coverage

Python 3.10 Test Coverage Report •
FileStmtsMissCoverMissing
semantic_kernel/connectors/ai/google_palm/services
   gp_chat_completion.py661676%10, 135–140, 151–152, 201, 204, 207–208, 216, 220–221, 229
   gp_text_completion.py38684%10, 57–58, 64–65, 94
   gp_text_embedding.py27581%10, 47–48, 57–58
semantic_kernel/connectors/ai/open_ai/services
   azure_chat_completion.py763455%248, 254–255, 264–265, 270–287, 291–298, 310–321
   open_ai_text_completion.py16194%128
   open_ai_text_embedding.py13192%83
semantic_kernel/connectors/search_engine
   bing_connector.py35350%3–71
semantic_kernel/core_plugins/sessions_python_tool
   sessions_python_plugin.py95991%76–82, 122, 168, 172
TOTAL5867101983% 

Python 3.10 Unit Test Overview

Tests Skipped Failures Errors Time
1278 1 💤 0 ❌ 0 🔥 13.545s ⏱️

Copy link
Member

@eavanvalkenburg eavanvalkenburg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small fix to make (but in multiple places, didn't add comments for all)

Copy link
Member

@eavanvalkenburg eavanvalkenburg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small notes, one thing to consider (but can also be done in a subsequent PR) is to make the memory_storage_base more universal (like ai_settings_base or something) and use that throughout

Copy link
Member

@eavanvalkenburg eavanvalkenburg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

@moonbox3 moonbox3 enabled auto-merge May 14, 2024 19:01
@moonbox3 moonbox3 added this pull request to the merge queue May 14, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks May 14, 2024
@moonbox3 moonbox3 added this pull request to the merge queue May 14, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks May 14, 2024
@moonbox3 moonbox3 enabled auto-merge May 16, 2024 00:45
@moonbox3 moonbox3 added this pull request to the merge queue May 16, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks May 16, 2024
@moonbox3 moonbox3 enabled auto-merge May 16, 2024 01:18
@moonbox3 moonbox3 added this pull request to the merge queue May 16, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks May 16, 2024
@moonbox3 moonbox3 added this pull request to the merge queue May 16, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks May 16, 2024
@moonbox3 moonbox3 enabled auto-merge May 16, 2024 11:41
@moonbox3 moonbox3 added this pull request to the merge queue May 16, 2024
Merged via the queue into microsoft:main with commit 46f5ea1 May 16, 2024
25 checks passed
@moonbox3 moonbox3 deleted the pydantic_settings branch May 16, 2024 11:54
LudoCorporateShark pushed a commit to LudoCorporateShark/semantic-kernel that referenced this pull request Aug 25, 2024
### 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.
- Closes microsoft#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 😄
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation memory python Pull requests for the Python Semantic Kernel
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Python: Remove dependencies to .env file
5 participants