Chat Completion API.
Chat Completion
import { Mistral } from "@mistralai/mistralai";
const mistral = new Mistral({
apiKey: process.env["MISTRAL_API_KEY"] ?? "",
});
async function run() {
const result = await mistral.chat.complete({
model: "mistral-large-latest",
messages: [
{
role: "user",
content: "Who is the best French painter? Answer in one short sentence.",
},
],
responseFormat: {
type: "text",
},
});
console.log(result);
}
run();The standalone function version of this method:
import { MistralCore } from "@mistralai/mistralai/core.js";
import { chatComplete } from "@mistralai/mistralai/funcs/chatComplete.js";
// Use `MistralCore` for best tree-shaking performance.
// You can create one instance of it to use across an application.
const mistral = new MistralCore({
apiKey: process.env["MISTRAL_API_KEY"] ?? "",
});
async function run() {
const res = await chatComplete(mistral, {
model: "mistral-large-latest",
messages: [
{
role: "user",
content: "Who is the best French painter? Answer in one short sentence.",
},
],
responseFormat: {
type: "text",
},
});
if (res.ok) {
const { value: result } = res;
console.log(result);
} else {
console.log("chatComplete failed:", res.error);
}
}
run();| Parameter | Type | Required | Description |
|---|---|---|---|
request |
components.ChatCompletionRequest | ✔️ | The request object to use for the request. |
options |
RequestOptions | ➖ | Used to set various options for making HTTP requests. |
options.fetchOptions |
RequestInit | ➖ | Options that are passed to the underlying HTTP request. This can be used to inject extra headers for examples. All Request options, except method and body, are allowed. |
options.retries |
RetryConfig | ➖ | Enables retrying HTTP requests under certain failure conditions. |
Promise<components.ChatCompletionResponse>
| Error Type | Status Code | Content Type |
|---|---|---|
| errors.HTTPValidationError | 422 | application/json |
| errors.SDKError | 4XX, 5XX | */* |
Mistral AI provides the ability to stream responses back to a client in order to allow partial results for certain requests. Tokens will be sent as data-only server-sent events as they become available, with the stream terminated by a data: [DONE] message. Otherwise, the server will hold the request open until the timeout or until completion, with the response containing the full result as JSON.
import { Mistral } from "@mistralai/mistralai";
const mistral = new Mistral({
apiKey: process.env["MISTRAL_API_KEY"] ?? "",
});
async function run() {
const result = await mistral.chat.stream({
model: "mistral-large-latest",
messages: [
{
role: "user",
content: "Who is the best French painter? Answer in one short sentence.",
},
],
responseFormat: {
type: "text",
},
});
for await (const event of result) {
console.log(event);
}
}
run();The standalone function version of this method:
import { MistralCore } from "@mistralai/mistralai/core.js";
import { chatStream } from "@mistralai/mistralai/funcs/chatStream.js";
// Use `MistralCore` for best tree-shaking performance.
// You can create one instance of it to use across an application.
const mistral = new MistralCore({
apiKey: process.env["MISTRAL_API_KEY"] ?? "",
});
async function run() {
const res = await chatStream(mistral, {
model: "mistral-large-latest",
messages: [
{
role: "user",
content: "Who is the best French painter? Answer in one short sentence.",
},
],
responseFormat: {
type: "text",
},
});
if (res.ok) {
const { value: result } = res;
for await (const event of result) {
console.log(event);
}
} else {
console.log("chatStream failed:", res.error);
}
}
run();| Parameter | Type | Required | Description |
|---|---|---|---|
request |
components.ChatCompletionStreamRequest | ✔️ | The request object to use for the request. |
options |
RequestOptions | ➖ | Used to set various options for making HTTP requests. |
options.fetchOptions |
RequestInit | ➖ | Options that are passed to the underlying HTTP request. This can be used to inject extra headers for examples. All Request options, except method and body, are allowed. |
options.retries |
RetryConfig | ➖ | Enables retrying HTTP requests under certain failure conditions. |
Promise<EventStream<components.CompletionEvent>>
| Error Type | Status Code | Content Type |
|---|---|---|
| errors.HTTPValidationError | 422 | application/json |
| errors.SDKError | 4XX, 5XX | */* |