From the course: Vector Databases in Practice: Deep Dive
Parsing query responses
- [Instructor] Our web app is now connected to the Weaviate instance and able to send queries using the user's inputs. Now let's pass the response back through the front end. Each response to a search will have an object attribute containing a list of objects, so we can use that to loop through the actual response like this. We can then retrieve each property through the properties attribute within each object. Now the Python client returns all the properties by default as a dictionary, so we can access each property as a key, like rating here and movie_id. Next, we need to retrieve the synopsis for each movie. In the query above, we requested the reference data through the hasSynopsis reference property. As a result, we'll have the requested data available through the references attribute within the response. And this is how you would fetch that data. Hopefully, this syntax is starting to look a little bit familiar. We fetch a specific reference through the references attribute using its property name, in this case, hasSynopsis. The corresponding value then is like a response on its own, giving a kind of recursive structure. So to drill down, we can do that the same way as before. The response, or in this case, the reference in this case will have an objects attribute. Now we know that that's going to contain a list, and we know that each movie has just a one synopsis. So we'll just grab the first object. And within that synopsis, we can grab the body property within the properties attribute. And that will be our synopsis. And since we've assigned that synopsis text into this variable, we can just display it just like we've done before. The next task is to add details to the movie_tab. Remember that this is where the user will enter a particular movie row ID and get details. So let's get rid of our placeholder data first. To get the movie data, we can rely on the movie's UUID. Remember that when we imported data, we generated the movie's UUID from the row ID. So let's do the same thing using the user's input. And then this movie_uuid can be fed into the fetch_object_by_id method, which lets us grab just the one specific object from Weaviate based on its object ID. Remember that the properties are returned by default, so we don't need to specify anything there. The return_references on the other hand are not, of course. So let's fetch the synopsis again using the exact syntax as before. So that should be all of our data, which means we can pass it to the placeholder values. In some of our other queries, we've had a response with multiple objects in it, but here, we've just fetched the one object, which means that the object itself called movie here will have a properties attribute with a dictionary so it can extract them just like I've shown here. And if I now run this app again, these features should be fully functioning. So our users can search our movie database like this, and if they like a particular movie, they can look into its details here, get the movie ID, and then enter into the Movie details tab, which we've just put together. It's pretty neat, right? So that's our movie search and details exploration tabs done. There's just a one more feature to add, which is possibly the most exciting. I'm talking, of course, about adding our recommendation feature using retrieval-augmented generation. We'll tackle that next.