2023-05-14 04:45:05 +00:00
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Metaphor Search"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
2023-07-20 22:50:54 +00:00
"Metaphor is a search engine fully designed to be used by LLMs. You can search and then get the contents for any page.\n",
"\n",
2023-05-14 04:45:05 +00:00
"This notebook goes over how to use Metaphor search.\n",
"\n",
2023-07-20 22:50:54 +00:00
"First, you need to set up the proper API keys and environment variables. Get 1000 free searches/month [here](https://platform.metaphor.systems/).\n",
2023-05-14 04:45:05 +00:00
"\n",
"Then enter your API key as an environment variable."
]
},
{
"cell_type": "code",
2023-06-18 23:53:24 +00:00
"execution_count": null,
2023-05-14 04:45:05 +00:00
"metadata": {},
"outputs": [],
"source": [
"import os\n",
2023-06-16 18:52:56 +00:00
"\n",
2023-05-14 04:45:05 +00:00
"os.environ[\"METAPHOR_API_KEY\"] = \"\""
]
},
{
"cell_type": "code",
2023-06-18 23:53:24 +00:00
"execution_count": null,
2023-05-14 04:45:05 +00:00
"metadata": {},
"outputs": [],
"source": [
"from langchain.utilities import MetaphorSearchAPIWrapper"
]
},
{
"cell_type": "code",
2023-06-18 23:53:24 +00:00
"execution_count": null,
2023-05-14 04:45:05 +00:00
"metadata": {},
"outputs": [],
"source": [
"search = MetaphorSearchAPIWrapper()"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Call the API\n",
"`results` takes in a Metaphor-optimized search query and a number of results (up to 500). It returns a list of results with title, url, author, and creation date."
]
},
{
"cell_type": "code",
2023-06-18 23:53:24 +00:00
"execution_count": null,
2023-05-14 04:45:05 +00:00
"metadata": {},
2023-06-18 23:53:24 +00:00
"outputs": [],
2023-05-14 04:45:05 +00:00
"source": [
"search.results(\"The best blog post about AI safety is definitely this: \", 10)"
]
},
2023-06-18 23:53:24 +00:00
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Adding filters\n",
"We can also add filters to our search. \n",
2023-07-20 22:50:54 +00:00
"\n",
2023-06-18 23:53:24 +00:00
"include_domains: Optional[List[str]] - List of domains to include in the search. If specified, results will only come from these domains. Only one of include_domains and exclude_domains should be specified.\n",
2023-07-20 22:50:54 +00:00
"\n",
2023-06-18 23:53:24 +00:00
"exclude_domains: Optional[List[str]] - List of domains to exclude in the search. If specified, results will only come from these domains. Only one of include_domains and exclude_domains should be specified.\n",
2023-07-20 22:50:54 +00:00
"\n",
2023-06-18 23:53:24 +00:00
"start_crawl_date: Optional[str] - \"Crawl date\" refers to the date that Metaphor discovered a link, which is more granular and can be more useful than published date. If start_crawl_date is specified, results will only include links that were crawled after start_crawl_date. Must be specified in ISO 8601 format (YYYY-MM-DDTHH:MM:SSZ)\n",
2023-07-20 22:50:54 +00:00
"\n",
2023-06-18 23:53:24 +00:00
"end_crawl_date: Optional[str] - \"Crawl date\" refers to the date that Metaphor discovered a link, which is more granular and can be more useful than published date. If endCrawlDate is specified, results will only include links that were crawled before end_crawl_date. Must be specified in ISO 8601 format (YYYY-MM-DDTHH:MM:SSZ)\n",
2023-07-20 22:50:54 +00:00
"\n",
2023-06-18 23:53:24 +00:00
"start_published_date: Optional[str] - If specified, only links with a published date after start_published_date will be returned. Must be specified in ISO 8601 format (YYYY-MM-DDTHH:MM:SSZ). Note that for some links, we have no published date, and these links will be excluded from the results if start_published_date is specified.\n",
2023-07-20 22:50:54 +00:00
"\n",
2023-06-18 23:53:24 +00:00
"end_published_date: Optional[str] - If specified, only links with a published date before end_published_date will be returned. Must be specified in ISO 8601 format (YYYY-MM-DDTHH:MM:SSZ). Note that for some links, we have no published date, and these links will be excluded from the results if end_published_date is specified.\n",
"\n",
2023-07-20 22:50:54 +00:00
"See full docs [here](https://metaphorapi.readme.io/)."
2023-06-18 23:53:24 +00:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Fix `make docs_build` and related scripts (#7276)
**Description: a description of the change**
Fixed `make docs_build` and related scripts which caused errors. There
are several changes.
First, I made the build of the documentation and the API Reference into
two separate commands. This is because it takes less time to build. The
commands for documents are `make docs_build`, `make docs_clean`, and
`make docs_linkcheck`. The commands for API Reference are `make
api_docs_build`, `api_docs_clean`, and `api_docs_linkcheck`.
It looked like `docs/.local_build.sh` could be used to build the
documentation, so I used that. Since `.local_build.sh` was also building
API Rerefence internally, I removed that process. `.local_build.sh` also
added some Bash options to stop in error or so. Futher more added `cd
"${SCRIPT_DIR}"` at the beginning so that the script will work no matter
which directory it is executed in.
`docs/api_reference/api_reference.rst` is removed, because which is
generated by `docs/api_reference/create_api_rst.py`, and added it to
.gitignore.
Finally, the description of CONTRIBUTING.md was modified.
**Issue: the issue # it fixes (if applicable)**
https://github.com/hwchase17/langchain/issues/6413
**Dependencies: any dependencies required for this change**
`nbdoc` was missing in group docs so it was added. I installed it with
the `poetry add --group docs nbdoc` command. I am concerned if any
modifications are needed to poetry.lock. I would greatly appreciate it
if you could pay close attention to this file during the review.
**Tag maintainer**
- General / Misc / if you don't know who to tag: @baskaryan
If this PR needs any additional changes, I'll be happy to make them!
---------
Co-authored-by: Bagatur <baskaryan@gmail.com>
2023-07-12 02:05:14 +00:00
"search.results(\n",
" \"The best blog post about AI safety is definitely this: \",\n",
" 10,\n",
" include_domains=[\"lesswrong.com\"],\n",
" start_published_date=\"2019-01-01\",\n",
")"
2023-06-18 23:53:24 +00:00
]
},
2023-05-14 04:45:05 +00:00
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Use Metaphor as a tool\n",
"Metaphor can be used as a tool that gets URLs that other tools such as browsing tools."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2023-06-18 23:53:24 +00:00
"%pip install playwright\n",
2023-05-14 04:45:05 +00:00
"from langchain.agents.agent_toolkits import PlayWrightBrowserToolkit\n",
"from langchain.tools.playwright.utils import (\n",
2023-06-16 18:52:56 +00:00
" create_async_playwright_browser, # A synchronous browser is available, though it isn't compatible with jupyter.\n",
2023-05-14 04:45:05 +00:00
")\n",
"\n",
"async_browser = create_async_playwright_browser()\n",
"toolkit = PlayWrightBrowserToolkit.from_browser(async_browser=async_browser)\n",
"tools = toolkit.get_tools()\n",
"\n",
"tools_by_name = {tool.name: tool for tool in tools}\n",
"print(tools_by_name.keys())\n",
"navigate_tool = tools_by_name[\"navigate_browser\"]\n",
"extract_text = tools_by_name[\"extract_text\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
2023-06-18 23:53:24 +00:00
"outputs": [],
2023-05-14 04:45:05 +00:00
"source": [
"from langchain.agents import initialize_agent, AgentType\n",
"from langchain.chat_models import ChatOpenAI\n",
"from langchain.tools import MetaphorSearchResults\n",
"\n",
"llm = ChatOpenAI(model_name=\"gpt-4\", temperature=0.7)\n",
"\n",
"metaphor_tool = MetaphorSearchResults(api_wrapper=search)\n",
"\n",
2023-06-16 18:52:56 +00:00
"agent_chain = initialize_agent(\n",
" [metaphor_tool, extract_text, navigate_tool],\n",
" llm,\n",
" agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION,\n",
" verbose=True,\n",
")\n",
2023-05-14 04:45:05 +00:00
"\n",
2023-06-16 18:52:56 +00:00
"agent_chain.run(\n",
" \"find me an interesting tweet about AI safety using Metaphor, then tell me the first sentence in the post. Do not finish until able to retrieve the first sentence.\"\n",
")"
2023-05-14 04:45:05 +00:00
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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",
2023-06-18 23:53:24 +00:00
"version": "3.10.11"
2023-05-14 04:45:05 +00:00
},
"vscode": {
"interpreter": {
"hash": "a0a0263b650d907a3bfe41c0f8d6a63a071b884df3cfdc1579f00cdc1aed6b03"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}