Community: Valyu Integration docs (#30926)

PR title:
docs: add Valyu integration documentation
Description:
This PR adds documentation and example notebooks for the Valyu
integration, including retriever and tool usage.
Issue:
N/A
Dependencies:
No new dependencies.

---------

Co-authored-by: ccurme <chester.curme@gmail.com>
This commit is contained in:
Alexander Ng 2025-04-21 22:43:00 +01:00 committed by GitHub
parent e8a84b05a4
commit 0f6fa34372
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 478 additions and 0 deletions

View File

@ -0,0 +1,103 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# ValyuContext\n",
"\n",
">[Valyu](https://www.valyu.network/) allows AI applications and agents to search the internet and proprietary data sources for relevant LLM ready information.\n",
"\n",
"This notebook goes over how to use Valyu in LangChain.\n",
"\n",
"First, get an Valyu API key and add it as an environment variable. Get $10 free credit by [signing up here](https://exchange.valyu.network/).\n",
"\n",
"## Setup\n",
"\n",
"The integration lives in the `langchain-valyu` package."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%pip install -qU langchain-valyu"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In order to use the package, you will also need to set the `VALYU_API_KEY` environment variable to your Valyu API key.\n",
"\n",
"## Context Retriever\n",
"\n",
"You can use the [`ValyuContextRetriever`](https://pypi.org/project/langchain-valyu/) in a standard retrieval pipeline."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from langchain_valyu import ValyuContextRetriever\n",
"\n",
"valyu_api_key = \"YOUR API KEY\"\n",
"\n",
"# Create a new instance of the ValyuContextRetriever\n",
"valyu_retriever = ValyuContextRetriever(valyu_api_key=valyu_api_key)\n",
"\n",
"# Search for a query and save the results\n",
"docs = valyu_retriever.invoke(\"What are the benefits of renewable energy?\")\n",
"\n",
"# Print the results\n",
"for doc in docs:\n",
" print(doc.page_content)\n",
" print(doc.metadata)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Context Search Tool\n",
"\n",
"You can use the `ValyuSearchTool` for advanced search queries."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from langchain_valyu import ValyuSearchTool\n",
"\n",
"# Initialize the ValyuSearchTool\n",
"search_tool = ValyuSearchTool(valyu_api_key=\"YOUR API KEY\")\n",
"\n",
"# Perform a search query\n",
"search_results = search_tool._run(\n",
" query=\"What are agentic search-enhanced large reasoning models?\",\n",
" search_type=\"all\",\n",
" max_num_results=5,\n",
" similarity_threshold=0.4,\n",
" query_rewrite=False,\n",
" max_price=20.0,\n",
")\n",
"\n",
"print(\"Search Results:\", search_results)"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

View File

@ -0,0 +1,178 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# ValyuContext\n",
"\n",
">[Valyu](https://www.valyu.network/) allows AI applications and agents to search the internet and proprietary data sources for relevant LLM ready information.\n",
"\n",
"This notebook goes over how to use Valyu context tool in LangChain.\n",
"\n",
"First, get an Valyu API key and add it as an environment variable. Get $10 free credit by [signing up here](https://exchange.valyu.network/).\n",
"\n",
"## Setup\n",
"\n",
"The integration lives in the `langchain-valyu` package."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%pip install -qU langchain-valyu"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In order to use the package, you will also need to set the `VALYU_API_KEY` environment variable to your Valyu API key."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"valyu_api_key = os.environ[\"VALYU_API_KEY\"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Instantiation\n",
"\n",
"Now we can instantiate our retriever:\n",
"The `ValyuContextRetriever` can be configured with several parameters:\n",
"\n",
"- `k: int = 5` \n",
" The number of top results to return for each query.\n",
"\n",
"- `search_type: str = \"all\"` \n",
" The type of search to perform. Options may include \"all\", \"web\", \"proprietary\", etc., depending on your use case.\n",
"\n",
"- `similarity_threshold: float = 0.4` \n",
" The minimum similarity score (between 0 and 1) required for a document to be considered relevant.\n",
"\n",
"- `query_rewrite: bool = False` \n",
" Whether to enable automatic rewriting of the query to improve search results.\n",
" \n",
"- `max_price: float = 20.0`\n",
" The maximum price (in USD) you are willing to spend per query.\n",
"\n",
"- `client: Optional[Valyu] = None` \n",
" An optional custom Valyu client instance. If not provided, a new client will be created internally.\n",
" \n",
"- `valyu_api_key: Optional[str] = None` \n",
" Your Valyu API key. If not provided, the retriever will look for the `VALYU_API_KEY` environment variable.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from langchain_valyu import ValyuContextRetriever\n",
"\n",
"retriever = ValyuContextRetriever(\n",
" k=5,\n",
" search_type=\"all\",\n",
" similarity_threshold=0.4,\n",
" query_rewrite=False,\n",
" max_price=20.0,\n",
" client=None,\n",
" valyu_api_key=os.environ[\"VALYU_API_KEY\"],\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Usage"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"query = \"What are the benefits of renewable energy?\"\n",
"docs = retriever.invoke(query)\n",
"\n",
"for doc in docs:\n",
" print(doc.page_content)\n",
" print(doc.metadata)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Use within a chain\n",
"\n",
"We can easily combine this retriever in to a chain."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from langchain_core.output_parsers import StrOutputParser\n",
"from langchain_core.prompts import ChatPromptTemplate\n",
"from langchain_core.runnables import RunnablePassthrough\n",
"from langchain_openai import ChatOpenAI\n",
"\n",
"prompt = ChatPromptTemplate.from_template(\n",
" \"\"\"Answer the question based only on the context provided.\n",
"\n",
"Context: {context}\n",
"\n",
"Question: {question}\"\"\"\n",
")\n",
"\n",
"llm = ChatOpenAI(model=\"gpt-4o-mini\")\n",
"\n",
"\n",
"def format_docs(docs):\n",
" return \"\\n\\n\".join(doc.page_content for doc in docs)\n",
"\n",
"\n",
"chain = (\n",
" {\"context\": retriever | format_docs, \"question\": RunnablePassthrough()}\n",
" | prompt\n",
" | llm\n",
" | StrOutputParser()\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## API reference\n",
"\n",
"For detailed documentation of all Valyu Context API features and configurations head to the API reference: https://docs.valyu.network/overview"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

View File

@ -0,0 +1,194 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# ValyuContext\n",
"\n",
">[Valyu](https://www.valyu.network/) allows AI applications and agents to search the internet and proprietary data sources for relevant LLM ready information.\n",
"\n",
"This notebook goes over how to use Valyu context tool in LangChain.\n",
"\n",
"First, get an Valyu API key and add it as an environment variable. Get $10 free credit by [signing up here](https://exchange.valyu.network/).\n",
"\n",
"## Overview\n",
"\n",
"### Integration details\n",
"| Class | Package | Serializable | JS support | Package latest |\n",
"|:--------------------------------------------------------------|:---------------------------------------------------------------| :---: | :---: | :---: |\n",
"| [Valyu Search](https://github.com/valyu-network/langchain-valyu) | [langchain-valyu](https://pypi.org/project/langchain-valyu/) | ✅ | ❌ | ![PyPI - Version](https://img.shields.io/pypi/v/langchain-valyu?style=flat-square&label=%20) |\n",
"\n",
"\n",
"\n",
"\n",
"## Setup\n",
"\n",
"The integration lives in the `langchain-valyu` package."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"%pip install -qU langchain-valyu"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In order to use the package, you will also need to set the `VALYU_API_KEY` environment variable to your Valyu API key."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import getpass\n",
"import os\n",
"\n",
"if not os.environ.get(\"VALYU_API_KEY\"):\n",
" os.environ[\"VALYU_API_KEY\"] = getpass.getpass(\"Valyu API key:\\n\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Instantiation\n",
"\n",
"Here we show how to instantiate an instance of the Valyu search tool. This tool allows you to complete search queries using Valyu's Context API endpoint.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from langchain_valyu import ValyuSearchTool\n",
"\n",
"tool = ValyuSearchTool()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Invocation\n",
"\n",
"### Invoke directly with args\n",
"\n",
"The Valyu search tool accepts the following arguments during invocation:\n",
"- `query` (required): A natural language search query\n",
"- `search_type` (optional): Type of search, e.g., \"all\"\n",
"- `max_num_results` (optional): Maximum number of results to return\n",
"- `similarity_threshold` (optional): Similarity threshold for results\n",
"- `query_rewrite` (optional): Whether to rewrite the query\n",
"- `max_price` (optional): Maximum price for the search\n",
"\n",
"For reliability and performance reasons, certain parameters may be required or restricted. See the [Valyu API documentation](https://docs.valyu.network/overview) for details."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"search_results = tool._run(\n",
" query=\"What are agentic search-enhanced large reasoning models?\",\n",
" search_type=\"all\",\n",
" max_num_results=5,\n",
" similarity_threshold=0.4,\n",
" query_rewrite=False,\n",
" max_price=20.0,\n",
")\n",
"\n",
"print(\"Search Results:\", search_results)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Use within an agent\n",
"\n",
"We can use our tools directly with an agent executor by binding the tool to the agent. This gives the agent the ability to dynamically set the available arguments to the Valyu search tool."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if not os.environ.get(\"OPENAI_API_KEY\"):\n",
" os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"OPENAI_API_KEY:\\n\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# | output: false\n",
"# | echo: false\n",
"\n",
"# !pip install -qU langchain langchain-openai\n",
"from langchain.chat_models import init_chat_model\n",
"\n",
"llm = init_chat_model(model=\"gpt-4o\", model_provider=\"openai\", temperature=0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from langchain_valyu import ValyuSearchTool\n",
"from langgraph.prebuilt import create_react_agent\n",
"\n",
"valyu_search_tool = ValyuSearchTool()\n",
"\n",
"agent = create_react_agent(llm, [valyu_search_tool])\n",
"\n",
"user_input = \"What are the key factors driving recent stock market volatility, and how do macroeconomic indicators influence equity prices across different sectors?\"\n",
"\n",
"for step in agent.stream(\n",
" {\"messages\": user_input},\n",
" stream_mode=\"values\",\n",
"):\n",
" step[\"messages\"][-1].pretty_print()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## API reference\n",
"\n",
"For detailed documentation of all Valyu Context API features and configurations head to the API reference: https://docs.valyu.network/overview"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

View File

@ -614,3 +614,6 @@ packages:
provider_page: galaxia
path: .
repo: rrozanski-smabbler/galaxia-langchain
- name: langchain-valyu
path: .
repo: valyu-network/langchain-valyu