-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathMIGRATION.md
242 lines (182 loc) · 7.21 KB
/
MIGRATION.md
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# Migration Guide for MistralAI Client from 0.\*.\* to 1.0.0
We have made significant changes to the `mistralai` library to improve its usability and consistency. This guide will help you migrate your code from the old client to the new one.
## Major Changes
1. **Unified Client Class**:
- The `MistralClient` and `MistralAsyncClient` classes have been consolidated into a single `Mistral` class.
- This simplifies the API by providing a single entry point for both synchronous and asynchronous operations.
2. **Method Names and Structure**:
- The method names and structure have been updated for better clarity and consistency.
- For example:
- `client.chat` is now `client.chat.complete` for non-streaming calls
- `client.chat_stream` is now `client.chat.stream` for streaming calls
- Async `client.chat` is now `client.chat.complete_async` for async non-streaming calls
- Async `client.chat_stream` is now `client.chat.stream_async` for async streaming calls
## Method changes
### Sync
| Old Methods | New Methods |
| -------------------------- | -------------------------------- |
| `MistralCLient` | `Mistral` |
| `client.chat` | `client.chat.complete` |
| `client.chat_stream` | `client.chat.stream` |
| `client.completions` | `client.fim.complete` |
| `client.completions_strem` | `client.fim.stream` |
| `client.embeddings` | `client.embeddings.create` |
| `client.list_models` | `client.models.list` |
| `client.delete_model` | `client.models.delete` |
| `client.files.create` | `client.files.upload` |
| `client.files.list` | `client.files.list` |
| `client.files.retrieve` | `client.files.retrieve` |
| `client.files.delete` | `client.files.delete` |
| `client.jobs.create` | `client.fine_tuning.jobs.create` |
| `client.jobs.list` | `client.fine_tuning.jobs.list` |
| `client.jobs.retrieve` | `client.fine_tuning.jobs.get` |
| `client.jobs.cancel` | `client.fine_tuning.jobs.cancel` |
### Async
| Old Methods | New Methods |
| -------------------------------- | -------------------------------------- |
| `MistralAsyncClient` | `Mistral` |
| `async_client.chat` | `client.chat.complete_async` |
| `async_client.chat_stream` | `client.chat.stream_async` |
| `async_client.completions` | `client.fim.complete_async` |
| `async_client.completions_strem` | `client.fim.stream_async` |
| `async_client.embeddings` | `client.embeddings.create_async` |
| `async_client.list_models` | `client.models.list_async` |
| `async_client.delete_model` | `client.models.delete_async` |
| `async_client.files.create` | `client.files.upload_async` |
| `async_client.files.list` | `client.files.list_async` |
| `async_client.files.retrieve` | `client.files.retrieve_async` |
| `async_client.files.delete` | `client.files.delete_async` |
| `async_client.jobs.create` | `client.fine_tuning.jobs.create_async` |
| `async_client.jobs.list` | `client.fine_tuning.jobs.list_async` |
| `async_client.jobs.retrieve` | `client.fine_tuning.jobs.get_async` |
| `async_client.jobs.cancel` | `client.fine_tuning.jobs.cancel_async` |
### Message Changes
The `ChatMessage` class has been replaced with a more flexible system. You can now use the `SystemMessage`, `UserMessage`, `AssistantMessage`, and `ToolMessage` classes to create messages.
The return object of the stream call methods have been modified to `chunk.data.choices[0].delta.content` from `chunk.choices[0].delta.content`.
## Example Migrations
### Example 1: Non-Streaming Chat
**Old:**
```python
from mistralai.client import MistralClient
from mistralai.models.chat_completion import ChatMessage
api_key = os.environ["MISTRAL_API_KEY"]
model = "mistral-large-latest"
client = MistralClient(api_key=api_key)
messages = [
ChatMessage(role="user", content="What is the best French cheese?")
]
# No streaming
chat_response = client.chat(
model=model,
messages=messages,
)
print(chat_response.choices[0].message.content)
```
**New:**
```python
import os
from mistralai import Mistral, UserMessage
api_key = os.environ["MISTRAL_API_KEY"]
model = "mistral-large-latest"
client = Mistral(api_key=api_key)
messages = [
{
"role": "user",
"content": "What is the best French cheese?",
},
]
# Or using the new message classes
# messages = [
# UserMessage(content="What is the best French cheese?"),
# ]
chat_response = client.chat.complete(
model=model,
messages=messages,
)
print(chat_response.choices[0].message.content)
```
### Example 2: Streaming Chat
**Old:**
```python
from mistralai.client import MistralClient
from mistralai.models.chat_completion import ChatMessage
api_key = os.environ["MISTRAL_API_KEY"]
model = "mistral-large-latest"
client = MistralClient(api_key=api_key)
messages = [
ChatMessage(role="user", content="What is the best French cheese?")
]
# With streaming
stream_response = client.chat_stream(model=model, messages=messages)
for chunk in stream_response:
print(chunk.choices[0].delta.content)
```
**New:**
```python
import os
from mistralai import Mistral, UserMessage
api_key = os.environ["MISTRAL_API_KEY"]
model = "mistral-large-latest"
client = Mistral(api_key=api_key)
messages = [
{
"role": "user",
"content": "What is the best French cheese?",
},
]
# Or using the new message classes
# messages = [
# UserMessage(content="What is the best French cheese?"),
# ]
stream_response = client.chat.stream(
model=model,
messages=messages,
)
for chunk in stream_response:
print(chunk.data.choices[0].delta.content)
```
### Example 3: Async
**Old:**
```python
from mistralai.async_client import MistralAsyncClient
from mistralai.models.chat_completion import ChatMessage
api_key = os.environ["MISTRAL_API_KEY"]
model = "mistral-large-latest"
client = MistralAsyncClient(api_key=api_key)
messages = [
ChatMessage(role="user", content="What is the best French cheese?")
]
# With async
async_response = client.chat_stream(model=model, messages=messages)
async for chunk in async_response:
print(chunk.choices[0].delta.content)
```
**New:**
```python
import asyncio
import os
from mistralai import Mistral, UserMessage
async def main():
client = Mistral(
api_key=os.getenv("MISTRAL_API_KEY", ""),
)
messages = [
{
"role": "user",
"content": "What is the best French cheese?",
},
]
# Or using the new message classes
# messages = [
# UserMessage(
# content="What is the best French cheese?",
# ),
# ]
async_response = await client.chat.stream_async(
messages=messages,
model="mistral-large-latest",
)
async for chunk in async_response:
print(chunk.data.choices[0].delta.content)
asyncio.run(main())
```