How to implement real time stream response using Azure Functions .NET?

Andrii K 0 Reputation points
2024-02-08T13:01:46.8733333+00:00

I found a topic which describes my needs. The provided answer is great looking solution that I'm looking for on C#. Is there any possibility to do the same as provided answer for python but on C#?

import json
import logging
import time

import azure.functions as func

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    req_body = req.get_body()
    req_json = json.loads(req_body)
    question = req_json['question']

    prompt = """
    # my prompt here
    """

    # query_engine is the engine to send query to Azure OpenAI with reference to the file text indexing
    llamaIndexresponse = query_engine.query((prompt+question))

    # Set the content type to text/event-stream
    headers = {
        'Content-Type': 'text/event-stream',
        'Cache-Control': 'no-cache',
        'Connection': 'keep-alive'
    }

    # Send the headers to the client
    response = func.HttpResponse(headers=headers)

    # Send the response to the client in real-time using SSE
    response.streaming = True

    # Send the initial SSE event to the client
    response.write('event: message\n')
    response.write('data: {}\n\n'.format(json.dumps({'response': 'Processing...'})))

    # Send the SSE events to the client
    while not llamaIndexresponse.done:
        response.write('event: message\n')
        response.write('data: {}\n\n'.format(json.dumps({'response': str(llamaIndexresponse.response).strip()})))
        time.sleep(1)

    # Send the final SSE event to the client
    response.write('event: message\n')
    response.write('data: {}\n\n'.format(json.dumps({'response': str(llamaIndexresponse.response).strip()})))

    return response

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,911 questions
Developer technologies | .NET | Other
Developer technologies | C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. MikeUrnun 9,777 Reputation points Moderator
    2024-02-13T07:19:55.3933333+00:00

    Hello @Andrii K - Thanks for reaching out, and engaging us on the MS Q&A community. Platform-wise, the HTTP feature in Azure Functions is fronted with Azure Load Balancer which imposes 230 Timeout. This will interrupt the persistent connection that SSE needs to update the Client. As such, Azure Functions may not be the best choice for your scenario for now. Sorry to be the bearer of bad news here but I'll update you here if I find out anything new.


    Please "Accept Answer" if the answer is helpful so that others in the community may benefit from your experience.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.