File size: 1,336 Bytes
ed4d993
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# langchain-mistralai

This package contains the LangChain integrations for [MistralAI](https://docs.mistral.ai) through their [mistralai](https://pypi.org/project/mistralai/) SDK.

## Installation

```bash
pip install -U langchain-mistralai
```

## Chat Models

This package contains the `ChatMistralAI` class, which is the recommended way to interface with MistralAI models.

To use, install the requirements, and configure your environment.

```bash
export MISTRAL_API_KEY=your-api-key
```

Then initialize

```python
from langchain_core.messages import HumanMessage
from langchain_mistralai.chat_models import ChatMistralAI

chat = ChatMistralAI(model="mistral-small")
messages = [HumanMessage(content="say a brief hello")]
chat.invoke(messages)
```

`ChatMistralAI` also supports async and streaming functionality:

```python
# For async...
await chat.ainvoke(messages)

# For streaming...
for chunk in chat.stream(messages):
    print(chunk.content, end="", flush=True)
```

## Embeddings

With `MistralAIEmbeddings`, you can directly use the default model 'mistral-embed', or set a different one if available.

### Choose model

`embedding.model = 'mistral-embed'`

### Simple query

`res_query = embedding.embed_query("The test information")`

### Documents

`res_document = embedding.embed_documents(["test1", "another test"])`