Skip to content

Commit

Permalink
Add truncate for context length
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickvonplaten committed Jan 14, 2025
1 parent a117be8 commit c2ee542
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/mistralai/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def complete(
frequency_penalty: Optional[float] = None,
n: OptionalNullable[int] = UNSET,
safe_prompt: Optional[bool] = None,
truncate_for_context_length: bool = False,
retries: OptionalNullable[utils.RetryConfig] = UNSET,
server_url: Optional[str] = None,
timeout_ms: Optional[int] = None,
Expand Down Expand Up @@ -93,6 +94,7 @@ def complete(
frequency_penalty=frequency_penalty,
n=n,
safe_prompt=safe_prompt,
truncate_for_context_length=truncate_for_context_length,
)

req = self._build_request(
Expand Down
4 changes: 4 additions & 0 deletions src/mistralai/models/chatcompletionrequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ class ChatCompletionRequest(BaseModel):
safe_prompt: Optional[bool] = None
r"""Whether to inject a safety prompt before all conversations."""

truncate_for_context_length: bool = False,
r"""Whether to truncate the history to fit the context length."""

@model_serializer(mode="wrap")
def serialize_model(self, handler):
optional_fields = [
Expand All @@ -146,6 +149,7 @@ def serialize_model(self, handler):
"frequency_penalty",
"n",
"safe_prompt",
"truncate_for_context_length",
]
nullable_fields = [
"model",
Expand Down

0 comments on commit c2ee542

Please sign in to comment.