Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump mistralai to > 1.0.0 in preparation for latest models such as Pixtral #531

Merged
merged 20 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/WELCOME.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Make your first call to an LLM to extract the title and author of a book from th
{% if provider == "Anthropic" %}
```python hl_lines="19-38 43"
{% elif provider == "Mistral" %}
```python hl_lines="19-40 45"
```python hl_lines="21-46 51"
{% elif provider == "Gemini" %}
```python hl_lines="19-57 62"
{% elif provider == "Cohere" %}
Expand Down
2 changes: 1 addition & 1 deletion docs/WHY.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ Let's compare structured outputs using Mirascope vs. the official SDKs:
{% if provider == "Anthropic" %}
```python hl_lines="19-38 43"
{% elif provider == "Mistral" %}
```python hl_lines="19-40 45"
```python hl_lines="21-46 51"
{% elif provider == "Gemini" %}
```python hl_lines="19-57 62"
{% elif provider == "Cohere" %}
Expand Down
2 changes: 2 additions & 0 deletions docs/learn/calls.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ Let's take a look at a basic example using Mirascope vs. official provider SDKs:
```python hl_lines="5-9"
{% elif provider == "Azure AI" %}
```python hl_lines="11-18"
{% elif provider == "Mistral" %}
```python hl_lines="10-15"
{% else %}
```python hl_lines="7-11"
{% endif %}
Expand Down
2 changes: 1 addition & 1 deletion docs/learn/response_models.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Let's take a look at a basic example using Mirascope vs. official provider SDKs:
{% if provider == "Anthropic" %}
```python hl_lines="19-38 43"
{% elif provider == "Mistral" %}
```python hl_lines="19-40 45"
```python hl_lines="21-46 51"
{% elif provider == "Gemini" %}
```python hl_lines="19-57 62"
{% elif provider == "Cohere" %}
Expand Down
2 changes: 2 additions & 0 deletions docs/learn/tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ Let's take a look at a basic example of each using Mirascope vs. official provid
```python hl_lines="16-22 31-43 45-47"
{% elif provider == "Vertex AI" %}
```python hl_lines="6-12 18-34 36-43"
{% elif provider == "Mistral" %}
```python hl_lines="10-16 23-36 38-44"
{% else %}
```python hl_lines="8-14 21-34 36-38"
{% endif %}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import os

from mirascope.core import BaseMessageParam, mistral
from mistralai.async_client import MistralAsyncClient
from mistralai import Mistral


@mistral.call("mistral-large-latest", client=MistralAsyncClient())
@mistral.call(
"mistral-large-latest", client=Mistral(api_key=os.environ["MISTRAL_API_KEY"])
willbakst marked this conversation as resolved.
Show resolved Hide resolved
)
async def recommend_book_async(genre: str) -> list[BaseMessageParam]:
return [BaseMessageParam(role="user", content=f"Recommend a {genre} book")]
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import os

from mirascope.core import Messages, mistral
from mistralai.async_client import MistralAsyncClient
from mistralai import Mistral


@mistral.call("mistral-large-latest", client=MistralAsyncClient())
@mistral.call(
"mistral-large-latest", client=Mistral(api_key=os.environ["MISTRAL_API_KEY"])
)
async def recommend_book_async(genre: str) -> Messages.Type:
return Messages.User(f"Recommend a {genre} book")
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import os

from mirascope.core import mistral
from mistralai.async_client import MistralAsyncClient
from mistralai import Mistral


@mistral.call("mistral-large-latest", client=MistralAsyncClient())
@mistral.call(
"mistral-large-latest", client=Mistral(api_key=os.environ["MISTRAL_API_KEY"])
)
async def recommend_book_async(genre: str) -> str:
return f"Recommend a {genre} book"
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import os

from mirascope.core import mistral, prompt_template
from mistralai.async_client import MistralAsyncClient
from mistralai import Mistral


@mistral.call("mistral-large-latest", client=MistralAsyncClient())
@mistral.call(
"mistral-large-latest", client=Mistral(api_key=os.environ["MISTRAL_API_KEY"])
)
@prompt_template("Recommend a {genre} book")
async def recommend_book_async(genre: str): ...
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import os

from mirascope.core import BaseMessageParam, mistral
from mistralai.async_client import MistralAsyncClient
from mistralai import Mistral


