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: Verify local models in Ollama and LM Studio are compatible with the OpenAI connector #6973

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

TaoChenOSU
Copy link
Contributor

Motivation and Context

Related to #6498

The use of local models presents a twofold benefit for developers: increased flexibility and reduced costs. Ollama and LM Studio are two well-known platforms that facilitate the hosting of models locally, both of which offer compatibility with OpenAI endpoints. As such, it is imperative that our OpenAI connector functions correctly when users are operating models on these platforms.

Description

  1. Verify that our OpenAI connector works as expected with models hosted locally using Ollama and LM Studio.
  2. Create three new samples (Ollama/chat, LM Studio/chat, LM Studio/Embedding) under /concepts/local_models to show how to using local models with the OpenAI connector.
  3. Fix a bug in test_sample_utils.py where if a test case is retried and input was never reset.

Contribution Checklist

@TaoChenOSU TaoChenOSU self-assigned this Jun 26, 2024
@TaoChenOSU TaoChenOSU requested a review from a team as a code owner June 26, 2024 20:59
@markwallace-microsoft markwallace-microsoft added the python Pull requests for the Python Semantic Kernel label Jun 26, 2024
@github-actions github-actions bot changed the title Verify local models in Ollama and LM Studio are compatible with the OpenAI connector Python: Verify local models in Ollama and LM Studio are compatible with the OpenAI connector Jun 26, 2024
@markwallace-microsoft
Copy link
Member

markwallace-microsoft commented Jun 26, 2024

Py3.10 Test Coverage

Python 3.10 Test Coverage Report •
FileStmtsMissCoverMissing
TOTAL660282787% 
report-only-changed-files is enabled. No files were changed during this commit :)

Python 3.10 Unit Test Overview

Tests Skipped Failures Errors Time
1558 1 💤 0 ❌ 0 🔥 24.544s ⏱️

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 note, let's make it so that you can just use the Completion service directly instead of having to create your own client!

Choose a reason for hiding this comment

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

When testing this sample locally with semantic-kernel==1.1.2, I needed to add an api_key (fake) to line 39 OpenAIChatCompletion as well. Otherwise I get a parameter validation error. Maybe I'm wrong, but please verify.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That shouldn't happen as the api_key is not a model field. Could you provide more information, such as the validation error message?

Choose a reason for hiding this comment

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

Running your ollama_chat_completion.py with semantic-kernel 1.1.2, openai 1.35.7 on macOS 14.5 produces the following Traceback (most recent call last): Note, I had to change phi3 to phi3:mini for my computer.

File "/opt/homebrew/Caskroom/miniconda/base/envs/sk/lib/python3.12/site-packages/semantic_kernel/connectors/ai/open_ai/services/open_ai_chat_completion.py", line 51, in init
openai_settings = OpenAISettings.create(
^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Caskroom/miniconda/base/envs/sk/lib/python3.12/site-packages/semantic_kernel/kernel_pydantic.py", line 56, in create
return cls(**data)
^^^^^^^^^^^
File "/opt/homebrew/Caskroom/miniconda/base/envs/sk/lib/python3.12/site-packages/pydantic_settings/main.py", line 140, in init
super().init(
File "/opt/homebrew/Caskroom/miniconda/base/envs/sk/lib/python3.12/site-packages/pydantic/main.py", line 176, in init
self.pydantic_validator.validate_python(data, self_instance=self)
pydantic_core._pydantic_core.ValidationError: 1 validation error for OpenAISettings
api_key

Field required [type=missing, input_value={'chat_model_id': 'phi3:mini'}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.7/v/missing

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/Users/andi/Projects/sk/ollama_chat_completion.py", line 39, in
kernel.add_service(OpenAIChatCompletion(service_id=service_id, ai_model_id="phi3:mini", async_client=openAIClient))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Caskroom/miniconda/base/envs/sk/lib/python3.12/site-packages/semantic_kernel/connectors/ai/open_ai/services/open_ai_chat_completion.py", line 59, in init
raise ServiceInitializationError("Failed to create OpenAI settings.", ex) from ex
semantic_kernel.exceptions.service_exceptions.ServiceInitializationError: ('Failed to create OpenAI settings.', 1 validation error for OpenAISettings
api_key
Field required [type=missing, input_value={'chat_model_id': 'phi3:mini'}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.7/v/missing)

If I change Line 39 by adding the fake API key as 2nd argument (to kernel.add_service(OpenAIChatCompletion(service_id=service_id, api_key="fake-key", ...) the error goes away and the code works.

Hope this helps, Andi

Copy link
Contributor

Choose a reason for hiding this comment

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

Hi @TaoChenOSU, yes, this is true as @AndreasKunar pointed out. In the OpenAISettings the model has the api_key as a required attribute. See here.

For Ollama local models that don't require an API key, and it is not provided, this will fail. Which is why Andi needed to provide a fake key to get this model validation to pass.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see. Then I guess the api key shouldn't be a required parameter but if one is not provided and no async_client is provided, it will be caught here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was able to run the sample without error because I had the env file.

Copy link
Contributor

Choose a reason for hiding this comment

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

Should we make the change to the OpenAI settings?

Copy link

@AndreasKunar AndreasKunar left a comment

Choose a reason for hiding this comment

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

Still fails for me because of the missing API-key parameter in line 39 (see comment for details on the validation error).

Choose a reason for hiding this comment

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

Running your ollama_chat_completion.py with semantic-kernel 1.1.2, openai 1.35.7 on macOS 14.5 produces the following Traceback (most recent call last): Note, I had to change phi3 to phi3:mini for my computer.

File "/opt/homebrew/Caskroom/miniconda/base/envs/sk/lib/python3.12/site-packages/semantic_kernel/connectors/ai/open_ai/services/open_ai_chat_completion.py", line 51, in init
openai_settings = OpenAISettings.create(
^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Caskroom/miniconda/base/envs/sk/lib/python3.12/site-packages/semantic_kernel/kernel_pydantic.py", line 56, in create
return cls(**data)
^^^^^^^^^^^
File "/opt/homebrew/Caskroom/miniconda/base/envs/sk/lib/python3.12/site-packages/pydantic_settings/main.py", line 140, in init
super().init(
File "/opt/homebrew/Caskroom/miniconda/base/envs/sk/lib/python3.12/site-packages/pydantic/main.py", line 176, in init
self.pydantic_validator.validate_python(data, self_instance=self)
pydantic_core._pydantic_core.ValidationError: 1 validation error for OpenAISettings
api_key

Field required [type=missing, input_value={'chat_model_id': 'phi3:mini'}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.7/v/missing

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/Users/andi/Projects/sk/ollama_chat_completion.py", line 39, in
kernel.add_service(OpenAIChatCompletion(service_id=service_id, ai_model_id="phi3:mini", async_client=openAIClient))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Caskroom/miniconda/base/envs/sk/lib/python3.12/site-packages/semantic_kernel/connectors/ai/open_ai/services/open_ai_chat_completion.py", line 59, in init
raise ServiceInitializationError("Failed to create OpenAI settings.", ex) from ex
semantic_kernel.exceptions.service_exceptions.ServiceInitializationError: ('Failed to create OpenAI settings.', 1 validation error for OpenAISettings
api_key
Field required [type=missing, input_value={'chat_model_id': 'phi3:mini'}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.7/v/missing)

If I change Line 39 by adding the fake API key as 2nd argument (to kernel.add_service(OpenAIChatCompletion(service_id=service_id, api_key="fake-key", ...) the error goes away and the code works.

Hope this helps, Andi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation python Pull requests for the Python Semantic Kernel
Projects
Status: No status
6 participants