Skip to content

Python: Issue 2346: (python): Add enterprise chat agent sample with Azure Functions and Cosmos DB#4906

Open
vj-msft wants to merge 14 commits intomicrosoft:mainfrom
vj-msft:issue-2436
Open

Python: Issue 2346: (python): Add enterprise chat agent sample with Azure Functions and Cosmos DB#4906
vj-msft wants to merge 14 commits intomicrosoft:mainfrom
vj-msft:issue-2436

Conversation

@vj-msft
Copy link
Member

@vj-msft vj-msft commented Mar 25, 2026

Motivation and Context

The existing ChatKit sample focuses on frontend widget integration. Enterprise teams need a backend API pattern that any client (web, mobile, Teams, CLI) can consume.

This sample provides a production-ready Chat API demonstrating:

  • Thread-based conversation management
  • Cosmos DB persistence with multi-tenant isolation
  • Runtime tool selection (agent autonomy)
  • OpenTelemetry observability
  • One-command Azure deployment (azd up)

Closes the issue : #2436

Description

New sample: python/samples/05-end-to-end/enterprise-chat-agent/

API Endpoints:

Method Path Description
POST /api/threads Create conversation thread
GET /api/threads List threads (with filters)
DELETE /api/threads/{id} Delete thread
POST /api/threads/{id}/messages Send message, get agent response
GET /api/threads/{id}/messages Get conversation history

Key Components:

  • function_app.py — Azure Functions entry point
  • services/agent_service.py — Agent with CosmosHistoryProvider
  • services/cosmos_store.py — Thread metadata storage
  • services/observability.py — OpenTelemetry instrumentation
  • tools/ — Weather, calculator, knowledge base tools
  • infra/ — Bicep infrastructure for azd deployment

Design Decisions:

  • MCP for RAG (Microsoft Learn docs) instead of Azure AI Search — no extra infrastructure needed
  • Two Cosmos containers: threads (metadata) + messages (CosmosHistoryProvider)
  • Buffered responses — Azure Functions limitation; README documents streaming alternatives

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? If yes, add "[BREAKING]" prefix to the title of the PR.

- Added demo HTTP requests for the Enterprise Chat Agent API.
- Developed the main function app with HTTP triggers for thread management, message handling, and health checks.
- Integrated Cosmos DB for persistent storage of threads and messages.
- Created tools for weather information, calculations, and knowledge base searches.
- Configured infrastructure using Bicep templates for Azure resources including Function App, Cosmos DB, Application Insights, and Log Analytics.
- Added example local settings for development and updated requirements for necessary packages.
…emetry instrumentation

- Added observability design documentation outlining principles and framework integration.
- Created route blueprints for threads, messages, and health check endpoints.
- Implemented health check endpoint to verify Cosmos DB connectivity.
- Developed message handling endpoints for sending and retrieving messages in conversation threads.
- Established thread management endpoints for creating, retrieving, and deleting threads.
- Integrated Cosmos DB storage for threads and messages with lazy initialization.
- Added observability module to manage custom spans for HTTP requests, Cosmos DB operations, and validation checks.
- Configured OpenTelemetry setup for tracing and metrics collection.
- Updated requirements for OpenTelemetry and Azure Monitor dependencies.
- Documented environment variables and Azure Functions configuration for observability.
- Added core modules for the Enterprise Chat Agent including services for Cosmos DB storage, observability, and agent functionality.
- Implemented CosmosConversationStore for managing conversation thread metadata in Azure Cosmos DB.
- Developed agent_service to handle chat agent creation with integrated tools for weather, calculation, and knowledge base search.
- Introduced observability features using OpenTelemetry for tracking HTTP requests and Cosmos DB operations.
- Created local tools for weather, calculation, and knowledge base search, enabling the agent to autonomously select tools based on user input.
- Removed outdated design documentation to streamline project structure.
- Updated DESIGN.md to reflect new architecture and goals for the Enterprise Chat Agent, including enhanced agent autonomy and integration with Microsoft Docs.
- Removed MCP_INTEGRATION.md and observability-design.md as their content is now integrated into DESIGN.md.
- Added new API endpoint to list conversation threads with optional filters for user ID and status.
- Implemented Cosmos DB query logic for listing threads in cosmos_store.py.
- Enhanced observability by initializing logging levels for httpx and httpcore to reduce verbosity.
- Updated routes/messages.py to utilize AgentSession for better context management during agent runs.
- Added debug endpoint to list session IDs for troubleshooting.
- Improved logging throughout the message handling process for better traceability.
Copilot AI review requested due to automatic review settings March 25, 2026 15:55
@markwallace-microsoft markwallace-microsoft added documentation Improvements or additions to documentation python labels Mar 25, 2026
@github-actions github-actions bot changed the title Issue 2346: (python): Add enterprise chat agent sample with Azure Functions and Cosmos DB Python: Issue 2346: (python): Add enterprise chat agent sample with Azure Functions and Cosmos DB Mar 25, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new Python end-to-end sample that demonstrates building a backend “enterprise chat API” on Azure Functions with Cosmos DB persistence, Agent Framework tools (local + MCP), and OpenTelemetry-based observability, along with azd/Bicep deployment assets.

