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 15 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="18-39 45"
{% 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="18-39 45"
{% 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="9-15 22-35 37-39"
{% 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,7 @@
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())
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,7 @@
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())
willbakst marked this conversation as resolved.
Show resolved Hide resolved
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,7 @@
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())
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,7 @@
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())
@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,12 @@
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(),
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
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(),
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
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(),
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
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(),
}
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
from typing import cast

client = MistralClient()
from mirascope.core import mistral
from mistralai import Mistral

client = Mistral(api_key=mistral.load_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,18 @@
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=mistral.load_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=mistral.load_api_key()),
)
async def recommend_book_async(genre: str) -> list[BaseMessageParam]:
return [BaseMessageParam(role="user", content=f"Recommend a {genre} book")]
15 changes: 13 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,18 @@
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=mistral.load_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=mistral.load_api_key()),
)
async def recommend_book_async(genre: str) -> Messages.Type:
return Messages.User(f"Recommend a {genre} book")
15 changes: 13 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,18 @@
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=mistral.load_api_key()),
)
def recommend_book(genre: str) -> str:
return f"Recommend a {genre} book"


@mistral.call(
"mistral-large-latest",
client=Mistral(api_key=mistral.load_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,18 @@
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=mistral.load_api_key()),
)
@prompt_template("Recommend a {genre} book")
def recommend_book(genre: str): ...


@mistral.call(
"mistral-large-latest",
client=Mistral(api_key=mistral.load_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,5 @@
from mirascope.core import BaseMessageParam, mistral
from mistralai.client import MistralClient
from mistralai import Mistral


@mistral.call("mistral-large-latest")
Expand All @@ -8,5 +8,5 @@ def recommend_book(genre: str) -> mistral.MistralDynamicConfig:
"messages": [
BaseMessageParam(role="user", content=f"Recommend a {genre} book")
],
"client": MistralClient(),
"client": Mistral(),
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
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(),
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
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(),
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
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(),
}
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
from typing import cast

from mirascope.core import mistral
from mistralai import Mistral
from pydantic import BaseModel

client = MistralClient()
client = Mistral(api_key=mistral.load_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
17 changes: 11 additions & 6 deletions examples/learn/tools/basic_usage/mistral/official_sdk.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import json
from typing import cast

from mistralai.client import MistralClient
from mistralai import Mistral

client = MistralClient()
client = Mistral()


def get_book_author(title: str) -> str:
Expand All @@ -14,8 +15,8 @@ def get_book_author(title: str) -> str:
return "Unknown"


def identify_author(book: str) -> str:
completion = client.chat(
def identify_author(book: str) -> str | None:
completion = client.chat.complete(
model="mistral-large-latest",
messages=[{"role": "user", "content": f"Who wrote {book}?"}],
tools=[
Expand All @@ -33,10 +34,14 @@ def identify_author(book: str) -> str:
}
],
)
if not completion or not completion.choices:
return None
if tool_calls := completion.choices[0].message.tool_calls:
if tool_calls[0].function.name == "get_book_author":
return get_book_author(**json.loads(tool_calls[0].function.arguments))
return completion.choices[0].message.content
return get_book_author(
**json.loads(cast(str, tool_calls[0].function.arguments))
)
return cast(str, completion.choices[0].message.content)


author = identify_author("The Name of the Wind")
Expand Down
Loading