Skip to content

Commit d84363f

Browse files
authored
Python: Migrate to new Google GenAI SDK (#13371)
### 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. --> Closes #12970, #13352 ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> 1. Migrate Google AI connector to use the new Google AI SDK. 2. This PR also marks the VertexAI connector as deprecated in favor of the Google AI connector. With this update, if users wants to connect to a VertexAI project instead of a Google AI endpoint, they can do so with the Google AI connector. 3. This PR also adds the ability for users to provide a custom client to the Google AI connector (#13352). 4. This PR also marks `AzureTextCompletion` deprecated. ### 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 😄
1 parent 5b30c8b commit d84363f

File tree

48 files changed

+4911
-4785
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+4911
-4785
lines changed

.github/workflows/python-integration-tests.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ env:
6767
GOOGLE_AI_GEMINI_MODEL_ID: ${{ vars.GOOGLE_AI_GEMINI_MODEL_ID }}
6868
GOOGLE_AI_EMBEDDING_MODEL_ID: ${{ vars.GOOGLE_AI_EMBEDDING_MODEL_ID }}
6969
GOOGLE_AI_API_KEY: ${{ secrets.GOOGLE_AI_API_KEY }}
70+
GOOGLE_AI_CLOUD_PROJECT_ID: ${{ vars.GOOGLE_AI_CLOUD_PROJECT_ID }}
7071
VERTEX_AI_PROJECT_ID: ${{ vars.VERTEX_AI_PROJECT_ID }}
7172
VERTEX_AI_GEMINI_MODEL_ID: ${{ vars.VERTEX_AI_GEMINI_MODEL_ID }}
7273
VERTEX_AI_EMBEDDING_MODEL_ID: ${{ vars.VERTEX_AI_EMBEDDING_MODEL_ID }}

python/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Highlights
44
- Flexible Agent Framework: build, orchestrate, and deploy AI agents and multi-agent systems
55
- Multi-Agent Systems: Model workflows and collaboration between AI specialists
66
- Plugin Ecosystem: Extend with Python, OpenAPI, Model Context Protocol (MCP), and more
7-
- LLM Support: OpenAI, Azure OpenAI, Hugging Face, Mistral, Vertex AI, ONNX, Ollama, NVIDIA NIM, and others
7+
- LLM Support: OpenAI, Azure OpenAI, Hugging Face, Mistral, Google AI, ONNX, Ollama, NVIDIA NIM, and others
88
- Vector DB Support: Azure AI Search, Elasticsearch, Chroma, and more
99
- Process Framework: Build structured business processes with workflow modeling
1010
- Multimodal: Text, vision, audio

python/pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ classifiers = [
2424
]
2525
dependencies = [
2626
# azure agents
27-
"azure-ai-projects >= 1.0.0b12",
27+
"azure-ai-projects ~= 1.0.0b12",
2828
"azure-ai-agents >= 1.2.0b3",
2929
"aiohttp ~= 3.8",
3030
"cloudevents ~=1.0",
@@ -93,7 +93,7 @@ faiss = [
9393
]
9494
google = [
9595
"google-cloud-aiplatform ~= 1.114.0",
96-
"google-generativeai ~= 0.8"
96+
"google-genai ~= 1.51.0"
9797
]
9898
hugging_face = [
9999
"transformers[torch] ~= 4.28",

python/samples/concepts/plugins/plugins_from_dir.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,21 @@
33
import asyncio
44
import os
55

6-
from azure.identity import AzureCliCredential
7-
86
from semantic_kernel import Kernel
9-
from semantic_kernel.connectors.ai.open_ai import AzureTextCompletion, OpenAITextCompletion
7+
from semantic_kernel.connectors.ai.open_ai import OpenAITextCompletion
108
from semantic_kernel.functions import KernelArguments
119

1210

1311
async def main():
1412
kernel = Kernel()
1513

16-
useAzureOpenAI = False
17-
model = "gpt-35-turbo-instruct" if useAzureOpenAI else "gpt-3.5-turbo-instruct"
14+
model = "gpt-3.5-turbo-instruct"
1815
service_id = model
1916

2017
# Configure AI service used by the kernel
21-
if useAzureOpenAI:
22-
kernel.add_service(
23-
AzureTextCompletion(service_id=service_id, credential=AzureCliCredential()),
24-
)
25-
else:
26-
kernel.add_service(
27-
OpenAITextCompletion(service_id=service_id, ai_model_id=model),
28-
)
18+
kernel.add_service(
19+
OpenAITextCompletion(service_id=service_id, ai_model_id=model),
20+
)
2921

3022
# note: using plugins from the samples folder
3123
plugins_directory = os.path.join(__file__, "../../../../../prompt_template_samples/")

python/samples/concepts/setup/ALL_SETTINGS.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
| | [OpenAITextToAudio](../../../semantic_kernel/connectors/ai/open_ai/services/open_ai_text_to_audio.py) | ai_model_id, <br> api_key, <br> org_id | OPENAI_TEXT_TO_AUDIO_MODEL_ID, <br> OPENAI_API_KEY, <br> OPENAI_ORG_ID | Yes, <br> Yes, <br> No | |
1212
| | [OpenAIAudioToText](../../../semantic_kernel/connectors/ai/open_ai/services/open_ai_audio_to_text.py) | ai_model_id, <br> api_key, <br> org_id | OPENAI_AUDIO_TO_TEXT_MODEL_ID, <br> OPENAI_API_KEY, <br> OPENAI_ORG_ID | Yes, <br> Yes, <br> No | |
1313
| Azure OpenAI | [AzureChatCompletion](../../../semantic_kernel/connectors/ai/open_ai/services/azure_chat_completion.py) | deployment_name, <br> api_key, <br> endpoint, <br> api_version, <br> base_url | AZURE_OPENAI_CHAT_DEPLOYMENT_NAME, <br> AZURE_OPENAI_API_KEY, <br> AZURE_OPENAI_ENDPOINT, <br> AZURE_OPENAI_API_VERSION, <br> AZURE_OPENAI_BASE_URL | Yes, <br> No, <br> Yes, <br> Yes, <br> Yes | [AzureOpenAISettings](../../../semantic_kernel/connectors/ai/open_ai/settings/azure_open_ai_settings.py) |
14-
| | [AzureTextCompletion](../../../semantic_kernel/connectors/ai/open_ai/services/azure_text_completion.py) | deployment_name, <br> api_key, <br> endpoint, <br> api_version, <br> base_url | AZURE_OPENAI_TEXT_DEPLOYMENT_NAME, <br> AZURE_OPENAI_API_KEY, <br> AZURE_OPENAI_ENDPOINT, <br> AZURE_OPENAI_API_VERSION, <br> AZURE_OPENAI_BASE_URL | Yes, <br> No, <br> Yes, <br> Yes, <br> Yes | |
1514
| | [AzureTextEmbedding](../../../semantic_kernel/connectors/ai/open_ai/services/azure_text_embedding.py) | deployment_name, <br> api_key, <br> endpoint, <br> api_version, <br> base_url | AZURE_OPENAI_EMBEDDING_DEPLOYMENT_NAME, <br> AZURE_OPENAI_API_KEY, <br> AZURE_OPENAI_ENDPOINT, <br> AZURE_OPENAI_API_VERSION, <br> AZURE_OPENAI_BASE_URL | Yes, <br> No, <br> Yes, <br> Yes, <br> Yes | |
1615
| | [AzureTextToImage](../../../semantic_kernel/connectors/ai/open_ai/services/azure_text_to_image.py) | deployment_name, <br> api_key, <br> endpoint | AZURE_OPENAI_TEXT_TO_IMAGE_DEPLOYMENT_NAME, <br> AZURE_OPENAI_API_KEY, <br> AZURE_OPENAI_ENDPOINT | Yes, <br> No, <br> Yes | |
1716
| | [AzureTextToAudio](../../../semantic_kernel/connectors/ai/open_ai/services/azure_text_to_audio.py) | deployment_name, <br> api_key, <br> endpoint | AZURE_OPENAI_TEXT_TO_AUDIO_DEPLOYMENT_NAME, <br> AZURE_OPENAI_API_KEY, <br> AZURE_OPENAI_ENDPOINT | Yes, <br> No, <br> Yes | |
@@ -25,9 +24,9 @@
2524
| Google AI | [GoogleAIChatCompletion](../../../semantic_kernel/connectors/ai/google/google_ai/services/google_ai_chat_completion.py) | gemini_model_id, <br> api_key | GOOGLE_AI_GEMINI_MODEL_ID, <br> GOOGLE_AI_API_KEY | Yes, <br> Yes | [GoogleAISettings](../../../semantic_kernel/connectors/ai/google/google_ai/google_ai_settings.py) |
2625
| | [GoogleAITextCompletion](../../../semantic_kernel/connectors/ai/google/google_ai/services/google_ai_text_completion.py) | gemini_model_id, <br> api_key | GOOGLE_AI_GEMINI_MODEL_ID, <br> GOOGLE_AI_API_KEY | Yes, <br> Yes | |
2726
| | [GoogleAITextEmbedding](../../../semantic_kernel/connectors/ai/google/google_ai/services/google_ai_text_embedding.py) | embedding_model_id, <br> api_key | GOOGLE_AI_EMBEDDING_MODEL_ID, <br> GOOGLE_AI_API_KEY | Yes, <br> Yes | |
28-
| Vertex AI | [VertexAIChatCompletion](../../../semantic_kernel/connectors/ai/google/vertex_ai/services/vertex_ai_chat_completion.py) | project_id, <br> region, <br> gemini_model_id | VERTEX_AI_PROJECT_ID, <br> VERTEX_AI_REGION, <br> VERTEX_AI_GEMINI_MODEL_ID | Yes, <br> No, <br> Yes | [VertexAISettings](../../../semantic_kernel/connectors/ai/google/vertex_ai/vertex_ai_settings.py) |
29-
| | [VertexAITextCompletion](../../../semantic_kernel/connectors/ai/google/google_ai/services/google_ai_text_completion.py) | project_id, <br> region, <br> gemini_model_id | VERTEX_AI_PROJECT_ID, <br> VERTEX_AI_REGION, <br> VERTEX_AI_GEMINI_MODEL_ID | Yes, <br> No, <br> Yes | |
30-
| | [VertexAITextEmbedding](../../../semantic_kernel/connectors/ai/google/google_ai/services/google_ai_text_embedding.py) | project_id, <br> region, <br> embedding_model_id | VERTEX_AI_PROJECT_ID, <br> VERTEX_AI_REGION, <br> VERTEX_AI_EMBEDDING_MODEL_ID | Yes, <br> No, <br> Yes | |
27+
| Vertex AI | [GoogleAIChatCompletion](../../../semantic_kernel/connectors/ai/google/google_ai/services/google_ai_chat_completion.py) | project_id, <br> region, <br> gemini_model_id | GOOGLE_AI_CLOUD_PROJECT_ID, <br> GOOGLE_AI_CLOUD_REGION, <br> GOOGLE_AI_GEMINI_MODEL_ID, <br> GOOGLE_AI_USE_VERTEXAI | Yes, <br> No, <br> Yes, <br> Yes (must set to true) | [GoogleAISettings](../../../semantic_kernel/connectors/ai/google/google_ai/google_ai_settings.py) |
28+
| | [GoogleAITextCompletion](../../../semantic_kernel/connectors/ai/google/google_ai/services/google_ai_text_completion.py) | project_id, <br> region, <br> gemini_model_id | GOOGLE_AI_CLOUD_PROJECT_ID, <br> GOOGLE_AI_CLOUD_REGION, <br> GOOGLE_AI_GEMINI_MODEL_ID, <br> GOOGLE_AI_USE_VERTEXAI | Yes, <br> No, <br> Yes, <br> Yes (must set to true) | |
29+
| | [GoogleAITextEmbedding](../../../semantic_kernel/connectors/ai/google/google_ai/services/google_ai_text_embedding.py) | project_id, <br> region, <br> embedding_model_id | GOOGLE_AI_CLOUD_PROJECT_ID, <br> GOOGLE_AI_CLOUD_REGION, <br> GOOGLE_AI_EMBEDDING_MODEL_ID, <br> GOOGLE_AI_USE_VERTEXAI | Yes, <br> No, <br> Yes, <br> Yes (must set to true) | |
3130
| HuggingFace | [HuggingFaceTextCompletion](../../../semantic_kernel/connectors/ai/hugging_face/services/hf_text_completion.py) | ai_model_id | N/A | Yes | |
3231
| | [HuggingFaceTextEmbedding](../../../semantic_kernel/connectors/ai/hugging_face/services/hf_text_embedding.py) | ai_model_id | N/A | Yes | |
3332
| NVIDIA NIM | [NvidiaChatCompletion](../../../semantic_kernel/connectors/ai/nvidia/services/nvidia_chat_completion.py) | ai_model_id, <br> api_key, <br> base_url | NVIDIA_CHAT_MODEL_ID, <br> NVIDIA_API_KEY, <br> NVIDIA_BASE_URL | Yes (default: meta/llama-3.1-8b-instruct), <br> Yes, <br> No | [NvidiaAISettings](../../../semantic_kernel/connectors/ai/nvidia/settings/nvidia_settings.py) |

python/samples/concepts/setup/chat_completion_services.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,7 @@ def get_google_ai_chat_completion_service_and_request_settings() -> tuple[
249249
Please refer to the Semantic Kernel Python documentation for more information:
250250
https://learn.microsoft.com/en-us/python/api/semantic-kernel/semantic_kernel?view=semantic-kernel
251251
"""
252-
from semantic_kernel.connectors.ai.google.google_ai import (
253-
GoogleAIChatCompletion,
254-
GoogleAIChatPromptExecutionSettings,
255-
)
252+
from semantic_kernel.connectors.ai.google import GoogleAIChatCompletion, GoogleAIChatPromptExecutionSettings
256253

257254
chat_service = GoogleAIChatCompletion(service_id=service_id)
258255
request_settings = GoogleAIChatPromptExecutionSettings(service_id=service_id)
@@ -356,13 +353,10 @@ def get_vertex_ai_chat_completion_service_and_request_settings() -> tuple[
356353
Please refer to the Semantic Kernel Python documentation for more information:
357354
https://learn.microsoft.com/en-us/python/api/semantic-kernel/semantic_kernel?view=semantic-kernel
358355
"""
359-
from semantic_kernel.connectors.ai.google.vertex_ai import (
360-
VertexAIChatCompletion,
361-
VertexAIChatPromptExecutionSettings,
362-
)
356+
from semantic_kernel.connectors.ai.google import GoogleAIChatCompletion, GoogleAIChatPromptExecutionSettings
363357

364-
chat_service = VertexAIChatCompletion(service_id=service_id)
365-
request_settings = VertexAIChatPromptExecutionSettings(service_id=service_id)
358+
chat_service = GoogleAIChatCompletion(service_id=service_id, use_vertexai=True)
359+
request_settings = GoogleAIChatPromptExecutionSettings(service_id=service_id)
366360

367361
return chat_service, request_settings
368362

python/samples/concepts/setup/text_completion_services.py

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ class Services(str, Enum):
1616
"""
1717

1818
OPENAI = "openai"
19-
AZURE_OPENAI = "azure_openai"
2019
BEDROCK = "bedrock"
2120
GOOGLE_AI = "google_ai"
2221
HUGGING_FACE = "huggingface"
@@ -36,7 +35,6 @@ def get_text_completion_service_and_request_settings(
3635
# Use lambdas or functions to delay instantiation
3736
text_services = {
3837
Services.OPENAI: lambda: get_openai_text_completion_service_and_request_settings(),
39-
Services.AZURE_OPENAI: lambda: get_azure_openai_text_completion_service_and_request_settings(),
4038
Services.BEDROCK: lambda: get_bedrock_text_completion_service_and_request_settings(),
4139
Services.GOOGLE_AI: lambda: get_google_ai_text_completion_service_and_request_settings(),
4240
Services.HUGGING_FACE: lambda: get_hugging_face_text_completion_service_and_request_settings(),
@@ -75,32 +73,6 @@ def get_openai_text_completion_service_and_request_settings() -> tuple[
7573
return text_service, request_settings
7674

7775

78-
def get_azure_openai_text_completion_service_and_request_settings() -> tuple[
79-
"TextCompletionClientBase", "PromptExecutionSettings"
80-
]:
81-
"""Return Azure OpenAI text completion service and request settings.
82-
83-
The service credentials can be read by 3 ways:
84-
1. Via the constructor
85-
2. Via the environment variables
86-
3. Via an environment file
87-
88-
The request settings control the behavior of the service. The default settings are sufficient to get started.
89-
However, you can adjust the settings to suit your needs.
90-
Note: Some of the settings are NOT meant to be set by the user.
91-
Please refer to the Semantic Kernel Python documentation for more information:
92-
https://learn.microsoft.com/en-us/python/api/semantic-kernel/semantic_kernel?view=semantic-kernel
93-
"""
94-
from azure.identity import AzureCliCredential
95-
96-
from semantic_kernel.connectors.ai.open_ai import AzureTextCompletion, OpenAITextPromptExecutionSettings
97-
98-
text_service = AzureTextCompletion(credential=AzureCliCredential())
99-
request_settings = OpenAITextPromptExecutionSettings()
100-
101-
return text_service, request_settings
102-
103-
10476
def get_bedrock_text_completion_service_and_request_settings() -> tuple[
10577
"TextCompletionClientBase", "PromptExecutionSettings"
10678
]:
@@ -262,12 +234,9 @@ def get_vertex_ai_text_completion_service_and_request_settings() -> tuple[
262234
Please refer to the Semantic Kernel Python documentation for more information:
263235
https://learn.microsoft.com/en-us/python/api/semantic-kernel/semantic_kernel?view=semantic-kernel
264236
"""
265-
from semantic_kernel.connectors.ai.google.vertex_ai import (
266-
VertexAITextCompletion,
267-
VertexAITextPromptExecutionSettings,
268-
)
237+
from semantic_kernel.connectors.ai.google import GoogleAITextCompletion, GoogleAITextPromptExecutionSettings
269238

270-
text_service = VertexAITextCompletion()
271-
request_settings = VertexAITextPromptExecutionSettings()
239+
text_service = GoogleAITextCompletion()
240+
request_settings = GoogleAITextPromptExecutionSettings()
272241

273242
return text_service, request_settings

python/samples/concepts/setup/text_embedding_services.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -296,13 +296,10 @@ def get_vertex_ai_text_embedding_service_and_request_settings() -> tuple[
296296
Please refer to the Semantic Kernel Python documentation for more information:
297297
https://learn.microsoft.com/en-us/python/api/semantic-kernel/semantic_kernel?view=semantic-kernel
298298
"""
299-
from semantic_kernel.connectors.ai.google.vertex_ai import (
300-
VertexAIEmbeddingPromptExecutionSettings,
301-
VertexAITextEmbedding,
302-
)
299+
from semantic_kernel.connectors.ai.google import GoogleAIEmbeddingPromptExecutionSettings, GoogleAITextEmbedding
303300

304-
embedding_service = VertexAITextEmbedding()
301+
embedding_service = GoogleAITextEmbedding()
305302
# Note: not all models support specifying the dimensions or there may be constraints on the dimensions
306-
request_settings = VertexAIEmbeddingPromptExecutionSettings(output_dimensionality=768)
303+
request_settings = GoogleAIEmbeddingPromptExecutionSettings(output_dimensionality=768)
307304

308305
return embedding_service, request_settings

python/samples/concepts/text_completion/text_completion.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
# You can select from the following text embedding services:
1414
# - Services.OPENAI
15-
# - Services.AZURE_OPENAI
1615
# - Services.BEDROCK
1716
# - Services.GOOGLE_AI
1817
# - Services.HUGGING_FACE

python/samples/concepts/text_completion/text_completion_streaming.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
# You can select from the following text embedding services:
1414
# - Services.OPENAI
15-
# - Services.AZURE_OPENAI
1615
# - Services.BEDROCK
1716
# - Services.GOOGLE_AI
1817
# - Services.HUGGING_FACE

0 commit comments

Comments
 (0)