What is cohere-ai?
The cohere-ai npm package provides access to Cohere's natural language processing (NLP) models, enabling developers to integrate advanced language understanding and generation capabilities into their applications. This includes functionalities such as text generation, text classification, and language detection.
What are cohere-ai's main functionalities?
Text Generation
This feature allows you to generate text based on a given prompt. The code sample initializes the cohere-ai package, sets the API key, and generates text using the 'large' model with a prompt 'Once upon a time'.
const cohere = require('cohere-ai');
cohere.init('YOUR_API_KEY');
async function generateText() {
const response = await cohere.generate({
model: 'large',
prompt: 'Once upon a time',
max_tokens: 50
});
console.log(response.body.generations[0].text);
}
generateText();
Text Classification
This feature allows you to classify text into predefined categories. The code sample initializes the cohere-ai package, sets the API key, and classifies the given inputs using the 'large' model.
const cohere = require('cohere-ai');
cohere.init('YOUR_API_KEY');
async function classifyText() {
const response = await cohere.classify({
model: 'large',
inputs: ['I love programming', 'I hate bugs']
});
console.log(response.body.classifications);
}
classifyText();
Language Detection
This feature allows you to detect the language of given texts. The code sample initializes the cohere-ai package, sets the API key, and detects the languages of the provided texts.
const cohere = require('cohere-ai');
cohere.init('YOUR_API_KEY');
async function detectLanguage() {
const response = await cohere.detectLanguage({
texts: ['Hello, how are you?', 'Bonjour, comment ça va?']
});
console.log(response.body.results);
}
detectLanguage();
Other packages similar to cohere-ai
openai
The openai npm package provides access to OpenAI's GPT-3 models, which offer similar functionalities such as text generation, text classification, and more. OpenAI's models are known for their high quality and versatility, making them a strong alternative to Cohere's offerings.
huggingface
The huggingface npm package provides access to Hugging Face's Transformers library, which includes a wide range of pre-trained models for tasks like text generation, classification, and language translation. Hugging Face is known for its extensive model repository and active community support.
wit
The wit npm package provides access to Wit.ai's natural language processing capabilities, including intent recognition and entity extraction. Wit.ai is particularly strong in building conversational interfaces and is backed by Facebook.
Cohere TypeScript SDK


The Cohere Typescript SDK allows access to Cohere models across many different platforms: the cohere platform, AWS (Bedrock, Sagemaker), Azure, GCP and Oracle OCI. For a full list of support and snippets, please take a look at the SDK support docs page.
Documentation
Cohere documentation and API reference is available here.
Installation
npm i -s cohere-ai
Usage
import { CohereClient } from "cohere-ai";
const cohere = new CohereClient({
token: "YOUR_API_KEY",
});
(async () => {
const chat = await cohere.chat({
model: "command",
message: "Tell me a story in 5 parts!",
});
console.log(chat);
})();
Streaming
The SDK supports streaming endpoints. To take advantage of this feature for chat,
use chatStream
.
import { CohereClient } from "cohere-ai";
const cohere = new CohereClient({
token: "YOUR_API_KEY",
});
(async () => {
const stream = await cohere.chatStream({
model: "command",
message: "Tell me a story in 5 parts!",
});
for await (const chat of stream) {
if (chat.eventType === "text-generation") {
process.stdout.write(chat.text);
}
}
})();
Errors
When the API returns a non-success status code (4xx or 5xx response),
a subclass of CohereError will be thrown:
import { CohereClient, CohereError, CohereTimeoutError } from "cohere-ai";
const cohere = new CohereClient({
token: "YOUR_API_KEY",
});
(async () => {
try {
await cohere.generate();
} catch (err) {
if (err instanceof CohereTimeoutError) {
console.log("Request timed out", err);
} else if (err instanceof CohereError) {
console.log(err.statusCode);
console.log(err.message);
console.log(err.body);
}
}
})();
Beta status
This SDK is in beta, and while we will try to avoid it, there may be breaking changes between versions without a major version update. Therefore, we recommend pinning the package version to a specific version in your package.json file. This way, you can install the same version each time without breaking changes unless you are intentionally looking for the latest version.
Contributing
While we value open-source contributions to this SDK, the code is generated programmatically. Additions made directly would have to be moved over to our generation code, otherwise they would be overwritten upon the next generated release. Feel free to open a PR as a proof of concept, but know that we will not be able to merge it as-is. We suggest opening an issue first to discuss with us!
On the other hand, contributions to the README are always very welcome!