|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "65142ccb18a6add5", |
| 6 | + "metadata": {}, |
| 7 | + "source": "# Oxylabs" |
| 8 | + }, |
| 9 | + { |
| 10 | + "cell_type": "markdown", |
| 11 | + "id": "19976df355f9c461", |
| 12 | + "metadata": {}, |
| 13 | + "source": [ |
| 14 | + ">[Oxylabs](https://oxylabs.io/) 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.\n", |
| 15 | + "\n", |
| 16 | + "## Overview\n", |
| 17 | + "\n", |
| 18 | + "This package contains the LangChain integration with Oxylabs, providing tools to scrape Google search results with Oxylabs Web Scraper API using LangChain's framework.\n", |
| 19 | + "\n", |
| 20 | + "The following classes are provided by this package:\n", |
| 21 | + "- `OxylabsSearchRun` - A tool that returns scraped Google search results in a formatted text\n", |
| 22 | + "- `OxylabsSearchResults` - A tool that returns scraped Google search results in a JSON format\n", |
| 23 | + "- `OxylabsSearchAPIWrapper` - An API wrapper for initializing Oxylabs API" |
| 24 | + ] |
| 25 | + }, |
| 26 | + { |
| 27 | + "cell_type": "markdown", |
| 28 | + "id": "76e6d8030199710d", |
| 29 | + "metadata": {}, |
| 30 | + "source": [ |
| 31 | + "| Pricing |\n", |
| 32 | + "|:-------------------------------:|\n", |
| 33 | + "| ✅ Free 5,000 results for 1 week |" |
| 34 | + ] |
| 35 | + }, |
| 36 | + { |
| 37 | + "cell_type": "markdown", |
| 38 | + "id": "eb59c5f3051be9d4", |
| 39 | + "metadata": {}, |
| 40 | + "source": "## Setup" |
| 41 | + }, |
| 42 | + { |
| 43 | + "cell_type": "markdown", |
| 44 | + "id": "45951d881c460419", |
| 45 | + "metadata": {}, |
| 46 | + "source": "Install the required dependencies." |
| 47 | + }, |
| 48 | + { |
| 49 | + "cell_type": "code", |
| 50 | + "id": "576e08c41f16ceda", |
| 51 | + "metadata": {}, |
| 52 | + "source": [ |
| 53 | + "%pip install -qU langchain-oxylabs" |
| 54 | + ], |
| 55 | + "outputs": [], |
| 56 | + "execution_count": null |
| 57 | + }, |
| 58 | + { |
| 59 | + "cell_type": "markdown", |
| 60 | + "id": "ce19bb3a52a3ab60", |
| 61 | + "metadata": {}, |
| 62 | + "source": "### Credentials" |
| 63 | + }, |
| 64 | + { |
| 65 | + "cell_type": "markdown", |
| 66 | + "id": "b8330dfd5861482e", |
| 67 | + "metadata": {}, |
| 68 | + "source": "Set up the proper API keys and environment variables. Create your API user credentials: Sign up for a free trial or purchase the product in the [Oxylabs dashboard](https://dashboard.oxylabs.io/en/registration) to create your API user credentials (OXYLABS_USERNAME and OXYLABS_PASSWORD)." |
| 69 | + }, |
| 70 | + { |
| 71 | + "cell_type": "code", |
| 72 | + "id": "474b6eaf6e35efda", |
| 73 | + "metadata": {}, |
| 74 | + "source": [ |
| 75 | + "import getpass\n", |
| 76 | + "import os\n", |
| 77 | + "\n", |
| 78 | + "os.environ[\"OXYLABS_USERNAME\"] = getpass.getpass(\"Enter your Oxylabs username: \")\n", |
| 79 | + "os.environ[\"OXYLABS_PASSWORD\"] = getpass.getpass(\"Enter your Oxylabs password: \")" |
| 80 | + ], |
| 81 | + "outputs": [], |
| 82 | + "execution_count": null |
| 83 | + }, |
| 84 | + { |
| 85 | + "cell_type": "markdown", |
| 86 | + "id": "ae310f86b113bd78", |
| 87 | + "metadata": {}, |
| 88 | + "source": "## Instantiation" |
| 89 | + }, |
| 90 | + { |
| 91 | + "cell_type": "code", |
| 92 | + "id": "69ea8139f6152b48", |
| 93 | + "metadata": {}, |
| 94 | + "source": [ |
| 95 | + "from langchain_oxylabs import OxylabsSearchAPIWrapper, OxylabsSearchRun\n", |
| 96 | + "\n", |
| 97 | + "oxylabs_wrapper = OxylabsSearchAPIWrapper()\n", |
| 98 | + "tool_ = OxylabsSearchRun(wrapper=oxylabs_wrapper)" |
| 99 | + ], |
| 100 | + "outputs": [], |
| 101 | + "execution_count": null |
| 102 | + }, |
| 103 | + { |
| 104 | + "cell_type": "markdown", |
| 105 | + "id": "636efff6267b9bc1", |
| 106 | + "metadata": {}, |
| 107 | + "source": "## Invocation" |
| 108 | + }, |
| 109 | + { |
| 110 | + "cell_type": "markdown", |
| 111 | + "id": "272f5fdaed5fa000", |
| 112 | + "metadata": {}, |
| 113 | + "source": "### Invoke directly with args" |
| 114 | + }, |
| 115 | + { |
| 116 | + "cell_type": "markdown", |
| 117 | + "id": "3c26724911f6cfdf", |
| 118 | + "metadata": {}, |
| 119 | + "source": "The `OxylabsSearchRun` tool takes a single \"query\" argument, which should be a natural language query and returns combined string format result:" |
| 120 | + }, |
| 121 | + { |
| 122 | + "cell_type": "code", |
| 123 | + "id": "b5f4d423cb3abf", |
| 124 | + "metadata": {}, |
| 125 | + "source": [ |
| 126 | + "tool_.invoke({\"query\": \"Restaurants in Paris.\"})" |
| 127 | + ], |
| 128 | + "outputs": [], |
| 129 | + "execution_count": null |
| 130 | + }, |
| 131 | + { |
| 132 | + "cell_type": "markdown", |
| 133 | + "id": "784aad18bba31062", |
| 134 | + "metadata": {}, |
| 135 | + "source": "### Invoke with ToolCall" |
| 136 | + }, |
| 137 | + { |
| 138 | + "cell_type": "code", |
| 139 | + "id": "8afd1652e2c4e7c9", |
| 140 | + "metadata": {}, |
| 141 | + "source": [ |
| 142 | + "tool_ = OxylabsSearchRun(\n", |
| 143 | + " wrapper=oxylabs_wrapper,\n", |
| 144 | + " kwargs={\n", |
| 145 | + " \"result_categories\": [\n", |
| 146 | + " \"local_information\",\n", |
| 147 | + " \"combined_search_result\",\n", |
| 148 | + " ]\n", |
| 149 | + " },\n", |
| 150 | + ")" |
| 151 | + ], |
| 152 | + "outputs": [], |
| 153 | + "execution_count": null |
| 154 | + }, |
| 155 | + { |
| 156 | + "cell_type": "code", |
| 157 | + "id": "1b903fb2f8272df2", |
| 158 | + "metadata": {}, |
| 159 | + "source": [ |
| 160 | + "from pprint import pprint\n", |
| 161 | + "\n", |
| 162 | + "model_generated_tool_call = {\n", |
| 163 | + " \"args\": {\n", |
| 164 | + " \"query\": \"Visit restaurants in Vilnius.\",\n", |
| 165 | + " \"geo_location\": \"Vilnius,Lithuania\",\n", |
| 166 | + " },\n", |
| 167 | + " \"id\": \"1\",\n", |
| 168 | + " \"name\": \"oxylabs_search\",\n", |
| 169 | + " \"type\": \"tool_call\",\n", |
| 170 | + "}\n", |
| 171 | + "tool_call_result = tool_.invoke(model_generated_tool_call)\n", |
| 172 | + "\n", |
| 173 | + "# The content is a JSON string of results\n", |
| 174 | + "pprint(tool_call_result.content)" |
| 175 | + ], |
| 176 | + "outputs": [], |
| 177 | + "execution_count": null |
| 178 | + }, |
| 179 | + { |
| 180 | + "cell_type": "markdown", |
| 181 | + "id": "a436fc39ce17b6da", |
| 182 | + "metadata": {}, |
| 183 | + "source": [ |
| 184 | + "## Use within an agent\n", |
| 185 | + "Install the required dependencies." |
| 186 | + ] |
| 187 | + }, |
| 188 | + { |
| 189 | + "cell_type": "code", |
| 190 | + "id": "a6f23e65092e8d27", |
| 191 | + "metadata": {}, |
| 192 | + "source": "%pip install -qU \"langchain[openai]\" langgraph", |
| 193 | + "outputs": [], |
| 194 | + "execution_count": null |
| 195 | + }, |
| 196 | + { |
| 197 | + "cell_type": "code", |
| 198 | + "id": "21078a0c265759ff", |
| 199 | + "metadata": {}, |
| 200 | + "source": [ |
| 201 | + "import getpass\n", |
| 202 | + "import os\n", |
| 203 | + "\n", |
| 204 | + "from langchain.chat_models import init_chat_model\n", |
| 205 | + "\n", |
| 206 | + "os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"Enter API key for OpenAI: \")\n", |
| 207 | + "llm = init_chat_model(\"gpt-4o-mini\", model_provider=\"openai\")" |
| 208 | + ], |
| 209 | + "outputs": [], |
| 210 | + "execution_count": null |
| 211 | + }, |
| 212 | + { |
| 213 | + "cell_type": "code", |
| 214 | + "id": "396548ee2f08fb30", |
| 215 | + "metadata": {}, |
| 216 | + "source": [ |
| 217 | + "from langgraph.prebuilt import create_react_agent\n", |
| 218 | + "\n", |
| 219 | + "# Initialize OxylabsSearchRun tool\n", |
| 220 | + "tool_ = OxylabsSearchRun(wrapper=oxylabs_wrapper)\n", |
| 221 | + "\n", |
| 222 | + "agent = create_react_agent(llm, [tool_])\n", |
| 223 | + "\n", |
| 224 | + "user_input = \"What happened in the latest Burning Man floods?\"\n", |
| 225 | + "\n", |
| 226 | + "for step in agent.stream(\n", |
| 227 | + " {\"messages\": user_input},\n", |
| 228 | + " stream_mode=\"values\",\n", |
| 229 | + "):\n", |
| 230 | + " step[\"messages\"][-1].pretty_print()" |
| 231 | + ], |
| 232 | + "outputs": [], |
| 233 | + "execution_count": null |
| 234 | + }, |
| 235 | + { |
| 236 | + "cell_type": "markdown", |
| 237 | + "id": "b10bbcf5ce299fd5", |
| 238 | + "metadata": {}, |
| 239 | + "source": [ |
| 240 | + "## JSON results\n", |
| 241 | + "`OxylabsSearchResults` tool can be used as an alternative to `OxylabsSearchRun` to retrieve results in a JSON format:" |
| 242 | + ] |
| 243 | + }, |
| 244 | + { |
| 245 | + "cell_type": "code", |
| 246 | + "id": "ef0d856f571c4938", |
| 247 | + "metadata": {}, |
| 248 | + "source": [ |
| 249 | + "import json\n", |
| 250 | + "\n", |
| 251 | + "from langchain_oxylabs import OxylabsSearchResults\n", |
| 252 | + "\n", |
| 253 | + "tool_ = OxylabsSearchResults(wrapper=oxylabs_wrapper)\n", |
| 254 | + "\n", |
| 255 | + "response_results = tool_.invoke({\"query\": \"What are the most famous artists?\"})\n", |
| 256 | + "response_results = json.loads(response_results)\n", |
| 257 | + "\n", |
| 258 | + "for result in response_results:\n", |
| 259 | + " for key, value in result.items():\n", |
| 260 | + " print(f\"{key}: {value}\")" |
| 261 | + ], |
| 262 | + "outputs": [], |
| 263 | + "execution_count": null |
| 264 | + }, |
| 265 | + { |
| 266 | + "cell_type": "markdown", |
| 267 | + "id": "7f16702d224dabb2", |
| 268 | + "metadata": {}, |
| 269 | + "source": [ |
| 270 | + "## API reference\n", |
| 271 | + "More information about this integration package can be found here: https://github.com/oxylabs/langchain-oxylabs\n", |
| 272 | + "\n", |
| 273 | + "Oxylabs Web Scraper API documentation: https://developers.oxylabs.io/scraper-apis/web-scraper-api\n" |
| 274 | + ] |
| 275 | + } |
| 276 | + ], |
| 277 | + "metadata": { |
| 278 | + "kernelspec": { |
| 279 | + "display_name": "Python 3", |
| 280 | + "language": "python", |
| 281 | + "name": "python3" |
| 282 | + }, |
| 283 | + "language_info": { |
| 284 | + "codemirror_mode": { |
| 285 | + "name": "ipython", |
| 286 | + "version": 2 |
| 287 | + }, |
| 288 | + "file_extension": ".py", |
| 289 | + "mimetype": "text/x-python", |
| 290 | + "name": "python", |
| 291 | + "nbconvert_exporter": "python", |
| 292 | + "pygments_lexer": "ipython2", |
| 293 | + "version": "2.7.6" |
| 294 | + } |
| 295 | + }, |
| 296 | + "nbformat": 4, |
| 297 | + "nbformat_minor": 5 |
| 298 | +} |
0 commit comments