Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions ionic_langchain/tool.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,26 +12,26 @@
class Ionic:
_sdk: IonicSDK

def __init__(self):
self._sdk = IonicSDK()
def __init__(self, sdk: Optional[IonicSDK] = None):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

injecting the SDK facilitates testing (and any other customization that may be desired by our users down the road)

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]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought this would be more useful than the entire response



# TODO StructuredTool or BaseTool
Expand All @@ -50,5 +50,5 @@ def tool(self) -> Tool:
func=self._ionic.query,
name="Ionic Shopping",
description=TOOL_PROMPT,
verbose=True
verbose=True,
)
6 changes: 3 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "ionic-langchain"
version = "0.1.0"
version = "0.1.1"
description = ""
authors = ["Owen Sims <owen@ionicapi.com>"]
readme = "README.md"
Expand Down
51 changes: 51 additions & 0 deletions tests/test_ionic.py
Original file line number Diff line number Diff line change
@@ -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",
]
13 changes: 13 additions & 0 deletions tests/test_ionic_tool.py
Original file line number Diff line number Diff line change
@@ -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")