|
| 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