Skip to content

Commit fb4477c

Browse files
committed
make the app work locally
1 parent 6aebb36 commit fb4477c

2 files changed

Lines changed: 73 additions & 43 deletions

File tree

README.md

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,41 @@ A FastAPI-based to-do list application with Azure AI Agents integration and Mode
3333
```bash
3434
python main.py
3535
```
36+
37+
> **Note**: The app will start successfully and provide basic to-do functionality without any environment variables. AI chat features require Azure resource deployment and configuration (see below).
3638
37-
3. **Access the app**:
38-
- To-do List: http://localhost:8000
39-
- AI Chat: http://localhost:8000/chat
40-
- Health Check: http://localhost:8000/health
39+
3. **Configure Azure AI (Optional)**:
40+
For AI chat features in your local environment, if you have AI Foundry resources already deployed, set these environment variables. Ensure you're logged into the Azure CLI using `az login` as that credential is required to access the chat.
41+
42+
**Option A: Create a `.env` file in the project root:**
43+
```bash
44+
# .env file - Optional for local AI Chat functionality
45+
AZURE_AI_PROJECT_ENDPOINT=https://my-ai-project-abc123.westus.ai.azure.com
46+
AZURE_AI_PROJECT_NAME=my-todo-project
47+
AZURE_OPENAI_DEPLOYMENT_NAME=gpt-4o
48+
AZURE_APP_SERVICE_URL=http://localhost:8000
49+
```
50+
51+
**Option B: Set environment variables in PowerShell:**
52+
```powershell
53+
$env:AZURE_AI_PROJECT_ENDPOINT="https://my-ai-project-abc123.westus.ai.azure.com"
54+
$env:AZURE_AI_PROJECT_NAME="my-todo-project"
55+
$env:AZURE_OPENAI_DEPLOYMENT_NAME="gpt-4o"
56+
$env:AZURE_APP_SERVICE_URL="http://localhost:8000"
57+
```
58+
59+
**Option C: Set environment variables in Command Prompt:**
60+
```cmd
61+
set AZURE_AI_PROJECT_ENDPOINT=https://my-ai-project-abc123.eastus2.ai.azure.com
62+
set AZURE_AI_PROJECT_NAME=my-todo-project
63+
set AZURE_OPENAI_DEPLOYMENT_NAME=gpt-4o
64+
set AZURE_APP_SERVICE_URL=http://localhost:8000
65+
```
66+
67+
4. **Access the app**:
68+
- To-do List: http://localhost:8000 ✅ (always works)
69+
- AI Chat: http://localhost:8000/chat ⚠️ (requires Azure setup)
70+
- Health Check: http://localhost:8000/health ✅ (always works)
4171

4272
### Azure Deployment
4373

@@ -148,22 +178,6 @@ curl -X POST http://localhost:8000/mcp/stream \
148178
- `GET /mcp/stream` - MCP server info
149179
- `POST /mcp/stream` - MCP JSON-RPC endpoint
150180

151-
## Configuration
152-
153-
Environment variables are automatically configured during Azure deployment:
154-
155-
- `AZURE_AI_PROJECT_ENDPOINT` - AI Foundry endpoint
156-
- `AZURE_OPENAI_DEPLOYMENT_NAME` - GPT-4o deployment name
157-
- `AZURE_APP_SERVICE_URL` - App service URL for MCP
158-
159-
## Development
160-
161-
The app uses:
162-
- **FastAPI** for the web framework
163-
- **Azure AI Agents** for AI functionality
164-
- **Bootstrap 5** for UI styling
165-
- **In-memory storage** for simplicity (easily upgradeable)
166-
167181
## License
168182

169183
MIT License

main.py

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,27 @@ def get_azure_ai_project_endpoint():
6363
raise ValueError(f"Invalid AZURE_AI_PROJECT_ENDPOINT format: {azure_ai_endpoint}")
6464

6565

