Skip to main content

Cohere

Setup

  1. You can get an API key from Cohere.
  2. The API key can be configured as an environment variable (COHERE_API_KEY) or passed in as an option into the model constructor.

Model Functions

Examples

Generate Text

CohereTextGenerationModel API

import { cohere, generateText } from "modelfusion";

const text = await generateText({
model: cohere.TextGenerator({
model: "command",
temperature: 0.7,
maxGenerationTokens: 500,
}),
prompt: "Write a short story about a robot learning to love:\n\n",
});

Stream Text

CohereTextGenerationModel API

import { cohere, streamText } from "modelfusion";

const textStream = await streamText({
model: cohere.TextGenerator({
model: "command",
temperature: 0.7,
maxGenerationTokens: 500,
}),
prompt: "Write a short story about a robot learning to love:\n\n",
});

for await (const textPart of textStream) {
process.stdout.write(textPart);
}

Embed Text

CohereTextEmbeddingModel API

import { cohere, embedMany } from "modelfusion";

const embeddings = await embedMany({
model: cohere.TextEmbedder({ model: "embed-english-light-v2.0" }),
values: [
"At first, Nox didn't know what to do with the pup.",
"He keenly observed and absorbed everything around him, from the birds in the sky to the trees in the forest.",
],
});

Tokenize Text

CohereTokenizer API

import { cohere, countTokens } from "modelfusion";

const tokenizer = cohere.Tokenizer({ model: "command" });

const text = "At first, Nox didn't know what to do with the pup.";

const tokenCount = await countTokens(tokenizer, text);
const tokens = await tokenizer.tokenize(text);
const tokensAndTokenTexts = await tokenizer.tokenizeWithTexts(text);
const reconstructedText = await tokenizer.detokenize(tokens);

Configuration

API Configuration

Cohere API Configuration

const api = cohere.Api({
apiKey: "my-api-key",
// ...
});

const model = cohere.TextGenerator({
api,
// ...
});