MediumGSoC Update: FastAPI Setup. As part of my Google Summer of Code and… | by Qixiang Wang | Medium
Sitemap

GSoC Update: FastAPI Setup

2 min readNov 1, 2024

--

As part of my Google Summer of Code and Sugar project, I have been working on setting up FastAPI, a modern web framework for building APIs with Python 3.7+ based on standard Python type hints. This update highlights the initial steps and configurations involved in setting up FastAPI, as well as some challenges and solutions I encountered along the way.

Press enter or click to view image in full size
FastAPI

In this phase, the primary objective was to deploy the large models behind several AI projects from Sugar onto a server. By wrapping these models with FastAPI, I aimed to construct APIs that enable seamless and efficient communication. This setup allows the Sugar platform to send requests directly to the corresponding endpoints and receive chatbot responses.

Main.py setting

For the main.py, I make changes as below:

1.Custom Documentation URL:

  • docs_url="/sugar-ai/docs": Configures the path for FastAPI's interactive API documentation. Developers can access it at /sugar-ai/docs.

2.Routers:

  • app.include_router(chat_router, prefix="/sugar-ai/chat"): Adds a router for chat-related endpoints with a prefix, organizing the API structure.
  • piggy_router is commented out but indicates potential for future expansion with a similar structure.

3.CORS Middleware:

Get Qixiang Wang’s stories in your inbox

Join Medium for free to get updates from this writer.

CORSMiddleware is added to handle cross-origin requests, allowing:

  • All origins (allow_origins=["*"])
  • Credentials
  • All HTTP methods
  • All headers

This setup ensures the API can be accessed flexibly from various clients and environments.

Router settings

For the router settings, I did the following.

1.Data Model with Pydantic:

  • class Question(BaseModel): Defines a data model using Pydantic, allowing automatic data validation and parsing. Here, it expects a query string from the request body.

2.Defining a POST Endpoint:

  • @router.post("/generate_answer"): Decorates the function to create a POST endpoint at /generate_answer. This enables handling of POST requests with JSON data structured according to the Question model.

3.Automatic Request Parsing and Validation:

  • def generate_answer(value: Question): FastAPI automatically parses and validates incoming JSON against the Question model, providing type-safe access to query.

4.Structured Response:

  • Returns a JSON response with a success flag and a result. In case of errors, it captures exceptions and returns a structured error message.
class Question(BaseModel):
query: str

@router.post("/generate_answer")
def generate_answer(value: Question):
try:
...
...
return {
'success': True,
'response': {
"result": adjusted_response
}
}

except Exception as e:
return {'success': False, 'response': str(e)}

This setup leverages FastAPI’s strengths in request validation, error handling, and automatic generation of interactive API documentation.

Future work

With the environment set up on our server, we can complete the full FastAPI configuration and model deployment. Following this, we will initiate requests from the Sugar platform to wrap up the project.

--

--