-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Clean input before passing it to the llm
- Loading branch information
1 parent
6814cf7
commit f40ca8c
Showing
3 changed files
with
67 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from graphiti_core.llm_client.client import LLMClient | ||
from graphiti_core.llm_client.config import LLMConfig | ||
|
||
|
||
class TestLLMClient(LLMClient): | ||
"""Concrete implementation of LLMClient for testing""" | ||
|
||
async def _generate_response(self, messages, response_model=None): | ||
return {'content': 'test'} | ||
|
||
|
||
def test_clean_input(): | ||
client = TestLLMClient(LLMConfig()) | ||
|
||
test_cases = [ | ||
# Basic text should remain unchanged | ||
('Hello World', 'Hello World'), | ||
# Control characters should be removed | ||
('Hello\x00World', 'HelloWorld'), | ||
# Newlines, tabs, returns should be preserved | ||
('Hello\nWorld\tTest\r', 'Hello\nWorld\tTest\r'), | ||
# Invalid Unicode should be removed | ||
('Hello\udcdeWorld', 'HelloWorld'), | ||
# Zero-width characters should be removed | ||
('Hello\u200bWorld', 'HelloWorld'), | ||
('Test\ufeffWord', 'TestWord'), | ||
# Multiple issues combined | ||
('Hello\x00\u200b\nWorld\udcde', 'Hello\nWorld'), | ||
# Empty string should remain empty | ||
('', ''), | ||
# Form feed and other control characters from the error case | ||
('{"edges":[{"relation_typ...\f\x04Hn\\?"}]}', '{"edges":[{"relation_typ...Hn\\?"}]}'), | ||
# More specific control character tests | ||
('Hello\x0cWorld', 'HelloWorld'), # form feed \f | ||
('Hello\x04World', 'HelloWorld'), # end of transmission | ||
# Combined JSON-like string with control characters | ||
('{"test": "value\f\x00\x04"}', '{"test": "value"}'), | ||
] | ||
|
||
for input_str, expected in test_cases: | ||
assert client._clean_input(input_str) == expected, f'Failed for input: {repr(input_str)}' |