Mistral 7B Chat
A state of the art seven billion parameter LLM for general chat tasks with an OpenAI ChatCompletions compatible endpoint.
Deploy Mistral 7B Chat behind an API endpoint in seconds.
Deploy modelExample usage
OpenAI Chat Completions Token Streaming Example
This code example shows how to invoke the model with the OpenAI Chat Completions API. The model has three main inputs:
messages
: This is a list of JSON objects. Each of those JSON objects should have a key called role which can have the value of either user or assistant. The JSON object should also have content which is the text passed to the large language model.stream
: Setting this to True allows you to stream the tokens as they get generated.max_tokens
: Allows you to control the length of the output sequence.
Because this code example streams the tokens as they get generated, it does not produce a JSON output.
1from openai import OpenAI
2import os
3
4# Replace the empty string with your model id below
5model_id = ""
6
7client = OpenAI(
8 api_key=os.environ["BASETEN_API_KEY"],
9 base_url=f"https://bridge.baseten.co/{model_id}/v1"
10)
11
12# Call model endpoint
13res = client.chat.completions.create(
14 model="mistral-7b",
15 messages=[
16 {"role": "user", "content": "What is a mistral?"},
17 {"role": "assistant", "content": "A mistral is a type of cold, dry wind that blows across the southern slopes of the Alps from the Valais region of Switzerland into the Ligurian Sea near Genoa. It is known for its strong and steady gusts, sometimes reaching up to 60 miles per hour."},
18 {"role": "user", "content": "How does the mistral wind form?"}
19 ],
20 temperature=0.5,
21 max_tokens=50,
22 top_p=0.95,
23 stream=True
24)
25
26# Print the generated tokens as they get streamed
27for chunk in res:
28 print(chunk.choices[0].delta.content)
1[
2 "A",
3 "mistral",
4 "is",
5 "a",
6 "type",
7 "...."
8]
OpenAI Chat Completions Without Streaming
The code example below shows how to use the same OpenAI Chat Completions API but without token streaming. To do this simply remove stream
from the API call.
The output will be the entire generated text produced by the model.
1from openai import OpenAI
2import os
3
4# Replace the empty string with your model id below
5model_id = ""
6
7client = OpenAI(
8 api_key=os.environ["BASETEN_API_KEY"],
9 base_url=f"https://bridge.baseten.co/{model_id}/v1"
10)
11
12# Call model endpoint
13res = client.chat.completions.create(
14 model="mistral-7b",
15 messages=[
16 {"role": "user", "content": "What is a mistral?"},
17 {"role": "assistant", "content": "A mistral is a type of cold, dry wind that blows across the southern slopes of the Alps from the Valais region of Switzerland into the Ligurian Sea near Genoa. It is known for its strong and steady gusts, sometimes reaching up to 60 miles per hour."},
18 {"role": "user", "content": "How does the mistral wind form?"}
19 ],
20 temperature=0.5,
21 max_tokens=50,
22 top_p=0.95
23)
24
25# Print the output of the model
26print(res.choices[0].message.content)
1{
2 "output": "[INST] What is a mistral? [/INST]A mistral is a type of cold, dry wind that blows across the southern slopes of the Alps from the Valais region of Switzerland into the Ligurian Sea near Genoa. It is known for its strong and steady gusts, sometimes reaching up to 60 miles per hour. [INST] How does the mistral wind form? [/INST]The mistral wind forms as a result of the movement of cold air from the high mountains of the Swiss Alps towards the sea. The cold air collides with the warmer air over the Mediterranean Sea, causing the cold air to rise rapidly and creating a cyclonic circulation. As the warm air rises, the cold air flows into the valley, creating a strong, steady wind known as the mistral.\n\nThe mistral is typically strongest during the winter months when the air is cold."
3}
REST API Streaming Example
Using the OpenAI Chat Completions API is optional. You can also make a REST API call using the requests library. To invoke the model using this method you need to same three inputs messages
, stream
, and max_new_tokens
.
Because this code example streams the tokens as they get generated, it does not produce a JSON output.
1import requests
2import os
3
4# Replace the empty string with your model id below
5model_id = ""
6baseten_api_key = os.environ["BASETEN_API_KEY"]
7
8messages = [
9 {"role": "user", "content": "What is a mistral?"},
10 {"role": "assistant", "content": "A mistral is a type of cold, dry wind that blows across the southern slopes of the Alps from the Valais region of Switzerland into the Ligurian Sea near Genoa. It is known for its strong and steady gusts, sometimes reaching up to 60 miles per hour."},
11 {"role": "user", "content": "How does the mistral wind form?"},
12]
13data = {
14 "messages": messages,
15 "stream": True,
16 "max_new_tokens": 512,
17 "temperature": 0.9
18}
19
20# Call model endpoint
21res = requests.post(
22 f"https://model-{model_id}.api.baseten.co/production/predict",
23 headers={"Authorization": f"Api-Key {baseten_api_key}"},
24 json=data,
25 stream=True
26)
27
28# Print the generated tokens as they get streamed
29for content in res.iter_content():
30 print(content.decode("utf-8"), end="", flush=True)
1[
2 "A",
3 "mistral",
4 "is",
5 "a",
6 "type",
7 "...."
8]
REST API Without Streaming Example
If you don't want to stream the tokens simply set the stream
parameter to False.
The output is the entire text generated by the model.
1import requests
2
3# Replace the empty string with your model id below
4model_id = ""
5baseten_api_key = os.environ["BASETEN_API_KEY"]
6
7messages = [
8 {"role": "user", "content": "What is a mistral?"},
9 {"role": "assistant", "content": "A mistral is a type of cold, dry wind that blows across the southern slopes of the Alps from the Valais region of Switzerland into the Ligurian Sea near Genoa. It is known for its strong and steady gusts, sometimes reaching up to 60 miles per hour."},
10 {"role": "user", "content": "How does the mistral wind form?"},
11]
12data = {
13 "messages": messages,
14 "stream": False,
15 "max_new_tokens": 512,
16 "temperature": 0.9
17}
18
19# Call model endpoint
20res = requests.post(
21 f"https://model-{model_id}.api.baseten.co/production/predict",
22 headers={"Authorization": f"Api-Key {baseten_api_key}"},
23 json=data
24)
25
26# Print the output of the model
27print(res.json())
1{
2 "output": "[INST] What is a mistral? [/INST]A mistral is a type of cold, dry wind that blows across the southern slopes of the Alps from the Valais region of Switzerland into the Ligurian Sea near Genoa. It is known for its strong and steady gusts, sometimes reaching up to 60 miles per hour. [INST] How does the mistral wind form? [/INST]The mistral wind forms as a result of the movement of cold air from the high mountains of the Swiss Alps towards the sea. The cold air collides with the warmer air over the Mediterranean Sea, causing the cold air to rise rapidly and creating a cyclonic circulation. As the warm air rises, the cold air flows into the valley, creating a strong, steady wind known as the mistral.\n\nThe mistral is typically strongest during the winter months when the air is cold."
3}