Skip to main content
Open In Colab在 GitHub 上打开

Apify 角色

Apify Actors 是专为各种 Web 抓取、爬取和数据提取任务而设计的云程序。这些参与者促进了从 Web 自动收集数据,使用户能够有效地提取、处理和存储信息。参与者可用于执行诸如抓取电子商务网站以获取产品详细信息、监控价格变化或收集搜索引擎结果等任务。它们与 Apify 数据集无缝集成,允许以 JSON、CSV 或 Excel 等格式存储、管理和导出演员收集的结构化数据,以供进一步分析或使用。

概述

此笔记本将引导您将 Apify Actors 与 LangChain 结合使用,以自动执行 Web 抓取和数据提取。这langchain-apifypackage 将 Apify 基于云的工具与 LangChain 代理集成,为 AI 应用程序实现高效的数据收集和处理。

设置

此集成位于 langchain-apify 包中。可以使用 pip 安装该软件包。

%pip install langchain-apify

先决条件

  • Apify 帐户在此处注册您的免费 Apify 帐户。
  • Apify API 令牌:在 Apify 文档中了解如何获取 API 令牌。
import os

os.environ["APIFY_API_TOKEN"] = "your-apify-api-token"
os.environ["OPENAI_API_KEY"] = "your-openai-api-key"

实例

这里我们实例化ApifyActorsTool能够调用 RAG Web 浏览器 Apify Actor。此 Actor 为 AI 和 LLM 应用程序提供 Web 浏览功能,类似于 ChatGPT 中的 Web 浏览功能。Apify Store 中的任何 Actor 都可以以这种方式使用。

from langchain_apify import ApifyActorsTool

tool = ApifyActorsTool("apify/rag-web-browser")

调用

ApifyActorsTool接受一个参数,即run_input- 作为 run input 传递给 Actor 的字典。Run input schema 文档可以在 Actor details (作者详细信息) 页面的 input 部分找到。请参阅 RAG Web 浏览器输入架构

tool.invoke({"run_input": {"query": "what is apify?", "maxResults": 2}})

链接

我们可以将创建的工具提供给代理。当要求搜索信息时,代理将调用 Apify Actor,后者将搜索 Web,然后检索搜索结果。

%pip install langgraph langchain-openai
from langchain_core.messages import ToolMessage
from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent

model = ChatOpenAI(model="gpt-4o")
tools = [tool]
graph = create_react_agent(model, tools=tools)
inputs = {"messages": [("user", "search for what is Apify")]}
for s in graph.stream(inputs, stream_mode="values"):
message = s["messages"][-1]
# skip tool messages
if isinstance(message, ToolMessage):
continue
message.pretty_print()
================================ Human Message =================================

search for what is Apify
================================== Ai Message ==================================
Tool Calls:
apify_actor_apify_rag-web-browser (call_27mjHLzDzwa5ZaHWCMH510lm)
Call ID: call_27mjHLzDzwa5ZaHWCMH510lm
Args:
run_input: {"run_input":{"query":"Apify","maxResults":3,"outputFormats":["markdown"]}}
================================== Ai Message ==================================

Apify is a comprehensive platform for web scraping, browser automation, and data extraction. It offers a wide array of tools and services that cater to developers and businesses looking to extract data from websites efficiently and effectively. Here's an overview of Apify:

1. **Ecosystem and Tools**:
- Apify provides an ecosystem where developers can build, deploy, and publish data extraction and web automation tools called Actors.
- The platform supports various use cases such as extracting data from social media platforms, conducting automated browser-based tasks, and more.

2. **Offerings**:
- Apify offers over 3,000 ready-made scraping tools and code templates.
- Users can also build custom solutions or hire Apify's professional services for more tailored data extraction needs.

3. **Technology and Integration**:
- The platform supports integration with popular tools and services like Zapier, GitHub, Google Sheets, Pinecone, and more.
- Apify supports open-source tools and technologies such as JavaScript, Python, Puppeteer, Playwright, Selenium, and its own Crawlee library for web crawling and browser automation.

4. **Community and Learning**:
- Apify hosts a community on Discord where developers can get help and share expertise.
- It offers educational resources through the Web Scraping Academy to help users become proficient in data scraping and automation.

5. **Enterprise Solutions**:
- Apify provides enterprise-grade web data extraction solutions with high reliability, 99.95% uptime, and compliance with SOC2, GDPR, and CCPA standards.

For more information, you can visit [Apify's official website](https://apify.com/) or their [GitHub page](https://github.com/apify) which contains their code repositories and further details about their projects.

API 参考

有关如何使用此集成的更多信息,请参阅 git 存储库Apify 集成文档