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

Added Azure AI Chat Completion Client #4723

Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Canonicalize the finish_reason in azure AzureAIChatCompletionClient
rohanthacker committed Jan 23, 2025
commit d3e985c11ea99dc2a0475b5799353e32c5f0e7d6
Original file line number Diff line number Diff line change
@@ -37,6 +37,7 @@
SystemMessage,
AssistantMessage,
FunctionExecutionResultMessage,
FinishReasons,
)
from autogen_core.tools import Tool, ToolSchema
from autogen_ext.models.azure.config import AzureAIChatCompletionClientConfig, GITHUB_MODELS_ENDPOINT
@@ -151,6 +152,20 @@ def assert_valid_name(name: str) -> str:
return name


def normalize_stop_reason(stop_reason: str|None) -> FinishReasons:
if stop_reason is None:
return "unknown"

stop_reason = stop_reason.lower()

KNOWN_STOP_MAPPINGS: Dict[str, FinishReasons] = {
"end_turn": "stop",
"tool_calls": "function_calls",
}

return KNOWN_STOP_MAPPINGS.get(stop_reason, "unknown")


class AzureAIChatCompletionClient(ChatCompletionClient):
"""
Chat completion client for models hosted on Azure AI Foundry or GitHub Models.
@@ -319,11 +334,11 @@ async def create(
]
finish_reason = "function_calls"
else:
finish_reason = choice.finish_reason.value
finish_reason = choice.finish_reason
content = choice.message.content or ""

response = CreateResult(
finish_reason=finish_reason, # type: ignore
finish_reason=normalize_stop_reason(finish_reason.value), # type: ignore
content=content,
usage=usage,
cached=False,
@@ -447,7 +462,7 @@ async def create_stream(
)

result = CreateResult(
finish_reason=finish_reason, # type: ignore
finish_reason=normalize_stop_reason(finish_reason), # type: ignore
content=content,
usage=usage,
cached=False,