# ChatMistralAI

> **Class** in `@langchain/mistralai`

📖 [View in docs](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI)

Mistral AI chat model integration.

Setup:
Install `@langchain/mistralai` and set an environment variable named `MISTRAL_API_KEY`.

```bash
npm install @langchain/mistralai
export MISTRAL_API_KEY="your-api-key"
```

## [Constructor args](https://api.js.langchain.com/classes/_langchain_mistralai.ChatMistralAI.html#constructor)

## [Runtime args](https://api.js.langchain.com/interfaces/_langchain_mistralai.ChatMistralAICallOptions.html)

Runtime args can be passed as the second argument to any of the base runnable methods `.invoke`. `.stream`, `.batch`, etc.
They can also be passed via `.withConfig`, or the second arg in `.bindTools`, like shown in the examples below:

```typescript
// When calling `.withConfig`, call options should be passed via the first argument
const llmWithArgsBound = llm.bindTools([...]) // tools array
  .withConfig({
    stop: ["\n"], // other call options
  });

// You can also bind tools and call options like this
const llmWithTools = llm.bindTools([...], {
  tool_choice: "auto",
});
```

## Examples

<details open>
<summary><strong>Instantiate</strong></summary>

```typescript
import { ChatMistralAI } from '@langchain/mistralai';

const llm = new ChatMistralAI({
  model: "mistral-large-2402",
  temperature: 0,
  // other params...
});
```
</details>

<br />

<details>
<summary><strong>Invoking</strong></summary>

```typescript
const input = `Translate "I love programming" into French.`;

// Models also accept a list of chat messages or a formatted prompt
const result = await llm.invoke(input);
console.log(result);
```

```txt
AIMessage {
  "content": "The translation of \"I love programming\" into French is \"J'aime la programmation\". Here's the breakdown:\n\n- \"I\" translates to \"Je\"\n- \"love\" translates to \"aime\"\n- \"programming\" translates to \"la programmation\"\n\nSo, \"J'aime la programmation\" means \"I love programming\" in French.",
  "additional_kwargs": {},
  "response_metadata": {
    "tokenUsage": {
      "completionTokens": 89,
      "promptTokens": 13,
      "totalTokens": 102
    },
    "finish_reason": "stop"
  },
  "tool_calls": [],
  "invalid_tool_calls": [],
  "usage_metadata": {
    "input_tokens": 13,
    "output_tokens": 89,
    "total_tokens": 102
  }
}
```
</details>

<br />

<details>
<summary><strong>Streaming Chunks</strong></summary>

```typescript
for await (const chunk of await llm.stream(input)) {
  console.log(chunk);
}
```

```txt
AIMessageChunk {
  "content": "The",
  "additional_kwargs": {},
  "response_metadata": {
    "prompt": 0,
    "completion": 0
  },
  "tool_calls": [],
  "tool_call_chunks": [],
  "invalid_tool_calls": []
}
AIMessageChunk {
  "content": " translation",
  "additional_kwargs": {},
  "response_metadata": {
    "prompt": 0,
    "completion": 0
  },
  "tool_calls": [],
  "tool_call_chunks": [],
  "invalid_tool_calls": []
}
AIMessageChunk {
  "content": " of",
  "additional_kwargs": {},
  "response_metadata": {
    "prompt": 0,
    "completion": 0
  },
  "tool_calls": [],
  "tool_call_chunks": [],
  "invalid_tool_calls": []
}
AIMessageChunk {
  "content": " \"",
  "additional_kwargs": {},
  "response_metadata": {
    "prompt": 0,
    "completion": 0
  },
  "tool_calls": [],
  "tool_call_chunks": [],
  "invalid_tool_calls": []
}
AIMessageChunk {
  "content": "I",
  "additional_kwargs": {},
  "response_metadata": {
    "prompt": 0,
    "completion": 0
  },
  "tool_calls": [],
  "tool_call_chunks": [],
  "invalid_tool_calls": []
}
AIMessageChunk {
 "content": ".",
 "additional_kwargs": {},
 "response_metadata": {
   "prompt": 0,
   "completion": 0
 },
 "tool_calls": [],
 "tool_call_chunks": [],
 "invalid_tool_calls": []
}
AIMessageChunk {
 "content": "",
 "additional_kwargs": {},
 "response_metadata": {
   "prompt": 0,
   "completion": 0
 },
 "tool_calls": [],
 "tool_call_chunks": [],
 "invalid_tool_calls": [],
 "usage_metadata": {
   "input_tokens": 13,
   "output_tokens": 89,
   "total_tokens": 102
 }
}
```
</details>

<br />

<details>
<summary><strong>Aggregate Streamed Chunks</strong></summary>

```typescript
import { AIMessageChunk } from '@langchain/core/messages';
import { concat } from '@langchain/core/utils/stream';

const stream = await llm.stream(input);
let full: AIMessageChunk | undefined;
for await (const chunk of stream) {
  full = !full ? chunk : concat(full, chunk);
}
console.log(full);
```

```txt
AIMessageChunk {
  "content": "The translation of \"I love programming\" into French is \"J'aime la programmation\". Here's the breakdown:\n\n- \"I\" translates to \"Je\"\n- \"love\" translates to \"aime\"\n- \"programming\" translates to \"la programmation\"\n\nSo, \"J'aime la programmation\" means \"I love programming\" in French.",
  "additional_kwargs": {},
  "response_metadata": {
    "prompt": 0,
    "completion": 0
  },
  "tool_calls": [],
  "tool_call_chunks": [],
  "invalid_tool_calls": [],
  "usage_metadata": {
    "input_tokens": 13,
    "output_tokens": 89,
    "total_tokens": 102
  }
}
```
</details>

<br />

<details>
<summary><strong>Bind tools</strong></summary>

```typescript
import { z } from 'zod';

const GetWeather = {
  name: "GetWeather",
  description: "Get the current weather in a given location",
  schema: z.object({
    location: z.string().describe("The city and state, e.g. San Francisco, CA")
  }),
}

const GetPopulation = {
  name: "GetPopulation",
  description: "Get the current population in a given location",
  schema: z.object({
    location: z.string().describe("The city and state, e.g. San Francisco, CA")
  }),
}

const llmWithTools = llm.bindTools([GetWeather, GetPopulation]);
const aiMsg = await llmWithTools.invoke(
  "Which city is hotter today and which is bigger: LA or NY?"
);
console.log(aiMsg.tool_calls);
```

```txt
[
  {
    name: 'GetWeather',
    args: { location: 'Los Angeles, CA' },
    type: 'tool_call',
    id: '47i216yko'
  },
  {
    name: 'GetWeather',
    args: { location: 'New York, NY' },
    type: 'tool_call',
    id: 'nb3v8Fpcn'
  },
  {
    name: 'GetPopulation',
    args: { location: 'Los Angeles, CA' },
    type: 'tool_call',
    id: 'EedWzByIB'
  },
  {
    name: 'GetPopulation',
    args: { location: 'New York, NY' },
    type: 'tool_call',
    id: 'jLdLia7zC'
  }
]
```
</details>

<br />

<details>
<summary><strong>Structured Output</strong></summary>

```typescript
import { z } from 'zod';

const Joke = z.object({
  setup: z.string().describe("The setup of the joke"),
  punchline: z.string().describe("The punchline to the joke"),
  rating: z.number().optional().describe("How funny the joke is, from 1 to 10")
}).describe('Joke to tell user.');

const structuredLlm = llm.withStructuredOutput(Joke, { name: "Joke" });
const jokeResult = await structuredLlm.invoke("Tell me a joke about cats");
console.log(jokeResult);
```

```txt
{
  setup: "Why don't cats play poker in the jungle?",
  punchline: 'Too many cheetahs!',
  rating: 7
}
```
</details>

<br />

<details>
<summary><strong>Usage Metadata</strong></summary>

```typescript
const aiMsgForMetadata = await llm.invoke(input);
console.log(aiMsgForMetadata.usage_metadata);
```

```txt
{ input_tokens: 13, output_tokens: 89, total_tokens: 102 }
```
</details>

<br />

## Signature

```javascript
class ChatMistralAI
```

## Extends

- `BaseChatModel<CallOptions, AIMessageChunk>`

## Implements

- `ChatMistralAIInput`

## Constructors

- [`constructor()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/constructor)

## Properties

- `apiKey`
- `beforeRequestHooks`
- `cache`
- `callbacks`
- `caller`
- `disableStreaming`
- `endpoint`
- `frequencyPenalty`
- `httpClient`
- `lc_kwargs`
- `lc_namespace`
- `lc_runnable`
- `lc_serializable`
- `maxRetries`
- `maxTokens`
- `metadata`
- `model`
- `name`
- `numCompletions`
- `outputVersion`
- `ParsedCallOptions`
- `presencePenalty`
- `randomSeed`
- `requestErrorHooks`
- `responseHooks`
- `safeMode`
- `safePrompt`
- `seed`
- `serverURL`
- `streaming`
- `streamUsage`
- `tags`
- `temperature`
- `topP`
- `verbose`
- `callKeys`
- `lc_aliases`
- `lc_attributes`
- `lc_id`
- `lc_secrets`
- `lc_serializable_keys`
- `profile`

## Methods

- [`_addVersion()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/_addVersion)
- [`_batchWithConfig()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/_batchWithConfig)
- [`_callWithConfig()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/_callWithConfig)
- [`_filterInvocationParamsForTracing()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/_filterInvocationParamsForTracing)
- [`_generateCached()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/_generateCached)
- [`_getOptionsList()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/_getOptionsList)
- [`_getSerializedCacheKeyParametersForCall()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/_getSerializedCacheKeyParametersForCall)
- [`_identifyingParams()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/_identifyingParams)
- [`_llmType()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/_llmType)
- [`_modelType()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/_modelType)
- [`_separateRunnableConfigFromCallOptions()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/_separateRunnableConfigFromCallOptions)
- [`_separateRunnableConfigFromCallOptionsCompat()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/_separateRunnableConfigFromCallOptionsCompat)
- [`_streamChatModelEvents()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/_streamChatModelEvents)
- [`_streamIterator()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/_streamIterator)
- [`_streamLog()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/_streamLog)
- [`_streamResponseChunks()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/_streamResponseChunks)
- [`_transformStreamWithConfig()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/_transformStreamWithConfig)
- [`addAllHooksToHttpClient()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/addAllHooksToHttpClient)
- [`assign()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/assign)
- [`asTool()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/asTool)
- [`batch()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/batch)
- [`bindTools()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/bindTools)
- [`completionWithRetry()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/completionWithRetry)
- [`generate()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/generate)
- [`generatePrompt()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/generatePrompt)
- [`getGraph()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/getGraph)
- [`getLsParams()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/getLsParams)
- [`getLsParamsWithDefaults()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/getLsParamsWithDefaults)
- [`getName()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/getName)
- [`getNumTokens()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/getNumTokens)
- [`invocationParams()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/invocationParams)
- [`invoke()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/invoke)
- [`pick()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/pick)
- [`pipe()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/pipe)
- [`removeAllHooksFromHttpClient()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/removeAllHooksFromHttpClient)
- [`removeHookFromHttpClient()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/removeHookFromHttpClient)
- [`serialize()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/serialize)
- [`stream()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/stream)
- [`streamEvents()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/streamEvents)
- [`streamLog()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/streamLog)
- [`streamV2()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/streamV2)
- [`toJSON()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/toJSON)
- [`toJSONNotImplemented()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/toJSONNotImplemented)
- [`transform()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/transform)
- [`withConfig()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/withConfig)
- [`withFallbacks()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/withFallbacks)
- [`withListeners()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/withListeners)
- [`withRetry()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/withRetry)
- [`withStructuredOutput()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/withStructuredOutput)
- [`_convertInputToPromptValue()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/_convertInputToPromptValue)
- [`deserialize()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/deserialize)
- [`isRunnable()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/isRunnable)
- [`lc_name()`](https://reference.langchain.com/javascript/langchain-mistralai/ChatMistralAI/lc_name)

---

[View source on GitHub](https://github.com/langchain-ai/langchainjs/blob/c5e896270207fcd3bdf1d1877e80b94c72d35f1e/libs/providers/langchain-mistralai/src/chat_models.ts#L910)