Welcome back to part 2 of our series on building an  AI-powered search engine! In the previous tutorial, we created a working command-line app that co

How to build an AI search engine (Part 2) - by Charlie Guo

submited by
Style Pass
2024-10-11 12:30:03

Welcome back to part 2 of our series on building an AI-powered search engine! In the previous tutorial, we created a working command-line app that combined search results with an LLM to generate comprehensive, cited answers to user queries. This time, we're going to take that backend and create a user-friendly web interface.

The first major change we need to make is converting our Python script into a proper web application using Flask. If you don't already have it installed, run:

Besides, we've also lost the helpful "thinking" messages that helped break up the wait time in the command-line interface, since our print statements don't ever make it to the user.

To solve said problems, we can start working with streaming responses with Flask's built-in stream_with_context helper. We're also going to move the bulk of our ask route into a new stream_response function, to easily replace our print statements with yield:

I hadn't used yield in this way before, but it provides an elegant way to send progress updates to the user as the LLM is working. Each "thinking" message can be sent individually, helping to keep the user engaged while they wait for the final response.

Leave a Comment