Skip to content

Commit

Permalink
Python: Migrate to Ollama Python SDK (#7165)
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.
-->

### 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
TaoChenOSU authored Jul 16, 2024
1 parent 4bbb694 commit 79207ff
Show file tree
Hide file tree
Showing 18 changed files with 1,074 additions and 307 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/python-integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ jobs:
python -m pip install --upgrade pip setuptools wheel
cd python
poetry install --with tests
- name: Install Ollama
if: matrix.os == 'ubuntu-latest'
run: |
curl -fsSL https://ollama.com/install.sh | sh
ollama serve &
sleep 5
- name: Pull model in Ollama
if: matrix.os == 'ubuntu-latest'
run: |
ollama pull ${{ vars.OLLAMA_MODEL }}
ollama list
- name: Run Integration Tests
id: run_tests
shell: bash
Expand Down Expand Up @@ -98,6 +109,7 @@ jobs:
ACA_POOL_MANAGEMENT_ENDPOINT: ${{secrets.ACA_POOL_MANAGEMENT_ENDPOINT}}
MISTRALAI_API_KEY: ${{secrets.MISTRALAI_API_KEY}}
MISTRALAI_CHAT_MODEL_ID: ${{ vars.MISTRALAI_CHAT_MODEL_ID }}
OLLAMA_MODEL: "${{ matrix.os == 'ubuntu-latest' && vars.OLLAMA_MODEL || '' }}" # phi3
run: |
if ${{ matrix.os == 'ubuntu-latest' }}; then
docker run -d --name redis-stack-server -p 6379:6379 redis/redis-stack-server:latest
Expand Down Expand Up @@ -139,6 +151,19 @@ jobs:
python -m pip install --upgrade pip setuptools wheel
cd python && poetry install --with tests
- name: Install Ollama
if: matrix.os == 'ubuntu-latest'
run: |
curl -fsSL https://ollama.com/install.sh | sh
ollama serve &
sleep 5
- name: Pull model in Ollama
if: matrix.os == 'ubuntu-latest'
run: |
ollama pull ${{ vars.OLLAMA_MODEL }}
ollama list
- name: Run Integration Tests
id: run_tests
shell: bash
Expand Down Expand Up @@ -167,6 +192,7 @@ jobs:
ACA_POOL_MANAGEMENT_ENDPOINT: ${{secrets.ACA_POOL_MANAGEMENT_ENDPOINT}}
MISTRALAI_API_KEY: ${{secrets.MISTRALAI_API_KEY}}
MISTRALAI_CHAT_MODEL_ID: ${{ vars.MISTRALAI_CHAT_MODEL_ID }}
OLLAMA_MODEL: "${{ matrix.os == 'ubuntu-latest' && vars.OLLAMA_MODEL || '' }}" # phi3
run: |
if ${{ matrix.os == 'ubuntu-latest' }}; then
docker run -d --name redis-stack-server -p 6379:6379 redis/redis-stack-server:latest
Expand Down
4 changes: 0 additions & 4 deletions python/mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ warn_untyped_fields = true
[mypy-semantic_kernel]
no_implicit_reexport = true

[mypy-semantic_kernel.connectors.ai.ollama.*]
ignore_errors = true
# TODO (eavanvalkenburg): remove this: https://github.com/microsoft/semantic-kernel/issues/7134

[mypy-semantic_kernel.memory.*]
ignore_errors = true
# TODO (eavanvalkenburg): remove this
Expand Down
20 changes: 17 additions & 3 deletions python/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ pymilvus = { version = ">=2.3,<2.4.4", optional = true}
milvus = { version = ">=2.3,<2.3.8", markers = 'sys_platform != "win32"', optional = true}
# mistralai
mistralai = { version = "^0.4.1", optional = true}
# ollama
ollama = { version = "^0.2.1", optional = true}
# pinecone
pinecone-client = { version = ">=3.0.0", optional = true}
# postgres
Expand Down Expand Up @@ -89,6 +91,7 @@ azure-search-documents = {version = "11.6.0b4", allow-prereleases = true}
azure-core = "^1.28.0"
azure-cosmos = "^4.7.0"
mistralai = "^0.4.1"
ollama = "^0.2.1"
transformers = { version = "^4.28.1", extras=["torch"]}
sentence-transformers = "^2.2.2"

Expand All @@ -113,6 +116,8 @@ pymilvus = ">=2.3,<2.4.4"
milvus = { version = ">=2.3,<2.3.8", markers = 'sys_platform != "win32"'}
# mistralai
mistralai = "^0.4.1"
# ollama
ollama = "^0.2.1"
# mongodb
motor = "^3.3.2"
# pinecone
Expand All @@ -131,13 +136,14 @@ weaviate-client = ">=3.18,<5.0"

# Extras are exposed to pip, this allows a user to easily add the right dependencies to their environment
[tool.poetry.extras]
all = ["transformers", "sentence-transformers", "qdrant-client", "chromadb", "pymilvus", "milvus","mistralai", "weaviate-client", "pinecone-client", "psycopg", "redis", "azure-ai-inference", "azure-search-documents", "azure-core", "azure-identity", "azure-cosmos", "usearch", "pyarrow", "ipykernel", "motor"]
all = ["transformers", "sentence-transformers", "qdrant-client", "chromadb", "pymilvus", "milvus", "mistralai", "ollama", "weaviate-client", "pinecone-client", "psycopg", "redis", "azure-ai-inference", "azure-search-documents", "azure-core", "azure-identity", "azure-cosmos", "usearch", "pyarrow", "ipykernel", "motor"]

azure = ["azure-ai-inference", "azure-search-documents", "azure-core", "azure-identity", "azure-cosmos", "msgraph-sdk"]
chromadb = ["chromadb"]
hugging_face = ["transformers", "sentence-transformers"]
milvus = ["pymilvus", "milvus"]
mistralai = ["mistralai"]
ollama = ["ollama"]
mongo = ["motor"]
notebooks = ["ipykernel"]
pinecone = ["pinecone-client"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,28 @@

from typing import Any, Literal

from pydantic import Field

from semantic_kernel.connectors.ai.prompt_execution_settings import PromptExecutionSettings


class OllamaPromptExecutionSettings(PromptExecutionSettings):
ai_model_id: str = Field("", serialization_alias="model")
"""Settings for Ollama prompt execution."""

format: Literal["json"] | None = None
options: dict[str, Any] | None = None
stream: bool = False


class OllamaTextPromptExecutionSettings(OllamaPromptExecutionSettings):
prompt: str | None = None
context: str | None = None
"""Settings for Ollama text prompt execution."""

system: str | None = None
template: str | None = None
raw: bool = False
context: str | None = None
raw: bool | None = None


class OllamaChatPromptExecutionSettings(OllamaPromptExecutionSettings):
messages: list[dict[str, str]] | None = None
"""Settings for Ollama chat prompt execution."""


class OllamaEmbeddingPromptExecutionSettings(OllamaPromptExecutionSettings):
"""Settings for Ollama embedding prompt execution."""
29 changes: 29 additions & 0 deletions python/semantic_kernel/connectors/ai/ollama/ollama_settings.py
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
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
Loading

0 comments on commit 79207ff

Please sign in to comment.