@mistral.call("mistral-large-latest")
async def recommend_book(genre: str) -> mistral.AsyncMistralDynamicConfig:
async def recommend_book(genre: str) -> mistral.MistralDynamicConfig:
return {
"messages": [
BaseMessageParam(role="user", content=f"Recommend a {genre} book")
],
"client": MistralAsyncClient(),
"client": Mistral(api_key=os.environ["MISTRAL_API_KEY"]),
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import os

from mirascope.core import Messages, mistral
from mistralai.async_client import MistralAsyncClient
from mistralai import Mistral


@mistral.call("mistral-large-latest")
async def recommend_book(genre: str) -> mistral.AsyncMistralDynamicConfig:
async def recommend_book(genre: str) -> mistral.MistralDynamicConfig:
return {
"messages": [Messages.User(f"Recommend a {genre} book")],
"client": MistralAsyncClient(),
"client": Mistral(api_key=os.environ["MISTRAL_API_KEY"]),
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import os

from mirascope.core import mistral, Messages
from mistralai.async_client import MistralAsyncClient
from mistralai import Mistral


@mistral.call("mistral-large-latest")
async def recommend_book(genre: str) -> mistral.AsyncMistralDynamicConfig:
async def recommend_book(genre: str) -> mistral.MistralDynamicConfig:
return {
"messages": [Messages.User(f"Recommend a {genre} book")],
"client": MistralAsyncClient(),
"client": Mistral(api_key=os.environ["MISTRAL_API_KEY"]),
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import os

from mirascope.core import mistral, prompt_template
from mistralai.async_client import MistralAsyncClient
from mistralai import Mistral


@mistral.call("mistral-large-latest")
@prompt_template("Recommend a {genre} book")
async def recommend_book(genre: str) -> mistral.AsyncMistralDynamicConfig:
async def recommend_book(genre: str) -> mistral.MistralDynamicConfig:
return {
"client": MistralAsyncClient(),
"client": Mistral(api_key=os.environ["MISTRAL_API_KEY"]),
}
14 changes: 9 additions & 5 deletions examples/learn/calls/basic_usage/mistral/official_sdk_call.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
from mistralai.client import MistralClient
import os
from typing import cast

client = MistralClient()
from mistralai import Mistral

client = Mistral(api_key=os.environ["MISTRAL_API_KEY"])

def recommend_book(genre: str) -> str:
completion = client.chat(

def recommend_book(genre: str) -> str | None:
completion = client.chat.complete(
model="mistral-large-latest",
messages=[{"role": "user", "content": f"Recommend a {genre} book"}],
)
return completion.choices[0].message.content
if completion and (choices := completion.choices):
return cast(str, choices[0].message.content)


output = recommend_book("fantasy")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import os

from mirascope.core import BaseMessageParam, mistral
from mistralai.client import MistralClient
from mistralai import Mistral


@mistral.call("mistral-large-latest", client=MistralClient())
@mistral.call(
"mistral-large-latest",
client=Mistral(api_key=os.environ["MISTRAL_API_KEY"]),
)
def recommend_book(genre: str) -> list[BaseMessageParam]:
return [BaseMessageParam(role="user", content=f"Recommend a {genre} book")]


@mistral.call(
"mistral-large-latest",
client=Mistral(api_key=os.environ["MISTRAL_API_KEY"]),
)
async def recommend_book_async(genre: str) -> list[BaseMessageParam]:
return [BaseMessageParam(role="user", content=f"Recommend a {genre} book")]
17 changes: 15 additions & 2 deletions examples/learn/calls/custom_client/decorator/mistral/messages.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import os

from mirascope.core import Messages, mistral
from mistralai.client import MistralClient
from mistralai import Mistral


@mistral.call("mistral-large-latest", client=MistralClient())
@mistral.call(
"mistral-large-latest",
client=Mistral(api_key=os.environ["MISTRAL_API_KEY"]),
)
def recommend_book(genre: str) -> Messages.Type:
return Messages.User(f"Recommend a {genre} book")


@mistral.call(
"mistral-large-latest",
client=Mistral(api_key=os.environ["MISTRAL_API_KEY"]),
)
async def recommend_book_async(genre: str) -> Messages.Type:
return Messages.User(f"Recommend a {genre} book")
17 changes: 15 additions & 2 deletions examples/learn/calls/custom_client/decorator/mistral/shorthand.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import os

from mirascope.core import mistral
from mistralai.client import MistralClient
from mistralai import Mistral


@mistral.call("mistral-large-latest", client=MistralClient())
@mistral.call(
"mistral-large-latest",
client=Mistral(api_key=os.environ["MISTRAL_API_KEY"]),
)
def recommend_book(genre: str) -> str:
return f"Recommend a {genre} book"


@mistral.call(
"mistral-large-latest",
client=Mistral(api_key=os.environ["MISTRAL_API_KEY"]),
)
async def recommend_book_async(genre: str) -> str:
return f"Recommend a {genre} book"
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import os

from mirascope.core import mistral, prompt_template
from mistralai.client import MistralClient
from mistralai import Mistral


@mistral.call("mistral-large-latest", client=MistralClient())
@mistral.call(
"mistral-large-latest",
client=Mistral(api_key=os.environ["MISTRAL_API_KEY"]),
)
@prompt_template("Recommend a {genre} book")
def recommend_book(genre: str): ...


@mistral.call(
"mistral-large-latest",
client=Mistral(api_key=os.environ["MISTRAL_API_KEY"]),
)
@prompt_template("Recommend a {genre} book")
async def recommend_book_async(genre: str): ...
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os

from mirascope.core import BaseMessageParam, mistral
from mistralai.client import MistralClient
from mistralai import Mistral


@mistral.call("mistral-large-latest")
Expand All @@ -8,5 +10,5 @@ def recommend_book(genre: str) -> mistral.MistralDynamicConfig:
"messages": [
BaseMessageParam(role="user", content=f"Recommend a {genre} book")
],
"client": MistralClient(),
"client": Mistral(api_key=os.environ["MISTRAL_API_KEY"]),
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import os

from mirascope.core import Messages, mistral
from mistralai.client import MistralClient
from mistralai import Mistral


@mistral.call("mistral-large-latest")
def recommend_book(genre: str) -> mistral.MistralDynamicConfig:
return {
"messages": [Messages.User(f"Recommend a {genre} book")],
"client": MistralClient(),
"client": Mistral(api_key=os.environ["MISTRAL_API_KEY"]),
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import os

from mirascope.core import mistral, Messages
from mistralai.client import MistralClient
from mistralai import Mistral


@mistral.call("mistral-large-latest")
def recommend_book(genre: str) -> mistral.MistralDynamicConfig:
return {
"messages": [Messages.User(f"Recommend a {genre} book")],
"client": MistralClient(),
"client": Mistral(api_key=os.environ["MISTRAL_API_KEY"]),
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import os

from mirascope.core import mistral, prompt_template
from mistralai.client import MistralClient
from mistralai import Mistral


@mistral.call("mistral-large-latest")
@prompt_template("Recommend a {genre} book")
def recommend_book(genre: str) -> mistral.MistralDynamicConfig:
return {
"client": MistralClient(),
"client": Mistral(api_key=os.environ["MISTRAL_API_KEY"]),
}
4 changes: 2 additions & 2 deletions examples/learn/calls/custom_messages/mistral_messages.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from mirascope.core import mistral
from mistralai.models.chat_completion import ChatMessage
from mistralai.models import UserMessage


@mistral.call("mistral-large-latest")
def recommend_book(genre: str) -> mistral.MistralDynamicConfig:
return {"messages": [ChatMessage(role="user", content=f"Recommend a {genre} book")]}
return {"messages": [UserMessage(role="user", content=f"Recommend a {genre} book")]}


response = recommend_book("fantasy")
Expand Down
20 changes: 13 additions & 7 deletions examples/learn/response_models/basic_usage/mistral/official_sdk.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from mistralai.client import MistralClient
from mistralai.models.chat_completion import ToolChoice
import os
from typing import cast

from mistralai import Mistral
from pydantic import BaseModel

client = MistralClient()
client = Mistral(api_key=os.environ["MISTRAL_API_KEY"])


class Book(BaseModel):
Expand All @@ -13,7 +15,7 @@ class Book(BaseModel):


def extract_book(text: str) -> Book:
completion = client.chat(
completion = client.chat.complete(
model="mistral-large-latest",
messages=[{"role": "user", "content": f"Extract {text}"}],
tools=[
Expand All @@ -33,10 +35,14 @@ def extract_book(text: str) -> Book:
"type": "function",
}
],
tool_choice=ToolChoice.any,
tool_choice="any",
)
if tool_calls := completion.choices[0].message.tool_calls:
return Book.model_validate_json(tool_calls[0].function.arguments)
if (
completion
and (choices := completion.choices)
and (tool_calls := choices[0].message.tool_calls)
):
return Book.model_validate_json(cast(str, tool_calls[0].function.arguments))
raise ValueError("No tool call found")


Expand Down
Loading
Loading