Skip to content

Commit

Permalink
Merge branch 'main' into bump-py-ver-20241121
Browse files Browse the repository at this point in the history
  • Loading branch information
moonbox3 authored Nov 21, 2024
2 parents 9af6055 + ea2ba47 commit 7142f0e
Show file tree
Hide file tree
Showing 56 changed files with 1,726 additions and 569 deletions.
3 changes: 1 addition & 2 deletions python/samples/concepts/chat_completion/chat_bedrock_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import asyncio

from semantic_kernel import Kernel
from semantic_kernel.connectors.ai.bedrock.bedrock_prompt_execution_settings import BedrockChatPromptExecutionSettings
from semantic_kernel.connectors.ai.bedrock.services.bedrock_chat_completion import BedrockChatCompletion
from semantic_kernel.connectors.ai.bedrock import BedrockChatCompletion, BedrockChatPromptExecutionSettings
from semantic_kernel.contents import ChatHistory

system_message = """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from openai import AsyncOpenAI

from semantic_kernel.connectors.ai.open_ai.services.open_ai_chat_completion import OpenAIChatCompletion
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
from semantic_kernel.contents.chat_history import ChatHistory
from semantic_kernel.functions.kernel_arguments import KernelArguments
from semantic_kernel.kernel import Kernel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from openai import AsyncOpenAI

from semantic_kernel.connectors.ai.open_ai.services.open_ai_text_embedding import OpenAITextEmbedding
from semantic_kernel.connectors.ai.open_ai import OpenAITextEmbedding
from semantic_kernel.core_plugins.text_memory_plugin import TextMemoryPlugin
from semantic_kernel.kernel import Kernel
from semantic_kernel.memory.semantic_text_memory import SemanticTextMemory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from openai import AsyncOpenAI

from semantic_kernel.connectors.ai.open_ai.services.open_ai_chat_completion import OpenAIChatCompletion
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
from semantic_kernel.contents.chat_history import ChatHistory
from semantic_kernel.functions.kernel_arguments import KernelArguments
from semantic_kernel.kernel import Kernel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
formatted_question,
formatted_system_message,
)
from semantic_kernel.connectors.ai.azure_ai_inference.services.azure_ai_inference_chat_completion import (
AzureAIInferenceChatCompletion,
)
from semantic_kernel.connectors.ai.azure_ai_inference import AzureAIInferenceChatCompletion
from semantic_kernel.connectors.ai.chat_completion_client_base import ChatCompletionClientBase
from semantic_kernel.contents.chat_history import ChatHistory
from semantic_kernel.functions.kernel_arguments import KernelArguments
Expand Down
134 changes: 46 additions & 88 deletions python/samples/concepts/setup/ALL_SETTINGS.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions python/samples/demos/process_with_dapr/fastapi_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ async def start_process(process_id: str):
process_id=process_id,
)
return JSONResponse(content={"processId": process_id}, status_code=200)
except Exception as e:
return JSONResponse(content={"error": str(e)}, status_code=500)
except Exception:
return JSONResponse(content={"error": "Error starting process"}, status_code=500)


if __name__ == "__main__":
Expand Down
5 changes: 2 additions & 3 deletions python/samples/demos/process_with_dapr/flask_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@ def start_process(process_id):
)

return jsonify({"processId": process_id}), 200
except Exception as e:
logging.exception("Error starting process")
return jsonify({"error": str(e)}), 500
except Exception:
return jsonify({"error": "Error starting process"}), 500


# Run application
Expand Down
52 changes: 52 additions & 0 deletions python/semantic_kernel/connectors/ai/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# AI Connectors

This directory contains the implementation of the AI connectors (aka AI services) that are used to interact with AI models.

Depending on the modality, the AI connector can inherit from one of the following classes:
- [`ChatCompletionClientBase`](./chat_completion_client_base.py) for chat completion tasks.
- [`TextCompletionClientBase`](./text_completion_client_base.py) for text completion tasks.
- [`AudioToTextClientBase`](./audio_to_text_client_base.py) for audio to text tasks.
- [`TextToAudioClientBase`](./text_to_audio_client_base.py) for text to audio tasks.
- [`TextToImageClientBase`](./text_to_image_client_base.py) for text to image tasks.
- [`EmbeddingGeneratorBase`](./embeddings/embedding_generator_base.py) for text embedding tasks.


All base clients inherit from the [`AIServiceClientBase`](../../services/ai_service_client_base.py) class.

## Existing AI connectors

| Services | Connectors |
|-------------------------|--------------------------------------|
| OpenAI | [`OpenAIChatCompletion`](./open_ai/services/open_ai_chat_completion.py) |
| | [`OpenAITextCompletion`](./open_ai/services/open_ai_text_completion.py) |
| | [`OpenAITextEmbedding`](./open_ai/services/open_ai_text_embedding.py) |
| | [`OpenAITextToImage`](./open_ai/services/open_ai_text_to_image.py) |
| | [`OpenAITextToAudio`](./open_ai/services/open_ai_text_to_audio.py) |
| | [`OpenAIAudioToText`](./open_ai/services/open_ai_audio_to_text.py) |
| Azure OpenAI | [`AzureChatCompletion`](./open_ai/services/azure_chat_completion.py) |
| | [`AzureTextCompletion`](./open_ai/services/azure_text_completion.py) |
| | [`AzureTextEmbedding`](./open_ai/services/azure_text_embedding.py) |
| | [`AzureTextToImage`](./open_ai/services/azure_text_to_image.py) |
| | [`AzureTextToAudio`](./open_ai/services/azure_text_to_audio.py) |
| | [`AzureAudioToText`](./open_ai/services/azure_audio_to_text.py) |
| Azure AI Inference | [`AzureAIInferenceChatCompletion`](./azure_ai_inference/services/azure_ai_inference_chat_completion.py) |
| | [`AzureAIInferenceTextEmbedding`](./azure_ai_inference/services/azure_ai_inference_text_embedding.py) |
| Anthropic | [`AnthropicChatCompletion`](./anthropic/services/anthropic_chat_completion.py) |
| [Bedrock](./bedrock/README.md) | [`BedrockChatCompletion`](./bedrock/services/bedrock_chat_completion.py) |
| | [`BedrockTextCompletion`](./bedrock/services/bedrock_text_completion.py) |
| | [`BedrockTextEmbedding`](./bedrock/services/bedrock_text_embedding.py) |
| [Google AI](./google/README.md) | [`GoogleAIChatCompletion`](./google/google_ai/services/google_ai_chat_completion.py) |
| | [`GoogleAITextCompletion`](./google/google_ai/services/google_ai_text_completion.py) |
| | [`GoogleAITextEmbedding`](./google/google_ai/services/google_ai_text_embedding.py) |
| [Vertex AI](./google/README.md) | [`VertexAIChatCompletion`](./google/vertex_ai/services/vertex_ai_chat_completion.py) |
| | [`VertexAITextCompletion`](./google/vertex_ai/services/vertex_ai_text_completion.py) |
| | [`VertexAITextEmbedding`](./google/vertex_ai/services/vertex_ai_text_embedding.py) |
| HuggingFace | [`HuggingFaceTextCompletion`](./hugging_face/services/hf_text_completion.py) |
| | [`HuggingFaceTextEmbedding`](./hugging_face/services/hf_text_embedding.py) |
| Mistral AI | [`MistralAIChatCompletion`](./mistral_ai/services/mistral_ai_chat_completion.py) |
| | [`MistralAITextEmbedding`](./mistral_ai/services/mistral_ai_text_embedding.py) |
| Ollama | [`OllamaChatCompletion`](./ollama/services/ollama_chat_completion.py) |
| | [`OllamaTextCompletion`](./ollama/services/ollama_text_completion.py) |
| | [`OllamaTextEmbedding`](./ollama/services/ollama_text_embedding.py) |
| Onnx | [`OnnxGenAIChatCompletion`](./onnx/services/onnx_gen_ai_chat_completion.py) |
| | [`OnnxGenAITextCompletion`](./onnx/services/onnx_gen_ai_text_completion.py) |
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def __enter__(self) -> None:
self.diagnostics_settings.enable_otel_diagnostics
or self.diagnostics_settings.enable_otel_diagnostics_sensitive
):
AIInferenceInstrumentor().instrument(
AIInferenceInstrumentor().instrument( # type: ignore
enable_content_recording=self.diagnostics_settings.enable_otel_diagnostics_sensitive
)

Expand Down
23 changes: 23 additions & 0 deletions python/semantic_kernel/connectors/ai/bedrock/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright (c) Microsoft. All rights reserved.

from semantic_kernel.connectors.ai.bedrock.bedrock_prompt_execution_settings import (
BedrockChatPromptExecutionSettings,
BedrockEmbeddingPromptExecutionSettings,
BedrockPromptExecutionSettings,
BedrockTextPromptExecutionSettings,
)
from semantic_kernel.connectors.ai.bedrock.bedrock_settings import BedrockSettings
from semantic_kernel.connectors.ai.bedrock.services.bedrock_chat_completion import BedrockChatCompletion
from semantic_kernel.connectors.ai.bedrock.services.bedrock_text_completion import BedrockTextCompletion
from semantic_kernel.connectors.ai.bedrock.services.bedrock_text_embedding import BedrockTextEmbedding

__all__ = [
"BedrockChatCompletion",
"BedrockChatPromptExecutionSettings",
"BedrockEmbeddingPromptExecutionSettings",
"BedrockPromptExecutionSettings",
"BedrockSettings",
"BedrockTextCompletion",
"BedrockTextEmbedding",
"BedrockTextPromptExecutionSettings",
]
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
GoogleAIChatPromptExecutionSettings,
GoogleAIEmbeddingPromptExecutionSettings,
GoogleAIPromptExecutionSettings,
GoogleAITextPromptExecutionSettings,
)
from semantic_kernel.connectors.ai.google.google_ai.services.google_ai_chat_completion import GoogleAIChatCompletion
from semantic_kernel.connectors.ai.google.google_ai.services.google_ai_text_completion import GoogleAITextCompletion
Expand All @@ -16,4 +17,5 @@
"GoogleAIPromptExecutionSettings",
"GoogleAITextCompletion",
"GoogleAITextEmbedding",
"GoogleAITextPromptExecutionSettings",
]
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def __init__(
If no arguments are provided, the service will attempt to load the settings from the environment.
The following environment variables are used:
- GOOGLE_AI_AI_MODEL_ID
- GOOGLE_AI_GEMINI_MODEL_ID
- GOOGLE_AI_API_KEY
Args:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
VertexAIChatPromptExecutionSettings,
VertexAIEmbeddingPromptExecutionSettings,
VertexAIPromptExecutionSettings,
VertexAITextPromptExecutionSettings,
)

__all__ = [
Expand All @@ -16,4 +17,5 @@
"VertexAIPromptExecutionSettings",
"VertexAITextCompletion",
"VertexAITextEmbedding",
"VertexAITextPromptExecutionSettings",
]
4 changes: 4 additions & 0 deletions python/semantic_kernel/connectors/ai/open_ai/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
from semantic_kernel.connectors.ai.open_ai.services.open_ai_text_embedding import OpenAITextEmbedding
from semantic_kernel.connectors.ai.open_ai.services.open_ai_text_to_audio import OpenAITextToAudio
from semantic_kernel.connectors.ai.open_ai.services.open_ai_text_to_image import OpenAITextToImage
from semantic_kernel.connectors.ai.open_ai.settings.azure_open_ai_settings import AzureOpenAISettings
from semantic_kernel.connectors.ai.open_ai.settings.open_ai_settings import OpenAISettings

__all__ = [
"ApiKeyAuthentication",
Expand All @@ -52,6 +54,7 @@
"AzureCosmosDBDataSourceParameters",
"AzureDataSourceParameters",
"AzureEmbeddingDependency",
"AzureOpenAISettings",
"AzureTextCompletion",
"AzureTextEmbedding",
"AzureTextToAudio",
Expand All @@ -66,6 +69,7 @@
"OpenAIChatPromptExecutionSettings",
"OpenAIEmbeddingPromptExecutionSettings",
"OpenAIPromptExecutionSettings",
"OpenAISettings",
"OpenAITextCompletion",
"OpenAITextEmbedding",
"OpenAITextPromptExecutionSettings",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,28 @@
from enum import Enum

from pydantic import HttpUrl
from typing_extensions import deprecated

from semantic_kernel.kernel_pydantic import KernelBaseModel


@deprecated("The `OpenAIAuthenticationType` class is deprecated; use the `OpenAPI` plugin instead.", category=None)
class OpenAIAuthenticationType(str, Enum):
"""OpenAI authentication types."""

OAuth = "oauth"
NoneType = "none"


@deprecated("The `OpenAIAuthenticationType` class is deprecated; use the `OpenAPI` plugin instead.", category=None)
class OpenAIAuthorizationType(str, Enum):
"""OpenAI authorization types."""

Bearer = "Bearer"
Basic = "Basic"


@deprecated("The `OpenAIAuthenticationConfig` class is deprecated; use the `OpenAPI` plugin instead.", category=None)
class OpenAIAuthenticationConfig(KernelBaseModel):
"""OpenAI authentication configuration."""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,47 @@


from collections.abc import Awaitable, Callable
from typing import Any
from typing import TYPE_CHECKING, Any
from urllib.parse import urlparse

from semantic_kernel.connectors.openapi_plugin.openapi_function_execution_parameters import (
OpenAPIFunctionExecutionParameters,
)
import httpx
from pydantic import Field
from typing_extensions import deprecated

from semantic_kernel.kernel_pydantic import KernelBaseModel

if TYPE_CHECKING:
from semantic_kernel.connectors.openapi_plugin import (
OperationSelectionPredicateContext,
)

OpenAIAuthCallbackType = Callable[..., Awaitable[Any]]


class OpenAIFunctionExecutionParameters(OpenAPIFunctionExecutionParameters):
@deprecated(
"The `OpenAIFunctionExecutionParameters` class is deprecated; use the `OpenAPI` plugin instead.", category=None
)
class OpenAIFunctionExecutionParameters(KernelBaseModel):
"""OpenAI function execution parameters."""

auth_callback: OpenAIAuthCallbackType | None = None
http_client: httpx.AsyncClient | None = None
server_url_override: str | None = None
ignore_non_compliant_errors: bool = False
user_agent: str | None = None
enable_dynamic_payload: bool = True
enable_payload_namespacing: bool = False
operations_to_exclude: list[str] = Field(default_factory=list, description="The operationId(s) to exclude")
operation_selection_predicate: Callable[["OperationSelectionPredicateContext"], bool] | None = None

def model_post_init(self, __context: Any) -> None:
"""Post initialization method for the model."""
from semantic_kernel.utils.telemetry.user_agent import HTTP_USER_AGENT

if self.server_url_override:
parsed_url = urlparse(self.server_url_override)
if not parsed_url.scheme or not parsed_url.netloc:
raise ValueError(f"Invalid server_url_override: {self.server_url_override}")

if not self.user_agent:
self.user_agent = HTTP_USER_AGENT
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
import logging
from typing import Any

from typing_extensions import deprecated

from semantic_kernel.exceptions.function_exceptions import PluginInitializationError

logger: logging.Logger = logging.getLogger(__name__)


@deprecated("The `OpenAIUtils` class is deprecated; use the `OpenAPI` plugin instead.", category=None)
class OpenAIUtils:
"""Utility functions for OpenAI plugins."""

Expand Down
6 changes: 5 additions & 1 deletion python/semantic_kernel/connectors/openapi_plugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@
from semantic_kernel.connectors.openapi_plugin.openapi_function_execution_parameters import (
OpenAPIFunctionExecutionParameters,
)
from semantic_kernel.connectors.openapi_plugin.openapi_parser import OpenApiParser
from semantic_kernel.connectors.openapi_plugin.operation_selection_predicate_context import (
OperationSelectionPredicateContext,
)

__all__ = ["OpenAPIFunctionExecutionParameters"]
__all__ = ["OpenAPIFunctionExecutionParameters", "OpenApiParser", "OperationSelectionPredicateContext"]
18 changes: 18 additions & 0 deletions python/semantic_kernel/connectors/openapi_plugin/const.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright (c) Microsoft. All rights reserved.


from enum import Enum

from semantic_kernel.utils.experimental_decorator import experimental_class


@experimental_class
class OperationExtensions(Enum):
"""The operation extensions."""

METHOD_KEY = "method"
OPERATION_KEY = "operation"
INFO_KEY = "info"
SECURITY_KEY = "security"
SERVER_URLS_KEY = "server-urls"
METADATA_KEY = "operation-extensions"
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@


@experimental_class
class RestApiOperationExpectedResponse:
"""RestApiOperationExpectedResponse."""
class RestApiExpectedResponse:
"""RestApiExpectedResponse."""

def __init__(self, description: str, media_type: str, schema: dict[str, str] | None = None):
"""Initialize the RestApiOperationExpectedResponse."""
"""Initialize the RestApiExpectedResponse."""
self.description = description
self.media_type = media_type
self.schema = schema
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright (c) Microsoft. All rights reserved.

from dataclasses import dataclass

from semantic_kernel.utils.experimental_decorator import experimental_class


@experimental_class
@dataclass
class RestApiOAuthFlow:
"""Represents the OAuth flow used by the REST API."""

authorization_url: str
token_url: str
scopes: dict[str, str]
refresh_url: str | None = None
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright (c) Microsoft. All rights reserved.

from dataclasses import dataclass

from semantic_kernel.connectors.openapi_plugin.models.rest_api_oauth_flow import RestApiOAuthFlow
from semantic_kernel.utils.experimental_decorator import experimental_class


@experimental_class
@dataclass
class RestApiOAuthFlows:
"""Represents the OAuth flows used by the REST API."""

implicit: RestApiOAuthFlow | None = None
password: RestApiOAuthFlow | None = None
client_credentials: RestApiOAuthFlow | None = None
authorization_code: RestApiOAuthFlow | None = None
Loading

0 comments on commit 7142f0e

Please sign in to comment.