Skip to main content

Cohere

Legacy

Cohere has marked their generate endpoint for LLMs as deprecated. Follow their migration guide to start using their Chat API via the ChatCohere integration.

caution

You are currently on a page documenting the use of Cohere models as text completion models. Many popular models available on Cohere are chat completion models.

You may be looking for this page instead.

This will help you get started with Cohere completion models (LLMs) using LangChain. For detailed documentation on Cohere features and configuration options, please refer to the API reference.

Overview

Integration details

ClassPackageLocalSerializablePY supportPackage downloadsPackage latest
Cohere@langchain/cohereNPM - DownloadsNPM - Version

Setup

To access Cohere models you’ll need to create a Cohere account, get an API key, and install the @langchain/cohere integration package.

Credentials

Head to cohere.com to sign up to Cohere and generate an API key. Once you’ve done this set the COHERE_API_KEY environment variable:

export COHERE_API_KEY="your-api-key"

If you want to get automated tracing of your model calls you can also set your LangSmith API key by uncommenting below:

# export LANGCHAIN_TRACING_V2="true"
# export LANGCHAIN_API_KEY="your-api-key"

Installation

The LangChain Cohere integration lives in the @langchain/cohere package:

yarn add @langchain/cohere @langchain/core

Instantiation

Now we can instantiate our model object and generate chat completions:

import { Cohere } from "@langchain/cohere";

const llm = new Cohere({
model: "command",
temperature: 0,
maxTokens: undefined,
maxRetries: 2,
// other params...
});

Invocation

const inputText = "Cohere is an AI company that ";

const completion = await llm.invoke(inputText);
completion;
Cohere is a company that provides natural language processing models that help companies improve human-machine interactions. Cohere was founded in 2019 by Aidan Gomez, Ivan Zhang, and Nick Frosst.

Chaining

We can chain our completion model with a prompt template like so:

import { PromptTemplate } from "@langchain/core/prompts";

const prompt = new PromptTemplate({
template: "How to say {input} in {output_language}:\n",
inputVariables: ["input", "output_language"],
});

const chain = prompt.pipe(llm);
await chain.invoke({
output_language: "German",
input: "I love programming.",
});
 Ich liebe Programming.

But for day to day purposes Ich mag Programming. would be enough and perfectly understood.

I love programming is "Ich liebe Programming" and I like programming is "Ich mag Programming" respectively.

There are also other ways to express this feeling, such as "Ich habe Spaß mit Programming", which means "I enjoy programming". But "Ich mag" and "Ich liebe" are the most common expressions for this.

Let me know if I can be of further help with something else!

API reference

For detailed documentation of all Cohere features and configurations head to the API reference: https://api.js.langchain.com/classes/langchain_cohere.Cohere.html


Was this page helpful?


You can also leave detailed feedback on GitHub.