Langchain Tool Development Guide
Overview
iPlay provides Langchain Tool integration, allowing AI assistants to directly call the iPlay player to open and play video links.
iplay_open_async Tool
Description
iplay_open_async is an asynchronous tool for opening video links in the iPlay player. This tool returns immediately without blocking program execution.
Function Signature
python
@tool
def iplay_open_async(url: str) -> bool:
"""
Open a video link in iPlay player
Args:
url: Video link to play
Returns:
bool: True if the link was successfully constructed and opened, False otherwise
"""Parameters
url(str): The video link to playasync: Implemented through schema name, indicating that the tool returns immediately after opening iPlay without blocking
Implementation Logic
URL Validation: Verify that the input URL conforms to basic URL format specifications
Protocol Handling:
If the protocol is
httporhttps, directly replace it withiplayoriplayspython# Example http://example.com/video.mp4 -> iplay://example.com/video.mp4 https://example.com/video.mp4 -> iplays://example.com/video.mp4If it's another protocol, use the generic format:
pythoniplay://play/any?type=url&url={base64_encoded_url}
Example Code
python
from langchain.tools import tool
from urllib.parse import urlparse, urlencode
import base64
import webbrowser
@tool
def iplay_open_async(url: str) -> bool:
"""Open a video link in iPlay player (async)"""
try:
# Validate URL
parsed = urlparse(url)
if not parsed.scheme or not parsed.netloc:
return False
# Construct iPlay protocol link based on protocol type
if parsed.scheme in ['http', 'https']:
# Direct protocol replacement
iplay_scheme = 'iplays' if parsed.scheme == 'https' else 'iplay'
iplay_url = url.replace(parsed.scheme, iplay_scheme, 1)
else:
# Use generic format
encoded_url = base64.b64encode(url.encode()).decode()
iplay_url = f"iplay://play/any?type=url&url={encoded_url}"
# Open link (async, returns immediately)
webbrowser.open(iplay_url)
return True
except Exception:
return FalseUsage Example
python
from langchain.agents import initialize_agent, AgentType
from langchain.llms import OpenAI
# Initialize tools and agent
llm = OpenAI(temperature=0)
tools = [iplay_open_async]
agent = initialize_agent(
tools=tools,
llm=llm,
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
verbose=True
)
# Use agent to play video
agent.run("Please play https://example.com/video.mp4 in iPlay")Notes
- Ensure that iPlay player is installed on the user's system
- This tool returns immediately without waiting for the player to start or video to load
- For unsupported URL formats, the tool returns
False - It's recommended to add more detailed error handling and logging in production environments