mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-26 19:24:27 +00:00
add test cases for token trimmer
This commit is contained in:
parent
f50da2e06e
commit
df80e62566
1 changed files with 58 additions and 0 deletions
58
litellm/tests/test_utils.py
Normal file
58
litellm/tests/test_utils.py
Normal file
|
@ -0,0 +1,58 @@
|
|||
import sys, os
|
||||
import traceback
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
import os
|
||||
|
||||
sys.path.insert(
|
||||
0, os.path.abspath("../..")
|
||||
) # Adds the parent directory to the system path
|
||||
import pytest
|
||||
import litellm
|
||||
from litellm.utils import safe_messages, get_token_count
|
||||
|
||||
# Assuming your trim_messages, shorten_message_to_fit_limit, and get_token_count functions are all in a module named 'message_utils'
|
||||
|
||||
# Test 1: Check trimming of normal message
|
||||
def test_basic_trimming():
|
||||
messages = [{"role": "user", "content": "This is a long message that definitely exceeds the token limit."}]
|
||||
trimmed_messages = safe_messages(messages, model="claude-2", max_tokens=8)
|
||||
print("trimmed messages")
|
||||
print(trimmed_messages)
|
||||
print(get_token_count(messages=trimmed_messages, model="claude-2"))
|
||||
assert (get_token_count(messages=trimmed_messages, model="claude-2")) <= 8
|
||||
# test_basic_trimming()
|
||||
|
||||
def test_multiple_messages_trimming():
|
||||
messages = [
|
||||
{"role": "user", "content": "This is a long message that will exceed the token limit."},
|
||||
{"role": "user", "content": "This is another long message that will also exceed the limit."}
|
||||
]
|
||||
trimmed_messages = safe_messages(messages=messages, model="gpt-3.5-turbo", max_tokens=20)
|
||||
print("Trimmed messages")
|
||||
print(trimmed_messages)
|
||||
print(get_token_count(messages=trimmed_messages, model="gpt-3.5-turbo"))
|
||||
assert(get_token_count(messages=trimmed_messages, model="gpt-3.5-turbo")) <= 20
|
||||
# test_multiple_messages_trimming()
|
||||
|
||||
def test_multiple_messages_no_trimming():
|
||||
messages = [
|
||||
{"role": "user", "content": "This is a long message that will exceed the token limit."},
|
||||
{"role": "user", "content": "This is another long message that will also exceed the limit."}
|
||||
]
|
||||
trimmed_messages = safe_messages(messages=messages, model="gpt-3.5-turbo", max_tokens=100)
|
||||
print("Trimmed messages")
|
||||
print(trimmed_messages)
|
||||
assert(messages==trimmed_messages)
|
||||
|
||||
# test_multiple_messages_no_trimming()
|
||||
|
||||
|
||||
def test_large_trimming():
|
||||
messages = [{"role": "user", "content": "This is a singlelongwordthatexceedsthelimit."}, {"role": "user", "content": "This is a singlelongwordthatexceedsthelimit."},{"role": "user", "content": "This is a singlelongwordthatexceedsthelimit."},{"role": "user", "content": "This is a singlelongwordthatexceedsthelimit."},{"role": "user", "content": "This is a singlelongwordthatexceedsthelimit."}]
|
||||
trimmed_messages = safe_messages(messages, max_tokens=20, model="random")
|
||||
print("trimmed messages")
|
||||
print(trimmed_messages)
|
||||
assert(get_token_count(messages=trimmed_messages, model="random")) <= 20
|
||||
# test_large_trimming()
|
Loading…
Add table
Add a link
Reference in a new issue