community[minor]: Vectara Integration Update - Streaming, FCS, Chat, updates to documentation and example notebooks (#21334)

Thank you for contributing to LangChain!

**Description:** update to the Vectara / Langchain integration to
integrate new Vectara capabilities:
- Full RAG implemented as a Runnable with as_rag()
- Vectara chat supported with as_chat()
- Both support streaming response
- Updated documentation and example notebook to reflect all the changes
- Updated Vectara templates

**Twitter handle:** ofermend

**Add tests and docs**: no new tests or docs, but updated both existing
tests and existing docs
This commit is contained in:
Ofer Mendelevitch
2024-06-04 12:57:28 -07:00
committed by GitHub
parent cb183a9bf1
commit ad502e8d50
14 changed files with 1103 additions and 1599 deletions

View File

@@ -5,7 +5,7 @@ This template performs multiquery RAG with vectara.
## Environment Setup
Set the `OPENAI_API_KEY` environment variable to access the OpenAI models.
Set the `OPENAI_API_KEY` environment variable to access the OpenAI models for the multi-query processing.
Also, ensure the following environment variables are set:
* `VECTARA_CUSTOMER_ID`

View File

@@ -1,6 +1,6 @@
[tool.poetry]
name = "rag-vectara-multiquery"
version = "0.1.0"
version = "0.2.0"
description = "RAG using vectara with multiquery retriever"
authors = [
"Ofer Mendelevitch <ofer@vectara.com>",

View File

@@ -1,11 +1,12 @@
import os
from langchain.retrievers.multi_query import MultiQueryRetriever
from langchain_community.chat_models import ChatOpenAI
from langchain_community.vectorstores import Vectara
from langchain_community.vectorstores.vectara import SummaryConfig, VectaraQueryConfig
from langchain_core.output_parsers import StrOutputParser
from langchain_core.pydantic_v1 import BaseModel
from langchain_core.runnables import RunnableParallel, RunnablePassthrough
from langchain_openai.chat_models import ChatOpenAI
if os.environ.get("VECTARA_CUSTOMER_ID", None) is None:
raise Exception("Missing `VECTARA_CUSTOMER_ID` environment variable.")
@@ -16,30 +17,20 @@ if os.environ.get("VECTARA_API_KEY", None) is None:
# Setup the Vectara retriever with your Corpus ID and API Key
vectara = Vectara()
# note you can customize the retriever behavior by passing additional arguments:
# - k: number of results to return (defaults to 5)
# - lambda_val: the
# [lexical matching](https://docs.vectara.com/docs/api-reference/search-apis/lexical-matching)
# factor for hybrid search (defaults to 0.025)
# - filter: a [filter](https://docs.vectara.com/docs/common-use-cases/filtering-by-metadata/filter-overview)
# to apply to the results (default None)
# - n_sentence_context: number of sentences to include before/after the actual matching
# segment when returning results. This defaults to 2.
# - mmr_config: can be used to specify MMR mode in the query.
# - is_enabled: True or False
# - mmr_k: number of results to use for MMR reranking
# - diversity_bias: 0 = no diversity, 1 = full diversity. This is the lambda
# parameter in the MMR formula and is in the range 0...1
vectara_retriever = Vectara().as_retriever()
# Define the query configuration:
summary_config = SummaryConfig(is_enabled=True, max_results=5, response_lang="eng")
config = VectaraQueryConfig(k=10, lambda_val=0.005, summary_config=summary_config)
# Setup the Multi-query retriever
llm = ChatOpenAI(temperature=0)
retriever = MultiQueryRetriever.from_llm(retriever=vectara_retriever, llm=llm)
retriever = MultiQueryRetriever.from_llm(
retriever=vectara.as_retriever(config=config), llm=llm
)
# Setup RAG pipeline with multi-query.
# We extract the summary from the RAG output, which is the last document
# (if summary is enabled)
# We extract the summary from the RAG output, which is the last document in the list.
# Note that if you want to extract the citation information, you can use res[:-1]]
chain = (
RunnableParallel({"context": retriever, "question": RunnablePassthrough()})

View File

@@ -5,8 +5,6 @@ This template performs RAG with vectara.
## Environment Setup
Set the `OPENAI_API_KEY` environment variable to access the OpenAI models.
Also, ensure the following environment variables are set:
* `VECTARA_CUSTOMER_ID`
* `VECTARA_CORPUS_ID`

View File

@@ -1,6 +1,6 @@
[tool.poetry]
name = "rag-vectara"
version = "0.1.0"
version = "0.2.0"
description = "RAG using vectara retriever"
authors = [
"Ofer Mendelevitch <ofer@vectara.com>",

View File

@@ -1,9 +1,8 @@
import os
from langchain_community.vectorstores import Vectara
from langchain_core.output_parsers import StrOutputParser
from langchain_community.vectorstores.vectara import SummaryConfig, VectaraQueryConfig
from langchain_core.pydantic_v1 import BaseModel
from langchain_core.runnables import RunnableParallel, RunnablePassthrough
if os.environ.get("VECTARA_CUSTOMER_ID", None) is None:
raise Exception("Missing `VECTARA_CUSTOMER_ID` environment variable.")
@@ -12,32 +11,14 @@ if os.environ.get("VECTARA_CORPUS_ID", None) is None:
if os.environ.get("VECTARA_API_KEY", None) is None:
raise Exception("Missing `VECTARA_API_KEY` environment variable.")
# Setup the Vectara retriever with your Corpus ID and API Key
# Setup the Vectara vectorstore with your Corpus ID and API Key
vectara = Vectara()
# note you can customize the retriever behavior by passing additional arguments:
# - k: number of results to return (defaults to 5)
# - lambda_val: the
# [lexical matching](https://docs.vectara.com/docs/api-reference/search-apis/lexical-matching)
# factor for hybrid search (defaults to 0.025)
# - filter: a [filter](https://docs.vectara.com/docs/common-use-cases/filtering-by-metadata/filter-overview)
# to apply to the results (default None)
# - n_sentence_context: number of sentences to include before/after the actual matching
# segment when returning results. This defaults to 2.
# - mmr_config: can be used to specify MMR mode in the query.
# - is_enabled: True or False
# - mmr_k: number of results to use for MMR reranking
# - diversity_bias: 0 = no diversity, 1 = full diversity. This is the lambda
# parameter in the MMR formula and is in the range 0...1
retriever = Vectara().as_retriever()
# Define the query configuration:
summary_config = SummaryConfig(is_enabled=True, max_results=5, response_lang="eng")
config = VectaraQueryConfig(k=10, lambda_val=0.005, summary_config=summary_config)
# RAG pipeline: we extract the summary from the RAG output, which is the last document
# (if summary is enabled)
# Note that if you want to extract the citation information, you can use res[:-1]]
chain = (
RunnableParallel({"context": retriever, "question": RunnablePassthrough()})
| (lambda res: res[-1])
| StrOutputParser()
)
rag = Vectara().as_rag(config)
# Add typing for input
@@ -45,4 +26,4 @@ class Question(BaseModel):
__root__: str
chain = chain.with_types(input_type=Question)
chain = rag.with_types(input_type=Question)