66-
PROJECT_ENDPOINT = get_azure_ai_project_endpoint()
67-
MODEL_DEPLOYMENT = os.getenv('AZURE_OPENAI_DEPLOYMENT_NAME')
68-
if not MODEL_DEPLOYMENT:
69-
raise ValueError("AZURE_OPENAI_DEPLOYMENT_NAME environment variable is required")
70-
71-
# MCP server URL from Bicep deployment
72-
azure_app_service_url = os.getenv('AZURE_APP_SERVICE_URL')
73-
if not azure_app_service_url:
74-
raise ValueError("AZURE_APP_SERVICE_URL environment variable is required")
75-
76-
MCP_SERVER_URL = azure_app_service_url + "/mcp/stream"
77-
MCP_SERVER_LABEL = "todolist"
66+
# Make Azure AI configuration optional for local development
67+
try:
68+
PROJECT_ENDPOINT = get_azure_ai_project_endpoint()
69+
MODEL_DEPLOYMENT = os.getenv('AZURE_OPENAI_DEPLOYMENT_NAME')
70+
if not MODEL_DEPLOYMENT:
71+
raise ValueError("AZURE_OPENAI_DEPLOYMENT_NAME environment variable is required")
72+
73+
# MCP server URL from Bicep deployment or default to localhost
74+
azure_app_service_url = os.getenv('AZURE_APP_SERVICE_URL', 'http://localhost:8000')
75+
MCP_SERVER_URL = azure_app_service_url + "/mcp/stream"
76+
MCP_SERVER_LABEL = "todolist"
77+
AI_CONFIG_AVAILABLE = True
78+
logger.info("✓ Azure AI configuration loaded successfully")
79+
except Exception as e:
80+
PROJECT_ENDPOINT = None
81+
MODEL_DEPLOYMENT = None
82+
MCP_SERVER_URL = None
83+
MCP_SERVER_LABEL = None
84+
AI_CONFIG_AVAILABLE = False
85+
logger.warning(f"⚠️ Azure AI not configured: {e}")
86+
logger.info("ℹ️ Running in local mode - AI chat features disabled")
7887

7988

8089
# Pydantic Models
@@ -312,18 +321,22 @@ async def chat_with_agent(self, message: str) -> ChatResponse:
312321
logger.error(f"Full traceback: {traceback.format_exc()}")
313322
return ChatResponse(response=f"Error: {str(e)}")
314323

315-
# Initialize Azure AI service
316-
ai_service = AzureAIAgentService()
324+
# Initialize Azure AI service only if configuration is available
325+
ai_service = AzureAIAgentService() if AI_CONFIG_AVAILABLE else None
317326

318327
# Template configuration
319328
templates = Jinja2Templates(directory="templates")
320329

321330
@asynccontextmanager
322331
async def lifespan(app: FastAPI):
323-
logger.info("Starting Todo MCP FastAPI Server with Azure AI Agents")
332+
logger.info("Starting To-do MCP FastAPI Server")
324333

325-
# Initialize Azure AI service
326-
await ai_service.initialize()
334+
# Initialize Azure AI service if available
335+
if ai_service and AI_CONFIG_AVAILABLE:
336+
await ai_service.initialize()
337+
logger.info("✓ Azure AI service initialized")
338+
else:
339+
logger.info("ℹ️ Running without Azure AI - basic to-do features only")
327340

328341
yield
329342
logger.info("Shutting down Todo MCP FastAPI Server")
@@ -365,7 +378,9 @@ async def health():
365378
"status": "healthy",
366379
"todos_count": len(todos_storage),
367380
"azure_ai_available": AZURE_AI_AVAILABLE,
368-
"ai_service_initialized": ai_service.is_initialized,
381+
"ai_service_initialized": (
382+
ai_service.is_initialized if ai_service else False
383+
),
369384
"project_endpoint": PROJECT_ENDPOINT,
370385
"mcp_server_url": MCP_SERVER_URL
371386
}
@@ -444,10 +459,10 @@ async def delete_todo_api(todo_id: int):
444459
@app.post("/api/chat/session", response_model=ChatSession)
445460
async def create_chat_session():
446461
"""Create a new chat session"""
447-
if not ai_service.is_initialized:
462+
if not ai_service or not ai_service.is_initialized:
448463
raise HTTPException(
449464
status_code=503,
450-
detail="Azure AI service is not available"
465+
detail="Azure AI service is not available. Please configure environment variables for AI chat features."
451466
)
452467

453468
session_id = generate_session_id()
@@ -486,10 +501,10 @@ async def chat_with_ai_session(chat_message: ChatSessionMessage):
486501
@app.post("/api/chat", response_model=ChatResponse)
487502
async def chat_with_ai(chat_message: ChatMessage):
488503
"""Send a message to the AI agent with MCP tools (legacy endpoint)"""
489-
if not ai_service.is_initialized:
504+
if not ai_service or not ai_service.is_initialized:
490505
raise HTTPException(
491506
status_code=503,
492-
detail="Azure AI service is not available"
507+
detail="Azure AI service is not available. Please configure environment variables for AI chat features."
493508
)
494509

495510
response = await ai_service.chat_with_agent(chat_message.message)
@@ -499,8 +514,9 @@ async def chat_with_ai(chat_message: ChatMessage):
499514
async def chat_status():
500515
"""Get the status of the Azure AI chat service"""
501516
return {
502-
"available": ai_service.is_initialized,
517+
"available": ai_service.is_initialized if ai_service else False,
503518
"azure_ai_packages_available": AZURE_AI_AVAILABLE,
519+
"ai_config_available": AI_CONFIG_AVAILABLE,
504520
"project_endpoint": PROJECT_ENDPOINT,
505521
"model_deployment": MODEL_DEPLOYMENT,
506522
"mcp_server_configured": bool(MCP_SERVER_URL)

0 commit comments

Comments
 (0)