Build An Audio AI App Transcripts
Chapter: Feature 4: Chat Q and A
Lecture: Asking LeMUR

Login or purchase this course to watch this video and the rest of the course contents.
0:00 It's time to go to our AI service and make it actually ask the chat. Now we created this little thing about creating a new chat,
0:09 but this is only really about juggling whether or not we've seen this before in the database. We're gonna have another function here called ask chat.
0:18 Similar information coming in, except for notice we're not passing in the prompt. So the user doesn't give us that.
0:24 We come up with it, you know, like right here as we have been. So the first thing we wanna do is make sure there's a transcript.
0:31 Now, I know the UI didn't exist, but we don't want people to maybe come around this sort of thing, somehow find a way to ask, you know,
0:40 abuse this function or as the app grows or whatever. So let's just make sure there is a transcript. All right, there we go.
0:46 So we're gonna need the transcript anyway. Remember, that's what we're providing to Lemur, but we're gonna check and make sure that it does exist.
0:55 Okay, the next thing we need to do is our prompt engineering and as you saw, this gets pretty interesting. Our prompt is gonna require the podcast
1:04 'cause like before, we wanna say the podcast is about this and so on. So we're gonna say podcast equals podcast service dot podcast by ID, podcast ID.
1:16 And again, one awaits it. Now, instead of me typing out this whole thing, let me just put the one I want in here and we'll discuss it.
1:23 Like before, you are an expert journalist. I'm gonna give you a transcript for this podcast. I want your answer. This is new to us.
1:33 I don't think we've done this sort of thing before, but I really like how it came out. I want your answer to include sources
1:40 and fragments from the transcript to support your response. The more open-ended we make it, it can start to make up things and imagine things.
1:49 And obviously there's many well-known cases of this hallucination as they call it. So I'm gonna say that I would both give it this instruction
1:56 to try to be real, and I'm gonna give it permission to say, I don't know. Rather than saying, well, they asked me a question, I have to have an answer.
2:05 Like, no, I don't want you to make anything up. It's okay to say, I don't know. All right? And then my question about this podcast episode is,
2:14 and then this is the question that the user has typed in. Pretty cool, right? So that's what we're gonna use. We'll see how well it comes out,
2:23 but actually it's gonna work pretty well. The next thing to do, now that we have the prompt,
2:28 is to see if this is already an asked and answered question. So we'll say chat equals, and if it's not new, we wanna save it for this user,
2:37 new chat, which is what we saw. All right, let's make sure I got this all in the right order here. Podcast ID, episode number, prompt, question, email.
2:48 Okay, and then how do we know if it's asked and answered? We'll say if chat.answer, return chat. We're good. We've already asked it. Here's the answer.
3:00 But if not, we're gonna need to ask Lemur. Now, we've done this four times already, I think, so, or multiple times at least.
3:08 So we're just gonna reuse basically the same code. We're gonna say, create the Lemur client. I'm gonna say, asking Lemur
3:14 about what the question is this time. Passing the prompt, the basic model, 2000.25, 25% variability.
3:24 And remember, we have to pass in the transcript text. And let's go up here. Let's look at this real quick. So what I did is I created a property
3:31 on our database record that does that join statement. Remember, we had a comprehension here that said w.text for w and self.words
3:41 and join that like this. We talked about that. Well, we're doing it here, but that's what that's about. I don't wanna keep,
3:50 in case we somehow change how we make this, I don't wanna have duplication of that little comprehension all over the place.
3:56 So here we have our response, and then it's just chat.answer equals response.response. And we've seen that there can be whitespace
4:06 in the beginning and other weird stuff. So strip. Now, let's save this into the database because that's always a good idea. Or in churn chat, right?
4:18 We didn't find one in the database before, so it's a new question in some way. So we're gonna do that and save it.

Talk Python's Mastodon Michael Kennedy's Mastodon