mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
Python: Fix migration samples (#5015)
* Fix migration samples * Fix migration samples 2 * Fix formatting * Comments
This commit is contained in:
committed by
GitHub
Unverified
parent
d992febe9b
commit
e43fc8ccec
@@ -16,8 +16,8 @@ import sys
|
||||
from collections.abc import Sequence
|
||||
from typing import Any, cast
|
||||
|
||||
from agent_framework import Agent, Message
|
||||
from agent_framework.foundry import FoundryChatClient
|
||||
from agent_framework import Agent, AgentResponseUpdate, Message
|
||||
from agent_framework.openai import OpenAIChatCompletionClient
|
||||
from agent_framework.orchestrations import GroupChatBuilder
|
||||
from azure.identity import AzureCliCredential
|
||||
from dotenv import load_dotenv
|
||||
@@ -82,9 +82,6 @@ def build_semantic_kernel_agents() -> list[ChatCompletionAgent]:
|
||||
class ChatCompletionGroupChatManager(GroupChatManager):
|
||||
"""Group chat manager that delegates orchestration decisions to an Azure OpenAI deployment."""
|
||||
|
||||
service: ChatCompletionClientBase
|
||||
topic: str
|
||||
|
||||
termination_prompt: str = (
|
||||
"You are coordinating a conversation about '{{$topic}}'. "
|
||||
"Decide if the discussion has produced a solid answer. "
|
||||
@@ -104,8 +101,11 @@ class ChatCompletionGroupChatManager(GroupChatManager):
|
||||
)
|
||||
|
||||
def __init__(self, *, topic: str, service: ChatCompletionClientBase, max_rounds: int | None = None) -> None:
|
||||
super().__init__(topic=topic, service=service, max_rounds=max_rounds)
|
||||
super().__init__(max_rounds=max_rounds)
|
||||
|
||||
self._round_robin_index = 0
|
||||
self._topic = topic
|
||||
self._service = service
|
||||
|
||||
async def _render_prompt(self, template: str, **kwargs: Any) -> str:
|
||||
prompt_template = KernelPromptTemplate(prompt_template_config=PromptTemplateConfig(template=template))
|
||||
@@ -117,7 +117,7 @@ class ChatCompletionGroupChatManager(GroupChatManager):
|
||||
|
||||
@override
|
||||
async def should_terminate(self, chat_history: ChatHistory) -> BooleanResult:
|
||||
rendered_prompt = await self._render_prompt(self.termination_prompt, topic=self.topic)
|
||||
rendered_prompt = await self._render_prompt(self.termination_prompt, topic=self._topic)
|
||||
chat_history.messages.insert(
|
||||
0,
|
||||
ChatMessageContent(role=AuthorRole.SYSTEM, content=rendered_prompt),
|
||||
@@ -126,11 +126,11 @@ class ChatCompletionGroupChatManager(GroupChatManager):
|
||||
ChatMessageContent(role=AuthorRole.USER, content="Decide if the discussion is complete."),
|
||||
)
|
||||
|
||||
response = await self.service.get_chat_message_content(
|
||||
response = await self._service.get_chat_message_content(
|
||||
chat_history,
|
||||
settings=PromptExecutionSettings(response_format=BooleanResult),
|
||||
)
|
||||
return BooleanResult.model_validate_json(response.content)
|
||||
return BooleanResult.model_validate_json(response.content) # type: ignore
|
||||
|
||||
@override
|
||||
async def select_next_agent(
|
||||
@@ -140,7 +140,7 @@ class ChatCompletionGroupChatManager(GroupChatManager):
|
||||
) -> StringResult:
|
||||
rendered_prompt = await self._render_prompt(
|
||||
self.selection_prompt,
|
||||
topic=self.topic,
|
||||
topic=self._topic,
|
||||
participants=", ".join(participant_descriptions.keys()),
|
||||
)
|
||||
chat_history.messages.insert(
|
||||
@@ -151,18 +151,18 @@ class ChatCompletionGroupChatManager(GroupChatManager):
|
||||
ChatMessageContent(role=AuthorRole.USER, content="Pick the next participant to speak."),
|
||||
)
|
||||
|
||||
response = await self.service.get_chat_message_content(
|
||||
response = await self._service.get_chat_message_content(
|
||||
chat_history,
|
||||
settings=PromptExecutionSettings(response_format=StringResult),
|
||||
)
|
||||
result = StringResult.model_validate_json(response.content)
|
||||
result = StringResult.model_validate_json(response.content) # type: ignore
|
||||
if result.result not in participant_descriptions:
|
||||
raise RuntimeError(f"Unknown participant selected: {result.result}")
|
||||
return result
|
||||
|
||||
@override
|
||||
async def filter_results(self, chat_history: ChatHistory) -> MessageResult:
|
||||
rendered_prompt = await self._render_prompt(self.summary_prompt, topic=self.topic)
|
||||
rendered_prompt = await self._render_prompt(self.summary_prompt, topic=self._topic)
|
||||
chat_history.messages.insert(
|
||||
0,
|
||||
ChatMessageContent(role=AuthorRole.SYSTEM, content=rendered_prompt),
|
||||
@@ -171,11 +171,11 @@ class ChatCompletionGroupChatManager(GroupChatManager):
|
||||
ChatMessageContent(role=AuthorRole.USER, content="Summarize the plan."),
|
||||
)
|
||||
|
||||
response = await self.service.get_chat_message_content(
|
||||
response = await self._service.get_chat_message_content(
|
||||
chat_history,
|
||||
settings=PromptExecutionSettings(response_format=StringResult),
|
||||
)
|
||||
string_result = StringResult.model_validate_json(response.content)
|
||||
string_result = StringResult.model_validate_json(response.content) # type: ignore
|
||||
return MessageResult(
|
||||
result=ChatMessageContent(role=AuthorRole.ASSISTANT, content=string_result.result),
|
||||
reason=string_result.reason,
|
||||
@@ -197,7 +197,7 @@ async def sk_agent_response_callback(message: ChatMessageContent | Sequence[Chat
|
||||
async def run_semantic_kernel_example(task: str) -> str:
|
||||
credential = AzureCliCredential()
|
||||
orchestration = GroupChatOrchestration(
|
||||
members=build_semantic_kernel_agents(),
|
||||
members=build_semantic_kernel_agents(), # type: ignore
|
||||
manager=ChatCompletionGroupChatManager(
|
||||
topic=DISCUSSION_TOPIC,
|
||||
service=AzureChatCompletion(credential=credential),
|
||||
@@ -225,7 +225,7 @@ async def run_semantic_kernel_example(task: str) -> str:
|
||||
|
||||
|
||||
async def run_agent_framework_example(task: str) -> str:
|
||||
credential = AzureCliCredential()
|
||||
client = OpenAIChatCompletionClient(credential=AzureCliCredential())
|
||||
|
||||
researcher = Agent(
|
||||
name="Researcher",
|
||||
@@ -234,32 +234,42 @@ async def run_agent_framework_example(task: str) -> str:
|
||||
"Gather concise facts or considerations that help plan a community hackathon. "
|
||||
"Keep your responses factual and scannable."
|
||||
),
|
||||
client=FoundryChatClient(credential=credential),
|
||||
client=client,
|
||||
)
|
||||
|
||||
planner = Agent(
|
||||
name="Planner",
|
||||
description="Turns the collected notes into a concrete action plan.",
|
||||
instructions=("Propose a structured action plan that accounts for logistics, roles, and timeline."),
|
||||
client=FoundryChatClient(credential=credential),
|
||||
client=client,
|
||||
)
|
||||
|
||||
workflow = GroupChatBuilder(
|
||||
participants=[researcher, planner],
|
||||
orchestrator_agent=Agent(client=FoundryChatClient(credential=credential)),
|
||||
orchestrator_agent=Agent(client=client),
|
||||
max_rounds=8,
|
||||
intermediate_outputs=True,
|
||||
).build()
|
||||
|
||||
final_response = ""
|
||||
output_messages: list[Message] = []
|
||||
last_message_id: str | None = None
|
||||
async for event in workflow.run(task, stream=True):
|
||||
if event.type == "output":
|
||||
data = event.data
|
||||
if isinstance(data, list) and len(data) > 0:
|
||||
# Get the final message from the conversation
|
||||
final_message = data[-1]
|
||||
final_response = final_message.text or "" if isinstance(final_message, Message) else str(data)
|
||||
if isinstance(event.data, AgentResponseUpdate):
|
||||
if event.data.message_id != last_message_id:
|
||||
last_message_id = event.data.message_id
|
||||
print(f"{event.data.author_name}: {event.data.text}", end="")
|
||||
else:
|
||||
print(event.data.text, end="")
|
||||
else:
|
||||
final_response = str(data)
|
||||
return final_response
|
||||
output_messages.extend(cast(list[Message], event.data))
|
||||
for message in output_messages:
|
||||
print(f"[{message.author_name}] {message.text}")
|
||||
|
||||
if output_messages:
|
||||
return output_messages[-1].text
|
||||
|
||||
return ""
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
|
||||
Reference in New Issue
Block a user