Skip to main content

Mistral

Generate text and embeddings using the Mistral platform.

Setup

  1. Sign up for the Mistral platform, activate your subscription and and generate an API key.
  2. The API key can be configured as an environment variable (MISTRAL_API_KEY) or passed in as an option into the model constructor.

Model Functions

Examples

Generate Text

MistralChatModel API

import { generateText, mistral } from "modelfusion";

const text = await generateText({
model: mistral.ChatTextGenerator({
model: "mistral-medium",
maxGenerationTokens: 120,
}),
prompt: [
{
role: "user",
content: "Write a short story about a robot learning to love:",
},
],
});

Stream Text

MistralChatModel API

import { mistral, streamText } from "modelfusion";

const textStream = await streamText({
model: mistral.ChatTextGenerator({
model: "mistral-medium",
maxGenerationTokens: 120,
}),
prompt: [
{
role: "user",
content: "Write a short story about a robot learning to love:",
},
],
});

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

Embed Text

MistralTextEmbeddingModel API

import { embedMany, mistral } from "modelfusion";

const embeddings = await embedMany({
model: mistral.TextEmbedder({ model: "mistral-embed" }),
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.",
],
});

Configuration

API Configuration

Mistral API Configuration

import { mistral } from "modelfusion";

const api = mistral.Api({
apiKey: "my-api-key", // optional; default: process.env.MISTRAL_API_KEY
// ...
});

const model = mistral.ChatTextGenerator({
api,
// ...
});