Get startedGet started for free

Building the API

1. Building the API

Now it’s time to make our model available to the outside world.

2. ML Server

We do that by running an application that exposes our model through a so-called API,

3. ML Server II

which stands for Application Programming Interface. Where humans communicate with applications via graphical user interfaces, applications communicate with each other via APIs.

4. ML Server III

In this relationship, the application that exposes our ML model is called the server

5. ML Server IV

and the application that uses it for making predictions is called the client.

6. Service to the client

Through an API, a client can do two things: One, run procedures on a server. That's for example what the Amazon Comprehend API for sentiment analysis provides. And two, access remote databases. An example would be the Twitter or The New York Times API.

7. API architectures

There are many architectural styles for building APIs, like REST, RPC, SOAP etc. Each of them would take a full course to explain, so we’ll give an architecture-agnostic view and explain the fundamental functional components that every API should possess.

8. Example: Navigation app

Let’s imagine we're building an API for a Navigation app, which client apps use to calculate the estimated time of arrival from their current location to the destination.

9. Simplest form

In the simplest form

10. Simplest form II

our API would accept whatever the client provides as velocity and distance

11. Simplest form III

run that through the model

12. Simplest form IV

and return the estimated time of arrival back to the client.

13. Not your fault, but...

If a client makes an invalid request - for example, by providing a non-numeric value for velocity

14. Not your fault II

our server would not be able to handle it

15. Not your fault III

and would return a so-called Internal Server Error, which would make us look bad.

16. Input validation

For this reason, it is standard practice to implement input data validation. In its simplest form, that means checking if the request contains all required input attributes and do their values have expected types.

17. Input validation II

In our example, we expect the field's velocity and distance, which should both be positive real numbers.

18. Input validation III

This allows us to spot bad requests immediately

19. Input validation IV

and provide users with an informative error message, pinpointing the exact issue.

20. Input validation V

We call this the request model, and it must be included in the API documentation provided to the users.

21. Output validation

Similarly, we don't want to return any data our model produces blindly.

22. Output validation II

A bug in the code could suddenly result in the model returning the estimated time of arrival as a negative number.

23. Output validation III

This could crash our client's application and damage our reputation.

24. Output validation IV

We should therefore implement a reference specification of output values, called the response model.

25. Output validation V

That will allow us to validate all outputs before sending them to the client and generate informative error messages when needed.

26. Output validation VI

Now our clients know exactly what data we expect from them, and they know exactly what to expect from us in the case of both successful and unsuccessful requests.

27. Authentication

Lastly, we want to allow only specific clients to use our API.

28. Authentication II

For this we need an authentication layer even before our input validation. And even for authorized users, we want to control the number of requests they can make per unit of time.

29. Throttling

This is called API throttling.

30. FastAPI

In a Python-based ML stack, a fantastic open-source framework for building APIs is called FastAPI. It provides all essential features out of the box and lets us build and launch APIs in no time. Check out the footnote of this slide for its official documentation.

31. Let's practice!

APIs should be much less of a mystery for us now, so let us do a quick knowledge check to solidify the concepts we have just encountered.

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.