Python: OpenAI Connector (#144)

* Initial checkin of openai connector

* add tests

* extensions work

* chat completion client implicitly implementing ChatClient

* remove AIServiceClientBase

* remove PromptExecutionSettings

* consolidate chat completion types

* add integration test

* fix pre-commit check errors

* remove usage statistics from OpenAIHandler

* Update python/extensions/agent-framework-openai/agent_framework/openai/exceptions.py

Co-authored-by: Dmytro Struk <13853051+dmytrostruk@users.noreply.github.com>

* PR comments

* fix merge

* fix test import

* remove tests for now because they just fail

* Remove fixed TODO

---------

Co-authored-by: Dmytro Struk <13853051+dmytrostruk@users.noreply.github.com>
This commit is contained in:
peterychang
2025-07-11 03:34:25 -04:00
committed by GitHub
Unverified
parent fef4fd2c18
commit e70401e658
15 changed files with 982 additions and 8 deletions
@@ -2,6 +2,7 @@
import importlib
import importlib.metadata
from typing import Any
try:
__version__ = importlib.metadata.version(__name__)
@@ -10,6 +11,8 @@ except importlib.metadata.PackageNotFoundError:
_IMPORTS = {
"get_logger": "._logging",
"AFBaseModel": "._pydantic",
"AFBaseSettings": "._pydantic",
"Agent": "._agents",
"AgentThread": "._agents",
"AITool": "._tools",
@@ -40,10 +43,12 @@ _IMPORTS = {
"EmbeddingGenerator": "._clients",
"InputGuardrail": ".guard_rails",
"OutputGuardrail": ".guard_rails",
"TextToSpeechOptions": "._types",
"SpeechToTextOptions": "._types",
}
def __getattr__(name: str):
def __getattr__(name: str) -> Any:
if name == "__version__":
return __version__
if name in _IMPORTS:
@@ -53,5 +58,5 @@ def __getattr__(name: str):
raise AttributeError(f"module {__name__} has no attribute {name}")
def __dir__():
def __dir__() -> list[str]:
return [*list(_IMPORTS.keys()), "__version__"]
@@ -4,6 +4,7 @@ from . import __version__ # type: ignore[attr-defined]
from ._agents import Agent, AgentThread
from ._clients import ChatClient, ChatClientBase, EmbeddingGenerator, use_tool_calling
from ._logging import get_logger
from ._pydantic import AFBaseModel, AFBaseSettings
from ._tools import AITool, ai_function
from ._types import (
AIContent,
@@ -20,9 +21,11 @@ from ._types import (
FunctionCallContent,
FunctionResultContent,
GeneratedEmbeddings,
SpeechToTextOptions,
StructuredResponse,
TextContent,
TextReasoningContent,
TextToSpeechOptions,
UriContent,
UsageContent,
UsageDetails,
@@ -30,6 +33,8 @@ from ._types import (
from .guard_rails import InputGuardrail, OutputGuardrail
__all__ = [
"AFBaseModel",
"AFBaseSettings",
"AIContent",
"AIContents",
"AITool",
@@ -52,9 +57,11 @@ __all__ = [
"GeneratedEmbeddings",
"InputGuardrail",
"OutputGuardrail",
"SpeechToTextOptions",
"StructuredResponse",
"TextContent",
"TextReasoningContent",
"TextToSpeechOptions",
"UriContent",
"UsageContent",
"UsageDetails",
@@ -1,10 +1,69 @@
# Copyright (c) Microsoft. All rights reserved.
from pydantic import BaseModel, ConfigDict
from typing import Any, ClassVar, TypeVar
from pydantic import BaseModel, ConfigDict, Field
from pydantic_settings import BaseSettings, SettingsConfigDict
class AFBaseModel(BaseModel):
"""Base class for all pydantic models in the Agent Framework."""
model_config = ConfigDict(populate_by_name=True, arbitrary_types_allowed=True, validate_assignment=True)
TSettings = TypeVar("TSettings", bound="AFBaseSettings")
class AFBaseSettings(BaseSettings):
"""Base class for all settings classes in the Agent Framework.
A subclass creates it's fields and overrides the env_prefix class variable
with the prefix for the environment variables.
In the case where a value is specified for the same Settings field in multiple ways,
the selected value is determined as follows (in descending order of priority):
- Arguments passed to the Settings class initializer.
- Environment variables, e.g. my_prefix_special_function as described above.
- Variables loaded from a dotenv (.env) file.
- Variables loaded from the secrets directory.
- The default field values for the Settings model.
"""
env_prefix: ClassVar[str] = ""
env_file_path: str | None = Field(default=None, exclude=True)
env_file_encoding: str | None = Field(default="utf-8", exclude=True)
model_config = SettingsConfigDict(
extra="ignore",
case_sensitive=False,
)
def __init__(
self,
**kwargs: Any,
) -> None:
"""Initialize the settings class."""
# Remove any None values from the kwargs so that defaults are used.
kwargs = {k: v for k, v in kwargs.items() if v is not None}
super().__init__(**kwargs)
def __new__(cls: type["TSettings"], *args: Any, **kwargs: Any) -> "TSettings":
"""Override the __new__ method to set the env_prefix."""
# for both, if supplied but None, set to default
if "env_file_encoding" in kwargs and kwargs["env_file_encoding"] is not None:
env_file_encoding = kwargs["env_file_encoding"]
else:
env_file_encoding = "utf-8"
if "env_file_path" in kwargs and kwargs["env_file_path"] is not None:
env_file_path = kwargs["env_file_path"]
else:
env_file_path = ".env"
cls.model_config.update( # type: ignore
env_prefix=cls.env_prefix,
env_file=env_file_path,
env_file_encoding=env_file_encoding,
)
cls.model_rebuild()
return super().__new__(cls) # type: ignore[return-value]
@@ -1552,3 +1552,76 @@ class GeneratedEmbeddings(AFBaseModel, MutableSequence[TEmbedding], Generic[TEmb
else:
self.embeddings += values
return self
# region: SpeechToTextOptions
class SpeechToTextOptions(AFBaseModel):
"""Common request settings for Speech to Text AI services."""
ai_model_id: Annotated[str | None, Field(serialization_alias="model")] = None
speech_language: Annotated[str | None, Field(description="Language of the input speech.")] = None
text_language: Annotated[str | None, Field(description="Language of the output text.")] = None
speech_sample_rate: Annotated[int | None, Field(description="Sample rate of the input speech.")] = None
additional_properties: dict[str, Any] = Field(
default_factory=dict, description="Provider-specific additional properties."
)
def to_provider_settings(self, by_alias: bool = True, exclude: set[str] | None = None) -> dict[str, Any]:
"""Convert the SpeechToTextOptions to a dictionary suitable for provider requests.
Args:
by_alias: Use alias names for fields if True.
exclude: Additional keys to exclude from the output.
Returns:
Dictionary of settings for provider.
"""
default_exclude = {"additional_properties"}
merged_exclude = default_exclude if exclude is None else default_exclude | set(exclude)
settings: dict[str, Any] = self.model_dump(exclude_none=True, by_alias=by_alias, exclude=merged_exclude)
settings = {k: v for k, v in settings.items() if not (isinstance(v, dict) and not v)}
settings.update(self.additional_properties)
for key in merged_exclude:
settings.pop(key, None)
return settings
# region: TextToSpeechOptions
class TextToSpeechOptions(AFBaseModel):
"""Request settings for text to speech services.
Tailor this to be more general as more models (aside from OpenAI) are added.
"""
ai_model_id: str | None = Field(None, serialization_alias="model")
voice: Literal["alloy", "ash", "ballad", "coral", "echo", "fable", "onyx", "nova", "sage", "shimmer"] = "alloy"
response_format: Literal["mp3", "opus", "aac", "flac", "wav", "pcm"] | None = None
speed: Annotated[float | None, Field(ge=0.25, le=4.0)] = None
additional_properties: dict[str, Any] = Field(
default_factory=dict, description="Provider-specific additional properties."
)
def to_provider_settings(self, by_alias: bool = True, exclude: set[str] | None = None) -> dict[str, Any]:
"""Convert the SpeechToTextOptions to a dictionary suitable for provider requests.
Args:
by_alias: Use alias names for fields if True.
exclude: Additional keys to exclude from the output.
Returns:
Dictionary of settings for provider.
"""
default_exclude = {"additional_properties"}
merged_exclude = default_exclude if exclude is None else default_exclude | set(exclude)
settings: dict[str, Any] = self.model_dump(exclude_none=True, by_alias=by_alias, exclude=merged_exclude)
settings = {k: v for k, v in settings.items() if not (isinstance(v, dict) and not v)}
settings.update(self.additional_properties)
for key in merged_exclude:
settings.pop(key, None)
return settings
@@ -0,0 +1,5 @@
# Copyright (c) Microsoft. All rights reserved.
from typing import Final
USER_AGENT: Final[str] = "User-Agent"
@@ -17,3 +17,48 @@ class AgentExecutionException(AgentException):
"""An error occurred while executing the agent."""
pass
# region: Service Exceptions
class ServiceException(AgentFrameworkException):
"""Base class for all service exceptions."""
pass
class ServiceInitializationError(ServiceException):
"""An error occurred while initializing the service."""
pass
class ServiceResponseException(ServiceException):
"""Base class for all service response exceptions."""
pass
class ServiceContentFilterException(ServiceResponseException):
"""An error was raised by the content filter of the service."""
pass
class ServiceInvalidExecutionSettingsError(ServiceResponseException):
"""An error occurred while validating the execution settings of the service."""
pass
class ServiceInvalidRequestError(ServiceResponseException):
"""An error occurred while validating the request to the service."""
pass
class ServiceInvalidResponseError(ServiceResponseException):
"""An error occurred while validating the response from the service."""
pass