LangChain Integration

AgentQL integrates with LangChain, allowing you to both extract data and take actions on the web.

LangChain helps to perform AI-powered research, workflow automation, and hands-free online interactions.

Set up AgentQL with LangChain

  1. Run the following command in your terminal to install LangChain:
terminal
pip install langchain
  1. Install a language model for your app to get started with:
terminal
pip install -qU "langchain[openai]"
  1. Create a new file main.py to instantiate your LLM containing the following:
main.py
python
import getpass
import os

from langchain.chat_models import init_chat_model

if not os.environ.get("OPENAI_API_KEY"):
  os.environ["OPENAI_API_KEY"] = getpass.getpass("Enter API key for OpenAI: ")

model = init_chat_model("gpt-4o-mini", model_provider="openai")
  1. Install AgentQL's tools:
terminal
pip install -U langchain-agentql
  1. Configure AgentQL

Configure the AGENTQL_API_KEY environment variable by running the command below in the terminal. You can get an AgentQL API key here.

terminal
export AGENTQL_API_KEY=<YOUR_AGENTQL_API_KEY>
  1. In the main.py file, import AgentQL's tools:
main.py
python
from langchain_agentql.tools import ExtractWebDataTool, ExtractWebDataBrowserTool, GetWebElementBrowserTool

You can now start building your first app on LangChain with AgentQL!

Usage

AgentQL provides the following three tools:

AgentQL also provides AgentQLBrowserToolkit that bundles ExtractWebDataBrowserTool and GetWebElementBrowserTool together. These two tools require a Playwright browser but ExtractWebDataTool doesn't. ExtractWebDataTool calls a REST API.

How to use AgentQL's AgentQLBrowserToolkit

  1. Instantiate your Playwright browser instance:
main.py
python
from langchain_agentql.utils import create_async_playwright_browser
async_browser = await create_async_playwright_browser()
  1. Choose the Playwright tools you would like to use:
main.py
python
from langchain_community.tools.playwright import NavigateTool, ClickTool

playwright_toolkit = [
    NavigateTool(async_browser=async_agent_browser),
    ClickTool(async_browser=async_agent_browser, visible_only=False)
]
playwright_toolkit

You can learn more about the AgentQL tools available and their usage in the AgentQL documentation on LangChain's site.

Support

If you have any questions about using AgentQL with LangChain, you can join the LangChain community.

Examples