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:
committed by
GitHub
Unverified
parent
498fc06fd6
commit
fb51d917fd
@@ -0,0 +1,50 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
"""Create an Azure AI agent using both Semantic Kernel and Agent Framework.
|
||||
|
||||
Prerequisites:
|
||||
- Azure AI agent resource with a deployed model.
|
||||
- Logged-in Azure CLI or other credential supported by AzureCliCredential.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
|
||||
|
||||
async def run_semantic_kernel() -> None:
|
||||
from azure.identity.aio import AzureCliCredential
|
||||
from semantic_kernel.agents import AzureAIAgent, AzureAIAgentSettings
|
||||
|
||||
async with AzureCliCredential() as credential:
|
||||
async with AzureAIAgent.create_client(credential=credential) as client:
|
||||
settings = AzureAIAgentSettings() # Reads env vars for region/deployment.
|
||||
# SK builds the remote agent definition then wraps it with AzureAIAgent.
|
||||
definition = await client.agents.create_agent(
|
||||
model=settings.model_deployment_name,
|
||||
name="Support",
|
||||
instructions="Answer customer questions in one paragraph.",
|
||||
)
|
||||
agent = AzureAIAgent(client=client, definition=definition)
|
||||
response = await agent.get_response("How do I upgrade my plan?")
|
||||
print("[SK]", response.message.content)
|
||||
|
||||
|
||||
async def run_agent_framework() -> None:
|
||||
from azure.identity.aio import AzureCliCredential
|
||||
from agent_framework.azure import AzureAIAgentClient
|
||||
|
||||
async with AzureCliCredential() as credential:
|
||||
async with AzureAIAgentClient(async_credential=credential).create_agent(
|
||||
name="Support",
|
||||
instructions="Answer customer questions in one paragraph.",
|
||||
) as agent:
|
||||
# AF client returns an asynchronous context manager for remote agents.
|
||||
reply = await agent.run("How do I upgrade my plan?")
|
||||
print("[AF]", reply.text)
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
await run_semantic_kernel()
|
||||
await run_agent_framework()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
"""Enable the hosted code interpreter for Azure AI agents in SK and AF.
|
||||
|
||||
The Azure AI service natively executes the code interpreter tool. Provide the
|
||||
resource details via AzureAIAgentSettings (SK) or environment variables consumed
|
||||
by AzureAIAgentClient (AF).
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
|
||||
|
||||
async def run_semantic_kernel() -> None:
|
||||
from azure.identity.aio import AzureCliCredential
|
||||
from semantic_kernel.agents import AzureAIAgent, AzureAIAgentSettings
|
||||
|
||||
async with AzureCliCredential() as credential:
|
||||
async with AzureAIAgent.create_client(credential=credential) as client:
|
||||
settings = AzureAIAgentSettings()
|
||||
# Register the hosted code interpreter tool with the remote agent.
|
||||
definition = await client.agents.create_agent(
|
||||
model=settings.model_deployment_name,
|
||||
name="Analyst",
|
||||
instructions="Use the code interpreter for numeric work.",
|
||||
tools=[{"type": "code_interpreter"}],
|
||||
)
|
||||
agent = AzureAIAgent(client=client, definition=definition)
|
||||
response = await agent.get_response(
|
||||
"Use Python to compute 42 ** 2 and explain the result.",
|
||||
)
|
||||
print("[SK]", response.message.content)
|
||||
|
||||
|
||||
async def run_agent_framework() -> None:
|
||||
from azure.identity.aio import AzureCliCredential
|
||||
from agent_framework.azure import AzureAIAgentClient, HostedCodeInterpreterTool
|
||||
|
||||
async with AzureCliCredential() as credential:
|
||||
async with AzureAIAgentClient(async_credential=credential).create_agent(
|
||||
name="Analyst",
|
||||
instructions="Use the code interpreter for numeric work.",
|
||||
tools=[HostedCodeInterpreterTool()],
|
||||
) as agent:
|
||||
# HostedCodeInterpreterTool mirrors the built-in Azure AI capability.
|
||||
reply = await agent.run(
|
||||
"Use Python to compute 42 ** 2 and explain the result.",
|
||||
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())
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
"""Maintain Azure AI agent conversation state across turns in SK and AF."""
|
||||
|
||||
import asyncio
|
||||
|
||||
|
||||
async def run_semantic_kernel() -> None:
|
||||
from azure.identity.aio import AzureCliCredential
|
||||
from semantic_kernel.agents import AzureAIAgent, AzureAIAgentSettings, AzureAIAgentThread
|
||||
|
||||
async with AzureCliCredential() as credential:
|
||||
async with AzureAIAgent.create_client(credential=credential) as client:
|
||||
settings = AzureAIAgentSettings()
|
||||
definition = await client.agents.create_agent(
|
||||
model=settings.model_deployment_name,
|
||||
name="Planner",
|
||||
instructions="Track follow-up questions within the same thread.",
|
||||
)
|
||||
agent = AzureAIAgent(client=client, definition=definition)
|
||||
|
||||
thread: AzureAIAgentThread | None = None
|
||||
# SK returns the updated AzureAIAgentThread on each response.
|
||||
first = await agent.get_response("Outline the onboarding checklist.", thread=thread)
|
||||
thread = first.thread
|
||||
print("[SK][turn1]", first.message.content)
|
||||
|
||||
second = await agent.get_response(
|
||||
"Highlight the items that require legal review.",
|
||||
thread=thread,
|
||||
)
|
||||
print("[SK][turn2]", second.message.content)
|
||||
if thread is not None:
|
||||
print("[SK][thread-id]", thread.id)
|
||||
|
||||
|
||||
async def run_agent_framework() -> None:
|
||||
from azure.identity.aio import AzureCliCredential
|
||||
from agent_framework.azure import AzureAIAgentClient
|
||||
|
||||
async with AzureCliCredential() as credential:
|
||||
async with AzureAIAgentClient(async_credential=credential).create_agent(
|
||||
name="Planner",
|
||||
instructions="Track follow-up questions within the same thread.",
|
||||
) as agent:
|
||||
thread = agent.get_new_thread()
|
||||
# AF threads are explicit and can be serialized for external storage.
|
||||
first = await agent.run("Outline the onboarding checklist.", thread=thread)
|
||||
print("[AF][turn1]", first.text)
|
||||
|
||||
second = await agent.run(
|
||||
"Highlight the items that require legal review.",
|
||||
thread=thread,
|
||||
)
|
||||
print("[AF][turn2]", second.text)
|
||||
|
||||
serialized = await thread.serialize()
|
||||
print("[AF][thread-json]", serialized)
|
||||
|
||||
|
||||
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