Skip to content

Commit

Permalink
Add a util for loading Mistral API key
Browse files Browse the repository at this point in the history
  • Loading branch information
skylarbpayne committed Oct 29, 2024
1 parent 6602329 commit 63bcfb8
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 24 deletions.
4 changes: 2 additions & 2 deletions examples/learn/calls/basic_usage/mistral/official_sdk_call.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from mirascope.core import mistral
from mistralai import Mistral
import os

client = Mistral(api_key=os.environ.get("MISTRAL_API_KEY", ""))
client = Mistral(api_key=mistral.load_api_key())


def recommend_book(genre: str) -> str:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import os

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


@mistral.call(
"mistral-large-latest",
client=Mistral(api_key=os.environ.get("MISTRAL_API_KEY", "")),
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=os.environ.get("MISTRAL_API_KEY", "")),
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")]
6 changes: 2 additions & 4 deletions examples/learn/calls/custom_client/mistral/messages.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import os

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


@mistral.call(
"mistral-large-latest",
client=Mistral(api_key=os.environ.get("MISTRAL_API_KEY", "")),
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=os.environ.get("MISTRAL_API_KEY", "")),
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")
6 changes: 2 additions & 4 deletions examples/learn/calls/custom_client/mistral/shorthand.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import os

from mirascope.core import mistral
from mistralai import Mistral


@mistral.call(
"mistral-large-latest",
client=Mistral(api_key=os.environ.get("MISTRAL_API_KEY", "")),
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=os.environ.get("MISTRAL_API_KEY", "")),
client=Mistral(api_key=mistral.load_api_key()),
)
async def recommend_book_async(genre: str) -> str:
return f"Recommend a {genre} book"
6 changes: 2 additions & 4 deletions examples/learn/calls/custom_client/mistral/string_template.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import os

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


@mistral.call(
"mistral-large-latest",
client=Mistral(api_key=os.environ.get("MISTRAL_API_KEY", "")),
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=os.environ.get("MISTRAL_API_KEY", "")),
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,10 +1,9 @@
import os

from mistralai.client import Mistral
from mirascope.core import mistral
from mistralai import Mistral
from mistralai.models import ToolChoice
from pydantic import BaseModel

client = Mistral(api_key=os.environ.get("MISTRAL_API_KEY", ""))
client = Mistral(api_key=mistral.load_api_key())


class Book(BaseModel):
Expand Down
2 changes: 2 additions & 0 deletions mirascope/core/mistral/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from ..base import BaseMessageParam
from ._call import mistral_call
from ._call import mistral_call as call
from ._utils._load_api_key import load_api_key
from .call_params import MistralCallParams
from .call_response import MistralCallResponse
from .call_response_chunk import MistralCallResponseChunk
Expand All @@ -25,6 +26,7 @@

__all__ = [
"call",
"load_api_key",
"MistralDynamicConfig",
"MistralCallParams",
"MistralCallResponse",
Expand Down
6 changes: 6 additions & 0 deletions mirascope/core/mistral/_utils/_load_api_key.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import os


def load_api_key() -> str:
"""Load the API key from the standard environment variable."""
return os.environ.get("MISTRAL_API_KEY", "")
4 changes: 2 additions & 2 deletions mirascope/core/mistral/_utils/_setup_call.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""This module contains the setup_call function for Mistral tools."""

import os
from collections.abc import (
Awaitable,
Callable,
Expand All @@ -19,6 +18,7 @@
UserMessage,
)

from ... import mistral
from ...base import BaseTool, _utils
from ...base._utils import AsyncCreateFn, CreateFn, get_async_create_fn, get_create_fn
from ...base._utils._protocols import fn_is_async
Expand Down Expand Up @@ -114,7 +114,7 @@ def setup_call(
call_kwargs |= {"model": model, "messages": messages}

if client is None:
client = Mistral(api_key=os.environ.get("MISTRAL_API_KEY", ""))
client = Mistral(api_key=mistral.load_api_key())
if fn_is_async(fn):
create_or_stream = get_async_create_fn(
client.chat.complete_async, client.chat.stream_async
Expand Down

0 comments on commit 63bcfb8

Please sign in to comment.