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/

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay