This commit is contained in:
adeelehsan 2025-04-24 00:02:27 +05:00
parent 4cec6a70b5
commit 32588b060f
No known key found for this signature in database
GPG Key ID: D19078FB7190E22E

View File

@ -5,7 +5,7 @@
"id": "559f8e0e",
"metadata": {},
"source": [
"# Vectara\n",
"## Overview\n",
"\n",
"[Vectara](https://vectara.com/) is the trusted AI Assistant and Agent platform which focuses on enterprise readiness for mission-critical applications.\n",
"Vectara serverless RAG-as-a-service provides all the components of RAG behind an easy-to-use API, including:\n",
@ -36,20 +36,10 @@
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": null,
"id": "dfdf03ba-d6f5-4b1e-86d3-a65c4bc99aa1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[2mUsing Python 3.12.0 environment at: /Users/adeel/vectara/langchain/.venv\u001b[0m\n",
"\u001b[2K\u001b[2mResolved \u001b[1m1 package\u001b[0m \u001b[2min 189ms\u001b[0m\u001b[0m \u001b[0m\n",
"\u001b[2mAudited \u001b[1m1 package\u001b[0m \u001b[2min 0.24ms\u001b[0m\u001b[0m\n"
]
}
],
"outputs": [],
"source": [
"!uv pip install -U pip && uv pip install -qU langchain-vectara langgraph"
]
@ -69,6 +59,8 @@
"To use LangChain with Vectara, you'll need to have these two values: `corpus_key` and `api_key`.\n",
"You can provide `VECTARA_API_KEY` to LangChain in two ways:\n",
"\n",
"## Instantiation\n",
"\n",
"1. Include in your environment these two variables: `VECTARA_API_KEY`.\n",
"\n",
" For example, you can set these variables using os.environ and getpass as follows:\n",
@ -186,21 +178,10 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 33,
"id": "9ecda054-96a8-4a91-aeae-32006efb1ac8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'{\\n \"summary\": \"President Biden discussed several key topics in his recent statements. He emphasized the importance of keeping schools open and noted that with a high vaccination rate and reduced hospitalizations, most Americans can safely return to normal activities [1]. He addressed the need to hold social media platforms accountable for their impact on children and called for stronger privacy protections and mental health services [2]. Biden also announced measures against Russia, including preventing its central bank from defending the Ruble and targeting Russian oligarchs\\' assets, as well as closing American airspace to Russian flights [3], [7]. Additionally, he reaffirmed the need to protect women\\'s rights, particularly the right to choose as affirmed in Roe v. Wade [5].\",\\n \"factual_consistency_score\": 0.5415039\\n}'"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"generation_config = GenerationConfig(\n",
" max_used_search_results=7,\n",
@ -232,7 +213,35 @@
" vectorstore=vectara,\n",
" corpus_key=corpus_key,\n",
" config=config,\n",
")\n",
")"
]
},
{
"cell_type": "markdown",
"id": "2693af4c-eac5-475f-8ea4-cf6a2cac6c05",
"metadata": {},
"source": [
"## Invocation"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "851b25f4-54a8-4220-9bf1-1f7071cba903",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'{\\n \"summary\": \"President Biden discussed several key topics in his recent statements. He emphasized the importance of keeping schools open and noted that with a high vaccination rate and reduced hospitalizations, most Americans can safely return to normal activities [1]. He addressed the need to hold social media platforms accountable for their impact on children and called for stronger privacy protections and mental health services [2]. Biden also announced measures against Russia, including preventing its central bank from defending the Ruble and targeting Russian oligarchs\\' assets, as well as closing American airspace to Russian flights [3], [7]. Additionally, he reaffirmed the need to protect women\\'s rights, particularly the right to choose as affirmed in Roe v. Wade [5].\",\\n \"factual_consistency_score\": 0.5415039\\n}'"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"vectara_rag_tool.run(query_str)"
]
},
@ -322,8 +331,6 @@
}
],
"source": [
"import json\n",
"\n",
"from langchain.prompts import ChatPromptTemplate\n",
"from langchain.schema.output_parser import StrOutputParser\n",
"from langchain.schema.runnable import RunnableSerializable\n",
@ -364,9 +371,9 @@
"id": "edc301b8-6397-4e86-88d2-05b29f093818",
"metadata": {},
"source": [
"## Agentic RAG with Vectara tools\n",
"## Use within an agent\n",
"\n",
"Agentic RAG is a powerful methodology to provide your RAG implementation more agency with approaches like ReAct. The code below demonstrates how to use Vectara with LangChain to create an agent that uses Vectara for RAG."
"The code below demonstrates how to use Vectara tools with LangChain to create an agent."
]
},
{
@ -394,8 +401,6 @@
"from langchain_openai.chat_models import ChatOpenAI\n",
"from langgraph.prebuilt import create_react_agent\n",
"\n",
"llm = ChatOpenAI(temperature=0)\n",
"\n",
"# Set up the tools and LLM\n",
"tools = [vectara_rag_tool]\n",
"llm = ChatOpenAI(model=\"gpt-4o-mini\", temperature=0)\n",
@ -463,6 +468,16 @@
")"
]
},
{
"cell_type": "markdown",
"id": "6481c0eb-d798-4d3d-a755-019c85243546",
"metadata": {},
"source": [
"## API reference\n",
"\n",
"For details checkout implementation of Vectara [tools](https://github.com/vectara/langchain-vectara/blob/main/libs/vectara/langchain_vectara/tools.py)."
]
},
{
"cell_type": "code",
"execution_count": null,