1. Explore the Codebase¶
Let's look at how the FastAPI application is implemented in the src/api/main.py
file. Open it now in Visual Studio Code and let's explore the code in sections. You can also expand the section below to see the code inline.
FASTAPI application server code
src/api/main.py | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
|
-
Import the chat function (line 11). The get_response function is the entry point into our Contoso Chat implementation. It expects a customer ID, a question, and the chat history, and returns a text response.
-
Instantiate the app server (line 19-43). We use the FastAPI application server, creating it with default configurations and configuring it to allow requests from specified origins (including GitHub Codespaces hosted clients).
-
Define a default route (line 46). The "/" route maps to the base URL for the application server.
- It accepts GET requests with no parameters (equivalent to a browser site visit).
- It returns a JSON response with a "Hello World" message.
- This serves as a "health check" for the app server, verifying it's alive (e.g., during setup).
-
Define the copilot route (line 51). The "/api/create_response" route maps to the endpoint where we can invoke the Contoso Chat implementation.
- It accepts POST requests from clients and extracts required parameters.
- It invokes our copilot get_request function with those parameters.
- It returns the copilot response to the client.
Now all we need to do is run the FastAPI server, and have it listen for incoming requests from clients on these two API routes ("/" for health checks and "/api/create_response" for Contoso Chat). In the next section, we'll see how to do this locally for rapid prototyping and testing.
CONGRATULATIONS. You just reviewed the FastAPI application structure!