mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
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:
@@ -0,0 +1,48 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
"""Issue a basic Responses API call using SK and Agent Framework."""
|
||||
|
||||
import asyncio
|
||||
|
||||
|
||||
async def run_semantic_kernel() -> None:
|
||||
from azure.identity import AzureCliCredential
|
||||
from semantic_kernel.agents import AzureResponsesAgent
|
||||
from semantic_kernel.connectors.ai.open_ai import AzureOpenAISettings
|
||||
|
||||
credential = AzureCliCredential()
|
||||
try:
|
||||
client = AzureResponsesAgent.create_client(credential=credential)
|
||||
# SK response agents wrap Azure OpenAI's hosted Responses API.
|
||||
agent = AzureResponsesAgent(
|
||||
ai_model_id=AzureOpenAISettings().responses_deployment_name,
|
||||
client=client,
|
||||
instructions="Answer in one concise sentence.",
|
||||
name="Expert",
|
||||
)
|
||||
response = await agent.get_response("Why is the sky blue?")
|
||||
print("[SK]", response.message.content)
|
||||
finally:
|
||||
await credential.close()
|
||||
|
||||
|
||||
async def run_agent_framework() -> None:
|
||||
from agent_framework import ChatAgent
|
||||
from agent_framework.openai import OpenAIResponsesClient
|
||||
|
||||
# AF ChatAgent can swap in an OpenAIResponsesClient directly.
|
||||
chat_agent = ChatAgent(
|
||||
chat_client=OpenAIResponsesClient(),
|
||||
instructions="Answer in one concise sentence.",
|
||||
name="Expert",
|
||||
)
|
||||
reply = await chat_agent.run("Why is the sky blue?")
|
||||
print("[AF]", reply.text)
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
await run_semantic_kernel()
|
||||
await run_agent_framework()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
"""Attach a lightweight function tool to the Responses API in SK and AF."""
|
||||
|
||||
import asyncio
|
||||
|
||||
|
||||
async def run_semantic_kernel() -> None:
|
||||
from azure.identity import AzureCliCredential
|
||||
from semantic_kernel.agents import AzureResponsesAgent
|
||||
from semantic_kernel.connectors.ai.open_ai import AzureOpenAISettings
|
||||
from semantic_kernel.functions import kernel_function
|
||||
|
||||
class MathPlugin:
|
||||
@kernel_function(name="add", description="Add two numbers")
|
||||
def add(self, a: float, b: float) -> float:
|
||||
return a + b
|
||||
|
||||
credential = AzureCliCredential()
|
||||
try:
|
||||
client = AzureResponsesAgent.create_client(credential=credential)
|
||||
# Plugins advertise callable tools to the Responses agent.
|
||||
agent = AzureResponsesAgent(
|
||||
ai_model_id=AzureOpenAISettings().responses_deployment_name,
|
||||
client=client,
|
||||
instructions="Use the add tool when math is required.",
|
||||
name="MathExpert",
|
||||
plugins=[MathPlugin()],
|
||||
)
|
||||
response = await agent.get_response("Use add(41, 1) and explain the result.")
|
||||
print("[SK]", response.message.content)
|
||||
finally:
|
||||
await credential.close()
|
||||
|
||||
|
||||
async def run_agent_framework() -> None:
|
||||
from agent_framework import ChatAgent
|
||||
from agent_framework._tools import ai_function
|
||||
from agent_framework.openai import OpenAIResponsesClient
|
||||
|
||||
@ai_function(name="add", description="Add two numbers")
|
||||
async def add(a: float, b: float) -> float:
|
||||
return a + b
|
||||
|
||||
chat_agent = ChatAgent(
|
||||
chat_client=OpenAIResponsesClient(),
|
||||
instructions="Use the add tool when math is required.",
|
||||
name="MathExpert",
|
||||
# AF registers the async function as a tool at construction.
|
||||
tools=[add],
|
||||
)
|
||||
reply = await chat_agent.run("Use add(41, 1) and explain the result.")
|
||||
print("[AF]", reply.text)
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
await run_semantic_kernel()
|
||||
await run_agent_framework()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
"""Request structured JSON output from the Responses API in SK and AF."""
|
||||
|
||||
import asyncio
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class ReleaseBrief(BaseModel):
|
||||
feature: str
|
||||
benefit: str
|
||||
launch_date: str
|
||||
|
||||
|
||||
async def run_semantic_kernel() -> None:
|
||||
from azure.identity import AzureCliCredential
|
||||
from semantic_kernel.agents import AzureResponsesAgent
|
||||
from semantic_kernel.connectors.ai.open_ai import AzureOpenAISettings
|
||||
|
||||
credential = AzureCliCredential()
|
||||
try:
|
||||
client = AzureResponsesAgent.create_client(credential=credential)
|
||||
# response_format requests schema-constrained output from the model.
|
||||
agent = AzureResponsesAgent(
|
||||
ai_model_id=AzureOpenAISettings().responses_deployment_name,
|
||||
client=client,
|
||||
instructions="Return launch briefs as structured JSON.",
|
||||
name="ProductMarketer",
|
||||
text=AzureResponsesAgent.configure_response_format(ReleaseBrief),
|
||||
)
|
||||
response = await agent.get_response(
|
||||
"Draft a launch brief for the Contoso Note app.",
|
||||
response_format=ReleaseBrief,
|
||||
)
|
||||
print("[SK]", response.message.content)
|
||||
finally:
|
||||
await credential.close()
|
||||
|
||||
|
||||
async def run_agent_framework() -> None:
|
||||
from agent_framework import ChatAgent
|
||||
from agent_framework.openai import OpenAIResponsesClient
|
||||
|
||||
chat_agent = ChatAgent(
|
||||
chat_client=OpenAIResponsesClient(),
|
||||
instructions="Return launch briefs as structured JSON.",
|
||||
name="ProductMarketer",
|
||||
)
|
||||
# AF forwards the same response_format payload at invocation time.
|
||||
reply = await chat_agent.run(
|
||||
"Draft a launch brief for the Contoso Note app.",
|
||||
response_format=ReleaseBrief,
|
||||
)
|
||||
print("[AF]", reply.text)
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
await run_semantic_kernel()
|
||||
await run_agent_framework()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user