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

Monday, December 12, 2022 by jakub.misilo
Cohere tutorial: How to use Cohere to retrieve data from a table

Mastering Cohere and FastAPI: A Comprehensive Tutorial for AI Enthusiasts

Tabular data, a prevalent format for data storage and exchange, plays a vital role in various applications like data warehouses, data lakes, and data marts. In this tutorial, we'll explore the creation of a FastAPI application that seamlessly employs Cohere to extract data from tables. This technique is invaluable for developing applications like bots, dashboards, and data explorers that rely on data retrieval.

After honing these skills, don't settle; the AI landscape is expansive and ever-changing, beckoning continual learning. To further practice your Qdrant abilities, consider constructing an API for application data indexing, new record additions, and searches. FastAPI's high-performance, user-friendly framework makes it the ideal tool for this endeavor.

Ready to push the boundaries of your expertise? Use your newly-acquired skills to design an AI-driven application. The upcoming AI Hackathons offers the ideal stage for your ambitions. Those events unites a passionate community of innovators and creators determined to shape the future with AI, providing an unmatched learning, networking, and growth platform.

Spot a problem in your environment and build a Cohere app to address it. Join our thriving network of pioneers and visionaries and make your mark in the AI realm! And while you're at it, don't miss our diverse lineup of events!

🎬 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:


Now let's install necessary libraries:

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

Now we can create an 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

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
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
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:

async def data_retreival(request: str):
    prompt = f'{COMPANY_DATA}\n\n{EXAMPLES}\nQ: {request}'
    response = co.generate(
    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

πŸ“š Wrapping Up FastAPI and Cohere combine to form a powerful duo in the realm of AI applications. This tutorial has hopefully illuminated the path to harnessing these tools, enabling you to retrieve data with ease.

FastAPI's simplicity and Cohere's robust capabilities make them an ideal pair for any developer looking to delve into the world of AI. Whether you're a seasoned pro or a budding enthusiast, these tools can help you build applications that are both efficient and effective.

If you're eager to learn more about Cohere, be sure to visit’s dedicated website. It's a treasure trove of information, providing insights and tutorials to help you get the most out of Cohere.

And don't forget about the AI Hackathons! These events are a fantastic opportunity to put your new skills to the test. You can check our upcoming Hackathons on our event page. It's a chance to learn, innovate, and maybe even create something groundbreaking.

So, what are you waiting for? Dive in, start exploring, and see what you can create with FastAPI and Cohere!

Discover tutorials with similar technologies

Upcoming AI Hackathons and Events