Skip to content

Instantly share code, notes, and snippets.

This is the generator function that streams back to the client with htmx SSE
async def process_and_stream_rag_response(user_query, search_terms, message_id, role="assistant"):
final_output = "" # Initialize an empty string to store the final output
async for text in retrieval_augmented_generation(user_query, search_terms):
# Convert the text from Markdown to HTML for each chunk
text_html = markdown.markdown(text, extensions=['tables', 'footnotes', 'fenced_code'])
# Apply nl2br_table to convert newlines to <br> tags in the HTML text