Expose API Using FastAPI

praveenr - Aug 21 - - Dev Community

In previous blogs we have seen how to install neo4j, load data into it and query it using natural language. This will be the final blog in this series, we are going to create a simple fastAPI app to expose the setup as an API.

You can find the code here - https://github.com/praveenr2998/Creating-Lightweight-RAG-Systems-With-Graphs/blob/main/fastapi_app/app.py

from fastapi import FastAPI
from pydantic import BaseModel
from query_engine import GraphQueryEngine


# Pydantic model
class QueryRequest(BaseModel):
    query: str


app = FastAPI()


@app.post("/process-query/")
async def process_query(request: QueryRequest):
    query_engine = GraphQueryEngine()
    cypher_queries = query_engine.get_response(request.query)
    cypher_queries = query_engine.populate_embedding_in_query(request.query, cypher_queries)
    fetched_data = query_engine.fetch_data(cypher_queries)
    response = query_engine.get_final_response(request.query, fetched_data)
    return {"response": response}

Enter fullscreen mode Exit fullscreen mode
  • To run this file use the command
uvicorn app:app --reload
Enter fullscreen mode Exit fullscreen mode

Image description

  • In the terminal you'll be able to see the endpoint in my case it is http://127.0.0.1:8000/ add docs to it to open swagger - http://127.0.0.1:8000/docs

  • click on the Try it out option to get response for your question, enter you question key of your input json

swagger

swagger hit 1

swagger hit 2

CURL COMMAND

curl -X 'POST' \
  'http://127.0.0.1:8000/process-query/' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
  "query": "do you have headphones within the price of 25000"
}'
Enter fullscreen mode Exit fullscreen mode

 
Hope this helps... !!!

LinkedIn - https://www.linkedin.com/in/praveenr2998/

. . . . . . . . . . . . . .
Terabox Video Player