Build An Audio AI App Transcripts
Chapter: Feature 4: Chat Q and A
Lecture: Starting a New Chat

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So we can prepare for chat, but let's actually start chatting, right? The thing we wanna do is create some kind of conversation.
0:09 And if we look over real quick here in the database section, a lurking here has been this chat Q&A,
0:16 and it will store things like when was the chat created? What prompt did we give it? That's ideally invisible to the user. What question did they ask?
0:27 What answer did Lemur give back? What is the email address of the person who did that? And what podcast did we ask it of?
0:35 There are a couple of reasons we might wanna store the chat in the database. One for the user, more importantly, one for us.
0:44 They might wanna see their history. They might wanna see what have I asked before and be able to quickly navigate back.
0:49 We are not building that part of our UI, but you're free to extend it and keep running with that idea for sure.
0:57 The part for us though is this takes time. It makes our website slow and it costs us money. If somebody asked the same question again,
1:07 or potentially even if some other user, if this was a real high traffic site and there were really highly popular podcasts,
1:15 maybe they asked the same question as another person of the same show. So what we're gonna do is store this so that if the same questions asked again,
1:25 we don't recompute it. We just say same question, same answer, same content. It's not always true. Maybe we wanna expire that at some point
1:33 because maybe the LLM has upgraded and it's better, but we're gonna assume that it's not significantly changing over time. So that'll be great.
1:43 Let's go over here and we're gonna have an async def as we do called new chat. Now this is not directly used by the rest of our app,
1:55 but let's go ahead and put a reasonable bit of code because there's a few steps to this. All right, we're gonna say we want the podcast is that,
2:02 the episode number is that. What else do we want? We saw that we needed a prompt, which is a string. We need a question, which is a string.
2:12 And we need an email, which is a string. And we'll enforce that that has to exist. Remember, you can log into the website.
2:18 If this is being associated with a user and potentially because we don't want just arbitrary people to come along and just start asking questions,
2:26 you know, they have to have an account. Ideally, maybe they have a certain number of questions they can ask, so on, or a certain plan.
2:32 We wanna associate this like in theory with this email address, okay? So what are we gonna do? Let's start by just putting this into the database
2:42 and then I'll work on the reuse. So we'll say chat equals chat Q&A. That's the database thing that we just saw. And we can just start setting stuff.
2:52 So podcast ID equals podcast ID. Here we go. Let's look over here one more time. Remember, Pydantic is very particular.
3:01 If it says it's a solid string, it has to be passed in. So create a date, we'll just get set by the default factory. And now, that's great.
3:10 This we're passing in. This we don't have yet. This is the question we're starting. We're gonna ask Gleamer, then we'll have an answer.
3:17 And these three things, perfect. It will be stored, by the way, in the chat history database or in the chat history collection within the database.
3:27 Okay, so there's this. And then we want to save it to the database. So we'll await chat save, return chat.
3:36 And then over here, let's give it a little more room. Chat Q&A. This looks great, right? Well, it's going to create a new chat for every conversation.
3:50 Even if the same person is asking the same question. So let's do this. We'll say existing chat equals, and let's do a query.
4:01 It's trying to help us, but we don't exactly want that. We'll say await chat QA dot find one.
4:07 And we'll say, just start passing in parameters like this, like the podcast ID must match and the episode number must match.
4:20 We're not going to say that the email does, however, because maybe someone else has asked this question and that's fine. So first of all, let's see,
4:29 is this actually their question? So we'll say existing chat is not none and existing chat dot email, close email, equals email. Then you know what?
4:41 They're asking the same question again. We're out. If it's not, let's create a new one, but we can also ask if, I think we could just,
4:51 yeah, we'll do it like this. We'll say if existing chat and existing chat dot answer, and let's go ahead and hit the answer
5:03 is set to be equal to this, okay? And we'll just save it and say, you know what? We've migrated this question over to you as well.
5:12 Let's be a little more careful here. Let's call this first one. Let's call this their chat for a second. And let me put one more parameter like this.
5:27 So why did I change that? Well, we could end up with lots of the same question and answer, you know, theoretically,
5:41 and I wasn't doing enough testing here. So let's go ahead and just say, look, if it's exactly there as before, that's great.
5:49 And down here, we'll say existing chat like this. So we know that it's not their email like that. All right.
6:03 That way we don't get duplicates for certain people for not checking super careful. All right, that should do it.
6:08 It's two database queries, but you know what? MongoDB is blazing fast. It doesn't matter. All right, so that maybe seemed like more work
6:16 than you might've guessed, like put this thing in the database, but remember, we're really trying to optimize
6:21 the speed of our website and save this in the database for that user without duplication and potentially reuse answers across users.
6:30 And so that's what this is hopefully accomplishing for us. We haven't got a chance to test it yet 'cause we need one more layer in our app
6:40 where basically the request can come in to the view and this will come down into the section where we run it, right?
6:48 Of course, we also have to ask Lemur to get the answer somewhere along the way.

Talk Python's Mastodon Michael Kennedy's Mastodon