Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Add question answering over a website to web scraping #10637

Merged
merged 2 commits into from
Sep 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
65 changes: 61 additions & 4 deletions docs/extras/use_cases/web_scraping.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -453,11 +453,11 @@
"\n",
"Related to scraping, we may want to answer specific questions using searched content.\n",
"\n",
"We can automate the process of [web research](https://blog.langchain.dev/automating-web-research/) using a retriver, such as the `WebResearchRetriever` ([docs](https://python.langchain.com/docs/modules/data_connection/retrievers/web_research)).\n",
"We can automate the process of [web research](https://blog.langchain.dev/automating-web-research/) using a retriever, such as the `WebResearchRetriever` ([docs](https://python.langchain.com/docs/modules/data_connection/retrievers/web_research)).\n",
"\n",
"![Image description](/img/web_research.png)\n",
"\n",
"Copy requirments [from here](https://github.com/langchain-ai/web-explorer/blob/main/requirements.txt):\n",
"Copy requirements [from here](https://github.com/langchain-ai/web-explorer/blob/main/requirements.txt):\n",
"\n",
"`pip install -r requirements.txt`\n",
" \n",
Expand Down Expand Up @@ -573,13 +573,70 @@
},
{
"cell_type": "markdown",
"id": "ff62e5f5",
"metadata": {},
"source": [
"### Going deeper \n",
"\n",
"* Here's a [app](https://github.com/langchain-ai/web-explorer/tree/main) that wraps this retriver with a lighweight UI."
]
},
{
"cell_type": "markdown",
"id": "312c399e",
"metadata": {},
"source": [
"## Question answering over a website\n",
"\n",
"To answer questions over a specific website, you can use Apify's [Website Content Crawler](https://apify.com/apify/website-content-crawler) Actor, which can deeply crawl websites such as documentation, knowledge bases, help centers, or blogs,\n",
"and extract text content from the web pages.\n",
"\n",
"In the example below, we will deeply crawl the Python documentation of LangChain's Chat LLM models and answer a question over it.\n",
"\n",
"First, install the requirements\n",
"`pip install apify-client openai langchain chromadb tiktoken`\n",
" \n",
"Next, set `OPENAI_API_KEY` and `APIFY_API_TOKEN` in your environment variables.\n",
"\n",
"The full code follows:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "9b08da5e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Yes, LangChain offers integration with OpenAI chat models. You can use the ChatOpenAI class to interact with OpenAI models.\n"
]
}
],
"source": [
"from langchain.docstore.document import Document\n",
"from langchain.indexes import VectorstoreIndexCreator\n",
"from langchain.utilities import ApifyWrapper\n",
"\n",
"apify = ApifyWrapper()\n",
"# Call the Actor to obtain text from the crawled webpages\n",
"loader = apify.call_actor(\n",
" actor_id=\"apify/website-content-crawler\",\n",
" run_input={\"startUrls\": [{\"url\": \"https://python.langchain.com/docs/integrations/chat/\"}]},\n",
" dataset_mapping_function=lambda item: Document(\n",
" page_content=item[\"text\"] or \"\", metadata={\"source\": item[\"url\"]}\n",
" ),\n",
")\n",
"\n",
"# Create a vector store based on the crawled data\n",
"index = VectorstoreIndexCreator().from_loaders([loader])\n",
"\n",
"# Query the vector store\n",
"query = \"Are any OpenAI chat models integrated in LangChain?\"\n",
"result = index.query(query)\n",
"print(result)"
]
}
],
"metadata": {
Expand All @@ -598,7 +655,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.1"
"version": "3.9.16"
}
},
"nbformat": 4,
Expand Down