Skip to main content

langchain-agentql

AgentQL provides web interaction and structured data extraction from any web page using an AgentQL query or a Natural Language prompt. AgentQL can be used across multiple languages and web pages without breaking over time and change.

Installation

pip install -U langchain-agentql

You also need to configure the AGENTQL_API_KEY environment variable. You can acquire an API key from our Dev Portal.

Document Loader

AgentQLLoader is a document loader that uses AgentQL query to extract structured data from a web page.

from langchain_agentql.document_loaders import AgentQLLoader

loader = AgentQLLoader(
    url="https://www.agentql.com/blog",
    query="""
    {
        posts[] {
            title
            url
            date
            author
        }
    }
    """,
    is_scroll_to_bottom_enabled=True
)
docs = loader.load()

You can learn more about how to use AgentQLLoader in this Jupyter notebook.

Tools/Toolkits

AgentQL provides the following three tools:

  • ExtractWebDataTool: Extracts structured data as JSON from a web page given a URL using either an AgentQL query or a Natural Language description of the data.

  • ExtractWebDataBrowserTool: Extracts structured data as JSON from the active web page in a browser using either an AgentQL query or a Natural Language description. This tool must be used with a Playwright browser.

  • GetWebElementBrowserTool: Finds a web element on the active web page in a browser using a Natural Language description and returns its CSS selector for further interaction. This tool must be used with a Playwright browser.

We also provide an AgentQLBrowserToolkit toolkit with both ExtractWebDataBrowserTool and GetWebElementBrowserTool browser tools bundled.

You can learn more about how to use AgentQL tools in this Jupyter notebook.

Extract data using REST API

from langchain_agentql.tools import ExtractWebDataTool

extract_web_data_tool = ExtractWebDataTool()
extract_web_data_tool.invoke({
    'url': 'https://www.agentql.com/blog', 
    'query': '{ posts[] { title url date author } }', 
})

Work with data and web elements using browser

Setup

In order to use the ExtractWebDataBrowserTool and GetWebElementBrowserTool, you need to have a Playwright browser instance. If you do not have an active instance, you can initiate one using the create_async_playwright_browser or create_sync_playwright_browser methods:

from langchain_agentql.utils import create_async_playwright_browser
async_browser = await create_async_playwright_browser()

You can also use an existing browser instance via Chrome DevTools Protocol (CDP) connection URL:

p = await async_playwright().start()
async_browser = await p.chromium.connect_over_cdp("CDP_CONNECTION_URL")

Extract data from the active browser page

from langchain_agentql.tools import ExtractWebDataBrowserTool

extract_web_data_browser_tool = ExtractWebDataBrowserTool(async_browser=async_browser)
json_data = await extract_web_data_browser_tool.ainvoke({'prompt': 'The blog posts with title, url, date of post and author'})

Find a web element on the active browser page

from langchain_agentql.tools import GetWebElementBrowserTool

get_web_element_browser_tool = GetWebElementBrowserTool(async_browser=async_browser)
selector = await get_web_element_browser_tool.ainvoke({'prompt': 'The next page navigation button'})

Agentic Usage

This tool has a more extensive example for agentic usage documented in this Jupyter notebook

Run Tests

In order to run integration tests, you need to configure LLM credentials by setting the OPENAI_API_KEY environment variables first. Then run the tests with the following command:

make integration_tests

Release files for langchain-agentql 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for langchain-agentql 1.0.1
File Size Uploaded
langchain_agentql-1.0.1.tar.gz 10.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for langchain-agentql 1.0.1
File Interpreter ABI Platform
langchain_agentql-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 28.0 kB

Release files / langchain_agentql-1.0.1.tar.gz

Download URL langchain_agentql-1.0.1.tar.gz
Size 10.9 kB
Tags Source
SHA-256 checksum
How to use checksums
39377193e88961f9b03aaa2ce90487415fbfe4a981dcb6cf0de7da67875189da
BLAKE2b-256 checksum
How to use checksums
ea0295900900b876723e9105174000929266c3d92e7971e914cab826281b27e9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

Release files / langchain_agentql-1.0.1-py3-none-any.whl

Download URL langchain_agentql-1.0.1-py3-none-any.whl
Size 17.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
59baa65d78a4084d54f1edf1e8dec9eea393a20c122722d8242da4a1a2d00fb6
BLAKE2b-256 checksum
How to use checksums
3a2a68e654e5bfacbdc02bb9b5ee794dbd676243248f9e07fcfc9d409381f246
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page