Python: semantic-kernel to agent-framework migration code samples (#1045)

* wip migrations

* Wip: workflow migrations

* Add migration samples for sk to af

* Fix typo

* Fixes
This commit is contained in:
Evan Mattson
2025-10-01 16:02:03 +09:00
committed by GitHub
Unverified
parent 498fc06fd6
commit fb51d917fd
23 changed files with 1817 additions and 5 deletions
@@ -0,0 +1,55 @@
# Copyright (c) Microsoft. All rights reserved.
"""Create an OpenAI Assistant using SK and Agent Framework."""
import asyncio
import os
ASSISTANT_MODEL = os.environ.get("OPENAI_ASSISTANT_MODEL", "gpt-4o-mini")
async def run_semantic_kernel() -> None:
from semantic_kernel.agents import AssistantAgentThread, OpenAIAssistantAgent
client = OpenAIAssistantAgent.create_client()
# Provision the assistant on the OpenAI Assistants service.
definition = await client.beta.assistants.create(
model=ASSISTANT_MODEL,
name="Helper",
instructions="Answer questions in one concise paragraph.",
)
agent = OpenAIAssistantAgent(client=client, definition=definition)
thread: AssistantAgentThread | None = None
response = await agent.get_response("What is the capital of Denmark?", thread=thread)
thread = response.thread
print("[SK]", response.message.content)
if thread is not None:
print("[SK][thread-id]", thread.id)
async def run_agent_framework() -> None:
from agent_framework.openai import OpenAIAssistantsClient
assistants_client = OpenAIAssistantsClient()
# AF wraps the assistant lifecycle with an async context manager.
async with assistants_client.create_agent(
name="Helper",
instructions="Answer questions in one concise paragraph.",
model=ASSISTANT_MODEL,
) as assistant_agent:
reply = await assistant_agent.run("What is the capital of Denmark?")
print("[AF]", reply.text)
follow_up = await assistant_agent.run(
"How many residents live there?",
thread=assistant_agent.get_new_thread(),
)
print("[AF][follow-up]", follow_up.text)
async def main() -> None:
await run_semantic_kernel()
await run_agent_framework()
if __name__ == "__main__":
asyncio.run(main())
@@ -0,0 +1,55 @@
# Copyright (c) Microsoft. All rights reserved.
"""Enable the code interpreter tool for OpenAI Assistants in SK and AF."""
import asyncio
async def run_semantic_kernel() -> None:
from semantic_kernel.agents import OpenAIAssistantAgent
from semantic_kernel.connectors.ai.open_ai import OpenAISettings
client = OpenAIAssistantAgent.create_client()
code_interpreter_tool, code_interpreter_tool_resources = OpenAIAssistantAgent.configure_code_interpreter_tool()
# Enable the hosted code interpreter tool on the assistant definition.
definition = await client.beta.assistants.create(
model=OpenAISettings().chat_deployment_name,
name="CodeRunner",
instructions="Run the provided request as code and return the result.",
tools=code_interpreter_tool,
tool_resources=code_interpreter_tool_resources,
)
agent = OpenAIAssistantAgent(client=client, definition=definition)
response = await agent.get_response(
"Use Python to calculate the mean of [41, 42, 45] and explain the steps.",
)
print(f"[SK]: {response}")
async def run_agent_framework() -> None:
from agent_framework import HostedCodeInterpreterTool
from agent_framework.openai import OpenAIAssistantsClient
assistants_client = OpenAIAssistantsClient()
# AF exposes the same tool configuration via create_agent.
async with assistants_client.create_agent(
name="CodeRunner",
instructions="Use the code interpreter when calculations are required.",
model="gpt-4.1",
tools=[HostedCodeInterpreterTool()],
) as assistant_agent:
response = await assistant_agent.run(
"Use Python to calculate the mean of [41, 42, 45] and explain the steps.",
tool_choice="auto",
)
print(f"[AF]: {response.text}")
async def main() -> None:
await run_semantic_kernel()
await run_agent_framework()
if __name__ == "__main__":
asyncio.run(main())
@@ -0,0 +1,89 @@
# Copyright (c) Microsoft. All rights reserved.
"""Implement a function tool for OpenAI Assistants in SK and AF."""
import asyncio
import os
from typing import Any
ASSISTANT_MODEL = os.environ.get("OPENAI_ASSISTANT_MODEL", "gpt-4o-mini")
async def fake_weather_lookup(city: str, day: str) -> dict[str, Any]:
"""Pretend to call a weather service."""
return {
"city": city,
"day": day,
"forecast": "Sunny with scattered clouds",
"high_c": 22,
"low_c": 14,
}
async def run_semantic_kernel() -> None:
from semantic_kernel.agents import AssistantAgentThread, OpenAIAssistantAgent
from semantic_kernel.functions import kernel_function
class WeatherPlugin:
@kernel_function(name="get_forecast", description="Look up the forecast for a city and day.")
async def fake_weather_lookup(city: str, day: str) -> dict[str, Any]:
"""Pretend to call a weather service."""
return {
"city": city,
"day": day,
"forecast": "Sunny with scattered clouds",
"high_c": 22,
"low_c": 14,
}
client = OpenAIAssistantAgent.create_client()
# Tool schema is registered on the assistant definition.
definition = await client.beta.assistants.create(
model=ASSISTANT_MODEL,
name="WeatherHelper",
instructions="Call get_forecast to fetch weather details.",
plugins=[WeatherPlugin()],
)
agent = OpenAIAssistantAgent(client=client, definition=definition)
thread: AssistantAgentThread | None = None
response = await agent.get_response(
"What will the weather be like in Seattle tomorrow?",
thread=thread,
)
thread = response.thread
print("[SK][initial]", response.message.content)
async def run_agent_framework() -> None:
from agent_framework._tools import ai_function
from agent_framework.openai import OpenAIAssistantsClient
@ai_function(
name="get_forecast",
description="Look up the forecast for a city and day.",
)
async def get_forecast(city: str, day: str) -> dict[str, Any]:
return await fake_weather_lookup(city, day)
assistants_client = OpenAIAssistantsClient()
# AF converts the decorated function into an assistant-compatible tool.
async with assistants_client.create_agent(
name="WeatherHelper",
instructions="Call get_forecast to fetch weather details.",
model=ASSISTANT_MODEL,
tools=[get_forecast],
) as assistant_agent:
reply = await assistant_agent.run(
"What will the weather be like in Seattle tomorrow?",
tool_choice="auto",
)
print("[AF]", reply.text)
async def main() -> None:
await run_semantic_kernel()
await run_agent_framework()
if __name__ == "__main__":
asyncio.run(main())