first version of long context compression

This commit is contained in:
garylin2099 2024-07-31 22:07:48 +08:00
parent 17247b5518
commit a6104f3931
5 changed files with 190 additions and 3 deletions

View file

@ -41,6 +41,7 @@ class MockBaseLLM(BaseLLM):
return default_resp_cont
@pytest.mark.skip
def test_base_llm():
message = Message(role="user", content="hello")
assert "role" in message.to_dict()
@ -92,6 +93,7 @@ def test_base_llm():
# assert resp == default_resp_cont
@pytest.mark.skip
@pytest.mark.asyncio
async def test_async_base_llm():
base_llm = MockBaseLLM()
@ -104,3 +106,44 @@ async def test_async_base_llm():
# resp = await base_llm.aask_code([prompt])
# assert resp == default_resp_cont
@pytest.mark.parametrize(
"compress_type", ["post_cut_by_msg", "post_cut_by_token", "pre_cut_by_msg", "pre_cut_by_token"]
)
def test_compress_messages_no_effect(compress_type):
base_llm = MockBaseLLM()
messages = [
{"role": "system", "content": "first system msg"},
{"role": "system", "content": "second system msg"},
]
for i in range(5):
messages.append({"role": "user", "content": f"u{i}"})
messages.append({"role": "assistant", "content": f"a{i}"})
compressed = base_llm.compress_messages(messages, compress_type=compress_type)
# should take no effect for short context
assert compressed == messages
@pytest.mark.parametrize(
"compress_type", ["post_cut_by_msg", "post_cut_by_token", "pre_cut_by_msg", "pre_cut_by_token"]
)
def test_compress_messages_long(compress_type):
base_llm = MockBaseLLM()
base_llm.config.model = "test_llm"
max_token_limit = 100
messages = [
{"role": "system", "content": "first system msg"},
{"role": "system", "content": "second system msg"},
]
for i in range(100):
messages.append({"role": "user", "content": f"u{i}" * 10}) # ~2x10x0.5 = 10 tokens
messages.append({"role": "assistant", "content": f"a{i}" * 10})
compressed = base_llm.compress_messages(messages, compress_type=compress_type, max_token=max_token_limit)
print(compressed)
print(len(compressed))
assert len(compressed) < len(messages)
assert compressed[0]["role"] == "system" and compressed[1]["role"] == "system"
assert compressed[2]["role"] != "system"

View file

@ -164,3 +164,47 @@ async def test_openai_acompletion(mocker):
assert resp.usage == usage
await llm_general_chat_funcs_test(llm, prompt, messages, resp_cont)
def test_count_tokens():
llm = LLM()
llm.config.model = "gpt-4o"
messages = [
llm._system_msg("some system msg"),
llm._system_msg("some system message 2"),
llm._user_msg("user 1"),
llm._assistant_msg("assistant 1"),
llm._user_msg("user 1"),
llm._assistant_msg("assistant 2"),
]
cnt = llm.count_tokens(messages)
print(cnt)
def test_count_tokens_long():
llm = LLM()
llm.config.model = "gpt-4-0613"
test_msg_content = " ".join([str(i) for i in range(100000)])
messages = [
llm._system_msg("You are a helpful assistant"),
llm._user_msg(test_msg_content + " what's the first number you see?"),
]
cnt = llm.count_tokens(messages) # 299023, ~300k
print(cnt)
llm.config.model = "test_llm" # a non-openai model, will use heuristics base count_tokens
cnt = llm.count_tokens(messages) # 294474, ~300k, ~2% difference
print(cnt)
@pytest.mark.skip
@pytest.mark.asyncio
async def test_aask_long():
llm = LLM()
llm.config.model = "deepseek-ai/DeepSeek-Coder-V2-Instruct" # deepseek-coder on siliconflow, limit 32k
test_msg_content = " ".join([str(i) for i in range(100000)]) # corresponds to ~300k tokens
messages = [
llm._system_msg("You are a helpful assistant"),
llm._user_msg(test_msg_content + " what's the first number you see?"),
]
await llm.aask(messages) # should not fail with context truncated