Skip to content

Commit

Permalink
Python: added embedding dimensions support (#6111)
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! -->

added embedding dimensions support (issue:
#5882)

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [] The code builds clean without any errors or warnings
- [] 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
- [] All unit tests pass, and I have added new tests where possible
- [] I didn't break anyone 😄

---------

Co-authored-by: Sin Yu Bonnie Ho <[email protected]>
Co-authored-by: Eduard van Valkenburg <[email protected]>
  • Loading branch information
3 people authored May 4, 2024
1 parent ce2d9c9 commit 2f4387d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,4 @@ class OpenAIEmbeddingPromptExecutionSettings(PromptExecutionSettings):
extra_query: Optional[Dict] = None
extra_body: Optional[Dict] = None
timeout: Optional[float] = None
dimensions: Optional[int] = Field(None, gt=0, le=3072)
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ async def test_azure_text_embedding_calls_with_parameters(mock_create) -> None:
api_key = "test_api_key"
api_version = "2023-03-15-preview"
texts = ["hello world", "goodbye world"]
embedding_dimensions = 1536

azure_text_embedding = AzureTextEmbedding(
deployment_name=deployment_name,
Expand All @@ -138,11 +139,12 @@ async def test_azure_text_embedding_calls_with_parameters(mock_create) -> None:
api_version=api_version,
)

await azure_text_embedding.generate_embeddings(texts)
await azure_text_embedding.generate_embeddings(texts, dimensions=embedding_dimensions)

mock_create.assert_awaited_once_with(
input=texts,
model=deployment_name,
dimensions=embedding_dimensions,
)


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright (c) Microsoft. All rights reserved.

from unittest.mock import AsyncMock, patch

import pytest
from openai.resources.embeddings import AsyncEmbeddings

from semantic_kernel.connectors.ai.open_ai.services.open_ai_text_embedding import OpenAITextEmbedding


@pytest.mark.asyncio
@patch.object(AsyncEmbeddings, "create", new_callable=AsyncMock)
async def test_openai_text_embedding_calls_with_parameters(mock_create) -> None:
ai_model_id = "test_model_id"
api_key = "test_api_key"
texts = ["hello world", "goodbye world"]
embedding_dimensions = 1536

openai_text_embedding = OpenAITextEmbedding(
ai_model_id=ai_model_id,
api_key=api_key,
)

await openai_text_embedding.generate_embeddings(texts, dimensions=embedding_dimensions)

mock_create.assert_awaited_once_with(
input=texts,
model=ai_model_id,
dimensions=embedding_dimensions,
)

0 comments on commit 2f4387d

Please sign in to comment.