Simple SWA chatbot app with backend in Python

Paul-Louis Pröve 15 Reputation points
2023-10-19T09:52:30.4366667+00:00

I want to build is an internal chat application based on LLM APIs that we can use for different purposes. My criteria are:

  • Frontend in a JS framework
  • Backend in Python
  • Support for streaming responses
  • Async processing
  • Simple project structure and deployment

I think Static Web Apps should and need to be a good fit for this. However, that doesn't seem to be the case.

The first problem arises with a simple Azure Function app as a SWA backend, because streaming responses are not supported at least in Python (https://learn.microsoft.com/en-us/answers/questions/1334227/how-to-send-out-the-azureopen-ai-response-in-real see comments).

So, I decided to use FastAPI within my Azure Function, but it seems that this is not supported by SWA (https://stackoverflow.com/questions/74902458/deploy-azure-static-web-app-with-sveltekit-and-fastapi). I believe the problem comes from the fact that SWA requires a route prefix of "/api", whereas using FastAPI in functions requires the prefix to be empty.

As the SO link suggests, it does work if we separate the backend into it's own Azure Function app but that it seems to me that there should be an easier solution for this problem.

I'm interested in fixing the 2 problems above or hearing about a recommended way of achieving my criteria. What can I do?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,400 questions
Azure Static Web Apps
Azure Static Web Apps
An Azure service that provides streamlined full-stack web app development.
786 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Pramod Valavala 20,591 Reputation points Microsoft Employee
    2023-10-19T14:46:59.3833333+00:00

    @Paul-Louis Pröve I believe a simple solution to the FastAPI route problem would be to mount your functions as a Sub Application at the /api route of the main application (which won't have any endpoints). So, your code would be something like this

    import azure.functions as func 
    from fastapi import FastAPI, Request, Response 
    
    wrapper = FastAPI()
    
    app = FastAPI()
    
    @app.get("/openai") 
    async def return_http_no_body(): 
        # Your OpenAI Code Here
        return Response(status_code=200) # Your Streaming Response Here
    
    wrapper.mount('/api', app)
    
    app = func.AsgiFunctionApp(app=wrapper, 
                               http_auth_level=func.AuthLevel.ANONYMOUS)
    

    The /api path is required by SWA because they need a definite way to route requests to the static web app vs linked APIs, and route is not rewritten when forwarded.