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

Limit tool call id to 9 chars for Mistral #279

Merged
merged 3 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions src/magentic/function_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@


def _create_unique_id() -> str:
# OpenAI has max length of 29 chars for function call IDs
return uuid4().hex[:29]
# Mistral has max length of 9 chars for tool call IDs, and OpenAI 29 chars
return uuid4().hex[:9]


class FunctionCall(Generic[T]):
Expand Down
35 changes: 34 additions & 1 deletion tests/chat_model/test_mistral_chat_model.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import pytest
from pydantic import BaseModel

from magentic.chat_model.message import Usage, UserMessage
from magentic.chat_model.message import (
AssistantMessage,
SystemMessage,
Usage,
UserMessage,
)
from magentic.chat_model.mistral_chat_model import MistralChatModel
from magentic.function_call import (
AsyncParallelFunctionCall,
Expand Down Expand Up @@ -90,6 +96,33 @@ def minus(a: int, b: int) -> int:
assert len(list(message.content)) == 2


@pytest.mark.mistral
def test_mistral_chat_model_few_shot_prompt():
class Quote(BaseModel):
quote: str
character: str

chat_model = MistralChatModel("mistral-large-latest")
message = chat_model.complete(
messages=[
SystemMessage("You are a movie buff."),
UserMessage("What is your favorite quote from Harry Potter?"),
AssistantMessage(
Quote(
quote="It does not do to dwell on dreams and forget to live.",
character="Albus Dumbledore",
)
),
# Mistral requires AssistantMessage after tool output
# TODO: Automatically add this in ChatModel.complete like for tool calls
AssistantMessage("."),
Comment on lines +116 to +118
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mnicstruwig FYI, should only be needed if doing few-shot prompting.

The OpenAI and Mistral APIs are starting to diverge so I've made an issue to switch to using the Mistral python client. #280

UserMessage("What is your favorite quote from {movie}?"),
],
output_types=[Quote],
)
assert isinstance(message.content, Quote)


@pytest.mark.parametrize(
("prompt", "output_types", "expected_output_type"),
[
Expand Down