Creating a Q&A Bot Quickly in Google Colab with Makersuite and PaLM API

creating-a-q&a-bot-quickly-in-google-colab-with-makersuite-and-palm-api

To build Q & A bots, we must create the underlying language model ourselves. However, this can be a time-consuming process. An alternative approach is to leverage third-party services like Google’s PaLM API. This API allows developers to develop programs that require generative language quickly. It simplifies the process by providing access to Google’s language models with powerful language understanding capabilities.

PaLM (Pathways Language Model) API is a generative AI tool that allows developers to build on top of Google’s best language models. It provides a fast and secure way to access language models for text, data, and chat use cases. To use the PaLM API, we can access it through the MakerSuite UI. MakerSuite also provides an easy way to access our API keys.

MakerSuite is a prototyping environment that allows developers to quickly build generative AI prototypes without needing Machine Learning expertise. It offers intuitive tools to prototype generative AI ideas for text, data, and chat use cases. To start using MakerSuite, we can choose a template that matches our use case, provide our example, test our commands, and get our code. We can also explore the prompt gallery for examples and inspiration.

Designing The Prompt

For the first step, we need to design the prompt and the parameters to send a request to the PaLM API. We can read the documentation for PaLM API here, https://developers.generativeai.google/api, or the easier and faster method is to use Makersuite, https://makersuite.google.com. We will use Makersuite because we don’t want to waste too much time reading the full documentation.

When you go to Makersuite’s website, you are required to sign in with your Google account. After that, you will be brought to the home page, which should look like the image above. There are 3 types of prompts you can design here on Makersuite: text prompt, data prompt, and chat prompt.

Text prompts are used to design a simple text prompt where the prompt is not designed to contain a history of previous conversations, so the bot can’t remember the context of the conversation. Data prompts are used when we want the bot to give an output in a specific way or format. It does so by letting us give examples of input and outputs to be used as a reference on how the bot should respond. This technique is also known as one-shot or multi-shot prompting, depending on how many examples we give. The last one, the chat prompt, is similar to how data prompts let us give examples, but the difference is that the prompts are designed to be for conversations. Since we are making a simple Q&A bot, the text prompt is what we’ll be using.

The text prompt page will look like in the image above. Here, we can give the context to instruct the bot on how it should respond. On the bottom, we can change the parameters that the model will use. Since we just want a simple Q&A bot, we can set the prompt as “you are a helpful Q&A bot that answers questions from the specified source.”. Then we create two variables so we can give the source document and our question as the input. We can do that by clicking on the Insert test input on the upper right side.

We can then make a place for the source document and our question in the prompt. Don’t forget to also specify a place for the model to answer so it knows that the model should give the answer after.

Now, we need to get the code by clicking on the ‘Get Code’ button on the upper right corner of the page. It will generate a code we can use to access the PaLM API with all the context, examples, and parameters we set before with the UI.

Here, we will be options on which language we are using, some languages have a specific library that can be used to access PaLM API. For other languages that are not supported, we can just send an HTTP request with the cURL code as a reference. But since we’re using Python in CoLab, we can just copy the code for Python into CoLab.

If you pay attention closely, PaLM API requires us to have an API Key. We can easily get this key from makersuite. On the left side of the page there is a navigation bar where we can go to the get to the ‘Get API Key’ page.

Click on ‘Create API key in new project’ and copy and paste the generated API key into the ‘palm.configure()’ api_key parameter, replacing “YOUR API KEY”.

Making The Q&A Bot

Now we go coding, we want to follow the instructions to install the required library first with pip install. We can do it by running this code in a separate cell.

!pip install google-generativeai

Then, we want to make an input form so we have at least a simple UI to submit our source document and questions. We can do that by clicking on the vertical three-dot icon (kebab menu) in the upper right corner of the cell containing the code for response generation and selecting “Add a Form”. After that, click on the kebab menu again, and you will see a new option. Choose “Form” > “Add a form field.” Then, a form field creation menu will appear. Change the variable name to “source”, and click save. And then do it a second time, but name the variable “question”.

If you follow the instructions, everything should look like the image above. Delete the code that sets source and question with an empty string above the code that sets the prompt variable.

Now that our code is complete, you can run it by copying the contents of a document into the source input form and asking your question in the question form. Click the play button, and you will get a response just like that. Here is an example output from the Q&A bot we created. The source was copied from the Wikipedia page for ‘Dirt’.

As we can see below, the language model actually gave an output on an actual line from the source.

Conclusion

Building a Q&A bot from scratch can often be a daunting and time-consuming task, especially for those without extensive machine learning expertise. However, with Google’s PaLM API and MakerSuite, the process becomes significantly more accessible and efficient. This guide has walked you through each step of the process, from designing the prompt to implementing the code in Google Colab.

We utilized the text prompt feature to create a simple yet effective Q&A bot without requiring the bot to remember conversation context. MakerSuite made the process of generating the code effortless, and by using Google Colab, we were able to implement the bot without setting up a complex development environment.


Creating a Q&A Bot Quickly in Google Colab with Makersuite and PaLM API was originally published in Google Developer Experts on Medium, where people are continuing the conversation by highlighting and responding to this story.

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
make-with-makersuite-part-2:-tuning-llms

Make with MakerSuite Part 2: Tuning LLMs

Next Post
first-hacktoberfest-pr

First Hacktoberfest PR

Related Posts