Cohere tutorial: How to use Cohere to retrieve data from a table

2022-12-12T09:17:40.398Z by jakub.misilo
Cohere tutorial: How to use Cohere to retrieve data from a table

Tabular data is a common format for storing and exchanging data. It is used in many applications, including data warehouses, data lakes and data marts. In this tutorial, we'll build a FastAPI application that uses Cohere to retrieve data from a table. It can be useful for building applications that need to retrieve data - like bots, dashboards or data explorers.

🎬 Introduction

Of course we need to create a new project first. Let's create a new project using terminal:

mkdir cohere-tab-Data
 
cd cohere-tab-data
python3 -m venv venv
source venv/bin/activate

We have to create .env file and add our Cohere API key to it:

COHERE_API_KEY=<COHERE API KEY>

Now let's install necessary libraries:

pip install python-dotenv
    pip install --upgrade cohere
    pip install fastapi[all]

Now we can create an app.py file and write code!

πŸ“ Let's code!

First, we need to import libraries:

import os
import cohere
from dotenv import load_dotenv
from fastapi import FastAPI
 
load_dotenv()

Then we need to create a FastAPI application and set up a Cohere client:

app = FastAPI()
 
co = cohere.Client(os.getenv('COHERE_API_KEY'))

I will define some example data that we will use in this tutorial:

# Example data
COMPANY_DATA = '''
ID | Name | Position           | Department | Salary
---|------|--------------------|------------|--------
1  | John | Product Manager    | Sales      | 100000
2  | Emmy | UI/UX              | Design     | 90000
3  | Bob  | Software Developer | IT         | 120000
4  | Jane | Data Scientist     | IT         | 160000
5  | Mike | Software Developer | IT         | 130000
6  | Alex | Data Engineer      | IT         | 140000
7  | Bill | Software Developer | IT         | 120000
8  | Kate | Business Analyst   | Sales      | 110000
9  | Mark | UI/UX              | Design     | 95000
10 | Lisa | Social Media       | Marketing  | 80000
11 | Matt | Ads Manager        | Marketing  | 85000
12 | Tom  | SEO                | Marketing  | 80000
'''
 
# Some example queries for model
EXAMPLES = '''
Q: What is the average salary of the Design department?
A: 92500.0
---
Q: Who is the highest paid employee?
A: Alex
---
Q: Who is working in the IT department?
A: Bob, Jane, Mike, Alex, Bill
---
Q: How many employees are there in the Marketing department?
A: 3
---
'''

Now we can create request handler for our FastAPI application:

@app.get('/')
async def data_retreival(request: str):
   
    prompt = f'{COMPANY_DATA}\n\n{EXAMPLES}\nQ: {request}'
    response = co.generate(
        model='xlarge',
        prompt=prompt,
        max_tokens=5,
        temperature=0.75,
        stop_sequences=['--'],
    )
 
    return response.generations[0].text.strip()

Okay, now we can run our application:

uvicorn app:app --reload

πŸŽ‰ Testing

Now we can test our application. Let's try to ask some questions:

Q: Who is the highest paid employee?
A: Jane
 
Q: How many employees are there in the IT department?
A: 5
 
Q: Who is the highest paid employee in Sales Department?
A: Kate

πŸ“š Conclusion

FastAPI is super easy to use and Cohere is a great tool for building applications that need to retrieve data. I hope you enjoyed this tutorial and learned something new. If you have any questions, feel free to ask them in the comments below. And if you want to learn more about Cohere, visit lablab.ai’s dedicated website. And if you want to check what you have learnt, AI Hackathons will be a great place to verify it! You can check our upcoming Hackathons on our event page. See you there!

Discover tutorials with similar technologies

Upcoming AI Hackathons and Events