Introduction to FastAPI and API Development

Welcome to the last lesson on building a FastAPI application for our short story generation service. In previous lessons, we've created essential components such as the PromptManager, StoryManager, and StoryGeneratorService. Now, we'll bring these components together by building a FastAPI application that will serve as the backbone of our service. An API (Application Programming Interface) allows different software components to communicate with each other. In our case, it will enable users to interact with our story generation service through HTTP requests.

FastAPI is a modern, fast (high-performance) web framework for building APIs. It is designed to be easy to use and to help you build robust APIs quickly and efficiently.

Creating a Basic FastAPI Application

Let's start by creating a simple FastAPI application. This will serve as the foundation for our API.

First, import the FastAPI class from the fastapi module and create an instance of it:

Here, app is an instance of the FastAPI class, which represents our web application.

Next, let's define a basic route that will render the homepage:

In this code snippet, @app.get("/") is a decorator that tells FastAPI to execute the index function when the root URL (/) is accessed. The function returns a simple welcome message as HTML.

Implementing API Endpoints

Now, let's implement the API endpoints that will handle story generation and retrieval.

We'll start by creating a POST endpoint to generate stories. This endpoint will accept user input and return a generated story.

  • @app.post("/api/generate_story"): This decorator defines a route for the /api/generate_story URL, specifying that it accepts POST requests.
  • await request.json(): This line extracts the JSON payload from the request.
  • data.get("user_input"): This retrieves the user_input field from the JSON data.
  • story_generator_controller.generate_story(user_input): This function call generates a story based on the user input.

Next, we'll create a GET endpoint to retrieve all generated stories.

  • @app.get("/api/get_stories"): This decorator defines a route for the /api/get_stories URL, specifying that it accepts requests.
Integrating the Story Generator Controller

The StoryGeneratorController is a crucial component that manages story generation and retrieval. Let's integrate it into our FastAPI application.

First, import the StoryGeneratorController and create an instance of it:

Now, use this instance within the API endpoints to handle story generation and retrieval, as shown in the previous section. This integration allows our FastAPI app to leverage the functionality provided by the StoryGeneratorController.

Running and Testing the FastAPI Application

To run the FastAPI application, use the following command in your terminal:

This will start the FastAPI development server, and you should see output indicating that the server is running.

To test the /api/generate_story endpoint using Python and the requests library:

This code sends a POST request with JSON data to the /api/generate_story endpoint and prints the response containing the generated story.

Summary and Next Steps

In this lesson, we've built a FastAPI application for our short story generation service. We covered the creation of a basic FastAPI application, implemented API endpoints for generating and retrieving stories, and integrated the StoryGeneratorController. You also learned how to run and test the application locally.

This is the last lesson of the course. As you move on to the practice exercises, you'll have the opportunity to reinforce your understanding of building a FastAPI application. Feel free to experiment with modifying the API and adding new features. Congratulations on reaching this point in the course, and keep up the great work!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal