FastAPI for AI Projects - Getting Started in 15 Minutes
By Dave Ebbelaar
Summary
Topics Covered
- Local Demos Need API Integration Layer
- FastAPI Auto-Generates Interactive Docs
- Modular Structure Beats Monolithic Files
- Pydantic Validates Incoming Data Automatically
- Async Endpoints Boost Scalability Effortlessly
Full Transcript
So, if you've been wondering how you can take your local AI demo and turn it into an actual proper AI application with a proper backend, then this video is for you. I'm going to walk you through my
you. I'm going to walk you through my fast API tutorial where I will show you how we implement these AI backends for all of the client projects that we work on at data luminina. In this tutorial,
I'm going to specifically focus on the very basics that you need to understand in order to build AI backends. So there
is really a lot that you can do with fast API. If you if you for example want
fast API. If you if you for example want to build out an entire web application, there's a lot more that you need to know, but actually when you're building AI backends, it's actually quite simple.
And fast API is currently really becoming the go-to library to build API endpoints with for AI applications. So
it's very worthwhile learning about this library. So I am going to explain
library. So I am going to explain essentially how we can do the following.
So by the end of this video, you'll understand how you essentially can connect from any type of application or or web hook or whatever to an API
endpoint layer that we are going to create where we can then validate that incoming data and then for example process it using AI logic or send it to
an LLM. So that's also the flow of
an LLM. So that's also the flow of operations when you are working with an API and when you're working with the back end like this. So really the goal of setting up this API endpoint is to
serve as an integration layer so that you can turn your local demo where you run locally on your laptop you run some examples you can now actually expose that logic and make it available in an application so that other users can
actually start to use it. That's the
goal of integrating an API. All right so that with that out of the way what I want to do right now is I want to first go through this quick start with you and you can follow along if you want. That's
what I recommend if you really want to learn this. And then we'll cover the
learn this. And then we'll cover the four files that are uh that are in here.
And they're very simple. This is a basic setup. This is really all you need to
setup. This is really all you need to know. And now in this video, I am
know. And now in this video, I am assuming you are already familiar with the basic API concept. So I won't go into all of the specific concepts about what is an endpoint, get post request.
So we can keep this tutorial lean and effective. So if you're entirely new and
effective. So if you're entirely new and have never worked with an API, it might make sense to do a little bit of digging, for example, with JBT to ask, okay, what is an API? How does it work?
And where does it fit into an entire architecture of an application? So let's
do that. We have the repository. Then we
did already did the UC UV sync. And now
I can come in here and I'm going to copy paste this command over here. I'll
explain what it does in a bit, but for now I'm just going to CD into the app.
Let's see. We're in here. All right. So
we can come in here and run uvorn main app reload. So this is now going to spin
app reload. So this is now going to spin up our server and this should be running on localhost port 8000. Right now we can
now access our API by for example going to localhost port 8000 and then going to the docs. And this is one super nice
the docs. And this is one super nice feature of fast API because by just defining your API endpoint you get documentation like this. And now you might be wondering okay what's going on?
This is cool. It seems to be working. We
have some documentation, but that's the quick start and I want to start there first so that we can now start to understand all of the different building blocks that we're working with right now
and how all of this works. So let's come back to the tutorial over here. So first
of all, let's understand what we're doing with this command over here and then look into the files. So whenever we spin up our uh fast API application, we
can use uicorn which is an ASGI server that actually runs the application. So
fast API defines really your API structure. That's what we're going to do
structure. That's what we're going to do inside the Python files. But uicorn is a server that handles the HTTP connections and really serves your application. So
it's really facilitates the communication between all of the different components here. And what
we're doing over here, so we're using that uvicorn command and then we say main and main refers to the main file that we have in here. And the app refers
to the variable app that we're creating over here. So we're uh importing the
over here. So we're uh importing the fast API library and uh we're initiating that using the app variable. So that's
what we're seeing over here. And the
d-reload simply means that as we go about changing and adding our files over here that this automatically reloads. So
if you're following along, congratulations. You have now set up
congratulations. You have now set up your first fast API endpoint. So now
let's dig a little bit deeper. So really
with the idea of starting with the end in mind, showing you end to end of what we're going to do. Now let's drill down and see how all of these components work together. And I have split the
together. And I have split the application up into uh three parts. So
we have main, router, and endpoint. And
I did that on purpose to emulate essentially how we are using this in our production applications. So already out
production applications. So already out of the box if you follow this tutorial you'll get a boiler plate that you can reuse because in a lot of tutorials you just find they have one big file and they add everything there just for the
sake of simplicity. But I'm going to show you how we can make this more modular. So out of the box this already
modular. So out of the box this already is something that you can actually put into a project. So let's start with the main.py. So that's really the entry
main.py. So that's really the entry point over here. And as you can see, there's not much going on over here.
That's also really what I love about Fast API. It's just super simple. It's
Fast API. It's just super simple. It's
super lightweight. And there's really not that much that you need to actually understand about the library if you're building lightweight uh AI endpoints like this. So like I've mentioned, we
like this. So like I've mentioned, we import the fast API library. And all
we're doing right now is we're including a router. So you can see from router we
a router. So you can see from router we import the process router and that is simply the next file that we're going to go to. So what this essentially allows
go to. So what this essentially allows us to do so we keep our main we keep our main.py very lean and minimal. So
main.py very lean and minimal. So
there's not much going on over here and essentially from here we route we essentially create routers so that we can move the data to the right uh API
endpoints. So now let's get into that
endpoints. So now let's get into that file. So that's the router. So route
file. So that's the router. So route
incoming requests to the appropriate endpoint handlers. So let's have a look
endpoint handlers. So let's have a look at what that looks like. So come in here you can see that now from fast API we import an API router and we we initiate
that router over here and then we essentially include that router and you see one more import over here that we're importing from the endpoint. So what
this essentially does is now we can say we want to create an endpoint forward/events so that we have our application running in this case on localhost host 8000 but when you put
this into production this is going to be a URL and then port 8000 and we're going to mention forward/events. So that's
going to be the URL that then our application needs to send information to. I'll show that in a bit on how that
to. I'll show that in a bit on how that actually on what that actually looks like and how that can come together. Now
just know that we're creating a router as a way to say okay everything that's in this endpoint we use the prefix forward slashevents and we can also give
it some text but uh the prefix is really the most important one. So then we get to the endpoint.py so this defines the data model and the endpoint logic for
the processing event. This is really the only part right now where we implement some custom logic. This right now is all pretty much boilerplate. And here we can
play a little bit with let's see let me close this down a little bit. Here we
can actually start to implement our logic. So now if we go over here and we
logic. So now if we go over here and we have a look at the files or the code that is uh that lives within this file we can see it's also very lean and we start by initiating the router. Again,
this is all just to follow the the same syntax that we can use to essentially say, okay, we want to have this event endpoint that we're creating over here
and we want to tie it to the router. Now
over here you can see that we have a pyic model. So here is that pyantic
pyic model. So here is that pyantic integration is coming in. So out of the box if you install fast API, pyantic will be installed as well. And here I
have defined an event schema. So let's
say you're building an AI application and the incoming data looks something like this where you have an event ID, you have an event type and you have some data in here which could be a dictionary. So let's say you're
dictionary. So let's say you're integrating with an email surface or with your Gmail and you set up a web hook. So every time you get an email
hook. So every time you get an email that is now sent to or that is the information that we want to send to this API endpoint. So then the first thing
API endpoint. So then the first thing that you want to do is figure out what that schema looks like and implement it into a pyantic base model. Now again for the sake of simplicity we're just using
this schema over here but I'm just explaining how this would actually what this would actually look like in the real world. So you would first figure
real world. So you would first figure out your data source implement the pantic base model and then go from there. And now if we then go to the
there. And now if we then go to the actual endpoint. So now we get to the
actual endpoint. So now we get to the actual Python logic, Python function where we can decide with our application about how we want to process this data.
So we have a simple function called handle event and we plug some data in there and we make sure that we set that uh we set the data type to the event
schema that we have just specified. So
in a bit we'll see how that works. But
because of this we can now validate the incoming data. Whereas if the incoming
incoming data. Whereas if the incoming data does not match this schema, our endpoint will give us an error. For the
sake of demonstration, I'm just going to print this data. But this logic that you apply over here, this should be the starting point of your AI logic. So if
you're for example already working on an AI demo, you might have a simple processing pipeline or workflow where you make multiple LLM calls, you implement some guardrails, you do rack, whatever, it doesn't really matter. all
of your logic that you already implemented, this is the entry point for you to then kick that off. So you most likely want to wrap that into some kind of workflow or class or function where
you have a single entry point where data is coming in and you can send it to your application and now the whole thing starts to work and then simply what we do is we return a response. So we import
that response from from Starlet and that is another um library that's working in the back end when using fast API. And
now we essentially give an HTTP status code of accepted. So if if all goes well, this will return a 200 or 202 message status. And then it will
message status. And then it will essentially ask the content. It will say we have the data received. So that's
pretty much all you need to know right now about how you set up endpoints. You
initiate the app, you create a routing point so that now we have forward/events and then we have the endpoint in here.
And now everything that's in here essentially all the uh events or sorry all the endpoints they they will live with the prefix forward/events. So that
if now we use this forward slash here this can be accessed by simply using the forward slashevents. And so now we tag
forward slashevents. And so now we tag the endpoint with decorator over here and this is just following the fast API syntax. So this now becomes an endpoint.
syntax. So this now becomes an endpoint.
So I have another file in here which is the request.py. So what this is doing is
the request.py. So what this is doing is this is using the uh request library to send an an event to send some data to our endpoint. So for this to work you
our endpoint. So for this to work you need to make sure that your application is running. So in the terminal below
is running. So in the terminal below here this should be running. And now
what we can do I'm going to run through this in the interactive window. So we
can really do this step by step. First
of all, the URL is we're running this on localhost port 8000 and we discovered that our endpoint is available at forward slashevents. So we have that as
forward slashevents. So we have that as our URL. Now we are going to create an
our URL. Now we are going to create an event data and this should match the pyantic model that we have specified. I
will show in a bit what happens if we don't do that. If we right now come in here and we have our event data so we can have a look at that. This is all nice and neat in the correct format and
we add some headers to this. What we can now do is we can now use the request library to do a post request. We have
the URL which we have over here and then we have our JSON data which we dump. So
this is a Python dictionary and we dump that to JSON and we include the headers and we can now send the response. So we
can also see right now below here in the terminal that we print our data that is coming in and that's because in the endpoint we simply do a print print statement. Now if we then come back to
statement. Now if we then come back to our response over here we can see okay we get a 202 status code the data is received and we get some additional
metadata to see what's going on behind the scenes. So we have now created a
the scenes. So we have now created a simple setup where let me scroll up. We
can now if this is available, if this endpoint is available, we can now send data to this fast API endpoint. It will
validate the data and we can then send it over to our A AI or LLM service to process that. So it's really about
process that. So it's really about creating a communication layer that we need. And now of course the API layer is
need. And now of course the API layer is just one component of your entire AI infrastructure and system that you need in order to really build production ready AI applications. And if you want
to learn how we do that at Data Luminina for all of the client projects that we work on, you might want to check out our Genai Launchpad, the first link in the description, you can get access to our entire production framework that's
available in a private GitHub repository. And also, you'll get access
repository. And also, you'll get access to all of the training tutorials that we have for this to really show you how you can set it up from A to C and start shipping real production ready AI
applications. Let me show you one more
applications. Let me show you one more time what happens if, for example, this is not a dictionary, but this is a string. So now we do it like this. Now
string. So now we do it like this. Now
we get a status code 422 because this is an unprocessable entity because if we look in the endpoint data is coming in we set the data we match it against the
event schema but the event schema is not matching right now because this is uh a string instead of a dictionary. So now
we can also start to catch that error and maybe also to the application or to to the user to instruct what they need to adjust. So for example here you can
to adjust. So for example here you can also see the input should be a valid dictionary. So that's out of the box
dictionary. So that's out of the box such a nice feature of fast API that it comes integrated with paidic and as you know if you've been building with uh language models you know one of the core
methods to make LLM applications more reliable is to work with structured output and most of the structured output approaches that you'll find within the
Python ecosystem are all based around pyentic and that's why we really love working with fast API because it just follows that same methodology And if you go back to the documentation, you can
also find how you can create uh synchronous versus asynchronous endpoints in fast API. And it is super simple. So in the current example that
simple. So in the current example that we have over here, this is a synchronous endpoint. But we can also make that
endpoint. But we can also make that async simply by turning the endpoint into an asynchronous functions and then essentially waiting awaiting the processing logic. So this is super
processing logic. So this is super helpful uh or super simple as well with fast API and this will already make your application more scalable. All right.
And then what if you want to protect your API endpoint which you should do if you're going to put this into production. Here is an exercise for you
production. Here is an exercise for you to go through how you on how you can set up security using a bearer token. So I
have an example over here how you can set it up and how you can also work with fast API and with the imports in here.
create a security token or an API token and then how you can uh send an authenticated request. So that's going
authenticated request. So that's going to be an exercise for you to implement on your own to not only better understand the code that we have over here but also to understand at a deeper
level like okay how do all of these components work together you can test some things change some things and then also implement the security components so that now you fully understand how to
use fast API to start really serve as the starting point for your AI backends.
All right and then that's it for this video. So, if you found it helpful,
video. So, if you found it helpful, please leave a like and also consider subscribing. And then, since you're into
subscribing. And then, since you're into building AI systems because you watch this video, you might want to check out this video next where I cover my entire strategy to build effective AI agents in
pure Python without relying on any other external frameworks. So go check that
external frameworks. So go check that out right
Loading video analysis...