Build your own free AI assistant that you can query from the web!
Create a virtual environment, please. This is important. You need to do this otherwise I'll be mad
python -m venv .venvActivate the virtual environment
windows
./.venv/Scripts/activatemac/linux
. ./.venv/bin/activateInstall dependancies
If you are cool and using uv
uv syncIf you are not cool and have not installed uv
pip install -r requirements.txtLet'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")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: strNow 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.textWe 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!)
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)Run it
uv run main.pyuvicorn main:app --host 127.0.0.1 --port 8000Test 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?"}'