4 minutes to read - Mar 30, 2023

Cohere tutorial: Answering questions based on given data

VISIT
Cohere tutorial: Answering questions based on given data
Why should I use Cohere to answer questions based on data? To answer questions based on data is basically extracting information from text, which is a common task in language processing. And as Coheres large language models are an AI based technology, this can improve the understanding and further processing of the data.
Table of Contents
1Create a new project
2API key
3Updating index.js
4Updating the API
5Creating the perfect prompt for the work
6We can start our application
7Testing the application
8Conclusion

Register to Cohere if you haven't done it already https://dashboard.cohere.ai/register

Before I was creating the nextjs project I was trying out my concept on the Cohere playground. It is a great tool to test out your ideas and get a feeling for the API. You can find it here: https://os.cohere.ai/playground

Create a new project

I was using our repository that is public on GitHub. You can find it here: https://github.com/lablab-ai/nextjs-cohere-boilerplate

You can clone the repository and install all the dependencies with npm or yarn.

API key

Create a new API key for you project on the Cohere dashboard. You can find it here: https://dashboard.cohere.ai/api-keys The created API you need to place into the env file of the project. (Never share your API key with anyone, don't commit it to a public repository!)

Updating index.js

Now we will update the index.js file to be able to handle more input than one. I also renamed the states to make it more clear.

Update the fetch request, as we will change the endpoint name and we will send more information.

We will also need to update the frontend as we will have two inputs:

Updating the API

Let's rename the api file to answer.js and update the code.

we will need the question and the companyDate fields from the request body.

We will still use the generate model., but we will change the prompt of course.

Creating the perfect prompt for the work

First of all, we will show an example of a good prompt and a bad prompt. I am using a short company data text from wikipedia.

First time we are asking about the company and we place the correct answer in the answer field. Second time I am creating a bad question, )not regarding the company). I used the question "How are you?" and placed the answer "This question is not related to the company". This way the model will know how to react on a question that is irrelevant and not covered in the company data text. My prompt is the following:

At the end I am slicing the last part of the response to get the correct answer.

We can start our application

Let's start our application with yarn or npm run dev script.

Testing the application

For a test porpose, I placed a short description about Robert Bosch GmbH from wikipedia:

I asked question regarding the company that the text contain. "What are the main sectors?" Answer: mobility, consumer goods, industrial technology and energy and building technology.

This is correct! The model was able to understand the question and answer it correctly. Of course the prompt can be always finetuned to get better results. Because the model will try to answer everything.

Conclusion

Cohere is a great tool for creating a chatbot. It is easy to use and it is free for a limited number of requests.

Article source
loading...