Skip to content
Open
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
20 changes: 20 additions & 0 deletions sdk/python/feast/context/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,21 @@

from feast.context.errors import (
ContextError,
RequiredSectionError,
TokenBudgetExceededError,
TokenizerNotFoundError,
TokenizerUnavailableError,
)
from feast.context.priority_select import (
DEFAULT_SEPARATOR,
Priority,
PrioritySpec,
Section,
SectionOrder,
SectionSpec,
Selection,
priority_select,
)
from feast.context.token_budget import TokenBudget
from feast.context.tokenizer import (
ApproximateTokenizer,
Expand All @@ -26,7 +37,15 @@
"ApproximateTokenizer",
"Cl100kBaseTokenizer",
"ContextError",
"DEFAULT_SEPARATOR",
"O200kBaseTokenizer",
"Priority",
"PrioritySpec",
"RequiredSectionError",
"Section",
"SectionOrder",
"SectionSpec",
"Selection",
"TiktokenTokenizer",
"TokenBudget",
"TokenBudgetExceededError",
Expand All @@ -36,4 +55,5 @@
"TokenizerSpec",
"TokenizerUnavailableError",
"get_tokenizer",
"priority_select",
]
37 changes: 34 additions & 3 deletions sdk/python/feast/context/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,45 @@ def http_status_code(self) -> int:


class TokenBudgetExceededError(ContextError):
def __init__(self, requested: int, remaining: int, max_tokens: int):
def __init__(
self,
requested: int,
remaining: int,
max_tokens: int,
*,
message: str = "",
):
super().__init__(
f"Cannot consume {requested} tokens: only {remaining} of "
f"{max_tokens} tokens remain in the budget."
message
or (
f"Cannot consume {requested} tokens: only {remaining} of "
f"{max_tokens} tokens remain in the budget."
)
)
self.requested = requested
self.remaining = remaining
self.max_tokens = max_tokens

def http_status_code(self) -> int:
return HttpStatusCode.HTTP_400_BAD_REQUEST


class RequiredSectionError(TokenBudgetExceededError):
"""A section that must not be dropped does not fit the budget.

Raised instead of quietly returning a prompt missing its instructions:
the caller has to widen the budget or shorten the section.
"""

def __init__(self, section: str, requested: int, remaining: int, max_tokens: int):
super().__init__(
requested,
remaining,
max_tokens,
message=(
f"Section {section} is critical and cannot be dropped, but it "
f"needs {requested} tokens with only {remaining} of "
f"{max_tokens} left in the budget."
),
)
self.section = section
Loading
Loading