DEV Community

praveenr
praveenr

Posted on

1

Expose API Using FastAPI

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/

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay