RepoMistral AIMistral AIpublished Dec 7, 2023seen 6d

mistralai/client-js

JavaScript

Open original ↗

Captured source

source ↗
published Dec 7, 2023seen 6dcaptured 15hhttp 200method plain

mistralai/client-js

Description: JS Client library for Mistral AI platform

Language: JavaScript

License: Apache-2.0

Stars: 203

Forks: 47

Open issues: 0

Created: 2023-12-07T10:40:00Z

Pushed: 2024-10-10T11:16:49Z

Default branch: main

Fork: no

Archived: yes

README:

📢🚨 This repository is now archived 📢🚨

We have deprecated this package in favor of mistralai/client-ts, which is the new official Mistral client, compatibile with both Typescript and Javascript.

You can find all installation information here.

This change is effective starting with version 1.0.0 of the npm package.

---

This JavaScript client is inspired from cohere-typescript

Mistral JavaScript Client

You can use the Mistral JavaScript client to interact with the Mistral AI API.

Installing

You can install the library in your project using:

npm install @mistralai/mistralai

Usage

You can watch a free course on using the Mistral JavaScript client here.

Set up

import MistralClient from '@mistralai/mistralai';

const apiKey = process.env.MISTRAL_API_KEY || 'your_api_key';

const client = new MistralClient(apiKey);

List models

const listModelsResponse = await client.listModels();
const listModels = listModelsResponse.data;
listModels.forEach((model) => {
console.log('Model:', model);
});

Chat with streaming

const chatStreamResponse = await client.chatStream({
model: 'mistral-tiny',
messages: [{role: 'user', content: 'What is the best French cheese?'}],
});

console.log('Chat Stream:');
for await (const chunk of chatStreamResponse) {
if (chunk.choices[0].delta.content !== undefined) {
const streamText = chunk.choices[0].delta.content;
process.stdout.write(streamText);
}
}

Chat without streaming

const chatResponse = await client.chat({
model: 'mistral-tiny',
messages: [{role: 'user', content: 'What is the best French cheese?'}],
});

console.log('Chat:', chatResponse.choices[0].message.content);

Embeddings

const input = [];
for (let i = 0; i

### Run the examples

MISTRAL_API_KEY='your_api_key' node chat_with_streaming.js

### Persisting the API key in environment

Set your Mistral API Key as an environment variable. You only need to do this once.

set Mistral API Key (using zsh for example)

$ echo 'export MISTRAL_API_KEY=[your_api_key]' >> ~/.zshenv

reload the environment (or just quit and open a new terminal)

$ source ~/.zshenv

You can then run the examples without appending the API key:

node chat_with_streaming.js

After the env variable setup the client will find the `MISTRAL_API_KEY` by itself

import MistralClient from '@mistralai/mistralai';

const client = new MistralClient();