Skip to content

hacksu/fastapi-gemini_wrapper

Repository files navigation

Intro

Build your own free AI assistant that you can query from the web!

Step 1

Create a virtual environment, please. This is important. You need to do this otherwise I'll be mad

python -m venv .venv

Activate the virtual environment

windows

./.venv/Scripts/activate

mac/linux

. ./.venv/bin/activate

Step 2

Install dependancies

If you are cool and using uv

uv sync

If you are not cool and have not installed uv

pip install -r requirements.txt

Step 3

Let's build the app. Open main.py.

It has imports and a listener at the bottom, but nothing in the middle.

First, let's set up the app. Add this around line 5:

app = FastAPI()
client = genai.Client(api_key="YOUR API KEY HERE")

Step 4

Next, we need to define what the user sends us. We use Pydantic for this because it's awesome and validates everything for us.

Add this right after the code you just added (around line 9):

class UserReq(BaseModel):
    message: str

Step 5

Now for the meat of the app. The API route. This is where the magic happens.

Add this after the UserReq class (around line 13):

@app.post("/")
def api(body: UserReq) -> str:
    response = client.models.generate_content(
        model="gemini-2.5-flash",
        contents=body.message,
    )
    return response.text

We use gemini-2.5-flash because it's free and has nice rate limits. This is a POST request, so we need to define what the user sends us, the body of the request (of type UserReq, containing a message!)

Step 6

Finally, we need a way to run this thing. We use uvicorn for that.

What is Uvicorn? FastAPI is an ASGI framework (Asynchronous Server Gateway Interface). It's super fast, but it needs an ASGI server to actually talk to the network.

This already exists at the bottom of your file to start the server (around line 21):

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)

Step 7

Run it

uv run main.py
uvicorn main:app --host 127.0.0.1 --port 8000

Step 8

Test it

curl -X POST http://localhost:8000/ -H "Content-Type: application/json" -d '{"message": "What is 2+2?"}'

or on Windows :(

Invoke-RestMethod -Method Post -Uri http://localhost:8000/ -Body '{"message": "What is 2+2?"}'

About

Learn FastAPI by building a Gemini chatbot wrapper.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages