## MistralAI

This notebook covers how to get started with MistralAI chat models, via their [API](https://docs.mistral.ai/api/).

A valid [API key](https://console.mistral.ai/users/api-keys/) is needed to communicate with the API.

Head to the [API reference](https://api.python.langchain.com/en/latest/chat_models/langchain_mistralai.chat_models.ChatMistralAI.html) for detailed documentation of all attributes and methods.

## Setup[​](#setup "Direct link to Setup")

You will need the `langchain-core` and `langchain-mistralai` package to use the API. You can install these with:

```
pip install -U langchain-core langchain-mistralaiWe'll also need to get a [Mistral API key](https://console.mistral.ai/users/api-keys/)::: {.cell execution_count=7}``` {.python .cell-code}import getpassapi_key = getpass.getpass()
```

:::

## Usage[​](#usage "Direct link to Usage")

```
from langchain_core.messages import HumanMessagefrom langchain_mistralai.chat_models import ChatMistralAI
```

```
# If api_key is not passed, default behavior is to use the `MISTRAL_API_KEY` environment variable.chat = ChatMistralAI(api_key=api_key)
```

```
messages = [HumanMessage(content="knock knock")]chat.invoke(messages)
```

```
AIMessage(content="Who's there? I was just about to ask the same thing! How can I assist you today?")
```

### Async[​](#async "Direct link to Async")

```
await chat.ainvoke(messages)
```

```
AIMessage(content='Who\'s there?\n\n(You can then continue the "knock knock" joke by saying the name of the person or character who should be responding. For example, if I say "Banana," you could respond with "Banana who?" and I would say "Banana bunch! Get it? Because a group of bananas is called a \'bunch\'!" and then we would both laugh and have a great time. But really, you can put anything you want in the spot where I put "Banana" and it will still technically be a "knock knock" joke. The possibilities are endless!)')
```

### Streaming[​](#streaming "Direct link to Streaming")

```
for chunk in chat.stream(messages):    print(chunk.content, end="")
```

```
Who's there?(After this, the conversation can continue as a call and response "who's there" joke. Here is an example of how it could go:You say: Orange.I say: Orange who?You say: Orange you glad I didn't say banana!?)But since you asked for a knock knock joke specifically, here's one for you:Knock knock.Me: Who's there?You: Lettuce.Me: Lettuce who?You: Lettuce in, it's too cold out here!I hope this brings a smile to your face! Do you have a favorite knock knock joke you'd like to share? I'd love to hear it.
```

### Batch[​](#batch "Direct link to Batch")

```
[AIMessage(content="Who's there? I was just about to ask the same thing! Go ahead and tell me who's there. I love a good knock-knock joke.")]
```

## Chaining[​](#chaining "Direct link to Chaining")

You can also easily combine with a prompt template for easy structuring of user input. We can do this using [LCEL](https://python.langchain.com/docs/expression_language/)

```
from langchain_core.prompts import ChatPromptTemplateprompt = ChatPromptTemplate.from_template("Tell me a joke about {topic}")chain = prompt | chat
```

```
chain.invoke({"topic": "bears"})
```

```
AIMessage(content='Why do bears hate shoes so much? They like to run around in their bear feet.')
```