Lab: APIs and Web Interfaces
Due: Wednesday, December 2 at 11:59pm (one week after it is assigned) · Worth: 8 points
Starter repository: github.com/rtealwitter/lab-fastapi
Let a classmate ask your document chatbot a question from a browser. Connect the supplied browser interface to a mock, then to a working project’s Chat class.
Start
Select Use this template to create an independent lab-fastapi repository under your account, then clone that copy. In that folder, set up its Python environment. Install the dependencies:
$ python -m pip install -r requirements.txtThe starter uses Gradio 6 and openai; update older copies of requirements.txt, gradio_server.py, and endpoint.py before beginning.
A Connection Check
The API receives conversation messages as JSON and returns a JSON reply. Run the supplied FastAPI server:
$ python endpoint.pyLeave that terminal running. Open a second terminal in the same folder, activate the same environment, and request each greeting with curl:
$ curl http://127.0.0.1:8000/
$ curl http://127.0.0.1:8000/spanish
$ curl http://127.0.0.1:8000/latinCheck for hello world, hola mundo, and salve munde, respectively. Use curl.exe in PowerShell, or open the addresses in your browser.
A Reply in JSON
Send a POST request with the chat route’s JSON body:
$ curl -X POST http://127.0.0.1:8000/v1/chat/completions -H "Content-Type: application/json" -d '{"messages":[{"role":"user","content":"hello"}]}'Run that command in Git Bash on Windows, or your terminal on macOS/Linux. Alternatively, open http://127.0.0.1:8000/docs, expand the POST chat route, and use Try it out to send the same JSON body. Find the mock’s fixed reply under choices[0].message.content.
Two Turns in the Browser
Keep endpoint.py running and launch the Gradio interface in the second terminal:
$ python gradio_server.py --url=http://127.0.0.1:8000/v1 --apikey=local-placeholderOpen the printed address, usually http://127.0.0.1:7860, and send two messages. Both should receive the mock reply. Port 8000 serves the API; 7860 serves the interface. The local endpoint needs only the placeholder API key. If connection fails, check that the endpoint is running and the URL ends in /v1. Stop old servers with Ctrl+C if a port is busy, or use --port=7861 for Gradio.
gradio_server.py converts browser history to the message list sent to the API. On the second turn, trace the earlier user message and assistant reply through its history loop. Gradio 6 can supply text as content blocks; the callback joins their text before adding the new user message. In endpoint.py, find where the preceding messages are appended to chat.messages and the newest content goes to send_message. A fresh Chat already contains its system instruction; extending the list keeps that instruction on the second turn too. Keep the supplied synchronous client, gradio.ChatInterface, and --url/--apikey arguments so the automatic checks can run your callback offline.
A Conversation About a Document
Fork and clone your partner’s working docchat project beside lab-fastapi, or use your own if no partner is available. Copy endpoint.py and gradio_server.py into it and replace the mock import with the project’s real Chat class. Install and record the lab’s dependencies in that project’s environment and dependency file.
Check the integration:
Live model calls need the project’s normal API key; grading uses an offline replacement. Commit and push the integration. If working with a partner, open a pull request to offer the change back and ask them to review it. A merged pull request is not required for credit.
Other Destinations (Optional)
For optional practice, connect the same client to Groq. Set GROQ_API_KEY in your shell using the Groq quickstart, then try:
$ python gradio_server.py --url=https://api.groq.com/openai/v1 --apikey="$GROQ_API_KEY" --model=openai/gpt-oss-20bUse Git Bash on Windows for this shell-variable syntax; Python loading a .env file does not set terminal variables. For authentication or model errors, check your shell’s key and the provider’s current model list.
You can also inspect https://www.reddit.com/r/ProgrammerHumor.json. If access is denied, use your local endpoint’s JSON response instead; external APIs are optional.
Submit
Submit the working docchat repository (your own or your fork) containing endpoint.py, gradio_server.py, and the real Chat implementation. The scratch repository’s mock does not meet this requirement. Commit and sync, then select that repository and branch through Gradescope’s GitHub option; see the first lab for the steps.
All 8 points are automatic: greeting routes (1), the Gradio callback (1), and the chat endpoint’s message forwarding, history, and response (6). No screenshot, merged pull request, or submission.toml is required.
Grading Rubric
Gradescope checks each item separately and adds the points earned.
Total: 8 points.