WritingFireworks AIFireworks AIpublished Aug 11, 2026seen Jun 26

Function Call Vercel Fastapi Serp

Open original ↗

Captured source

source ↗
published Aug 11, 2026seen Jun 26captured Jun 28http 200method plain

Build Your Own Flight Recommendation System using FastAPI, SerpAPI, and Firefunction

GLM 5.2 is live! Opus-level intelligence at open-source rates. Pay per token on serverless. Try it today.

Blog

Function Call Vercel Fastapi Serp Build Your Own Flight Recommendation System using FastAPI, SerpAPI, and Firefunction

PUBLISHED 8/29/2024

Table of Contents Prerequisites Tech Stack High-Level Data Flow and Operations Steps Generate the Generate the SerpApi API Key Create a new FastAPI application

Install Dependencies

Define Data Models using Pydantic

Initialize FastAPI App

Integration with Firefunction V2

Create a Chat API endpoint

Use SerpApi to generate recommendations from Google Flights in real-time

Run FastAPI App Locally Create a new Next.js application

Install AI SDK

Build Conversation User Interface

Run Next.js Application Locally Conclusion

Table of Contents

Imagine you're planning a last-minute getaway. You've got a few days off, but you're not sure where to go, or how to get there. Instead of spending hours scouring the internet for travel options, wouldn't it be great if you could simply type in your preferences and instantly receive tailored suggestions? That's exactly what we'll be creating in this guide: a personalized recommendation system for flights using Fireworks , SerpApi , FastAPI , and Next.js . In this tutorial, we're going to create a Flight Recommendation System utilizing Firefunction-v2 to streamline the extraction of Departure and Arrival Airport Code (IATA Code) and the date or time of travel recommendations, as well as flight details, from dynamically received user inputs.

Prerequisites

You'll need the following: • Node.js 18 or later • A Fireworks account • A SerpApi account

Tech Stack

Following technologies are used in creating our RAG application: Technology Type Description FastAPI Framework A high performance framework to build APIs with Python 3.8+. Next.js Framework The React Framework for the Web. TailwindCSS Framework CSS framework for building custom designs. Fireworks Platform Blazing fast LLM Inference platform. SerpApi Platform A real-time API to access Google search results.

High-Level Data Flow and Operations

This is a high-level diagram of how data is flowing and operations that take place 👇🏻

When a user types in a query like “Flights from San Francisco to Dulles”, a tool call is generated as per the registered function spec in Firefunction v2. Further, they are used to query SerpApi for real-time results. The response is then returned to the user. Steps

Generate the Fireworks AI API Key

HTTP requests to the Fireworks API require an API Key. To generate this API key, log in to your Fireworks account and navigate to API Keys . Enter a name for your API key and click the  Create Key  button to generate a new API key. Copy and securely store this token for later use as  FIREWORKS_API_KEY  environment variable. Locally, set and export the  FIREWORKS_API_KEY  environment variable by executing the following command: 1 2 export FIREWORKS_API_KEY = "YOUR_FIREWORKS_API_KEY"

Generate the SerpApi API Key

HTTP requests to the SerpApi require an authorization token. To generate this token, while logged into your SerpApi account, navigate to the  dashboard , scroll down to Your Private API Key section, and click the Clipboard icon. Copy and securely store this token for later use as SERPAPI_API_KEY environment variable.

Locally, set and export the SERPAPI_API_KEY environment variable by executing the following command: 1 2 export SERPAPI_API_KEY = "YOUR_SERPAPI_API_KEY"

Create a new FastAPI application

First, let's start by creating a new project. You can create a new directory by executing the following command in your terminal window: 1 2 3 4

Create and move to the new directory

mkdir genai - functions cd genai - functions

Install Dependencies

Next, you can install the required dependencies by executing the following command in your terminal window: 1 2 3 4 5 pip install fastapi "uvicorn[standard]" pip install openai pip install fireworks - ai pip install google - search - results

The above command installs the required libraries to run ASGI Server, FastAPI, OpenAI, Fireworks AI, and SerpAPI in your Python project. Next, create a file main.py with the following code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 import os import json from typing import List from datetime import datetime

OpenAI

import openai

SerpApi

from serpapi import GoogleSearch

FastAPI

from fastapi import FastAPI from pydantic import BaseModel

Enable CORS utility

from fastapi . middleware . cors import CORSMiddleware

The above code imports the following: • os module to use the environment variables you’ve set earlier. • List to denote a list of elements of specific type. • json to parse string model outputs as JSON. • datetime module to get today’s date. • openai module to conveniently call OpenAI API. • serpapi module to scrape and parse search results from Google Search. • BaseModel class to define models of the request body FastAPI endpoints. • CORSMiddleware FastAPI middleware to enable Cross Origin Resource Sharing of FastAPI endpoints.

Define Data Models using Pydantic

To create the data types of request body in your FastAPI endpoints, append the following code in main.py file: 1 2 3 4 5 6 7 8 9

Class representing a single message of the conversation between RAG application and user.

class Message ( BaseModel ) : role : str content : str

Class representing collection of messages above.

class Messages ( BaseModel ) : messages : List [ Message ]

The above code defines two Pydantic models: • Message : a model that will store each message containing two fields, role and content . • Messages : a model that will store the input as a list of Message model.

Initialize FastAPI App

To initialize a FastAPI application, append the following code in main.py file: 1 2 3 4 5 6 7 8 9 10 11 12

Initialize FastAPI App

app = FastAPI ( )

Add CORS middleware

app . add_middleware ( CORSMiddleware , allow_origins = [ "*" ] , allow_credentials = True , allow_methods = [ "*" ] , allow_headers = [ "*" ] , )

The code above creates a FastAPI instance and uses the CORSMiddleware middleware to enable Cross Origin requests. This allows your frontend to successfully POST to the GenAI application endpoints to fetch responses to the user query, regardless of the port it is running on....

Excerpt shown — open the source for the full document.

Notability

notability 3.0/10

routine tutorial/integration post