mirror of
https://github.com/hwchase17/langchain
synced 2024-10-31 15:20:26 +00:00
238 lines
6.7 KiB
Plaintext
238 lines
6.7 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# DataForSeo API Wrapper\n",
|
|
"This notebook demonstrates how to use the DataForSeo API wrapper to obtain search engine results. The DataForSeo API allows users to retrieve SERP from most popular search engines like Google, Bing, Yahoo. It also allows to get SERPs from different search engine types like Maps, News, Events, etc.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from langchain.utilities.dataforseo_api_search import DataForSeoAPIWrapper"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Setting up the API wrapper with your credentials\n",
|
|
"You can obtain your API credentials by registering on the DataForSeo website."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import os\n",
|
|
"\n",
|
|
"os.environ[\"DATAFORSEO_LOGIN\"] = \"your_api_access_username\"\n",
|
|
"os.environ[\"DATAFORSEO_PASSWORD\"] = \"your_api_access_password\"\n",
|
|
"\n",
|
|
"wrapper = DataForSeoAPIWrapper()"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"The run method will return the first result snippet from one of the following elements: answer_box, knowledge_graph, featured_snippet, shopping, organic."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"wrapper.run(\"Weather in Los Angeles\")"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## The Difference Between `run` and `results`\n",
|
|
"`run` and `results` are two methods provided by the `DataForSeoAPIWrapper` class.\n",
|
|
"\n",
|
|
"The `run` method executes the search and returns the first result snippet from the answer box, knowledge graph, featured snippet, shopping, or organic results. These elements are sorted by priority from highest to lowest.\n",
|
|
"\n",
|
|
"The `results` method returns a JSON response configured according to the parameters set in the wrapper. This allows for more flexibility in terms of what data you want to return from the API."
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Getting Results as JSON\n",
|
|
"You can customize the result types and fields you want to return in the JSON response. You can also set a maximum count for the number of top results to return."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"json_wrapper = DataForSeoAPIWrapper(\n",
|
|
" json_result_types=[\"organic\", \"knowledge_graph\", \"answer_box\"],\n",
|
|
" json_result_fields=[\"type\", \"title\", \"description\", \"text\"],\n",
|
|
" top_count=3,\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"json_wrapper.results(\"Bill Gates\")"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Customizing Location and Language\n",
|
|
"You can specify the location and language of your search results by passing additional parameters to the API wrapper."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"customized_wrapper = DataForSeoAPIWrapper(\n",
|
|
" top_count=10,\n",
|
|
" json_result_types=[\"organic\", \"local_pack\"],\n",
|
|
" json_result_fields=[\"title\", \"description\", \"type\"],\n",
|
|
" params={\"location_name\": \"Germany\", \"language_code\": \"en\"},\n",
|
|
")\n",
|
|
"customized_wrapper.results(\"coffee near me\")"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Customizing the Search Engine\n",
|
|
"You can also specify the search engine you want to use."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"customized_wrapper = DataForSeoAPIWrapper(\n",
|
|
" top_count=10,\n",
|
|
" json_result_types=[\"organic\", \"local_pack\"],\n",
|
|
" json_result_fields=[\"title\", \"description\", \"type\"],\n",
|
|
" params={\"location_name\": \"Germany\", \"language_code\": \"en\", \"se_name\": \"bing\"},\n",
|
|
")\n",
|
|
"customized_wrapper.results(\"coffee near me\")"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Customizing the Search Type\n",
|
|
"The API wrapper also allows you to specify the type of search you want to perform. For example, you can perform a maps search."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"maps_search = DataForSeoAPIWrapper(\n",
|
|
" top_count=10,\n",
|
|
" json_result_fields=[\"title\", \"value\", \"address\", \"rating\", \"type\"],\n",
|
|
" params={\n",
|
|
" \"location_coordinate\": \"52.512,13.36,12z\",\n",
|
|
" \"language_code\": \"en\",\n",
|
|
" \"se_type\": \"maps\",\n",
|
|
" },\n",
|
|
")\n",
|
|
"maps_search.results(\"coffee near me\")"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Integration with Langchain Agents\n",
|
|
"You can use the `Tool` class from the `langchain.agents` module to integrate the `DataForSeoAPIWrapper` with a langchain agent. The `Tool` class encapsulates a function that the agent can call."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from langchain.agents import Tool\n",
|
|
"\n",
|
|
"search = DataForSeoAPIWrapper(\n",
|
|
" top_count=3,\n",
|
|
" json_result_types=[\"organic\"],\n",
|
|
" json_result_fields=[\"title\", \"description\", \"type\"],\n",
|
|
")\n",
|
|
"tool = Tool(\n",
|
|
" name=\"google-search-answer\",\n",
|
|
" description=\"My new answer tool\",\n",
|
|
" func=search.run,\n",
|
|
")\n",
|
|
"json_tool = Tool(\n",
|
|
" name=\"google-search-json\",\n",
|
|
" description=\"My new json tool\",\n",
|
|
" func=search.results,\n",
|
|
")"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.10.11"
|
|
},
|
|
"orig_nbformat": 4
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|