A CLI tool for interacting with the Vectara platform, including advanced text processing and indexing features.
Project description
vectara-cli
vectara-cli
is a Python package designed to interact with the Vectara platform, providing a command-line interface (CLI) and a set of APIs for indexing and querying documents, managing corpora, and performing advanced text analysis and processing tasks. This package is particularly useful for developers and data scientists working on search and information retrieval applications.
Features
- Indexing text and documents into Vectara corpora.
- Querying indexed documents.
- Creating and deleting corpora.
- Advanced text processing and analysis using pre-trained models (optional advanced package).
Installation
Basic Installation
The basic installation includes the core functionality for interacting with the Vectara platform.
pip install vectara-cli
Advanced Installation
The advanced installation includes additional dependencies for advanced text processing and analysis features. This requires PyTorch, Transformers, and Accelerate, which can be substantial in size.
pip install vectara-cli[advanced]
Ensure you have an appropriate PyTorch version installed for your system, especially if you're installing on a machine with GPU support. Refer to the official PyTorch installation guide for more details.
Usage
Basic Usage
To enhance the "Basic Usage" section of your README.md for the PyPi package, we'll include examples that cover all the functionalities provided by core.py
. This will give users a comprehensive guide on how to utilize the VectaraClient class for various operations such as indexing text, querying documents, creating and deleting corpora, indexing documents from a folder, and handling document uploads.
Basic Usage
Setting Up a Vectara Client
First, initialize the Vectara client with your customer ID and API key. This client will be used for all subsequent operations.
from vectara_cli.core import VectaraClient
customer_id = 'your_customer_id'
api_key = 'your_api_key'
vectara_client = VectaraClient(customer_id, api_key)
Indexing a Document
To index a document, you need its corpus ID, a unique document ID, and the text you want to index. Optionally, you can include context, metadata in JSON format, and custom dimensions.
corpus_id = 'your_corpus_id'
document_id = 'unique_document_id'
text = 'This is the document text you want to index.'
context = 'Document context'
metadata_json = '{"author": "John Doe"}'
vectara_client.index_text(corpus_id, document_id, text, context, metadata_json)
Indexing Documents from a Folder
To index all documents from a specified folder into a corpus, provide the corpus ID and the folder path.
corpus_id = 'your_corpus_id'
folder_path = '/path/to/your/documents'
results = vectara_client.index_documents_from_folder(corpus_id, folder_path)
for document_id, success, extracted_text in results:
if success:
print(f"Successfully indexed document {document_id}.")
else:
print(f"Failed to index document {document_id}.")
Querying Documents
To query documents, specify your search query, the number of results you want to return, and the corpus ID.
query_text = 'search query'
num_results = 10 # Number of results to return
corpus_id = 'your_corpus_id'
results = vectara_client.query(query_text, num_results, corpus_id)
print(results)
Creating a Corpus
You can create a new corpus by specifying its ID, name, description, and other settings.
create_corpus_response = vectara_client.create_corpus(
corpus_id=123456789,
name="Example Corpus",
description="This is an example corpus.",
dtProvision=1234567890,
enabled=True,
swapQenc=False,
swapIenc=False,
textless=False,
encrypted=False,
encoderId="default",
metadataMaxBytes=10000,
customDimensions=[
{"name": "dimension1", "description": "First custom dimension", "servingDefault": 1.0, "indexingDefault": 1.0}
],
filterAttributes=[
{"name": "filter1", "description": "First filter attribute", "indexed": True, "type": "FILTER_ATTRIBUTE_TYPE__UNDEFINED", "level": "FILTER_ATTRIBUTE_LEVEL__UNDEFINED"}
]
)
print(create_corpus_response)
Deleting a Corpus
To delete a corpus, you only need to provide its ID.
corpus_id = 'your_corpus_id'
response, success = vectara_client.delete_corpus(corpus_id)
if success:
print("Corpus deleted successfully.")
else:
print("Failed to delete corpus:", response)
Uploading a Document
To upload and index a document, specify the corpus ID, the path to the document, and optionally, a document ID and metadata.
corpus_id = 'your_corpus_id'
file_path = '/path/to/your/document.pdf'
document_id = 'unique_document_id' # Optional
metadata = {"author": "Author Name", "title": "Document Title"} # Optional
try:
response, status = vectara_client.upload_document(corpus_id, file_path, document_id, metadata)
print("Upload successful:", response)
except Exception as e:
print("Upload failed:", str(e))
Advanced Usage
The advanced features allow you to enrich your indexes with additional information automatically. This should produce better results for retrieval.
Non-Commercial Advanced Rag Using Rebel
insert images here
Use the use the Rebel Class
for advanced indexing. This will automatically extract named entities
, key phrases
, and other relevant information from your documents :
from vectara_cli.advanced.non_commercial.rebel import Rebel
folder_path = '/path/to/your/documents'
query_text = 'search query'
num_results = 10 # Number of results to return
# Initialize the Rebel instance for advanced non-commercial text processing
rebel = Rebel()
# Perform advanced indexing
corpus_id_1, corpus_id_2 = rebel.advanced_upsert_folder(vectara_client, corpus_id_1, corpus_id_2, folder_path)
# Vanilla Retrieval
plain_results = vectara_client.query(query_text, num_results, corpus_id_1)
# Enhanced Retrieval
enhanced_results = vectara_client.query(query_text, num_results, corpus_id_2)
# Print Results
print("=== Plain Results ===")
for result in plain_results:
print(f"Document ID: {result['documentIndex']}, Score: {result['score']}, Text: {result['text'][:100]}...")
print("\n=== Enhanced Results ===")
for result in enhanced_results:
print(f"Document ID: {result['documentIndex']}, Score: {result['score']}, Text: {result['text'][:100]}...")
Contributing
Contributions to vectara-cli
are welcome! Please refer to the contributing guidelines in the repository for more information on how to contribute.
License
vectara-cli
is MIT licensed. See the LICENSE file for more details.
This README provides a comprehensive guide for installing and using the vectara-cli
package. For further information or assistance, please refer to the Vectara documentation or submit an issue on the GitHub repository.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for vectara_cli-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3514263bbfb3cc629716c669f59f62d7eaca827c65dc5cb4ae312a5198c82c57 |
|
MD5 | ad5beb6bcf6b7e873d7ad0a9ed5d803e |
|
BLAKE2b-256 | e84d4cda863b076e609d56a5c3c1baba72ceaa41156442279ef04becc1c1c22f |