mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
15256bb616
* Renamed AgentProtocol to AgentLike * Resolved comments * Renamed AgentLike to SupportsAgentRun * Resolved comments
5.0 KiB
5.0 KiB
Core Package (agent-framework-core)
The foundation package containing all core abstractions, types, and built-in OpenAI/Azure OpenAI support.
Module Structure
agent_framework/
├── __init__.py # Public API exports
├── _agents.py # Agent implementations
├── _clients.py # Chat client base classes and protocols
├── _types.py # Core types (ChatMessage, ChatResponse, Content, etc.)
├── _tools.py # Tool definitions and function invocation
├── _middleware.py # Middleware system for request/response interception
├── _threads.py # AgentThread and message store abstractions
├── _memory.py # Context providers for memory/RAG
├── _mcp.py # Model Context Protocol support
├── _workflows/ # Workflow orchestration (sequential, concurrent, handoff, etc.)
├── openai/ # Built-in OpenAI client
├── azure/ # Lazy-loading entry point for Azure integrations
└── <provider>/ # Other lazy-loading provider folders
Core Classes
Agents (_agents.py)
SupportsAgentRun- Protocol defining the agent interfaceBaseAgent- Abstract base class for agentsChatAgent- Main agent class wrapping a chat client with tools, instructions, and middleware
Chat Clients (_clients.py)
ChatClientProtocol- Protocol for chat client implementationsBaseChatClient- Abstract base class with middleware support; subclasses implement_inner_get_response()and_inner_get_streaming_response()
Types (_types.py)
ChatMessage- Represents a chat message with role, content, and metadataChatResponse- Response from a chat client containing messages and usageChatResponseUpdate- Streaming response updateAgentResponse/AgentResponseUpdate- Agent-level response wrappersContent- Base class for message content (text, function calls, images, etc.)ChatOptions- TypedDict for chat request options
Tools (_tools.py)
ToolProtocol- Protocol for tool definitionsFunctionTool- Wraps Python functions as tools with JSON schema generation@tooldecorator - Converts functions to toolsuse_function_invocation()- Decorator to add automatic function calling to chat clients
Middleware (_middleware.py)
AgentMiddleware- Intercepts agentrun()callsChatMiddleware- Intercepts chat clientget_response()callsFunctionMiddleware- Intercepts function/tool invocationsAgentContext/ChatContext/FunctionInvocationContext- Context objects passed through middleware
Threads (_threads.py)
AgentThread- Manages conversation history for an agentChatMessageStoreProtocol- Protocol for persistent message storageChatMessageStore- Default in-memory implementation
Memory (_memory.py)
ContextProvider- Protocol for providing additional context to agents (RAG, memory systems)Context- Container for context data
Workflows (_workflows/)
Workflow- Graph-based workflow definitionWorkflowBuilder- Fluent API for building workflows- Orchestrators:
SequentialOrchestrator,ConcurrentOrchestrator,GroupChatOrchestrator,MagenticOrchestrator,HandoffOrchestrator
Built-in Providers
OpenAI (openai/)
OpenAIChatClient- Chat client for OpenAI APIOpenAIResponsesClient- Client for OpenAI Responses API
Azure OpenAI (azure/)
AzureOpenAIChatClient- Chat client for Azure OpenAIAzureOpenAIResponsesClient- Client for Azure OpenAI Responses API
Key Patterns
Creating an Agent
from agent_framework import ChatAgent
from agent_framework.openai import OpenAIChatClient
agent = ChatAgent(
chat_client=OpenAIChatClient(),
instructions="You are helpful.",
tools=[my_function],
)
response = await agent.run("Hello")
Using as_agent() Shorthand
agent = OpenAIChatClient().as_agent(
name="Assistant",
instructions="You are helpful.",
)
Middleware Pipeline
from agent_framework import ChatAgent, AgentMiddleware, AgentContext
class LoggingMiddleware(AgentMiddleware):
async def process(self, context: AgentContext, next) -> AgentResponse:
print(f"Input: {context.messages}")
response = await next(context)
print(f"Output: {response}")
return response
agent = ChatAgent(..., middleware=[LoggingMiddleware()])
Custom Chat Client
from agent_framework import BaseChatClient, ChatResponse, ChatMessage
class MyClient(BaseChatClient):
async def _inner_get_response(self, *, messages, options, **kwargs) -> ChatResponse:
# Call your LLM here
return ChatResponse(messages=[ChatMessage(role="assistant", text="Hi!")])
async def _inner_get_streaming_response(self, *, messages, options, **kwargs):
yield ChatResponseUpdate(...)