Python: Fixed SK migration samples (#4046)

* Fixed sk migration provider samples

* Fixes to SK migration samples
This commit is contained in:
Dmytro Struk
2026-02-18 12:20:21 -08:00
committed by GitHub
Unverified
parent aab80d9ed9
commit c23bc1371c
17 changed files with 96 additions and 101 deletions
@@ -32,7 +32,7 @@ PROMPT = "Explain the concept of temperature from multiple scientific perspectiv
######################################################################
def build_semantic_kernel_agents() -> list[Agent]:
def build_semantic_kernel_agents() -> list[ChatCompletionAgent]:
credential = AzureCliCredential()
physics_agent = ChatCompletionAgent(
@@ -20,7 +20,7 @@ from agent_framework import Agent, Message
from agent_framework.azure import AzureOpenAIChatClient, AzureOpenAIResponsesClient
from agent_framework.orchestrations import GroupChatBuilder
from azure.identity import AzureCliCredential
from semantic_kernel.agents import Agent, ChatCompletionAgent, GroupChatOrchestration
from semantic_kernel.agents import ChatCompletionAgent, GroupChatOrchestration
from semantic_kernel.agents.orchestration.group_chat import (
BooleanResult,
GroupChatManager,
@@ -50,7 +50,7 @@ DISCUSSION_TOPIC = "What are the essential steps for launching a community hacka
######################################################################
def build_semantic_kernel_agents() -> list[Agent]:
def build_semantic_kernel_agents() -> list[ChatCompletionAgent]:
credential = AzureCliCredential()
researcher = ChatCompletionAgent(
@@ -82,25 +82,25 @@ class ChatCompletionGroupChatManager(GroupChatManager):
topic: str
termination_prompt: str = (
"You are coordinating a conversation about '{{topic}}'. "
"You are coordinating a conversation about '{{$topic}}'. "
"Decide if the discussion has produced a solid answer. "
'Respond using JSON: {"result": true|false, "reason": "..."}.'
)
selection_prompt: str = (
"You are coordinating a conversation about '{{topic}}'. "
"You are coordinating a conversation about '{{$topic}}'. "
"Choose the next participant by returning JSON with keys (result, reason). "
"The result must match one of: {{participants}}."
"The result must match one of: {{$participants}}."
)
summary_prompt: str = (
"You have just finished a discussion about '{{topic}}'. "
"You have just finished a discussion about '{{$topic}}'. "
"Summarize the plan and highlight key takeaways. Return JSON with keys (result, reason) where "
"result is the final response text."
)
def __init__(self, *, topic: str, service: ChatCompletionClientBase) -> None:
super().__init__(topic=topic, service=service)
def __init__(self, *, topic: str, service: ChatCompletionClientBase, max_rounds: int | None = None) -> None:
super().__init__(topic=topic, service=service, max_rounds=max_rounds)
self._round_robin_index = 0
async def _render_prompt(self, template: str, **kwargs: Any) -> str:
@@ -20,7 +20,7 @@ from agent_framework import (
WorkflowEvent,
)
from agent_framework.azure import AzureOpenAIChatClient
from agent_framework.orchestrations import HandoffBuilder, HandoffUserInputRequest
from agent_framework.orchestrations import HandoffAgentUserRequest, HandoffBuilder
from azure.identity import AzureCliCredential
from semantic_kernel.agents import Agent, ChatCompletionAgent, HandoffOrchestration, OrchestrationHandoffs
from semantic_kernel.agents.runtime import InProcessRuntime
@@ -223,7 +223,7 @@ async def _drain_events(stream: AsyncIterable[WorkflowEvent]) -> list[WorkflowEv
def _collect_handoff_requests(events: list[WorkflowEvent]) -> list[WorkflowEvent]:
requests: list[WorkflowEvent] = []
for event in events:
if event.type == "request_info" and isinstance(event.data, HandoffUserInputRequest):
if event.type == "request_info" and isinstance(event.data, HandoffAgentUserRequest):
requests.append(event)
return requests
@@ -241,12 +241,16 @@ async def run_agent_framework_example(initial_task: str, scripted_responses: Seq
triage, refund, status, returns = _create_af_agents(client)
workflow = (
HandoffBuilder(name="sk_af_handoff_migration", participants=[triage, refund, status, returns])
.set_coordinator(triage)
HandoffBuilder(
name="sk_af_handoff_migration",
participants=[triage, refund, status, returns],
termination_condition=lambda conv: sum(1 for m in conv if m.role == "user") >= 4,
)
.with_start_agent(triage)
.add_handoff(triage, [refund, status, returns])
.add_handoff(refund, [status, triage])
.add_handoff(status, [refund, triage])
.add_handoff(returns, triage)
.add_handoff(returns, [triage])
.build()
)
@@ -260,7 +264,7 @@ async def run_agent_framework_example(initial_task: str, scripted_responses: Seq
user_reply = next(scripted_iter)
except StopIteration:
user_reply = "Thanks, that's all."
responses = {request.request_id: user_reply for request in pending}
responses = {request.request_id: [Message(role="user", text=user_reply)] for request in pending}
final_events = await _drain_events(workflow.run(stream=True, responses=responses))
pending = _collect_handoff_requests(final_events)
@@ -19,7 +19,6 @@ from agent_framework import Agent
from agent_framework.openai import OpenAIChatClient, OpenAIResponsesClient
from agent_framework.orchestrations import MagenticBuilder
from semantic_kernel.agents import (
Agent,
ChatCompletionAgent,
MagenticOrchestration,
OpenAIAssistantAgent,
@@ -44,7 +43,7 @@ PROMPT = (
######################################################################
async def build_semantic_kernel_agents() -> list[Agent]:
async def build_semantic_kernel_agents() -> list:
research_agent = ChatCompletionAgent(
name="ResearchAgent",
description="A helpful assistant with access to web search. Ask it to perform web searches.",
@@ -135,19 +134,19 @@ async def run_agent_framework_example(prompt: str) -> str | None:
instructions=(
"You are a Researcher. You find information without additional computation or quantitative analysis."
),
client=OpenAIChatClient(ai_model_id="gpt-4o-search-preview"),
client=OpenAIChatClient(model_id="gpt-4o-search-preview"),
)
# Create code interpreter tool using instance method
# Create code interpreter tool using static method
coder_client = OpenAIResponsesClient()
code_interpreter_tool = coder_client.get_code_interpreter_tool()
code_interpreter_tool = OpenAIResponsesClient.get_code_interpreter_tool()
coder = Agent(
name="CoderAgent",
description="A helpful assistant that writes and executes code to process and analyze data.",
instructions="You solve questions using code. Please provide detailed analysis and computation process.",
client=coder_client,
tools=code_interpreter_tool,
tools=[code_interpreter_tool],
)
# Create a manager agent for orchestration
@@ -158,12 +157,22 @@ async def run_agent_framework_example(prompt: str) -> str | None:
client=OpenAIChatClient(),
)
workflow = MagenticBuilder(participants=[researcher, coder], manager_agent=manager_agent).build()
workflow = MagenticBuilder(
participants=[researcher, coder], manager_agent=manager_agent
).build()
final_text: str | None = None
async for event in workflow.run(prompt, stream=True):
if event.type == "output":
final_text = cast(str, event.data)
data = event.data
if isinstance(data, str):
final_text = data
elif isinstance(data, list):
# Extract text from the last assistant message
for msg in reversed(data):
if hasattr(msg, "text") and msg.text:
final_text = msg.text
break
return final_text