3. Implement predictive support functionality
In the final video, we create and test our prompt and hook it up to our app to implement the predictive support functionality.
Write and test the prompt in AI Studio
Visit aistudio.google.com to write and test the prompt. You can find the FAQs in the prompt.js
file.
You can select a piece of text and then click on the {{ }} Test input
button in AI Studio to make this an input. You will then be able to test your prompt with different inputs. That's a very neat feature!
Updating the prompt in prompt.js
Now, open the prompt.js
and replace it with the following code:
export function getPrompt() {
// Gather context
const h1 = document.querySelector("h1")?.textContent;
const currentPage = window.location.pathname;
// Debug context
console.log({ h1, currentPage });
// Return the prompt
return `You are a predictive support agent.
Based on the user's context, you will show the most relevant Question from the FAQ list below.
Take the additional context into consideration. If there's more than one relevant question, choose the one that is more relevant based on the context.
If there are no relevant questions, do not create a new question and instead answer with the generic: How can we help you today?
---
FAQs:
How can I reset my password?
Answer: You can reset your password from the top-right menu.
Context: profile.html page
How can I ask for a refund?
Answer: You can ask for a refund on the order page from the bottom-right corner.
Context: orders.html page, only if the user has placed at least 1 order.
How do I place an order?
Answer: you can place an order by clicking on the Add order button.
Context: orders.html page
How can I change my email address?
Answer: You can change your email address from the top-right menu.
Context: when logged in and browsing the profile.html page
---
User context:
Current page: ${currentPage}
h1: ${h1}
`;
}
Make sure to copy your prompt from AI Studio and paste it in prompt.js
when you're done.
Then, replace the two test inputs with the following:
const h1 = document.querySelector("h1")?.textContent;
const currentPage = window.location.pathname;
You can now interpolate these two variables into the prompt.
Interpolating content into your prompt opens up the possibility of prompt injection. Gemini has built-in security features against that, but make sure you test your prompt with various scenarios. If possible, it's best to sanitize content before you interpolate it. For example, you can limit h1
's to a specific length and make sure the pathname refers to an actual url on your site.Implementing predictive support
Now that we have finalized the prompt, we can use it in the main.js
by calling the getPrompt()
function. This function was already imported at the top of the file.
Also, make sure to move your code into the event listener of the helpButton
.
helpButton.addEventListener("click", async () => {
helpBody.textContent = "Loading...";
const result = await model.generateContent(getPrompt());
helpBody.textContent = result.response.text();
});
Congratulations! You now have just implemented predictive support functionality in your app.
This course was developed by Jad Joubran – author of Learn JavaScript.
With thanks to Maud Nalpas for reviewing the content of this course.