Cohere AI is a technology company focusing on large language model (LLMs) technologies for enterprise use cases. It provides LLM-based solutions that help customers understand, generate, and work with human language. Through its services, organizations can automate customer support, generate content, and extract insights from large volumes of text.
Cohere AI's technology is based on LLMs similar to those used in Google's BERT or OpenAI's GPT. Their models are trained on diverse datasets that encompass a range of language use, allowing them to perform various NLP tasks with high accuracy and context understanding. They can be easily fine tuned for specific use cases, and provide enterprise-grade capabilities including the ability to deploy the models on-premises.
This is part of a series of articles about LLM Platform.
Cohere offers three primary models: Command, Embed, and Rerank.
Cohere's Command models are designed to follow user instructions to generate relevant text, making them suitable for various conversational applications. The different versions of the Command models are:
Note: Command and Command Light are legacy models, and Cohere recommends using the newer R and R+.
Cohere's Embed models are specialized for generating text embeddings, which can be used to determine semantic similarity, classify text, or perform other analysis tasks. The key models include:
Cohere also offers its previous embedding model, Embed v2.0, with the same variants as above.
Cohere's Rerank models are used to reorder search results or document lists to improve relevance based on specific criteria. It is primarily used for semantic search use cases. The main models include:
Cohere also offers its previous embedding model, Rerank v2.0, with the same variants as above.
Cohere offers three pricing tiers:
The Cohere Toolkit is a collection of pre-built components designed to streamline the development and deployment of retrieval augmented generation (RAG) applications. It reduces the time needed to bring RAG solutions to market, allowing developers to go from concept to deployment in a few minutes.
The Toolkit is divided into two main components:
Here's an image that shows how these different components work together:
Source: Cohere
The Cohere Chat API allows developers to interact with Cohere's language models via a conversational interface. This tutorial will guide you through setting up and using the Chat API with code examples in Python. The code in this tutorial is adapted from the Cohere documentation.
To use the Chat API, you need to have a Cohere account and an API key. You can install the Cohere Python client using
pip install cohere
pip3 install cohere
Here's how to generate text using the Chat API. This example demonstrates how to prompt the model to create a blog post title about API design.
import cohere # Start the Cohere client using the API key co = cohere.Client(api_key="<MY API KEY>") # Send the model a message to retrieve a response response = co.chat( model="command-r-plus", message="Generate a title for an article about web design. Output only the title text." ) # Print out the generated text print(response.text)
The expected output should be something like this:
A typical response from the Chat API includes several fields:
{ "text": "The Science of Web Design: How to Build an Engaging Web Page", "generation_id": "dd84b9fc-928d-4e17-4819-8ffde9366947", "chat_history": [ { "role": "USER", "message": "Generate a title for an article about web design. Output only the title text." }, { "role": "CHATBOT", "message": "The Science of Web Design: How to Build an Engaging Web Page" } ], "finish_reason": "COMPLETE", "meta": { "api_version": { "version": "1" }, "billed_units": { "input_tokens": 16, "output_tokens": 14 }, "tokens": { "input_tokens": 16, "output_tokens": 14 } } }
Key fields include:
You can maintain a conversation by including the
chat_history
import cohere co = cohere.Client(api_key="<MY API KEY>") # The original user message message = "What are generative models?" # Send a message alongside the chat history response = co.chat( model="command-r-plus", chat_history=[ {"role": "USER", "text": "Hello, my name is George!"}, {"role": "CHATBOT", "text": "Hello George! How can I help you?"}, ], message=message ) print(response.text)
The response should look like this:
You can build the chat history dynamically during a conversation. Here's a script that interacts with the user for multiple turns:
import cohere co = cohere.Client(api_key="<MY API KEY>") chat_history = [] max_turns = 10 for _ in range(max_turns): # Get the user input message = input("Send a message to the model: ") # Generate the response using the chat history response = co.chat( message=message, temperature=0.3, chat_history=chat_history ) answer = response.text print(answer) # Add a message and an answer to the chat history user_message = {"role": "USER", "text": message} bot_message = {"role": "CHATBOT", "text": answer} chat_history.append(user_message) chat_history.append(bot_message)
The response should look like this:
An alternative to passing the full chat history is to use a
conversation_id
import cohere co = cohere.Client(api_key="<YOUR API KEY>") # Initiate a conversation with the user-defined ID response = co.chat( model="command-r-plus", message="The secret number is 'three', remember this.", conversation_id='user_defined_id_1', ) print(response.text) # Use the same ID to continue the conversation response2 = co.chat( model="command-r-plus", message="What is the secret number?", conversation_id='user_defined_id_1' ) print(response2.text) # "The secret number is 'three'"
The output should look like this:
Note that
conversation_id
chat_history
Note: To get cleaner output, add the
-W ignore
To download GPTScript visit https://gptscript.ai. As we expand on the capabilities with GPTScript, we are also expanding our list of tools. With these tools, you can build any application imaginable: check out tools.gptscript.ai and start building today.
Randall Babaoye is a full stack software engineer with experience in application development and DevOps.