mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
Python: name changes executed (#607)
* name changes executed * updated adr to accepted * renamed openai base config * renamed openai config to mixin * added renames in user docs * reverted mcperror * fix tests * remove sse from tests
This commit is contained in:
committed by
GitHub
Unverified
parent
6310ca5be0
commit
40ab6e9d67
@@ -58,7 +58,7 @@ async def main():
|
||||
|
||||
# Step 3: Run the workflow with an initial message.
|
||||
completion_event = None
|
||||
async for event in workflow.run_streaming("hello world"):
|
||||
async for event in workflow.run_stream("hello world"):
|
||||
print(f"Event: {event}")
|
||||
if isinstance(event, WorkflowCompletedEvent):
|
||||
# The WorkflowCompletedEvent contains the final result.
|
||||
|
||||
@@ -110,7 +110,7 @@ async def main():
|
||||
)
|
||||
|
||||
# Step 3: Run the workflow with an input message.
|
||||
async for event in workflow.run_streaming("This is a spam."):
|
||||
async for event in workflow.run_stream("This is a spam."):
|
||||
print(f"Event: {event}")
|
||||
|
||||
|
||||
|
||||
@@ -105,7 +105,7 @@ async def main():
|
||||
|
||||
# Step 3: Run the workflow and print the events.
|
||||
iterations = 0
|
||||
async for event in workflow.run_streaming(NumberSignal.INIT):
|
||||
async for event in workflow.run_stream(NumberSignal.INIT):
|
||||
if isinstance(event, ExecutorCompletedEvent) and event.executor_id == guess_number_executor.id:
|
||||
iterations += 1
|
||||
print(f"Event: {event}")
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
import asyncio
|
||||
|
||||
from agent_framework import ChatMessage, ChatRole
|
||||
from agent_framework import ChatMessage, Role
|
||||
from agent_framework.azure import AzureChatClient
|
||||
from agent_framework.workflow import (
|
||||
AgentExecutor,
|
||||
@@ -36,7 +36,7 @@ class RoundRobinGroupChatManager(Executor):
|
||||
@handler
|
||||
async def start(self, task: str, ctx: WorkflowContext[AgentExecutorRequest]) -> None:
|
||||
"""Execute the task by sending messages to the next executor in the round-robin sequence."""
|
||||
initial_message = ChatMessage(ChatRole.USER, text=task)
|
||||
initial_message = ChatMessage(Role.USER, text=task)
|
||||
|
||||
# Send the initial message to the members
|
||||
await asyncio.gather(*[
|
||||
@@ -132,7 +132,7 @@ async def main():
|
||||
|
||||
# Step 3: Run the workflow with an initial message.
|
||||
completion_event = None
|
||||
async for event in workflow.run_streaming(
|
||||
async for event in workflow.run_stream(
|
||||
"Create a slogan for a new electric SUV that is affordable and fun to drive."
|
||||
):
|
||||
if isinstance(event, AgentRunEvent):
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
import asyncio
|
||||
|
||||
from agent_framework import ChatMessage, ChatRole
|
||||
from agent_framework import ChatMessage, Role
|
||||
from agent_framework.azure import AzureChatClient
|
||||
from agent_framework.workflow import (
|
||||
AgentExecutor,
|
||||
@@ -39,7 +39,7 @@ class CriticGroupChatManager(Executor):
|
||||
@handler
|
||||
async def start(self, task: str, ctx: WorkflowContext[AgentExecutorRequest]) -> None:
|
||||
"""Handler that starts the group chat with an initial task."""
|
||||
initial_message = ChatMessage(ChatRole.USER, text=task)
|
||||
initial_message = ChatMessage(Role.USER, text=task)
|
||||
|
||||
# Send the initial message to the members
|
||||
await asyncio.gather(*[
|
||||
@@ -129,7 +129,7 @@ class CriticGroupChatManager(Executor):
|
||||
return False
|
||||
|
||||
last_message = self._chat_history[-1]
|
||||
return bool(last_message.role == ChatRole.USER and "approve" in last_message.text.lower())
|
||||
return bool(last_message.role == Role.USER and "approve" in last_message.text.lower())
|
||||
|
||||
def _should_request_info(self) -> bool:
|
||||
"""Determine if the group chat should request HIL based on the last message."""
|
||||
@@ -137,7 +137,7 @@ class CriticGroupChatManager(Executor):
|
||||
return True
|
||||
|
||||
last_message = self._chat_history[-1]
|
||||
return last_message.role == ChatRole.ASSISTANT
|
||||
return last_message.role == Role.ASSISTANT
|
||||
|
||||
def _get_next_member(self) -> str:
|
||||
"""Get the next member in the round-robin sequence."""
|
||||
@@ -200,12 +200,12 @@ async def main():
|
||||
# Depending on whether we have a RequestInfoEvent event, we either
|
||||
# run the workflow normally or send the message to the HIL executor.
|
||||
if not request_info_event:
|
||||
response_stream = workflow.run_streaming(
|
||||
response_stream = workflow.run_stream(
|
||||
"Create a slogan for a new electric SUV that is affordable and fun to drive."
|
||||
)
|
||||
else:
|
||||
response_stream = workflow.send_responses_streaming({
|
||||
request_info_event.request_id: [ChatMessage(ChatRole.USER, text=user_input)]
|
||||
request_info_event.request_id: [ChatMessage(Role.USER, text=user_input)]
|
||||
})
|
||||
request_info_event = None
|
||||
|
||||
|
||||
@@ -314,7 +314,7 @@ async def main():
|
||||
|
||||
# Step 4: Run the workflow with the raw text as input.
|
||||
completion_event = None
|
||||
async for event in workflow.run_streaming(raw_text):
|
||||
async for event in workflow.run_stream(raw_text):
|
||||
print(f"Event: {event}")
|
||||
if isinstance(event, WorkflowCompletedEvent):
|
||||
completion_event = event
|
||||
|
||||
@@ -135,7 +135,7 @@ async def main():
|
||||
)
|
||||
|
||||
print("Running workflow with initial message...")
|
||||
async for event in workflow.run_streaming(message="hello world"):
|
||||
async for event in workflow.run_stream(message="hello world"):
|
||||
print(f"Event: {event}")
|
||||
|
||||
# Inspect checkpoints
|
||||
@@ -179,7 +179,7 @@ async def main():
|
||||
)
|
||||
|
||||
print(f"\nResuming from checkpoint: {checkpoint_id}")
|
||||
async for event in new_workflow.run_streaming_from_checkpoint(checkpoint_id, checkpoint_storage=checkpoint_storage):
|
||||
async for event in new_workflow.run_stream_from_checkpoint(checkpoint_id, checkpoint_storage=checkpoint_storage):
|
||||
print(f"Resumed Event: {event}")
|
||||
|
||||
"""
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
import asyncio
|
||||
import logging
|
||||
|
||||
from agent_framework import ChatClientAgent, HostedCodeInterpreterTool
|
||||
from agent_framework import ChatAgent, HostedCodeInterpreterTool
|
||||
from agent_framework.openai import OpenAIChatClient, OpenAIResponsesClient
|
||||
from agent_framework_workflow import (
|
||||
MagenticAgentDeltaEvent,
|
||||
@@ -25,9 +25,9 @@ Magentic Workflow (multi-agent) sample.
|
||||
This sample shows how to orchestrate multiple agents using the
|
||||
MagenticBuilder:
|
||||
|
||||
- ResearcherAgent (ChatClientAgent backed by an OpenAI chat client) for
|
||||
- ResearcherAgent (ChatAgent backed by an OpenAI chat client) for
|
||||
finding information.
|
||||
- CoderAgent (ChatClientAgent backed by OpenAI Assistants with the hosted
|
||||
- CoderAgent (ChatAgent backed by OpenAI Assistants with the hosted
|
||||
code interpreter tool) for analysis and computation.
|
||||
|
||||
The workflow is configured with:
|
||||
@@ -42,7 +42,7 @@ events to the console, and prints the final aggregated answer at completion.
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
researcher_agent = ChatClientAgent(
|
||||
researcher_agent = ChatAgent(
|
||||
name="ResearcherAgent",
|
||||
description="Specialist in research and information gathering",
|
||||
instructions=(
|
||||
@@ -50,11 +50,11 @@ async def main() -> None:
|
||||
),
|
||||
# This agent requires the gpt-4o-search-preview model to perform web searches.
|
||||
# Feel free to explore with other agents that support web search, for example,
|
||||
# the `OpenAIResponseAgent` or `AzureAIAgent` with bing grounding.
|
||||
# the `OpenAIResponseAgent` or `AzureAgentProtocol` with bing grounding.
|
||||
chat_client=OpenAIChatClient(ai_model_id="gpt-4o-search-preview"),
|
||||
)
|
||||
|
||||
coder_agent = ChatClientAgent(
|
||||
coder_agent = ChatAgent(
|
||||
name="CoderAgent",
|
||||
description="A helpful assistant that writes and executes code to process and analyze data.",
|
||||
instructions="You solve questions using code. Please provide detailed analysis and computation process.",
|
||||
@@ -129,7 +129,7 @@ async def main() -> None:
|
||||
|
||||
try:
|
||||
completion_event = None
|
||||
async for event in workflow.run_streaming(task):
|
||||
async for event in workflow.run_stream(task):
|
||||
print(f"Event: {event}")
|
||||
|
||||
if isinstance(event, WorkflowCompletedEvent):
|
||||
|
||||
@@ -4,7 +4,7 @@ import asyncio
|
||||
import logging
|
||||
from typing import cast
|
||||
|
||||
from agent_framework import ChatClientAgent, HostedCodeInterpreterTool
|
||||
from agent_framework import ChatAgent, HostedCodeInterpreterTool
|
||||
from agent_framework.openai import OpenAIChatClient, OpenAIResponsesClient
|
||||
from agent_framework_workflow import (
|
||||
MagenticAgentDeltaEvent,
|
||||
@@ -30,8 +30,8 @@ Magentic workflow with human-in-the-loop plan review and update.
|
||||
This sample builds a Magentic workflow with two cooperating agents and enables
|
||||
plan review so a human can approve or revise the plan before execution:
|
||||
|
||||
- researcher: ChatClientAgent backed by OpenAIChatClient (web/search-capable model)
|
||||
- coder: ChatClientAgent backed by OpenAIAssistantsClient with the Hosted Code Interpreter tool
|
||||
- researcher: ChatAgent backed by OpenAIChatClient (web/search-capable model)
|
||||
- coder: ChatAgent backed by OpenAIAssistantsClient with the Hosted Code Interpreter tool
|
||||
|
||||
Key behaviors demonstrated:
|
||||
- with_plan_review(): requests a PlanReviewRequest before coordination begins
|
||||
@@ -46,7 +46,7 @@ clients can run. You can swap clients/models as needed.
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
researcher_agent = ChatClientAgent(
|
||||
researcher_agent = ChatAgent(
|
||||
name="ResearcherAgent",
|
||||
description="Specialist in research and information gathering",
|
||||
instructions=(
|
||||
@@ -54,11 +54,11 @@ async def main() -> None:
|
||||
),
|
||||
# This agent requires the gpt-4o-search-preview model to perform web searches.
|
||||
# Feel free to explore with other agents that support web search, for example,
|
||||
# the `OpenAIResponseAgent` or `AzureAIAgent` with bing grounding.
|
||||
# the `OpenAIResponseAgent` or `AzureAgentProtocol` with bing grounding.
|
||||
chat_client=OpenAIChatClient(ai_model_id="gpt-4o-search-preview"),
|
||||
)
|
||||
|
||||
coder_agent = ChatClientAgent(
|
||||
coder_agent = ChatAgent(
|
||||
name="CoderAgent",
|
||||
description="A helpful assistant that writes and executes code to process and analyze data.",
|
||||
instructions="You solve questions using code. Please provide detailed analysis and computation process.",
|
||||
@@ -140,7 +140,7 @@ async def main() -> None:
|
||||
while True:
|
||||
# Phase 1: run until either completion or a HIL request
|
||||
if pending_request is None:
|
||||
async for event in workflow.run_streaming(task):
|
||||
async for event in workflow.run_stream(task):
|
||||
print(f"Event: {event}")
|
||||
|
||||
if isinstance(event, WorkflowCompletedEvent):
|
||||
|
||||
+11
-11
@@ -4,7 +4,7 @@ import asyncio
|
||||
from dataclasses import dataclass
|
||||
from uuid import uuid4
|
||||
|
||||
from agent_framework import AgentRunResponseUpdate, AIContents, ChatClient, ChatMessage, ChatRole
|
||||
from agent_framework import AgentRunResponseUpdate, ChatClientProtocol, ChatMessage, Contents, Role
|
||||
from agent_framework.openai import OpenAIChatClient
|
||||
from agent_framework.workflow import AgentRunUpdateEvent, Executor, WorkflowBuilder, WorkflowContext, handler
|
||||
from pydantic import BaseModel
|
||||
@@ -51,7 +51,7 @@ class ReviewResponse:
|
||||
class Reviewer(Executor):
|
||||
"""An executor that reviews messages and provides feedback."""
|
||||
|
||||
def __init__(self, chat_client: ChatClient) -> None:
|
||||
def __init__(self, chat_client: ChatClientProtocol) -> None:
|
||||
super().__init__()
|
||||
self._chat_client = chat_client
|
||||
|
||||
@@ -69,7 +69,7 @@ class Reviewer(Executor):
|
||||
# Define the system prompt.
|
||||
messages = [
|
||||
ChatMessage(
|
||||
role=ChatRole.SYSTEM,
|
||||
role=Role.SYSTEM,
|
||||
text="You are a reviewer for an AI agent, please provide feedback on the "
|
||||
"following exchange between a user and the AI agent, "
|
||||
"and indicate if the agent's responses are approved or not.\n"
|
||||
@@ -91,7 +91,7 @@ class Reviewer(Executor):
|
||||
|
||||
# Add add one more instruction for the assistant to follow.
|
||||
messages.append(
|
||||
ChatMessage(role=ChatRole.USER, text="Please provide a review of the agent's responses to the user.")
|
||||
ChatMessage(role=Role.USER, text="Please provide a review of the agent's responses to the user.")
|
||||
)
|
||||
|
||||
print("🔍 Reviewer: Sending review request to LLM...")
|
||||
@@ -113,7 +113,7 @@ class Reviewer(Executor):
|
||||
class Worker(Executor):
|
||||
"""An executor that performs tasks for the user."""
|
||||
|
||||
def __init__(self, chat_client: ChatClient) -> None:
|
||||
def __init__(self, chat_client: ChatClientProtocol) -> None:
|
||||
super().__init__()
|
||||
self._chat_client = chat_client
|
||||
self._pending_requests: dict[str, tuple[ReviewRequest, list[ChatMessage]]] = {}
|
||||
@@ -124,7 +124,7 @@ class Worker(Executor):
|
||||
|
||||
# Handle user messages and prepare a review request for the reviewer.
|
||||
# Define the system prompt.
|
||||
messages = [ChatMessage(role=ChatRole.SYSTEM, text="You are a helpful assistant.")]
|
||||
messages = [ChatMessage(role=Role.SYSTEM, text="You are a helpful assistant.")]
|
||||
|
||||
# Add user messages.
|
||||
messages.extend(user_messages)
|
||||
@@ -163,14 +163,14 @@ class Worker(Executor):
|
||||
print("✅ Worker: Response approved! Emitting to external consumer...")
|
||||
# If approved, emit the agent run response update to the workflow's
|
||||
# external consumer.
|
||||
contents: list[AIContents] = []
|
||||
contents: list[Contents] = []
|
||||
for message in request.agent_messages:
|
||||
contents.extend(message.contents)
|
||||
# Emitting an AgentRunUpdateEvent in a workflow wrapped by a WorkflowAgent
|
||||
# will send the AgentRunResponseUpdate to the WorkflowAgent's
|
||||
# event stream.
|
||||
await ctx.add_event(
|
||||
AgentRunUpdateEvent(self.id, data=AgentRunResponseUpdate(contents=contents, role=ChatRole.ASSISTANT))
|
||||
AgentRunUpdateEvent(self.id, data=AgentRunResponseUpdate(contents=contents, role=Role.ASSISTANT))
|
||||
)
|
||||
return
|
||||
|
||||
@@ -178,12 +178,12 @@ class Worker(Executor):
|
||||
print("🔧 Worker: Incorporating feedback and regenerating response...")
|
||||
|
||||
# Construct new messages with feedback.
|
||||
messages.append(ChatMessage(role=ChatRole.SYSTEM, text=review.feedback))
|
||||
messages.append(ChatMessage(role=Role.SYSTEM, text=review.feedback))
|
||||
|
||||
# Add additional instruction to address the feedback.
|
||||
messages.append(
|
||||
ChatMessage(
|
||||
role=ChatRole.SYSTEM,
|
||||
role=Role.SYSTEM,
|
||||
text="Please incorporate the feedback above, and provide a response to user's next message.",
|
||||
)
|
||||
)
|
||||
@@ -234,7 +234,7 @@ async def main() -> None:
|
||||
print("-" * 50)
|
||||
|
||||
# Run the agent and stream events.
|
||||
async for event in agent.run_streaming(
|
||||
async for event in agent.run_stream(
|
||||
"Write code for parallel reading 1 million files on disk and write to a sorted output file."
|
||||
):
|
||||
print(f"📤 Agent Response: {event}")
|
||||
|
||||
+2
-2
@@ -5,9 +5,9 @@ from dataclasses import dataclass
|
||||
|
||||
from agent_framework import (
|
||||
ChatMessage,
|
||||
ChatRole,
|
||||
FunctionCallContent,
|
||||
FunctionResultContent,
|
||||
Role,
|
||||
)
|
||||
from agent_framework.openai import OpenAIChatClient
|
||||
from agent_framework.workflow import (
|
||||
@@ -133,7 +133,7 @@ async def main() -> None:
|
||||
result=human_response,
|
||||
)
|
||||
# Send the human review result back to the agent.
|
||||
response = await agent.run(ChatMessage(role=ChatRole.TOOL, contents=[human_review_function_result]))
|
||||
response = await agent.run(ChatMessage(role=Role.TOOL, contents=[human_review_function_result]))
|
||||
print(f"📤 Agent Response: {response.messages[-1].text}")
|
||||
|
||||
print("=" * 50)
|
||||
|
||||
Reference in New Issue
Block a user