Module 6 Wrap-up: Shipping the API
Hands-on: Build a functional REST API with FastAPI that exposes multiple Bedrock models.
Module 6 Wrap-up: The Backend Architect
You have moved from a "Scripting" mindset to a "Service" mindset. You understand that your AI code doesn't live in a vacuum—it lives behind a Validation Gateway (FastAPI) and must manage State (History) to feel like a real conversation.
Hands-on Exercise: The Multi-Model API
1. The Goal
Create a FastAPI server with a single /chat endpoint.
The user should be able to send a model_name as a parameter (e.g., "claude" or "llama").
2. The Implementation Plan
- Define a dictionary that maps simple names to Bedrock ARNs.
- Add a
model_nicknamefield to your Pydantic request model. - Use the nickname to look up the correct
modelIdbefore calling Bedrock. - Handle the case where the nickname doesn't exist by returning a
404error.
3. Verification
Use a tool like Thunder Client or cURL to send a request to your local server:
curl -X POST http://localhost:8000/chat -H "Content-Type: application/json" -d '{"user_prompt": "Hi", "model_nickname": "claude"}'
Module 6 Summary
- FastAPI: The asynchronous core of modern AI backends.
- Pydantic: Ensuring your prompts are safe and well-formatted.
- Statelessness: Bedrock requires you to send the history every time.
- Modular Design: Separating model IDs from your API logic for easy maintenance.
Coming Up Next...
In Module 7, we enter the world of Knowledge Bases. We will learn why models hallucinate when asked about private data and how to build a RAG (Retrieval-Augmented Generation) architecture using OpenSearch Serverless.
Module 6 Checklist
- I have installed
fastapianduvicorn. - I can describe the difference between a stateful and stateless API.
- I have successfully run a FastAPI server locally.
- I know how to validate input using Pydantic.
- I understand how history growth impacts token costs.