Skip to main content
Open In ColabOpen on GitHub

Oxylabs

Oxylabs is a market-leading web intelligence collection platform, driven by the highest business, ethics, and compliance standards, enabling companies worldwide to unlock data-driven insights.

概览

此包包含 LangChain 与 Oxylabs 的集成,提供了利用 LangChain 框架通过 Oxylabs Web Scraper API 抓取 Google 搜索结果的实用工具。

本软件包提供以下类:

  • OxylabsSearchRun - 一个以格式化文本返回抓取到的 Google 搜索结果的工具
  • OxylabsSearchResults - 一个以 JSON 格式返回抓取到的 Google 搜索结果的实用工具
  • OxylabsSearchAPIWrapper - 用于初始化 Oxylabs API 的 API 封装器
定价
✅ Free 5,000 results for 1 week

设置

安装所需的依赖项。

%pip install -qU langchain-oxylabs

凭据

设置正确的 API 密钥和环境变量。创建您的 API 用户凭据:在 Oxylabs 仪表板 中注册免费试用或购买产品,以创建您的 API 用户凭据(OXYLABS_USERNAME 和 OXYLABS_PASSWORD)。

import getpass
import os

os.environ["OXYLABS_USERNAME"] = getpass.getpass("Enter your Oxylabs username: ")
os.environ["OXYLABS_PASSWORD"] = getpass.getpass("Enter your Oxylabs password: ")

实例化

from langchain_oxylabs import OxylabsSearchAPIWrapper, OxylabsSearchRun

oxylabs_wrapper = OxylabsSearchAPIWrapper()
tool_ = OxylabsSearchRun(wrapper=oxylabs_wrapper)

调用

使用参数直接调用

OxylabsSearchRun 工具接受单个"query"参数,该参数应为自然语言查询,并返回组合字符串格式的结果:

tool_.invoke({"query": "Restaurants in Paris."})

使用 ToolCall 调用

tool_ = OxylabsSearchRun(
wrapper=oxylabs_wrapper,
kwargs={
"result_categories": [
"local_information",
"combined_search_result",
]
},
)
from pprint import pprint

model_generated_tool_call = {
"args": {
"query": "Visit restaurants in Vilnius.",
"geo_location": "Vilnius,Lithuania",
},
"id": "1",
"name": "oxylabs_search",
"type": "tool_call",
}
tool_call_result = tool_.invoke(model_generated_tool_call)

# The content is a JSON string of results
pprint(tool_call_result.content)

在代理中使用

安装所需的依赖项。

%pip install -qU "langchain[openai]" langgraph
import getpass
import os

from langchain.chat_models import init_chat_model

os.environ["OPENAI_API_KEY"] = getpass.getpass("Enter API key for OpenAI: ")
llm = init_chat_model("gpt-4o-mini", model_provider="openai")
API 参考:init_chat_model
from langgraph.prebuilt import create_react_agent

# Initialize OxylabsSearchRun tool
tool_ = OxylabsSearchRun(wrapper=oxylabs_wrapper)

agent = create_react_agent(llm, [tool_])

user_input = "What happened in the latest Burning Man floods?"

for step in agent.stream(
{"messages": user_input},
stream_mode="values",
):
step["messages"][-1].pretty_print()
API 参考:create_react_agent

JSON 结果

OxylabsSearchResults 工具可作为 OxylabsSearchRun 的替代方案,用于以 JSON 格式检索结果:

import json

from langchain_oxylabs import OxylabsSearchResults

tool_ = OxylabsSearchResults(wrapper=oxylabs_wrapper)

response_results = tool_.invoke({"query": "What are the most famous artists?"})
response_results = json.loads(response_results)

for result in response_results:
for key, value in result.items():
print(f"{key}: {value}")

API 参考

有关此集成包的更多信息,请参见此处:https://github.com/oxylabs/langchain-oxylabs

Oxylabs 网络爬虫 API 文档:https://developers.oxylabs.io/scraper-apis/web-scraper-api