Azure-Samples/python-agentframework-demos
关于
This repository provides examples of Microsoft Agent Framework using LLMs from Microsoft Foundry or other model providers.
开发工具实测星标增长
- 24小时
- 0
- 7天
- 0
- 15天
- 正在收集历史
- 1个月
- 正在收集历史
各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。
README.md
原文Python Agent Framework Demos
This repository provides examples of Microsoft Agent Framework using LLMs from Microsoft Foundry or other model providers.
Getting started
You have a few options for getting started with this repository. The quickest way to get started is GitHub Codespaces, since it will setup everything for you, but you can also set it up locally.
GitHub Codespaces
You can run this repository virtually by using GitHub Codespaces. The button will open a web-based VS Code instance in your browser:
Open the repository (this may take several minutes):
Open a terminal window
Continue with the steps to run the examples
VS Code Dev Containers
A related option is VS Code Dev Containers, which will open the project in your local VS Code using the Dev Containers extension:
Start Docker Desktop (install it if not already installed)
Open the project:
In the VS Code window that opens, once the project files show up (this may take several minutes), open a terminal window.
Continue with the steps to run the examples
The dev container includes a Redis server, which is used by the agent_history_redis.py example.
Local environment
Make sure the following tools are installed:
- Python 3.10+
- uv
- Git
Clone the repository:
git clone https://github.com/Azure-Samples/python-agentframework-demos cd python-agentframework-demosInstall the dependencies:
uv syncOptional: To run the
agent_history_redis.pyexample, you need a Redis server running locally:docker run -d -p 6379:6379 redis:7-alpineOptional: To run the PostgreSQL examples (
agent_knowledge_postgres.py,agent_knowledge_pg.py,agent_knowledge_pg_rewrite.py), you need PostgreSQL with pgvector running locally:docker run -d -p 5432:5432 -e POSTGRES_USER=admin -e POSTGRES_PASSWORD=LocalPasswordOnly pgvector/pgvector:pg17
Configuring model providers
These examples can be run with Microsoft Foundry or OpenAI.com, depending on the environment variables you set. All the scripts reference the environment variables from a .env file, and an example .env.sample file is provided. Host-specific instructions are below.
Using Microsoft Foundry models
This project includes infrastructure as code (IaC) to provision Azure OpenAI deployments of "gpt-5.4" and "text-embedding-3-large" via Microsoft Foundry. The IaC is defined in the infra directory and uses the Azure Developer CLI to provision the resources.
Make sure the Azure Developer CLI (azd) is installed.
Login to Azure:
azd auth loginFor GitHub Codespaces users, if the previous command fails, try:
azd auth login --use-device-codeIf you are using a tenant besides the default tenant, you may need to also login with Azure CLI to that tenant:
az login --tenant your-tenant-idProvision the OpenAI account:
azd provisionIt will prompt you to provide an
azdenvironment name (like "agents-demos"), select a subscription from your Azure account, and select a location. Then it will provision the resources in your account.Once the resources are provisioned, you should now see a local
.envfile with all the environment variables needed to run the scripts.To delete the resources, run:
azd down
Using OpenAI.com models
Create a
.envfile by copying the.env.samplefile and updating it with your OpenAI API key and desired model name.cp .env.sample .envUpdate the
.envfile with your OpenAI API key and desired model name:API_HOST=openai OPENAI_API_KEY=your_openai_api_key OPENAI_MODEL=gpt-4o-mini
Using local Ollama models
Most examples can also run against local Ollama models through Ollama's OpenAI-compatible endpoint. First install Ollama, start it, and pull chat and embedding models:
ollama pull qwen3.5:4b
ollama pull nomic-embed-text
Then configure .env:
API_HOST=ollama
OLLAMA_ENDPOINT=http://localhost:11434/v1
OLLAMA_API_KEY=nokeyneeded
OLLAMA_MODEL=qwen3.5:4b
OLLAMA_EMBEDDING_MODEL=nomic-embed-text
EMBEDDING_DIMENSIONS=256
Use http://localhost:11434/v1 when Ollama and the Python process run on the same machine. If the examples run in a
dev container while Ollama runs on the host, use http://host.docker.internal:11434/v1 instead.
Running the Python examples
You can run the examples in this repository by executing the scripts in the examples directory. Each script demonstrates a different Agent Framework pattern.
| Example | Description |
|---|---|
| agent_basic.py | A basic informational agent. |
| agent_tool.py | An agent with a single weather tool. |
| agent_tools.py | A weekend planning agent with multiple tools. |
| agent_session.py | In-memory sessions for multi-turn conversations with memory across messages. |
| agent_history_sqlite.py | Persistent chat history with a custom SQLite history provider for local file-based conversation persistence. |
| agent_history_redis.py | Persistent chat history with Redis for conversation history that survives restarts. |
| agent_memory_redis.py | Long-term memory with RedisContextProvider, storing and retrieving conversational context from Redis. |
| agent_memory_mem0.py | Long-term memory with Mem0 OSS, extracting and recalling distilled user facts across sessions. |
| agent_supervisor.py | A supervisor orchestrating activity and recipe sub-agents. |
| agent_with_subagent.py | Context isolation with sub-agents to keep prompts focused on relevant tools. |
| agent_without_subagent.py | Context bloat example where one agent carries all tool schemas in a single prompt. |
| agent_summarization.py | Context compaction via summarization middleware to reduce token usage in long conversations. |
| workflow_magenticone.py | A MagenticOne multi-agent workflow. |
| agent_tool_approval.py | Standalone agent with tool approval — gates sensitive operations before execution. |
| agent_middleware.py | Agent, chat, and function middleware for logging, timing, and blocking. |
| agent_knowledge_aisearch.py | Knowledge retrieval (RAG) using Azure AI Search with AgentFrameworkAzureAISearchRAG. |
| agent_knowledge_sqlite.py | Knowledge retrieval (RAG) using a custom context provider with SQLite FTS5. |
| agent_knowledge_pg.py | Knowledge retrieval (RAG) with PostgreSQL hybrid search (pgvector + full-text) using Reciprocal Rank Fusion. |
| agent_knowledge_pg_rewrite.py | Knowledge retrieval with query rewriting for multi-turn conversations over PostgreSQL. |
| agent_knowledge_postgres.py | Knowledge retrieval (RAG) with PostgreSQL hybrid search (pgvector + full-text) using Reciprocal Rank Fusion. |
| agent_mcp_remote.py | An agent using a remote MCP server (Microsoft Learn) for documentation search. |
| agent_mcp_local.py | An agent connected to a local MCP server (e.g. for expense logging). |
| openai_tool_calling.py | Tool calling with the low-level OpenAI SDK, showing manual tool dispatch. |
| workflow_rag_ingest.py | A RAG ingestion pipeline using plain Python executors: fetch a document with markitdown, split into chunks, and embed with an OpenAI model. |
| workflow_fan_out_fan_in_edges.py | Fan-out/fan-in with explicit edge groups using add_fan_out_edges and add_fan_in_edges. |
| workflow_aggregator_summary.py | Fan-out/fan-in with LLM summarization: synthesize expert outputs into an executive brief. |
| workflow_aggregator_structured.py | Fan-out/fan-in with LLM structured extraction into a typed Pydantic model (response_format). |
| workflow_aggregator_voting.py | Fan-out/fan-in with majority-vote aggregation across multiple classifiers (pure logic tally). |
| workflow_aggregator_ranked.py | Fan-out/fan-in with LLM-as-judge ranking: score and rank multiple candidates into a typed list. |
| workflow_agents.py | A workflow with AI agents as executors: a Writer drafts content and a Reviewer provides feedback. |
| workflow_agents_sequential.py | A sequential orchestration using SequentialBuilder: Writer and Reviewer run in order while sharing full conversation history. |
| workflow_agents_streaming.py | The same Writer → Reviewer workflow using run(stream=True) to observe executor_invoked, executor_completed, and streaming output events in real-time. |
| workflow_agents_concurrent.py | Concurrent orchestration using ConcurrentBuilder: run specialist agents in parallel and collect merged conversations. |
| workflow_conditional.py | A minimal workflow with conditional edges: the Reviewer routes to a Publisher (approved) or Editor (needs revision) based on a sentinel token. |
| workflow_conditional_structured.py | The same conditional-edge routing pattern, but with structured reviewer output (response_format) for typed branch decisions instead of sentinel string matching. |
| workflow_conditional_state.py | A stateful conditional workflow with iterative revision loops: stores the latest draft in workflow state and publishes from that state after approval. |
| workflow_conditional_state_isolated.py | The stateful conditional workflow using a create_workflow(...) factory to build fresh agents/workflow per task for state isolation and thread safety. |
| workflow_switch_case.py | A workflow with switch-case routing: a Classifier agent uses structured outputs to categorize a message and route to a specialized handler. |
| workflow_multi_selection_edge_group.py | LLM-powered multi-selection routing using add_multi_selection_edge_group to activate one-or-many downstream handlers. |
| workflow_converge.py | A branch-and-converge workflow: Reviewer routes to Publisher or Editor, then converges before final summary output. |
| workflow_handoffbuilder.py | Autonomous handoff orchestration using HandoffBuilder (agents transfer control without human-in-the-loop). |
| workflow_handoffbuilder_rules.py | Handoff orchestration with explicit routing rules using HandoffBuilder.add_handoff(). |
| workflow_hitl_requests.py | Simple HITL chat — always pause for human input after every agent response (ctx.request_info, @response_handler). |
| workflow_hitl_requests_structured.py | Trip planner HITL with structured outputs — agent decides when to ask vs. finish via PlannerOutput.status. |
| workflow_hitl_tool_approval.py | Email agent workflow with @tool(approval_mode="always_require") for gating sensitive tool calls. |
| workflow_hitl_checkpoint.py | Content review with FileCheckpointStorage — pause, exit process, and resume from checkpoint. |
| workflow_hitl_checkpoint_pg.py | Same content review workflow with a custom PostgresCheckpointStorage backend. |
| workflow_hitl_handoff.py | Interactive handoff (no autonomous mode) — framework pauses for user input via HandoffAgentUserRequest. |
| agent_otel_aspire.py | An agent with OpenTelemetry tracing, metrics, and structured logs exported to the Aspire Dashboard. |
| agent_otel_appinsights.py | An agent with OpenTelemetry tracing, metrics, and structured logs exported to Azure Application Insights. Requires Azure provisioning via azd provision. |
| agent_evaluation_generate.py | Generate synthetic evaluation data for the travel planner agent. |
| agent_evaluation.py | Evaluate a travel planner agent using Azure AI Evaluation agent evaluators (IntentResolution, ToolCallAccuracy, TaskAdherence, ResponseCompleteness). Optionally set AZURE_AI_PROJECT in .env to log results to Microsoft Foundry. |
| agent_evaluation_batch.py | Batch evaluation of agent responses using Azure AI Evaluation's evaluate() function. |
| agent_redteam.py | Red-team a financial advisor agent using Azure AI Evaluation to test resilience against adversarial attacks across risk categories (Violence, HateUnfairness, Sexual, SelfHarm). Requires AZURE_AI_PROJECT in .env. |
Using the Aspire Dashboard for telemetry
The agent_otel_aspire.py example can export OpenTelemetry traces, metrics, and structured logs to a Aspire Dashboard.
In GitHub Codespaces / Dev Containers
The Aspire Dashboard runs automatically as a service alongside the dev container. No extra setup is needed.
The
OTEL_EXPORTER_OTLP_ENDPOINTenvironment variable is already set by the dev container.Run the example:
uv run examples/agent_otel_aspire.pyOpen the dashboard at http://localhost:18888 and explore:
- Traces: See the full span tree — agent invocation → chat completion → tool execution
- Metrics: View token usage and operation duration histograms
- Structured Logs: Browse conversation messages (system, user, assistant, tool)
- GenAI visualizer: Select a chat completion span to see the rendered conversation
Local environment (without Dev Containers)
If you're running locally without Dev Containers, you need to start the Aspire Dashboard manually:
Start the Aspire Dashboard:
docker run --rm -it -d -p 18888:18888 -p 4317:18889 --name aspire-dashboard \ -e DASHBOARD__FRONTEND__AUTHMODE=Unsecured \ mcr.microsoft.com/dotnet/aspire-dashboard:latestAdd the OTLP endpoint to your
.envfile:OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317Run the example:
uv run agent_otel_aspire.pyOpen the dashboard at http://localhost:18888 and explore.
When done, stop the dashboard:
docker stop aspire-dashboard
For the full Python + Aspire guide, see Use the Aspire dashboard with Python apps.
Exporting telemetry to Azure Application Insights
The agent_otel_appinsights.py example exports OpenTelemetry traces, metrics, and structured logs to Azure Application Insights.
Setup
This example requires an APPLICATIONINSIGHTS_CONNECTION_STRING environment variable. You can get this automatically or manually:
Option A: Automatic via azd provision
If you run azd provision (see Using Microsoft Foundry models), the Application Insights resource is provisioned automatically and the connection string is written to your .env file.
Option B: Manual from the Azure Portal
Create an Application Insights resource in the Azure Portal.
Copy the connection string from the resource's Overview page.
Add it to your
.envfile:APPLICATIONINSIGHTS_CONNECTION_STRING=InstrumentationKey=...;IngestionEndpoint=...
Running the example
uv run examples/agent_otel_appinsights.py
Viewing telemetry
After running the example, navigate to your Application Insights resource in the Azure Portal:
- Transaction search: See end-to-end traces for agent invocations, chat completions, and tool executions.
- Live Metrics: Monitor real-time request rates and performance.
- Performance: Analyze operation durations and identify bottlenecks.
Telemetry data may take 2–5 minutes to appear in the portal.
Resources
探索相关项目
加载中…