-
Notifications
You must be signed in to change notification settings - Fork 0
[ION-281] use ionic SDK in langchain tool #1
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
8 commits
Select commit
Hold shift + click to select a range
7d6387e
📦 upgrade langchain to 0.0.350
b450d47
🙈 add .idea to .gitignore
9031b0c
📦 install and use ionic-api-sdk
2278638
✂️ cut IonicPluginTool
12e8cb1
specify num_results
a1955d2
🏷️ bump minor version
37e3b28
Revert "specify num_results"
6a197ee
🎨 optimize imports
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,60 +1,54 @@ | ||
| # Temp inlining of "SDK" | ||
| import requests | ||
| import dataclasses | ||
| from typing import Any | ||
|
|
||
| from langchain.tools import AIPluginTool, Tool | ||
| 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_langchain.prompt import TOOL_PROMPT | ||
|
|
||
|
|
||
| class Ionic: | ||
| PROD_SERVER = "https://api.ionicapi.com" | ||
|
|
||
| def __init__(self, base_url=None): | ||
| self.base_url = base_url or self.PROD_SERVER | ||
|
|
||
| def query(self, queries: str): | ||
| url = f"{self.base_url}/query" | ||
|
|
||
| query_list = [{"query": query} for query in queries.split(", ")] | ||
|
|
||
| payload = {"queries": query_list} | ||
| result = requests.post(url, json=payload) | ||
| _sdk: IonicSDK | ||
|
|
||
| def __init__(self): | ||
| self._sdk = IonicSDK() | ||
|
|
||
| def query(self, queries: str) -> dict[str, Any]: | ||
| """ | ||
| FIXME: handle non-200 responses | ||
| TODO: better typing in response | ||
| """ | ||
| request = QueryAPIRequest( | ||
| queries=[ | ||
| Query(query=query) | ||
| for query in queries.split(", ") | ||
| ], | ||
| ) | ||
| response: QueryResponse = self._sdk.query( | ||
| request=request, | ||
| security=QuerySecurity(), | ||
| ) | ||
|
|
||
| return result.json() | ||
| return dataclasses.asdict(response) | ||
|
|
||
|
|
||
| # TODO StructuredTool or BaseTool | ||
| # https://github.com/langchain-ai/langchain/issues/4197 | ||
| # https://python.langchain.com/docs/modules/agents/tools/multi_input_tool | ||
| # https://github.com/langchain-ai/langchain/issues/4197 | ||
| # https://python.langchain.com/docs/modules/agents/tools/multi_input_tool | ||
|
|
||
|
|
||
| class IonicTool(): | ||
| def __init__(self, base_url=None): | ||
| self.ionic = Ionic() | ||
| class IonicTool: | ||
| _ionic: Ionic | ||
|
|
||
| if base_url: | ||
| self.ionic.base_url = base_url | ||
| def __init__(self): | ||
| self._ionic = Ionic() | ||
|
|
||
| def tool(self): | ||
| def tool(self) -> Tool: | ||
| return Tool( | ||
| func=self.ionic.query, | ||
| func=self._ionic.query, | ||
| name="Ionic Shopping", | ||
| description=TOOL_PROMPT, | ||
| verbose=True | ||
| ) | ||
|
|
||
|
|
||
|
|
||
|
|
||
| class IonicPluginTool(): | ||
| def __init__(self, base_url="https://api.ionicapi.com"): | ||
| self.base_url = base_url | ||
|
|
||
| def tool(self): | ||
| # tool = AIPluginTool.from_plugin_url("https://www.klarna.com/.well-known/ai-plugin.json") | ||
| tool = AIPluginTool.from_plugin_url(f'{self.base_url}/.well-known/ai-plugin.json') | ||
| print(tool) | ||
|
|
||
| return tool | ||
|
|
||
|
|
||
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.
@owensims1 is this still relevant? I see it commented out in the demobot
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.
there's gonna be two ways to integrate a langchsin tool-- first is using the sdk which is how i had been using since the beginning.
this is the second, where after we built the openai plugin route, we noticed that langchsin has an integration solution for this, but i found it to not work anywhere near as well as advertised, and i lost interest in pursuing at the time.
so, feel free to remove entirely for now until we can bring it back in an official capacity. let's stick with the sdk connection that affords us a bit more control