Backend API¶
In the previous lab, a LangChain agent was created armed with tools to do vector lookups and concrete document id lookups via function calling. In this lab, the agent functionality needs to be extracted into a backend api for the frontend application that will allow users to interact with the agent.
The information provided in this section assumes that the dependent infrastructure is deployed and have completed the previous labs in this dev guide.
Overview¶
The backend api is a Python FastAPI application that will expose endpoints for the frontend application to interact with. The backend api is a containerized application that will be deployed to Azure Container Apps.
Clone the Backend API¶
Create a folder to house the repository. Open a terminal and navigate to the folder. Clone the repository, then navigate to the Backend
folder within the repository.
Bash | |
---|---|
1 2 3 4 5 |
|
Run the backend api locally¶
When developing a backend api, it is often useful to run the application locally to test and debug. This section outlines how to run the backend api locally while watching the file system for code changes. Any detected changes will automatically restart the backend api.
-
Open the backend api folder location in VS Code.
-
Open a Terminal window in VS Code (CTRL+`).
-
Setup the
.env
file. Copy the.env.example
file to.env
and update the values. These are the same environment variables used in the previous labs.Bash 1
cp .env.example .env
-
Using the Terminal window, create a virtual environment and activate it.
-
Run the following command to install the dependencies.
Bash 1
pip install -r requirements.txt
-
Run the following command to start the backend api in the virtual environment.
Bash 1
uvicorn --host "0.0.0.0" --port 8000 app:app --reload
-
Open a browser and navigate to
http://localhost:8000/docs
to view the Swagger UI. -
Expand the GET / Root endpoint and select Try it out. Select Execute to send the request. The response should display a status of
ready
. -
Expand the POST /ai endpoint and select Try it out. In the Request body field, enter the following JSON.
JSON 1 2 3 4
{ "session_id": "abc123", "prompt": "What was the price of the product with sku `FR-R92B-58`" }
-
Select Execute to send the request. Observe that the response indicates the price as being
$1431.50
. -
In the Terminal window, press CTRL+C to stop the backend api.
Build and run the backend api container locally in Docker Desktop¶
When deployed to Azure, the backend api will be running in a container. It is important to test the container locally to ensure it is working as expected. Containers are important because they provide a consistent environment for the application to run in. This consistency allows the application to run the same way in development, test, and production environments - whether they be locally or in the cloud.
The backend api contains a Dockerfile
that defines the container image and is used by Docker to build the container image. The Dockerfile contains instructions for Docker to build the container image. The container image is a snapshot of the application and its dependencies. The container image can be thought of an installer for the application to be deployed as needed in any environment.
The Dockerfile
for the backend api is shown below.
Docker | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 |
|
Notice the steps of installing the pip dependencies, and running the uvicorn command line similar to what was done in the previous section.
-
Ensure Docker Desktop is running.
-
Open a Terminal window in VS Code (CTRL+`).
-
If the terminal displays the virtual environment name, deactivate the virtual environment by running the following command.
Bash 1
deactivate
-
Run the following command to build the container image. Once complete, a message displays the operation has
FINISHED
.Bash 1
docker build --pull --rm -f "DOCKERFILE" -t devguidebackendapi:latest "."
-
Lastly, run the container in Docker Desktop using the following command.
Bash 1
docker run -d -p 4242:80 --name devguide-backend-api devguidebackendapi:latest
-
Open a browser and navigate to
http://localhost:4242/docs
to view the Swagger UI. -
Repeat steps 8-10 from the previous section to test the backend api running in a container on Docker Desktop.
Deploy the backend api to Azure Container Apps¶
Retrieve the Azure Container Registry login server and admin credentials¶
The backend api container image needs to be pushed to an Azure Container Registry (ACR) before it can be deployed to Azure Container Apps. The ACR is a private container registry that will store the container image. Azure Container Apps will pull the container image from the ACR to deploy the backend api.
-
In the Azure portal, open the provisioned resource group and locate and open the Container Registry resource.
-
Select Access keys from the left-hand menu. Record the Login server value and the Username and Password values for later use.
Push the backend api container image to the Azure Container Registry¶
Earlier, the backend api container image was built locally. Now that the ACR login server and admin credentials are known, the container image can be pushed to the Azure Container Registry.
-
Return to the terminal window in VS Code.
-
Run the following command to tag the container image with the ACR login server. Replace the
<login server>
value. This command will silently complete with no output.Bash 1
docker tag devguidebackendapi:latest <login server>/devguidebackendapi:v1
-
Run the following command to log into the ACR. Replace the
<login server>
,<username>
, and<password>
values. The messageLogin Succeeded
displays when the login is successful.Bash 1
docker login <login server> -u <username> -p <password>
-
Once authenticated, push the container image to the ACR using the following command. Replace the
<login server>
value.Bash 1
docker push <login server>/devguidebackendapi:v1
Deploy the backend api ACR container image to Azure Container Apps¶
The last step is to deploy the backend api container image to Azure Container Apps. Azure Container Apps is a fully managed serverless container platform that allows developers to deploy containerized applications without having to manage any infrastructure.
-
In the Azure portal, open the provisioned resource group and locate and open the Container App resource, the name will end in
-api
. Record the name of this resource. -
Back in the resource group, locate the Container Apps Environment resource, the name will end in
-containerappenv
. Record the name of this resource. -
Also record the name of the resource group.
-
Return to the terminal window in VS Code.
-
Log into the Azure CLI using the following command.
Bash 1
az login
-
Optionally set the current subscription to the correct subscription using the following command.
Bash 1
az account set --subscription <subscription id>
-
Install the Azure Container Apps extension for the CLI using the following command.
Bash 1
az extension add --name containerapp --upgrade
-
Run the following command to deploy the backend api container image to the existing Azure Container Apps resource. Replace the
<container app name>
,<login server>
,<resource group name>
, and<container app environment name>
values.Bash 1
az containerapp up --name <container app name> --image <login server>/devguidebackendapi:v1 --resource-group <resource group name> --environment <container app environment name> --ingress external
-
In the Azure Portal, locate and open the Container App resource ending in
-api
. -
Notice on the Overview screen, there is a failed container revision. This is because the
hello-world
container is running at the same binding address as the backend api container. -
View the error from the logs, or optionally select Log stream from the left menu, then select the api container log stream.
-
To rectify this, the
hello-world
container needs to be deleted. Select Containers from the left menu, then choose the Edit and Deploy button from the toolbar. -
On the Create and deploy new revision screen, beneath the Container image heading, check the box next to the
hello-world
container, then select Delete. -
Select Create to deploy the new revision. In less than a minute, the new revision is deployed.
-
Refresh the browser, then from the left menu, select Overview. Select the Application URL link.
-
The UI should show the status as
ready
. -
In the address bar of the browser, append
/docs
to the URL and press ENTER to view the Swagger UI. -
Repeat steps 8-10 from the Run the backend api locally section to test the backend api running in a container on Azure Container Apps.
Congratulations on the successful deployment of the backend api to Azure Container Apps where it is ready to service the frontend application.