mirror of
https://github.com/tiennm99/DocsGPT.git
synced 2026-10-14 04:15:35 +00:00
fix: cleanup models, better retry strat
This commit is contained in:
1 parent
c3d605f07a
commit
32597df153
20 files changed
+849
-671
No files matched your search
@@ -124,114 +124,6 @@ class TestBaseConnectorAbstract:
|
||||
assert "secret" not in result
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 20. application/llm/sagemaker.py (lines 52,60,64,67,74,88,106,140)
|
||||
# ---------------------------------------------------------------------------
|
||||
@pytest.mark.unit
|
||||
class TestSagemakerLineIterator:
|
||||
def test_line_iterator_basic(self):
|
||||
from application.llm.sagemaker import LineIterator
|
||||
|
||||
chunks = [
|
||||
{"PayloadPart": {"Bytes": b'{"outputs": [" hello"]}\n'}},
|
||||
{"PayloadPart": {"Bytes": b'{"outputs": [" world"]}\n'}},
|
||||
]
|
||||
it = LineIterator(iter(chunks))
|
||||
lines = list(it)
|
||||
assert len(lines) == 2
|
||||
assert b"hello" in lines[0]
|
||||
|
||||
def test_line_iterator_split_json(self):
|
||||
from application.llm.sagemaker import LineIterator
|
||||
|
||||
chunks = [
|
||||
{"PayloadPart": {"Bytes": b'{"outputs": '}},
|
||||
{"PayloadPart": {"Bytes": b'[" split"]}\n'}},
|
||||
]
|
||||
it = LineIterator(iter(chunks))
|
||||
lines = list(it)
|
||||
assert len(lines) == 1
|
||||
|
||||
def test_line_iterator_unknown_event(self):
|
||||
from application.llm.sagemaker import LineIterator
|
||||
|
||||
# The source code on line 55 does `print("Unknown event type:" + chunk)`
|
||||
# which will TypeError when chunk is a dict. We verify that line 54
|
||||
# is covered by catching the error.
|
||||
chunks = [
|
||||
{"InternalServerException": {"Message": "oops"}},
|
||||
{"PayloadPart": {"Bytes": b'{"outputs": ["ok"]}\n'}},
|
||||
]
|
||||
it = LineIterator(iter(chunks))
|
||||
# The first chunk triggers line 54 branch, but line 55 raises
|
||||
# TypeError due to str + dict concat bug in source.
|
||||
# We just confirm the branch is reached.
|
||||
with pytest.raises(TypeError):
|
||||
list(it)
|
||||
|
||||
def test_sagemaker_llm_init(self):
|
||||
with patch("boto3.client") as mock_boto:
|
||||
mock_boto.return_value = MagicMock()
|
||||
from application.llm.sagemaker import SagemakerAPILLM
|
||||
|
||||
llm = SagemakerAPILLM(api_key="k", user_api_key="uk")
|
||||
assert llm.api_key == "k"
|
||||
assert llm.user_api_key == "uk"
|
||||
assert llm.runtime is not None
|
||||
|
||||
def test_sagemaker_raw_gen(self):
|
||||
with patch("boto3.client") as mock_boto:
|
||||
mock_runtime = MagicMock()
|
||||
body_content = json.dumps(
|
||||
[{"generated_text": "PREFIX ANSWER"}]
|
||||
).encode("utf-8")
|
||||
mock_body = MagicMock()
|
||||
mock_body.read.return_value = body_content
|
||||
mock_runtime.invoke_endpoint.return_value = {"Body": mock_body}
|
||||
mock_boto.return_value = mock_runtime
|
||||
|
||||
from application.llm.sagemaker import SagemakerAPILLM
|
||||
|
||||
llm = SagemakerAPILLM()
|
||||
messages = [
|
||||
{"content": "context", "role": "system"},
|
||||
{"content": "question", "role": "user"},
|
||||
]
|
||||
result = llm._raw_gen(None, "model", messages)
|
||||
assert isinstance(result, str)
|
||||
|
||||
def test_sagemaker_raw_gen_stream(self):
|
||||
with patch("boto3.client") as mock_boto:
|
||||
mock_runtime = MagicMock()
|
||||
|
||||
event_stream = [
|
||||
{
|
||||
"PayloadPart": {
|
||||
"Bytes": b'{"token": {"text": "hello"}}\n'
|
||||
}
|
||||
},
|
||||
{
|
||||
"PayloadPart": {
|
||||
"Bytes": b'{"token": {"text": "</s>"}}\n'
|
||||
}
|
||||
},
|
||||
]
|
||||
mock_runtime.invoke_endpoint_with_response_stream.return_value = {
|
||||
"Body": iter(event_stream)
|
||||
}
|
||||
mock_boto.return_value = mock_runtime
|
||||
|
||||
from application.llm.sagemaker import SagemakerAPILLM
|
||||
|
||||
llm = SagemakerAPILLM()
|
||||
messages = [
|
||||
{"content": "context", "role": "system"},
|
||||
{"content": "question", "role": "user"},
|
||||
]
|
||||
chunks = list(llm._raw_gen_stream(None, "model", messages))
|
||||
assert "hello" in chunks
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 9. application/agents/tools/spec_parser.py (lines 58-59, 71-82, 173-176, 179-180)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
Reference in new issue
Block a user