diff --git a/llama-index-integrations/indices/llama-index-indices-managed-vectara/Changelog.md b/llama-index-integrations/indices/llama-index-indices-managed-vectara/Changelog.md index 2d038347be523be3d61a2e36a499e2a4aa8e3865..c275d3189f188285e5556cd76f8281d8e64e0bf1 100644 --- a/llama-index-integrations/indices/llama-index-indices-managed-vectara/Changelog.md +++ b/llama-index-integrations/indices/llama-index-indices-managed-vectara/Changelog.md @@ -1,5 +1,9 @@ # CHANGELOG — llama-index-indices-managed-vectara +## [0.4.1] + +Added vectara_base_url to support custom Vectara server, and vectara_verify_ssl to enable ignoring SSL when needed. + ## [0.4.0] Implementation switched from using Vectara API v1 to API v2. diff --git a/llama-index-integrations/indices/llama-index-indices-managed-vectara/llama_index/indices/managed/vectara/base.py b/llama-index-integrations/indices/llama-index-indices-managed-vectara/llama_index/indices/managed/vectara/base.py index e040c4f6516a4642c24c4af6a90c26cae0342e62..74773720d68f53671b936173aef9b703351e189b 100644 --- a/llama-index-integrations/indices/llama-index-indices-managed-vectara/llama_index/indices/managed/vectara/base.py +++ b/llama-index-integrations/indices/llama-index-indices-managed-vectara/llama_index/indices/managed/vectara/base.py @@ -65,10 +65,14 @@ class VectaraIndex(BaseManagedIndex): vectara_api_key: Optional[str] = None, parallelize_ingest: bool = False, x_source_str: str = "llama_index", + vectara_base_url: str = "https://api.vectara.io", + vectara_verify_ssl: bool = True, **kwargs: Any, ) -> None: """Initialize the Vectara API.""" self.parallelize_ingest = parallelize_ingest + self._base_url = vectara_base_url.rstrip("/") + index_struct = VectaraIndexStruct( index_id=str(vectara_corpus_key), summary="Vectara Index", @@ -99,6 +103,8 @@ class VectaraIndex(BaseManagedIndex): # setup requests session with max 3 retries and 90s timeout # for calling Vectara API self._session = requests.Session() # to reuse connections + if not vectara_verify_ssl: + self._session.verify = False # to ignore SSL verification adapter = requests.adapters.HTTPAdapter(max_retries=3) self._session.mount("https://", adapter) self.vectara_api_timeout = 90 @@ -138,7 +144,7 @@ class VectaraIndex(BaseManagedIndex): valid_corpus_key = self._get_corpus_key(corpus_key) body = {} response = self._session.delete( - f"https://api.vectara.io/v2/corpora/{valid_corpus_key}/documents/{doc_id}", + f"{self._base_url}/v2/corpora/{valid_corpus_key}/documents/{doc_id}", data=json.dumps(body), verify=True, headers=self._get_post_headers(), @@ -156,7 +162,7 @@ class VectaraIndex(BaseManagedIndex): def _index_doc(self, doc: dict, corpus_key) -> str: response = self._session.post( headers=self._get_post_headers(), - url=f"https://api.vectara.io/v2/corpora/{corpus_key}/documents", + url=f"{self._base_url}/v2/corpora/{corpus_key}/documents", data=json.dumps(doc), timeout=self.vectara_api_timeout, verify=True, @@ -366,7 +372,7 @@ class VectaraIndex(BaseManagedIndex): headers.pop("Content-Type") valid_corpus_key = self._get_corpus_key(corpus_key) response = self._session.post( - f"https://api.vectara.io/v2/corpora/{valid_corpus_key}/upload_file", + f"{self._base_url}/v2/corpora/{valid_corpus_key}/upload_file", files=files, verify=True, headers=headers, @@ -426,7 +432,7 @@ class VectaraIndex(BaseManagedIndex): doc_id = document.doc_id body = {"metadata": update_kwargs["metadata"]} response = self._session.patch( - f"https://api.vectara.io/v2/corpora/{valid_corpus_key}/documents/{doc_id}", + f"{self._base_url}/v2/corpora/{valid_corpus_key}/documents/{doc_id}", data=json.dumps(body), verify=True, headers=self._get_post_headers(), diff --git a/llama-index-integrations/indices/llama-index-indices-managed-vectara/llama_index/indices/managed/vectara/retriever.py b/llama-index-integrations/indices/llama-index-indices-managed-vectara/llama_index/indices/managed/vectara/retriever.py index fbfb134fac0f0108b892890cad943957671430a5..aac12b7a5315dc320fab973b149c9feb5b368225 100644 --- a/llama-index-integrations/indices/llama-index-indices-managed-vectara/llama_index/indices/managed/vectara/retriever.py +++ b/llama-index-integrations/indices/llama-index-indices-managed-vectara/llama_index/indices/managed/vectara/retriever.py @@ -338,7 +338,7 @@ class VectaraRetriever(BaseRetriever): model_parameters["presence_penalty"] = self._presence_penalty if len(model_parameters) > 0: - summary_config["model_parameters"] = model_paramters + summary_config["model_parameters"] = model_parameters citations_config = {} if self._citations_style: @@ -392,7 +392,7 @@ class VectaraRetriever(BaseRetriever): conv_id = conv_id or self._conv_id response = self._index._session.post( headers=self._get_post_headers(), - url=f"https://api.vectara.io/v2/chats/{conv_id}/turns", + url=f"{self._index._base_url}/v2/chats/{conv_id}/turns", data=json.dumps(body), timeout=self._index.vectara_api_timeout, stream=True, @@ -400,7 +400,7 @@ class VectaraRetriever(BaseRetriever): else: response = self._index._session.post( headers=self._get_post_headers(), - url="https://api.vectara.io/v2/chats", + url=f"{self._index._base_url}/v2/chats", data=json.dumps(body), timeout=self._index.vectara_api_timeout, stream=True, @@ -409,7 +409,7 @@ class VectaraRetriever(BaseRetriever): else: response = self._index._session.post( headers=self._get_post_headers(), - url="https://api.vectara.io/v2/query", + url=f"{self._index._base_url}/v2/query", data=json.dumps(body), timeout=self._index.vectara_api_timeout, stream=True, @@ -518,14 +518,14 @@ class VectaraRetriever(BaseRetriever): if conv_id: response = self._index._session.post( headers=self._get_post_headers(), - url=f"https://api.vectara.io/v2/chats/{conv_id}/turns", + url=f"{self._index._base_url}/v2/chats/{conv_id}/turns", data=json.dumps(data), timeout=self._index.vectara_api_timeout, ) else: response = self._index._session.post( headers=self._get_post_headers(), - url="https://api.vectara.io/v2/chats", + url=f"{self._index._base_url}/v2/chats", data=json.dumps(data), timeout=self._index.vectara_api_timeout, ) @@ -533,7 +533,7 @@ class VectaraRetriever(BaseRetriever): else: response = self._index._session.post( headers=self._get_post_headers(), - url="https://api.vectara.io/v2/query", + url=f"{self._index._base_url}/v2/query", data=json.dumps(data), timeout=self._index.vectara_api_timeout, ) diff --git a/llama-index-integrations/indices/llama-index-indices-managed-vectara/pyproject.toml b/llama-index-integrations/indices/llama-index-indices-managed-vectara/pyproject.toml index cb31692b7703045a07969d0b404852de521ee905..526c8b1e06b5464452cb41b75c30745d1aeb8593 100644 --- a/llama-index-integrations/indices/llama-index-indices-managed-vectara/pyproject.toml +++ b/llama-index-integrations/indices/llama-index-indices-managed-vectara/pyproject.toml @@ -31,7 +31,7 @@ exclude = ["**/BUILD"] license = "MIT" name = "llama-index-indices-managed-vectara" readme = "README.md" -version = "0.4.0" +version = "0.4.1" [tool.poetry.dependencies] python = ">=3.9,<4.0" diff --git a/llama-index-integrations/indices/llama-index-indices-managed-vectara/tests/test_indices_managed_vectara.py b/llama-index-integrations/indices/llama-index-indices-managed-vectara/tests/test_indices_managed_vectara.py index a0b644f58a92546ca02131b4a8d825a980268c1c..2825bc14c76e6a2b70879b48c202c7233e1879ab 100644 --- a/llama-index-integrations/indices/llama-index-indices-managed-vectara/tests/test_indices_managed_vectara.py +++ b/llama-index-integrations/indices/llama-index-indices-managed-vectara/tests/test_indices_managed_vectara.py @@ -95,6 +95,7 @@ def get_nodes() -> List[Node]: return nodes +# Normal vectara index fixture @pytest.fixture() def vectara1(): docs = get_docs() @@ -110,6 +111,24 @@ def vectara1(): vectara1.delete_ref_doc(id) +# vectara index fixture, where we specify the base url +@pytest.fixture() +def vectara1_custom(): + docs = get_docs() + try: + vectara1_custom = VectaraIndex.from_documents( + docs, vectara_base_url="https://api.vectara.io" + ) + except ValueError: + pytest.skip("Missing Vectara credentials, skipping test") + + yield vectara1_custom + + # Tear down code + for id in vectara1_custom.doc_ids: + vectara1_custom.delete_ref_doc(id) + + def test_simple_retrieval(vectara1) -> None: docs = get_docs() qe = vectara1.as_retriever(similarity_top_k=1) @@ -119,6 +138,15 @@ def test_simple_retrieval(vectara1) -> None: assert res[0].node.node_id == docs[1].doc_id +def test_simple_retrieval_with_custom_base_url(vectara1_custom) -> None: + docs = get_docs() + qe = vectara1_custom.as_retriever(similarity_top_k=1) + res = qe.retrieve("Find me something different") + assert len(res) == 1 + assert res[0].node.get_content() == docs[1].text + assert res[0].node.node_id == docs[1].doc_id + + def test_mmr_retrieval(vectara1) -> None: docs = get_docs() @@ -257,8 +285,6 @@ def test_chain_rerank_retrieval(vectara1) -> None: def test_custom_prompt(vectara1) -> None: - docs = get_docs() - qe = vectara1.as_query_engine( similarity_top_k=3, n_sentences_before=0, @@ -269,11 +295,10 @@ def test_custom_prompt(vectara1) -> None: prompt_text='[\n {"role": "system", "content": "You are an expert in summarizing the future of Vectara\'s inegration with LlamaIndex. Your summaries are insightful, concise, and highlight key innovations and changes."},\n #foreach ($result in $vectaraQueryResults)\n {"role": "user", "content": "What are the key points in result number $vectaraIdxWord[$foreach.index] about Vectara\'s LlamaIndex integration?"},\n {"role": "assistant", "content": "In result number $vectaraIdxWord[$foreach.index], the key points are: ${result.getText()}"},\n #end\n {"role": "user", "content": "Can you generate a comprehensive summary on \'Vectara\'s LlamaIndex Integration\' incorporating all the key points discussed?"}\n]\n', ) - res = qe.query("How will Vectara's integration look in the future?") + res = qe.query("How does Vectara's integration with llamaindex work?") assert "integration" in str(res).lower() assert "llamaindex" in str(res).lower() assert "vectara" in str(res).lower() - assert "result" in str(res).lower() def test_update_doc(vectara1) -> None: @@ -337,7 +362,7 @@ def test_file_upload(vectara2) -> None: query_engine = vectara2.as_query_engine(similarity_top_k=3) res = query_engine.query("How is Paul related to Reddit?") summary = res.response - assert "paul graham" in summary.lower() and "reddit" in summary.lower() + assert "paul" in summary.lower() and "reddit" in summary.lower() assert "https://www.paulgraham.com/worked.html" in str(res.source_nodes) @@ -419,7 +444,7 @@ def test_chat(vectara2) -> None: res = chat_engine.chat("What did he learn at the graduate school he selected?") summary = res.response - assert "learn" in summary.lower() + assert "paul" in summary.lower() assert "harvard" in summary.lower() assert res.metadata["fcs"] > 0 assert chat_engine.conv_id == chat_id