Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Asking Questions With Context

00:00 You’ve built a workable review chain that you can extend through the templates that you’re working with. Now, let’s give that a try by actually pasting in a bunch more reviews and then asking a question or two to the model that it will answer based on the context.

00:17 So to have more context than just, oh, I had a great experience. Here are some generated patient reviews that I have inside of a dictionary. Let’s take a look at those.

00:29 We have 24 reviews that are in a list of dictionaries. And so, for example, one of those reviews

00:39 has a review_id, a visit_id, and then the text for the review, and then also a physician_name, patient_name, and hospital_name.

00:47 Now these are all generated results, so this is not real patient information. I’m going to pass this whole dictionary just as it is as context to when I am going to use invoke() to get a response from the LLM.

01:00 So let’s try that. I’m going to say review_chain.invoke() and then use the input dictionary. And in here we said we have these two placeholders "context".

01:11 It’s going to now point to the reviews list that holds a bunch of dictionaries. And then I’m also going to ask a question that the language model is going to answer based on the context, and the prompts that you set up earlier.

01:27 So this question, let’s start with our standard question: “Are there any positive reviews?”

01:40 And we’ll wait for the API call to complete, and here is the answer. “Yes, there are several positive reviews, here are some examples.” And then it actually mentions the specific reviews that had some positive things to say.

01:53 Okay, so this works great. Let’s actually wrap it in a print() call maybe, just so that we can read it a bit better. I’m going to do the same call again, and then we’re going to try a different question, but passing the same context.

02:05 So here you go, this is the output. Now I’m going to try a different question. Now I’m going to ask for a question about the dataset essentially. “How many reviews do you have access to?”

02:24 I have access to 24 patient reviews. Reviews 24. Great!

02:32 As you can see, you can now ask questions that consider the context, and you’re not just getting answers to these questions, but what LangChain is doing is it takes the inputs that they give, assembles the prompt, passes it to the model, and then also formats it so that as an output, you only get the string, not the entire AI message object.

02:53 In the next lesson, you’ll go over what you learned, and I’ll give you a couple of other resources that you can continue learning with.

Become a Member to join the conversation.