Skip to content

Instantly share code, notes, and snippets.

@patrickkwang
Last active February 16, 2021 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patrickkwang/56f7992fec75cc6b17f3265204a7c0b0 to your computer and use it in GitHub Desktop.
Save patrickkwang/56f7992fec75cc6b17f3265204a7c0b0 to your computer and use it in GitHub Desktop.
Learn how to use reasoner-pydantic and FastAPI to set up a TRAPI service with just 6 lines of code!

reasoner-pydantic & FastAPI

Set up a TRAPI web server in 6 lines of Python!

What you get

  • a fast web server
    • optionally asynchronous using asyncio
  • auto-generated OpenAPI schema and Swagger UI
  • request and response validation
  • full TRAPI compliance, guaranteed1

How-to

1. Install Python dependencies

pip install fastapi reasoner-pydantic uvicorn

2. Write code

server.py:

from fastapi import FastAPI
from reasoner_pydantic import Query, Response

APP = FastAPI(title="TRAPI service")

@APP.post("/query", response_model=Response)
def trapi_operation(query: Query) -> Response:
    # perform operation on query.message...
    return Response(message=query.message)

3. Run

Using uvicorn:

uvicorn server:APP --host 0.0.0.0 --port 8080

Go to your Swagger UI at http://localhost:8080/docs

How it works

First, we import the FastAPI class and the two top-level TRAPI components: Query and Response.

from fastapi import FastAPI
from reasoner_pydantic import Query, Response

Then we initialize the FastAPI application object.

APP = FastAPI(title="TRAPI service")

The FastAPI.post decorator declares the decorated function as an API endpoint handler. Here we assign a handler to the /query endpoint, and provide the model for its response.

@APP.post("/query", response_model=Response)

The handler function uses type hints to describe its input and output. The pydantic models can be used directly or can be easily converted to/from dictionaries.

def trapi_operation(query: Query) -> Response:
    # perform operation on query.message...
    return Response(message=query.message)

Tools

  • pydantic: "Data validation and settings management using Python type hinting. Fast and extensible, pydantic plays nicely with your linters/IDE/brain. Define how data should be in pure, canonical Python 3.6+; validate it with pydantic."
  • FastAPI: "FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints."
  • reasoner-pydantic: pydantic models reflecting the TRAPI (v1.0.3) components

How to get help

Submit questions here.

Submit bug reports here.

Next steps

For a more complete example using reasoner-pydantic & FastAPI, see filter_results_top_n. This includes:

  • OpenAPI request and response examples
  • required SmartAPI metadata (x-translator, etc.)
  • Docker-ization
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment