-
Notifications
You must be signed in to change notification settings - Fork 0
[ION-281] use all the API parameters #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1c8e832
📦 add black/poethepoet
023bb7f
WIP many args
8d11a4c
wip
stewartjarod f96b50c
Merge main/ion 281 (#16)
stewartjarod 57635bd
Merge branch 'main' into gmkohler/ION-281
stewartjarod b9b31c8
add poe tests
stewartjarod d6892bc
Test multiple queries
stewartjarod e516ada
update Ionic SDK and only allow a single query
stewartjarod f409645
update docstring
stewartjarod 0b1d061
revert back to using query_input
stewartjarod 262741b
Allow a single comma seperated string input
stewartjarod f1406f5
refactor and add tests
stewartjarod File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,10 +1,12 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import dataclasses | ||
| from typing import Any, Optional | ||
| from typing import Any, Optional, Sequence | ||
|
|
||
| from ionic import Ionic as IonicSDK | ||
| from ionic.models.components import QueryAPIRequest, Query | ||
| from ionic.models.operations import QuerySecurity, QueryResponse | ||
| from langchain.tools import Tool | ||
| from ionic.models.components import Query as SDKQuery, QueryAPIRequest | ||
| from ionic.models.operations import QueryResponse, QuerySecurity | ||
| from langchain_core.tools import Tool | ||
|
|
||
| from ionic_langchain.prompt import TOOL_PROMPT | ||
|
|
||
|
|
@@ -18,20 +20,53 @@ def __init__(self, sdk: Optional[IonicSDK] = None): | |
| else: | ||
| self._sdk = IonicSDK() | ||
|
|
||
| def query(self, queries: str) -> list: | ||
| """ | ||
| FIXME: handle non-200 responses | ||
| TODO: better typing in response | ||
| """ | ||
| def query( | ||
| self, | ||
| query_input: str, | ||
| ) -> Sequence[dict[str, Any]]: | ||
| if not query_input: | ||
| raise ValueError("query must not be empty") | ||
|
|
||
| query, num_results, min_price, max_price = self.gen_query_request(query_input) | ||
| request = QueryAPIRequest( | ||
| queries=[Query(query=query) for query in queries.split(", ")], | ||
| query=SDKQuery( | ||
| query=query, | ||
| num_results=num_results, | ||
| min_price=min_price, | ||
| max_price=max_price, | ||
| ) | ||
| ) | ||
| response: QueryResponse = self._sdk.query( | ||
| request=request, | ||
| security=QuerySecurity(), | ||
| ) | ||
|
|
||
| return [dataclasses.asdict(r) for r in response.query_api_response.results] | ||
| return [ | ||
| dataclasses.asdict(result) for result in response.query_api_response.results | ||
| ] | ||
|
|
||
| def _parse_number(self, value: str) -> int | None: | ||
| return int(value) if value and int(value) >= 0 else None | ||
|
|
||
| def gen_query_request(self, query_input: str) -> tuple[str, int | None, int | None, int | None]: | ||
| if not query_input: | ||
| raise ValueError("query must not be empty") | ||
|
|
||
| split_query = query_input.split(",") | ||
| len4_query = split_query + [None] * (4 - len(split_query)) # pad with None | ||
|
|
||
| query, num_results, min_price, max_price, *rest = [ # *rest ignores extra values | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How certain are we that the given
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. its the format that is specified in the prompt. 😅 |
||
| item.strip() if item is not None else None for item in len4_query | ||
| ] | ||
| if not query: | ||
| raise ValueError("query must not be empty") | ||
|
|
||
| return ( | ||
| str(query), | ||
| self._parse_number(num_results), | ||
| self._parse_number(min_price), | ||
| self._parse_number(max_price), | ||
| ) | ||
|
|
||
|
|
||
| class IonicTool: | ||
|
|
@@ -44,7 +79,11 @@ def __init__(self, ionic: Optional[Ionic] = None): | |
| self._ionic = Ionic() | ||
|
|
||
| def tool(self) -> Tool: | ||
| return Tool( | ||
| """ | ||
| - https://github.com/langchain-ai/langchain/issues/4197 | ||
| - https://python.langchain.com/docs/modules/agents/tools/multi_input_tool | ||
| """ | ||
| return Tool.from_function( | ||
| func=self._ionic.query, | ||
| name="Ionic Commerce Shopping Tool", | ||
| description=TOOL_PROMPT, | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could stand for a broader update this is pretty old. Added a ticket for later: https://linear.app/ionic-commerce/issue/ION-535/update-langchain-prompt