Building a new ai api server in python

2023-04-16

Started a new project.

Set up a virtual environment

I think this is the standard procedure for Python project. I’m using Python 3.10.6 which already includes virtual environments

I’m going to call my new ai project kai

mkdir kai
cd kai
python -m venv kai-env

Whenever I revisit this project to work on it, I have to remember to load the virtual environment.

source kai-env/bin/activate

The virtual environment will help contain all the dependencies for this project so they won’t conflict with other python projects.

Install FastApi and Uvicorn

FastAPI is the framework for building the api and Uvicorn is to run the server.

pip install fastapi uvicorn

Initial project files

Create a new Python file called main.py .

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
    return { "message": "Hellow, world"}

Test run

Run the app instance from the main module and reload on changes

uvicorn main:app --reload

EZ PZ.