Cohere tutorial: Build a product description generator API with Cohere

Tuesday, August 09, 2022 by Flafi
Cohere tutorial: Build a product description generator API with Cohere

🎯 Journey's End: Mastering Cohere for Product Descriptions

By the time you've reached this point, you've successfully built a product description generator API using Cohere. This API is a powerful tool for generating descriptions for your own products. This tutorial is designed for those with a basic understanding of Javascript/NodeJS. If you're new to this language, consider exploring some resources before diving in. The journey doesn't end here, so stay tuned for more Cohere tutorials!

Tutorial accompaniment image

We will be using xlarge model from Cohere.

Getting Cohere API Key

You can get one by signing up https://app.cohere.io/dashboard. Visit the Cohere dashboard to retrieve your API key. If you haven't previously connected a session, you should see it in the main screen. Otherwise, you can find it in the settings page.

Clone the Express boilerplate from Github

In this tutorial we will be using an express boilerplate that will make our lives easier.

Copy this repository Express Boilerplate onto your computer and add to your own repositories

Running the project locally

  1. Install the dependencies with yarn or npm
  2. Run the server with yarn dev or npm dev

Add the Cohere API key to the .env file

Create a .env file in the root of the project and add your API key to it. Remember never share your API key with anyone. COHERE_API_KEY={YOUR_API_KEY}

Create Routing for the API

  1. Create routes folder in the root of the project
  2. Create description.js file in the routes folder
  3. Add the following code to the description.js file to have a working router
const express = require("express");
const router = express.Router();

router.post("/", async (req, res) => {
  res.send("Hello World!");
});

module.exports = router;
  1. Add the route to the index.js file in the root of the project
  2. Add the import to the top of the file `const description = require("./routes/description.js");
  3. Add the route under the app.get route app.use("/", description);

Create generator function

  1. Create a folder in the root of the project called lib
  2. Create a file name called description-generator.js in the lib folder
  3. Add the following code to the description-generator.js file to have a working generator function
require("dotenv").config();
const cohere = require("cohere-ai");
cohere.init(process.env.COHERE_API_KEY);

const generator = async ({ product, keywords }) => {
  const response = await cohere.generate({
    model: "xlarge",
    prompt: `Given a product and keywords, this program will generate exciting product descriptions. Here are some examples:\n\nProduct: Monitor\nKeywords: curved, gaming\nExciting Product Description: When it comes to serious gaming, every moment counts. This curved gaming monitor delivers the unprecedented immersion you need to play your best.\n--\nProduct: Surfboard\nKeywords: 6”, matte finish\nExciting Product Description: This 6” surfboard is designed for fun. It\'s a board that almost anyone can pick up, ride, and get psyched on.\n--\nProduct: ${product}\nKeywords: ${keywords}\nExciting Product Description:`,
    max_tokens: 50,
    temperature: 0.8,
    k: 0,
    p: 1,
    frequency_penalty: 0,
    presence_penalty: 0,
    stop_sequences: ["--"],
    return_likelihoods: "NONE",
  });
  return response.body.generations[0].text;
};
module.exports = generator;

Use the generator function in the POST route

Update the description.js file with the following code to use the generator function in the POST route

const express = require("express");
const router = express.Router();
const generator = require("../lib/description-generator");

router.post("/", async (req, res) => {
  const { product, keywords } = req.body;
  const description = await generator({ product, keywords });
  res.send(description.slice(0, -3));
});

module.exports = router;

Run the project locally and test it with Postman or Insomnia or whatever you prefer :)

Send a Post request to localhost:3000/description with these keys, product and keywords

{
  "product": "LG Gamin monitor",
  "keywords": "curved, gaming, 120hz, 4k"
}

🌟 Parting Thoughts: Changing the World with AI

We trust you found value in this straightforward tutorial. Feel free to tweak the prompt and have some fun with it! Don't miss our upcoming AI Hackathons and seize the opportunity to supercharge your application through our AI slingshot program.

Let's change the world with AI, one application at a time!

Discover tutorials with similar technologies

Upcoming AI Hackathons and Events