-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We can set `usage_limit` to be sure that we do not exceed a token limit. Raises `LotusUsageLimitException` if exceeded.
- Loading branch information
Showing
4 changed files
with
135 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,41 @@ | ||
import pytest | ||
|
||
from lotus.models import LM | ||
from lotus.types import LotusUsageLimitException, UsageLimit | ||
from tests.base_test import BaseTest | ||
|
||
|
||
class TestLM(BaseTest): | ||
def test_lm_initialization(self): | ||
lm = LM(model="gpt-4o-mini") | ||
assert isinstance(lm, LM) | ||
|
||
def test_lm_token_usage_limit(self): | ||
# Test prompt token limit | ||
usage_limit = UsageLimit(prompt_tokens_limit=100) | ||
lm = LM(model="gpt-4o-mini", usage_limit=usage_limit) | ||
short_prompt = "What is the capital of France? Respond in one word." | ||
messages = [[{"role": "user", "content": short_prompt}]] | ||
lm(messages) | ||
|
||
long_prompt = "What is the capital of France? Respond in one word." * 50 | ||
messages = [[{"role": "user", "content": long_prompt}]] | ||
with pytest.raises(LotusUsageLimitException): | ||
lm(messages) | ||
|
||
# Test completion token limit | ||
usage_limit = UsageLimit(completion_tokens_limit=10) | ||
lm = LM(model="gpt-4o-mini", usage_limit=usage_limit) | ||
long_response_prompt = "Write a 100 word essay about the history of France" | ||
messages = [[{"role": "user", "content": long_response_prompt}]] | ||
with pytest.raises(LotusUsageLimitException): | ||
lm(messages) | ||
|
||
# Test total token limit | ||
usage_limit = UsageLimit(total_tokens_limit=50) | ||
lm = LM(model="gpt-4o-mini", usage_limit=usage_limit) | ||
messages = [[{"role": "user", "content": short_prompt}]] | ||
lm(messages) # First call should work | ||
with pytest.raises(LotusUsageLimitException): | ||
for _ in range(5): # Multiple calls to exceed total limit | ||
lm(messages) |