Ionic Langchain

Ionic LangChain Tool

Ionic Langchain provides a wrapper around the Ionic Commerce’s SDK for use as a Tool in a custom Langchain agent. This tool will enable e-commerce for your agent, allowing your users to ask for product recommendations and purchase products through the agent chat interface.

Langchain Template

Get started quickly using LangServe and the Ionic Shopping Assistant Template

Installation

This tool requires at least langchain@0.1 and can work with any greater patch release the ^0.1.x series.

We currently support python 3.8.10 and above, but if you need support for a lower version, please open an issue and we will add support.

You can install the package from PyPI using pip:

python3 -m pip install ionic-langchain

or poetry:

poetry add ionic-langchain

Usage

from ionic_langchain.tool import IonicTool
from langchain import hub
from langchain.agents import AgentExecutor, create_openai_tools_agent
from langchain_openai import ChatOpenAI

# Based on OpenAI Tools Agent
# https://python.langchain.com/docs/modules/agents/agent_types/openai_tools

model = "gpt-3.5-turbo-1106"
temperature = 0.6

# Choose the LLM that will drive the agent
# Only certain models support this
llm = ChatOpenAI(model=model, temperature=temperature, openai_api_key=openai_api_key)

ionic_tool = IonicTool().tool()

# The tool comes with its own prompt,
# but you may also update it directly via the description attribute:
ionic_tool.description = str(
    """
Ionic is an e-commerce shopping tool. Assistant uses the Ionic Commerce Shopping Tool to find, discover, and compare products from thousands of online retailers. Assistant should use the tool when the user is looking for a product recommendation or trying to find a specific product.

The user may specify the number of results, minimum price, and maximum price for which they want to see results.
Ionic Tool input is a comma-separated string of values:
  - query string (required, must not include commas)
  - number of results (default to 4, no more than 10)
  - minimum price in cents ($5 becomes 500)
  - maximum price in cents
For example, if looking for coffee beans between 5 and 10 dollars, the tool input would be `coffee beans, 5, 500, 1000`.

Return them as a markdown formatted list with each recommendation from tool results, being sure to include the full PDP URL. For example:

1. Product 1: [Price] -- link
2. Product 2: [Price] -- link
3. Product 3: [Price] -- link
4. Product 4: [Price] -- link
"""
)

tools = [ionic_tool]

# default prompt for openai tools agent
prompt = hub.pull("hwchase17/openai-tools-agent")

# Construct the OpenAI Tools agent
agent = create_openai_tools_agent(llm, tools, prompt)

agent_executor = AgentExecutor(
    agent=agent, tools=tools, handle_parsing_errors=True, verbose=True, max_iterations=5
)

Customizing the SDK

ionic_langchain.tool.IonicTool’s constructor accepts an instance of ionic_langchain.tool.Ionic, a wrapper around our SDK. ionic_langchain.tool.Ionic, in turn accepts an instance of that SDK, so you can provide the tool with a custom configuration:

import os
from ionic.sdk import Ionic as IonicSDK
from ionic_langchain.tool import Ionic, IonicTool


sdk = IonicSDK(api_key_header=os.environ.get("IONIC_API_KEY"))
ionic = Ionic(sdk=sdk)
tool = IonicTool(ionic=ionic).tool()