This example shows how to create chat completions.
# Synchronous Example
from mistralai import Mistral
import os
with Mistral(
api_key=os.getenv("MISTRAL_API_KEY", ""),
) as mistral:
res = mistral.chat.complete(model="mistral-small-latest", messages=[
{
"content": "Who is the best French painter? Answer in one short sentence.",
"role": "user",
},
])
assert res is not None
# Handle response
print(res)
The same SDK client can also be used to make asychronous requests by importing asyncio.
# Asynchronous Example
import asyncio
from mistralai import Mistral
import os
async def main():
async with Mistral(
api_key=os.getenv("MISTRAL_API_KEY", ""),
) as mistral:
res = await mistral.chat.complete_async(model="mistral-small-latest", messages=[
{
"content": "Who is the best French painter? Answer in one short sentence.",
"role": "user",
},
])
assert res is not None
# Handle response
print(res)
asyncio.run(main())
This example shows how to upload a file.
# Synchronous Example
from mistralai import Mistral
import os
with Mistral(
api_key=os.getenv("MISTRAL_API_KEY", ""),
) as mistral:
res = mistral.files.upload(file={
"file_name": "example.file",
"content": open("example.file", "rb"),
})
assert res is not None
# Handle response
print(res)
The same SDK client can also be used to make asychronous requests by importing asyncio.
# Asynchronous Example
import asyncio
from mistralai import Mistral
import os
async def main():
async with Mistral(
api_key=os.getenv("MISTRAL_API_KEY", ""),
) as mistral:
res = await mistral.files.upload_async(file={
"file_name": "example.file",
"content": open("example.file", "rb"),
})
assert res is not None
# Handle response
print(res)
asyncio.run(main())
This example shows how to create agents completions.
# Synchronous Example
from mistralai import Mistral
import os
with Mistral(
api_key=os.getenv("MISTRAL_API_KEY", ""),
) as mistral:
res = mistral.agents.complete(messages=[
{
"content": "Who is the best French painter? Answer in one short sentence.",
"role": "user",
},
], agent_id="<value>")
assert res is not None
# Handle response
print(res)
The same SDK client can also be used to make asychronous requests by importing asyncio.
# Asynchronous Example
import asyncio
from mistralai import Mistral
import os
async def main():
async with Mistral(
api_key=os.getenv("MISTRAL_API_KEY", ""),
) as mistral:
res = await mistral.agents.complete_async(messages=[
{
"content": "Who is the best French painter? Answer in one short sentence.",
"role": "user",
},
], agent_id="<value>")
assert res is not None
# Handle response
print(res)
asyncio.run(main())
This example shows how to create embedding request.
# Synchronous Example
from mistralai import Mistral
import os
with Mistral(
api_key=os.getenv("MISTRAL_API_KEY", ""),
) as mistral:
res = mistral.embeddings.create(inputs=[
"Embed this sentence.",
"As well as this one.",
], model="Wrangler")
assert res is not None
# Handle response
print(res)
The same SDK client can also be used to make asychronous requests by importing asyncio.
# Asynchronous Example
import asyncio
from mistralai import Mistral
import os
async def main():
async with Mistral(
api_key=os.getenv("MISTRAL_API_KEY", ""),
) as mistral:
res = await mistral.embeddings.create_async(inputs=[
"Embed this sentence.",
"As well as this one.",
], model="Wrangler")
assert res is not None
# Handle response
print(res)
asyncio.run(main())