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,46 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
"""Basic SK ChatCompletionAgent vs Agent Framework ChatAgent.
|
||||
|
||||
Both samples expect OpenAI-compatible environment variables (OPENAI_API_KEY or
|
||||
Azure OpenAI configuration). Update the prompts or client wiring to match your
|
||||
model of choice before running.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
|
||||
|
||||
async def run_semantic_kernel() -> None:
|
||||
"""Call SK's ChatCompletionAgent for a simple question."""
|
||||
from semantic_kernel.agents import ChatCompletionAgent
|
||||
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
|
||||
|
||||
# SK agent holds the thread state internally via ChatCompletionAgent.
|
||||
agent = ChatCompletionAgent(
|
||||
service=OpenAIChatCompletion(),
|
||||
name="Support",
|
||||
instructions="Answer in one sentence.",
|
||||
)
|
||||
response = await agent.get_response(messages="How do I reset my bike tire?")
|
||||
print("[SK]", response.message.content)
|
||||
|
||||
|
||||
async def run_agent_framework() -> None:
|
||||
"""Call Agent Framework's ChatAgent created from OpenAIChatClient."""
|
||||
from agent_framework.openai import OpenAIChatClient
|
||||
|
||||
# AF constructs a lightweight ChatAgent backed by OpenAIChatClient.
|
||||
chat_agent = OpenAIChatClient().create_agent(
|
||||
name="Support",
|
||||
instructions="Answer in one sentence.",
|
||||
)
|
||||
reply = await chat_agent.run("How do I reset my bike tire?")
|
||||
print("[AF]", reply.text)
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
await run_semantic_kernel()
|
||||
await run_agent_framework()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
"""Demonstrate SK plugins vs Agent Framework tools with a chat agent.
|
||||
|
||||
Configure your OpenAI or Azure OpenAI credentials before running. The example
|
||||
exposes a "specials" tool that both SDKs call during the conversation.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
|
||||
|
||||
async def run_semantic_kernel() -> None:
|
||||
from semantic_kernel.agents import ChatCompletionAgent, ChatHistoryAgentThread
|
||||
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
|
||||
from semantic_kernel.functions import kernel_function
|
||||
|
||||
class SpecialsPlugin:
|
||||
@kernel_function(name="specials", description="List daily specials")
|
||||
def specials(self) -> str:
|
||||
return "Clam chowder, Cobb salad, Chai tea"
|
||||
|
||||
# SK advertises tools by attaching plugin instances at construction time.
|
||||
agent = ChatCompletionAgent(
|
||||
service=OpenAIChatCompletion(),
|
||||
name="Host",
|
||||
instructions="Answer menu questions accurately.",
|
||||
plugins=[SpecialsPlugin()],
|
||||
)
|
||||
thread = ChatHistoryAgentThread()
|
||||
response = await agent.get_response(
|
||||
messages="What soup can I order today?",
|
||||
thread=thread,
|
||||
)
|
||||
print("[SK]", response.message.content)
|
||||
|
||||
|
||||
async def run_agent_framework() -> None:
|
||||
from agent_framework._tools import ai_function
|
||||
from agent_framework.openai import OpenAIChatClient
|
||||
|
||||
@ai_function(name="specials", description="List daily specials")
|
||||
async def specials() -> str:
|
||||
return "Clam chowder, Cobb salad, Chai tea"
|
||||
|
||||
# AF tools are provided as callables on each agent instance.
|
||||
chat_agent = OpenAIChatClient().create_agent(
|
||||
name="Host",
|
||||
instructions="Answer menu questions accurately.",
|
||||
tools=[specials],
|
||||
)
|
||||
thread = chat_agent.get_new_thread()
|
||||
reply = await chat_agent.run(
|
||||
"What soup can I order today?",
|
||||
thread=thread,
|
||||
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())
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
"""Compare conversation threading and streaming responses for chat agents.
|
||||
|
||||
Both implementations reuse a conversation thread across turns and stream output
|
||||
for the second turn.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
|
||||
|
||||
async def run_semantic_kernel() -> None:
|
||||
from semantic_kernel.agents import ChatCompletionAgent, ChatHistoryAgentThread
|
||||
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
|
||||
|
||||
# SK thread object keeps the conversation history on the agent side.
|
||||
agent = ChatCompletionAgent(
|
||||
service=OpenAIChatCompletion(),
|
||||
name="Writer",
|
||||
instructions="Keep answers short and friendly.",
|
||||
)
|
||||
thread = ChatHistoryAgentThread()
|
||||
|
||||
first = await agent.get_response(
|
||||
messages="Suggest a catchy headline for our product launch.",
|
||||
thread=thread,
|
||||
)
|
||||
print("[SK]", first.message.content)
|
||||
|
||||
print("[SK][stream]", end=" ")
|
||||
async for update in agent.invoke_stream(
|
||||
messages="Draft a 2 sentence blurb.",
|
||||
thread=thread,
|
||||
):
|
||||
if update.message:
|
||||
print(update.message.content, end="", flush=True)
|
||||
print()
|
||||
|
||||
|
||||
async def run_agent_framework() -> None:
|
||||
from agent_framework.openai import OpenAIChatClient
|
||||
|
||||
# AF thread objects are requested explicitly from the agent.
|
||||
chat_agent = OpenAIChatClient().create_agent(
|
||||
name="Writer",
|
||||
instructions="Keep answers short and friendly.",
|
||||
)
|
||||
thread = chat_agent.get_new_thread()
|
||||
|
||||
first = await chat_agent.run(
|
||||
"Suggest a catchy headline for our product launch.",
|
||||
thread=thread,
|
||||
)
|
||||
print("[AF]", first.text)
|
||||
|
||||
print("[AF][stream]", end=" ")
|
||||
async for chunk in chat_agent.run_stream(
|
||||
"Draft a 2 sentence blurb.",
|
||||
thread=thread,
|
||||
):
|
||||
if chunk.text:
|
||||
print(chunk.text, end="", flush=True)
|
||||
print()
|
||||
|
||||
|
||||
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