Changes:

  • Introduces Azure Functions HTTP API routes for thread and message operations plus health checks.
  • Adds an Agent Framework configuration using Azure OpenAI + CosmosHistoryProvider and local tools (weather/calculator/KB) plus MCP docs search.
  • Adds infra-as-code (Bicep + azd) and supporting docs/demo assets (README, design doc, demo UI/HTTP).

Reviewed changes

Copilot reviewed 30 out of 30 changed files in this pull request and generated 19 comments.

Show a summary per file
File Description
python/samples/05-end-to-end/enterprise-chat-agent/function_app.py Azure Functions entrypoint registering route blueprints and initializing observability
python/samples/05-end-to-end/enterprise-chat-agent/host.json Functions host config (logging + OpenTelemetry mode)
python/samples/05-end-to-end/enterprise-chat-agent/azure.yaml azd service definition for Function App deployment
python/samples/05-end-to-end/enterprise-chat-agent/requirements.txt Sample dependency list (Agent Framework, Cosmos provider, OTEL, Azure SDK)
python/samples/05-end-to-end/enterprise-chat-agent/local.settings.json.example Local configuration template for running the sample
python/samples/05-end-to-end/enterprise-chat-agent/prompts/system_prompt.txt System instructions for the agent/tool usage guidance
python/samples/05-end-to-end/enterprise-chat-agent/routes/init.py Blueprint exports for routes package
python/samples/05-end-to-end/enterprise-chat-agent/routes/threads.py Thread CRUD endpoints + debug sessions endpoint
python/samples/05-end-to-end/enterprise-chat-agent/routes/messages.py Send/retrieve message endpoints integrating agent + Cosmos history
python/samples/05-end-to-end/enterprise-chat-agent/routes/health.py Health endpoint with Cosmos connectivity check
python/samples/05-end-to-end/enterprise-chat-agent/services/init.py Exports service helpers (agent, cosmos store, observability spans)
python/samples/05-end-to-end/enterprise-chat-agent/services/agent_service.py Agent + CosmosHistoryProvider setup, MCP tool factory
python/samples/05-end-to-end/enterprise-chat-agent/services/cosmos_store.py Cosmos thread metadata store implementation
python/samples/05-end-to-end/enterprise-chat-agent/services/observability.py Wrapper spans for HTTP, Cosmos, validation using framework OTEL APIs
python/samples/05-end-to-end/enterprise-chat-agent/tools/init.py Aggregates local tool exports
python/samples/05-end-to-end/enterprise-chat-agent/tools/weather.py Simulated weather tool
python/samples/05-end-to-end/enterprise-chat-agent/tools/calculator.py AST-based “safe” calculator tool
python/samples/05-end-to-end/enterprise-chat-agent/tools/knowledge_base.py Simulated knowledge base search tool
python/samples/05-end-to-end/enterprise-chat-agent/infra/main.parameters.json azd parameter bindings for Bicep
python/samples/05-end-to-end/enterprise-chat-agent/infra/main.bicep Top-level deployment wiring (RG, monitoring, storage, cosmos, function app)
python/samples/05-end-to-end/enterprise-chat-agent/infra/abbreviations.json Resource name abbreviation config used by Bicep
python/samples/05-end-to-end/enterprise-chat-agent/infra/core/monitor/monitoring.bicep Log Analytics + App Insights resources
python/samples/05-end-to-end/enterprise-chat-agent/infra/core/storage/storage-account.bicep Storage account for Function App deployment assets
python/samples/05-end-to-end/enterprise-chat-agent/infra/core/database/cosmos-nosql.bicep Cosmos NoSQL account + database + container provisioning
python/samples/05-end-to-end/enterprise-chat-agent/infra/core/host/function-app.bicep Flex Consumption Function App + identity + role assignment + app settings
python/samples/05-end-to-end/enterprise-chat-agent/docs/DESIGN.md Design/architecture write-up for the sample
python/samples/05-end-to-end/enterprise-chat-agent/demo.http REST client demo script for the API
python/samples/05-end-to-end/enterprise-chat-agent/demo-ui.html Simple browser UI demo for thread/message interactions
python/samples/05-end-to-end/enterprise-chat-agent/README.md Setup, usage, API docs, streaming notes, and project structure
python/samples/05-end-to-end/enterprise-chat-agent/.gitignore Sample-local gitignore for Functions/azd/python artifacts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants