Skip to content

Commit 0f6fa34

Browse files
alexngysccurme
andauthored
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>
1 parent e8a84b0 commit 0f6fa34

File tree

4 files changed

+478
-0
lines changed

4 files changed

+478
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# ValyuContext\n",
8+
"\n",
9+
">[Valyu](https://www.valyu.network/) allows AI applications and agents to search the internet and proprietary data sources for relevant LLM ready information.\n",
10+
"\n",
11+
"This notebook goes over how to use Valyu in LangChain.\n",
12+
"\n",
13+
"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",
14+
"\n",
15+
"## Setup\n",
16+
"\n",
17+
"The integration lives in the `langchain-valyu` package."
18+
]
19+
},
20+
{
21+
"cell_type": "code",
22+
"execution_count": null,
23+
"metadata": {},
24+
"outputs": [],
25+
"source": [
26+
"%pip install -qU langchain-valyu"
27+
]
28+
},
29+
{
30+
"cell_type": "markdown",
31+
"metadata": {},
32+
"source": [
33+
"In order to use the package, you will also need to set the `VALYU_API_KEY` environment variable to your Valyu API key.\n",
34+
"\n",
35+
"## Context Retriever\n",
36+
"\n",
37+
"You can use the [`ValyuContextRetriever`](https://pypi.org/project/langchain-valyu/) in a standard retrieval pipeline."
38+
]
39+
},
40+
{
41+
"cell_type": "code",
42+
"execution_count": null,
43+
"metadata": {},
44+
"outputs": [],
45+
"source": [
46+
"from langchain_valyu import ValyuContextRetriever\n",
47+
"\n",
48+
"valyu_api_key = \"YOUR API KEY\"\n",
49+
"\n",
50+
"# Create a new instance of the ValyuContextRetriever\n",
51+
"valyu_retriever = ValyuContextRetriever(valyu_api_key=valyu_api_key)\n",
52+
"\n",
53+
"# Search for a query and save the results\n",
54+
"docs = valyu_retriever.invoke(\"What are the benefits of renewable energy?\")\n",
55+
"\n",
56+
"# Print the results\n",
57+
"for doc in docs:\n",
58+
" print(doc.page_content)\n",
59+
" print(doc.metadata)"
60+
]
61+
},
62+
{
63+
"cell_type": "markdown",
64+
"metadata": {},
65+
"source": [
66+
"## Context Search Tool\n",
67+
"\n",
68+
"You can use the `ValyuSearchTool` for advanced search queries."
69+
]
70+
},
71+
{
72+
"cell_type": "code",
73+
"execution_count": null,
74+
"metadata": {},
75+
"outputs": [],
76+
"source": [
77+
"from langchain_valyu import ValyuSearchTool\n",
78+
"\n",
79+
"# Initialize the ValyuSearchTool\n",
80+
"search_tool = ValyuSearchTool(valyu_api_key=\"YOUR API KEY\")\n",
81+
"\n",
82+
"# Perform a search query\n",
83+
"search_results = search_tool._run(\n",
84+
" query=\"What are agentic search-enhanced large reasoning models?\",\n",
85+
" search_type=\"all\",\n",
86+
" max_num_results=5,\n",
87+
" similarity_threshold=0.4,\n",
88+
" query_rewrite=False,\n",
89+
" max_price=20.0,\n",
90+
")\n",
91+
"\n",
92+
"print(\"Search Results:\", search_results)"
93+
]
94+
}
95+
],
96+
"metadata": {
97+
"language_info": {
98+
"name": "python"
99+
}
100+
},
101+
"nbformat": 4,
102+
"nbformat_minor": 2
103+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# ValyuContext\n",
8+
"\n",
9+
">[Valyu](https://www.valyu.network/) allows AI applications and agents to search the internet and proprietary data sources for relevant LLM ready information.\n",
10+
"\n",
11+
"This notebook goes over how to use Valyu context tool in LangChain.\n",
12+
"\n",
13+
"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",
14+
"\n",
15+
"## Setup\n",
16+
"\n",
17+
"The integration lives in the `langchain-valyu` package."
18+
]
19+
},
20+
{
21+
"cell_type": "code",
22+
"execution_count": null,
23+
"metadata": {},
24+
"outputs": [],
25+
"source": [
26+
"%pip install -qU langchain-valyu"
27+
]
28+
},
29+
{
30+
"cell_type": "markdown",
31+
"metadata": {},
32+
"source": [
33+
"In order to use the package, you will also need to set the `VALYU_API_KEY` environment variable to your Valyu API key."
34+
]
35+
},
36+
{
37+
"cell_type": "code",
38+
"execution_count": null,
39+
"metadata": {},
40+
"outputs": [],
41+
"source": [
42+
"import os\n",
43+
"\n",
44+
"valyu_api_key = os.environ[\"VALYU_API_KEY\"]"
45+
]
46+
},
47+
{
48+
"cell_type": "markdown",
49+
"metadata": {},
50+
"source": [
51+
"## Instantiation\n",
52+
"\n",
53+
"Now we can instantiate our retriever:\n",
54+
"The `ValyuContextRetriever` can be configured with several parameters:\n",
55+
"\n",
56+
"- `k: int = 5` \n",
57+
" The number of top results to return for each query.\n",
58+
"\n",
59+
"- `search_type: str = \"all\"` \n",
60+
" The type of search to perform. Options may include \"all\", \"web\", \"proprietary\", etc., depending on your use case.\n",
61+
"\n",
62+
"- `similarity_threshold: float = 0.4` \n",
63+
" The minimum similarity score (between 0 and 1) required for a document to be considered relevant.\n",
64+
"\n",
65+
"- `query_rewrite: bool = False` \n",
66+
" Whether to enable automatic rewriting of the query to improve search results.\n",
67+
" \n",
68+
"- `max_price: float = 20.0`\n",
69+
" The maximum price (in USD) you are willing to spend per query.\n",
70+
"\n",
71+
"- `client: Optional[Valyu] = None` \n",
72+
" An optional custom Valyu client instance. If not provided, a new client will be created internally.\n",
73+
" \n",
74+
"- `valyu_api_key: Optional[str] = None` \n",
75+
" Your Valyu API key. If not provided, the retriever will look for the `VALYU_API_KEY` environment variable.\n"
76+
]
77+
},
78+
{
79+
"cell_type": "code",
80+
"execution_count": null,
81+
"metadata": {},
82+
"outputs": [],
83+
"source": [
84+
"from langchain_valyu import ValyuContextRetriever\n",
85+
"\n",
86+
"retriever = ValyuContextRetriever(\n",
87+
" k=5,\n",
88+
" search_type=\"all\",\n",
89+
" similarity_threshold=0.4,\n",
90+
" query_rewrite=False,\n",
91+
" max_price=20.0,\n",
92+
" client=None,\n",
93+
" valyu_api_key=os.environ[\"VALYU_API_KEY\"],\n",
94+
")"
95+
]
96+
},
97+
{
98+
"cell_type": "markdown",
99+
"metadata": {},
100+
"source": [
101+
"## Usage"
102+
]
103+
},
104+
{
105+
"cell_type": "code",
106+
"execution_count": null,
107+
"metadata": {},
108+
"outputs": [],
109+
"source": [
110+
"query = \"What are the benefits of renewable energy?\"\n",
111+
"docs = retriever.invoke(query)\n",
112+
"\n",
113+
"for doc in docs:\n",
114+
" print(doc.page_content)\n",
115+
" print(doc.metadata)"
116+
]
117+
},
118+
{
119+
"cell_type": "markdown",
120+
"metadata": {},
121+
"source": [
122+
"## Use within a chain\n",
123+
"\n",
124+
"We can easily combine this retriever in to a chain."
125+
]
126+
},
127+
{
128+
"cell_type": "code",
129+
"execution_count": null,
130+
"metadata": {},
131+
"outputs": [],
132+
"source": [
133+
"from langchain_core.output_parsers import StrOutputParser\n",
134+
"from langchain_core.prompts import ChatPromptTemplate\n",
135+
"from langchain_core.runnables import RunnablePassthrough\n",
136+
"from langchain_openai import ChatOpenAI\n",
137+
"\n",
138+
"prompt = ChatPromptTemplate.from_template(\n",
139+
" \"\"\"Answer the question based only on the context provided.\n",
140+
"\n",
141+
"Context: {context}\n",
142+
"\n",
143+
"Question: {question}\"\"\"\n",
144+
")\n",
145+
"\n",
146+
"llm = ChatOpenAI(model=\"gpt-4o-mini\")\n",
147+
"\n",
148+
"\n",
149+
"def format_docs(docs):\n",
150+
" return \"\\n\\n\".join(doc.page_content for doc in docs)\n",
151+
"\n",
152+
"\n",
153+
"chain = (\n",
154+
" {\"context\": retriever | format_docs, \"question\": RunnablePassthrough()}\n",
155+
" | prompt\n",
156+
" | llm\n",
157+
" | StrOutputParser()\n",
158+
")"
159+
]
160+
},
161+
{
162+
"cell_type": "markdown",
163+
"metadata": {},
164+
"source": [
165+
"## API reference\n",
166+
"\n",
167+
"For detailed documentation of all Valyu Context API features and configurations head to the API reference: https://docs.valyu.network/overview"
168+
]
169+
}
170+
],
171+
"metadata": {
172+
"language_info": {
173+
"name": "python"
174+
}
175+
},
176+
"nbformat": 4,
177+
"nbformat_minor": 2
178+
}

0 commit comments

Comments
 (0)