From 700858dc0f3f7341049a82859880ca89d3b29ecd Mon Sep 17 00:00:00 2001 From: Gregory Kohler Date: Thu, 21 Dec 2023 10:52:15 -0800 Subject: [PATCH 1/3] tests --- ionic_langchain/tool.py | 20 ++++++++-------- tests/test_ionic.py | 51 ++++++++++++++++++++++++++++++++++++++++ tests/test_ionic_tool.py | 13 ++++++++++ 3 files changed, 74 insertions(+), 10 deletions(-) create mode 100644 tests/test_ionic.py create mode 100644 tests/test_ionic_tool.py diff --git a/ionic_langchain/tool.py b/ionic_langchain/tool.py index 6983cb5..e601850 100644 --- a/ionic_langchain/tool.py +++ b/ionic_langchain/tool.py @@ -1,5 +1,5 @@ import dataclasses -from typing import Any +from typing import Any, Optional from ionic import Ionic as IonicSDK from ionic.models.components import QueryAPIRequest, Query @@ -12,26 +12,26 @@ class Ionic: _sdk: IonicSDK - def __init__(self): - self._sdk = IonicSDK() + def __init__(self, sdk: Optional[IonicSDK] = None): + if sdk: + self._sdk = sdk + else: + self._sdk = IonicSDK() - def query(self, queries: str) -> dict[str, Any]: + def query(self, queries: str) -> list[dict[str, Any]]: """ FIXME: handle non-200 responses TODO: better typing in response """ request = QueryAPIRequest( - queries=[ - Query(query=query) - for query in queries.split(", ") - ], + queries=[Query(query=query) for query in queries.split(", ")], ) response: QueryResponse = self._sdk.query( request=request, security=QuerySecurity(), ) - return dataclasses.asdict(response) + return [dataclasses.asdict(r) for r in response.query_api_response.results] # TODO StructuredTool or BaseTool @@ -50,5 +50,5 @@ def tool(self) -> Tool: func=self._ionic.query, name="Ionic Shopping", description=TOOL_PROMPT, - verbose=True + verbose=True, ) diff --git a/tests/test_ionic.py b/tests/test_ionic.py new file mode 100644 index 0000000..dbaff34 --- /dev/null +++ b/tests/test_ionic.py @@ -0,0 +1,51 @@ +import pytest +from ionic import Ionic as IonicSdk +from ionic.models.errors import HTTPValidationError + +from ionic_langchain.tool import Ionic + + +def test_ionic_num_results(): + """ + requires server to be running (i.e. not compatible with CI) + """ + ionic = Ionic( + sdk=IonicSdk(server_url="http://localhost:8080"), + ) + results = ionic.query(queries="Reindeer Jerky, Salmon Jerky") + + assert len(results) == 2, "results are returned for each query" + reindeer_jerky_result = results[0] + assert ( + reindeer_jerky_result["query"]["query"] == "Reindeer Jerky" + ), "query should be included in response object" + assert reindeer_jerky_result["query"]["num_results"] is None + assert reindeer_jerky_result["query"]["max_price"] is None + assert reindeer_jerky_result["query"]["min_price"] is None + assert "products" in reindeer_jerky_result + assert ( + len(reindeer_jerky_result["products"]) == 5 + ), "num_results should be the server default" + + +@pytest.mark.skip("we aren't yet passing in the validated params") +def test_ionic_bad_input(): + """ + requires server to be running + """ + ionic = Ionic( + sdk=IonicSdk( + server_url="http://localhost:8080", + ), + ) + + with pytest.raises(HTTPValidationError) as exc_info: + ionic.query(queries="") + + problems = [det.loc[-1] for det in exc_info.value.detail] + assert len(problems) == 3, "all problems are included in error" + assert sorted(problems) == [ + "max_price", + "min_price", + "num_results", + ] diff --git a/tests/test_ionic_tool.py b/tests/test_ionic_tool.py new file mode 100644 index 0000000..ecee650 --- /dev/null +++ b/tests/test_ionic_tool.py @@ -0,0 +1,13 @@ +import pytest + +from ionic_langchain.tool import IonicTool + + +def test_ionic_tool_is_valid(): + """ + sanity check to ensure tool is valid + """ + try: + IonicTool().tool() + except Exception: + pytest.fail("unexpected exception %s initializing IonicTool#tool") \ No newline at end of file From 0e610a3393a77a7c1d070aea2078f305e1e4a685 Mon Sep 17 00:00:00 2001 From: Gregory Kohler Date: Thu, 21 Dec 2023 11:16:03 -0800 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=A6=20update=20ionic-sdk-python?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- poetry.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/poetry.lock b/poetry.lock index 4f9eb22..0f4c314 100644 --- a/poetry.lock +++ b/poetry.lock @@ -453,13 +453,13 @@ files = [ [[package]] name = "ionic-api-sdk" -version = "0.5.2" +version = "0.5.3" description = "Python Client SDK" optional = false python-versions = ">=3.8" files = [ - {file = "Ionic-API-SDK-0.5.2.tar.gz", hash = "sha256:c52df7e680628bafb685b8af8f1332eebaa6d3c1df749a82fe6930285a0efc65"}, - {file = "Ionic_API_SDK-0.5.2-py3-none-any.whl", hash = "sha256:9508ba1cb63ff8f366acac3455f73fd3a4d0afde639b48f24559b20e3f1aea15"}, + {file = "Ionic-API-SDK-0.5.3.tar.gz", hash = "sha256:f57e9e6150b0839d34eafbc8e2039b5bdfdb0eb8a87f8ccf72c4501104297f08"}, + {file = "Ionic_API_SDK-0.5.3-py3-none-any.whl", hash = "sha256:afa5e3f8bf66e809d30180133ad04d896465dbeeaeee1bb24bad0bc651383722"}, ] [package.dependencies] From a23ab93f921854abac104beb1967c8116dddef31 Mon Sep 17 00:00:00 2001 From: Gregory Kohler Date: Thu, 21 Dec 2023 11:17:26 -0800 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=8F=B7=EF=B8=8F=20bump=20version?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 505d5c4..5b57bd5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "ionic-langchain" -version = "0.1.0" +version = "0.1.1" description = "" authors = ["Owen Sims "] readme = "README.md"