-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Python: Migrate to Ollama Python SDK (#7165)
### 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. --> ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Currently, the Ollama AI connector uses `aiohttp` sessions to interface with the local Ollama LLM service. However, we have made the decision to transition to the official Ollama Python SDK. Related to: #6841, #7134 Test coverage report: ![image](https://github.com/microsoft/semantic-kernel/assets/12570346/8d414f71-fc1b-4870-b038-0f7d2985b985) MyPy warnings: all cleared ### 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 - [x] I didn't break anyone 😄
- Loading branch information
1 parent
4bbb694
commit 79207ff
Showing
18 changed files
with
1,074 additions
and
307 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
python/semantic_kernel/connectors/ai/ollama/ollama_settings.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Copyright (c) Microsoft. All rights reserved. | ||
|
||
from typing import ClassVar | ||
|
||
from semantic_kernel.kernel_pydantic import KernelBaseSettings | ||
|
||
|
||
class OllamaSettings(KernelBaseSettings): | ||
"""Ollama settings. | ||
The settings are first loaded from environment variables with | ||
the prefix 'OLLAMA_'. | ||
If the environment variables are not found, the settings can | ||
be loaded from a .env file with the encoding 'utf-8'. | ||
If the settings are not found in the .env file, the settings | ||
are ignored; however, validation will fail alerting that the | ||
settings are missing. | ||
Required settings for prefix 'OLLAMA' are: | ||
- model: str - Model name. (Env var OLLAMA_MODEL) | ||
Optional settings for prefix 'OLLAMA' are: | ||
- host: HttpsUrl - The endpoint of the Ollama service. (Env var OLLAMA_HOST) | ||
""" | ||
|
||
env_prefix: ClassVar[str] = "OLLAMA_" | ||
|
||
model: str | ||
host: str | None = None |
17 changes: 17 additions & 0 deletions
17
python/semantic_kernel/connectors/ai/ollama/services/ollama_base.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Copyright (c) Microsoft. All rights reserved. | ||
|
||
from abc import ABC | ||
|
||
from ollama import AsyncClient | ||
|
||
from semantic_kernel.kernel_pydantic import KernelBaseModel | ||
|
||
|
||
class OllamaBase(KernelBaseModel, ABC): | ||
"""Ollama service base. | ||
Args: | ||
client [AsyncClient]: An Ollama client to use for the service. | ||
""" | ||
|
||
client: AsyncClient |
Oops, something went wrong.