Skip to content

Instantly share code, notes, and snippets.

@halfprice06
Created April 10, 2024 00:45
Show Gist options
  • Save halfprice06/01ebaacdf3873834b28a7f650347e1e7 to your computer and use it in GitHub Desktop.
Save halfprice06/01ebaacdf3873834b28a7f650347e1e7 to your computer and use it in GitHub Desktop.
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
final_output = nl2br_table(text_html)
yield f'<div hx-swap-oob="true" class="pl-11 pr-4 text-justify text-gray-700" id="{role}_message_{message_id}">{final_output}</div>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment