generated from langchain-ai/integration-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from 3coins/add-stream-for-sm
Added stream function, added tests
- Loading branch information
Showing
2 changed files
with
140 additions
and
45 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
92 changes: 85 additions & 7 deletions
92
libs/aws/tests/integration_tests/llms/test_sagemaker_endpoint.py
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,21 +1,99 @@ | ||
import json | ||
from typing import Dict | ||
from unittest.mock import Mock | ||
|
||
from langchain_aws.llms import SagemakerEndpoint | ||
from langchain_aws.llms.sagemaker_endpoint import LLMContentHandler | ||
|
||
|
||
class ContentHandler(LLMContentHandler): | ||
class DefaultHandler(LLMContentHandler): | ||
accepts = "application/json" | ||
content_type = "application/json" | ||
|
||
def transform_input(self, prompt: str, model_kwargs: Dict) -> bytes: | ||
return b"" | ||
return prompt.encode() | ||
|
||
def transform_output(self, output: bytes) -> str: | ||
return "" | ||
body = json.loads(output.decode()) | ||
return body[0]["generated_text"] | ||
|
||
|
||
def test_sagemaker_endpoint_invoke() -> None: | ||
client = Mock() | ||
response = { | ||
"ContentType": "application/json", | ||
"Body": b'[{"generated_text": "SageMaker Endpoint"}]', | ||
} | ||
client.invoke_endpoint.return_value = response | ||
|
||
def test_sagemaker_endpoint_name_param() -> None: | ||
llm = SagemakerEndpoint( | ||
endpoint_name="foo", | ||
content_handler=ContentHandler(), | ||
endpoint_name="my-endpoint", | ||
region_name="us-west-2", | ||
content_handler=DefaultHandler(), | ||
model_kwargs={ | ||
"parameters": { | ||
"max_new_tokens": 50, | ||
} | ||
}, | ||
client=client, | ||
) | ||
|
||
service_response = llm.invoke("What is Sagemaker endpoints?") | ||
|
||
assert service_response == "SageMaker Endpoint" | ||
client.invoke_endpoint.assert_called_once_with( | ||
EndpointName="my-endpoint", | ||
Body=b"What is Sagemaker endpoints?", | ||
ContentType="application/json", | ||
Accept="application/json", | ||
) | ||
|
||
|
||
def test_sagemaker_endpoint_stream() -> None: | ||
class ContentHandler(LLMContentHandler): | ||
accepts = "application/json" | ||
content_type = "application/json" | ||
|
||
def transform_input(self, prompt: str, model_kwargs: Dict) -> bytes: | ||
body = json.dumps({"inputs": prompt, **model_kwargs}) | ||
return body.encode() | ||
|
||
def transform_output(self, output: bytes) -> str: | ||
body = json.loads(output) | ||
return body.get("outputs")[0] | ||
|
||
body = ( | ||
{"PayloadPart": {"Bytes": b'{"outputs": ["S"]}\n'}}, | ||
{"PayloadPart": {"Bytes": b'{"outputs": ["age"]}\n'}}, | ||
{"PayloadPart": {"Bytes": b'{"outputs": ["Maker"]}\n'}}, | ||
) | ||
|
||
response = {"ContentType": "application/json", "Body": body} | ||
|
||
client = Mock() | ||
client.invoke_endpoint_with_response_stream.return_value = response | ||
|
||
llm = SagemakerEndpoint( | ||
endpoint_name="my-endpoint", | ||
region_name="us-west-2", | ||
content_handler=ContentHandler(), | ||
client=client, | ||
model_kwargs={"parameters": {"max_new_tokens": 50}}, | ||
) | ||
|
||
expected_body = json.dumps( | ||
{"inputs": "What is Sagemaker endpoints?", "parameters": {"max_new_tokens": 50}} | ||
).encode() | ||
|
||
chunks = ["S", "age", "Maker"] | ||
service_chunks = [] | ||
|
||
for chunk in llm.stream("What is Sagemaker endpoints?"): | ||
service_chunks.append(chunk) | ||
|
||
assert service_chunks == chunks | ||
client.invoke_endpoint_with_response_stream.assert_called_once_with( | ||
EndpointName="my-endpoint", | ||
Body=expected_body, | ||
ContentType="application/json", | ||
) | ||
assert llm.endpoint_name == "foo" |