PythonSDK
Rivalz Python SDK for developers
https://pypi.org/project/rivalz-client/0.2.2/
from mimetypes import knownfiles
Rivalz Python Client
rivalz-client
is a Python client designed for interacting with the Rivalz API. It enables developers to upload, download, and manage files on the Rivalz platform using IPFS (InterPlanetary File System).
Features
Upload Files: Upload any file to the Rivalz platform and get an IPFS hash.
Upload Passport Images: Upload passport images to the Rivalz platform.
Download Files: Download files from the Rivalz platform using an IPFS hash.v
Delete Files: Delete files from the Rivalz platform using an IPFS hash.
Vectorize Documents: Vectorize documents to create a RAG (Retrieval-Augmented Generation) based on the document uploaded.
Create conversations: Create conversations based on the document uploaded.
Installation
You can install the rivalz-client package via pip using the following command:
pip install rivalz-client
Usage
This guide provides detailed instructions on how to use the rivalz-client to interact with the Rivalz API.
Initialization
First, import the RivalzClient
class and initialize it with your secret token. If you don’t provide a token, it will use a default example token.
from rivalz_client.client import RivalzClient
# Initialize the client with your secret token
client = RivalzClient('your_secret_token')
1. Uploading a File
To upload a file to the Rivalz, you can use the upload_file
method. Simply provide the path to the file you want to upload as an argument
response = client.upload_file('path/to/your/file.txt')
print(response)
2. Uploading a Passport Image
To upload a passport image, use the upload_passport
method. Provide the path to the passport image file.
response = client.upload_passport('path/to/your/passport_image.jpg')
print(response)
3. Downloading a File
To download a file, use the download_file
method with the IPFS hash of the file and the directory where you want to save the file.
file_path = client.download_file('QmSampleHash', 'save/directory')
print(f"File downloaded to: {file_path}")
4. Deleting a File
To delete a file, use the delete_file
method with the IPFS hash of the file you want to delete.
response = client.delete_file('QmSampleHash')
print(response)
5. Getting all uploaded files
To get your uploaded files, use the get_upload_history
method. This method accept page and size for pagination. Page start from 0. And default is page 0 (first page) and size is 10.
total_files_uploaded, upload_histories = client.get_upload_history(0, 10)
print(f"Total files uploaded: {total_files_uploaded} ")
print(f"Upload histories: {upload_histories}")
6. RAG (Retrieval-Augmented Generation) API
Prerequisites
Before using the RAG API, you need api key and some rivalz credits. Claim for free now here
Creating a knowledge base from a document
To vectorize a document and create a knowledge base for Retrieval-Augmented Generation (RAG), use the create_rag_knowledge_base
method, which takes the document's file path as input. This method generates a vectorized embedding of the document, assigns it a knowledge base ID, and stores it for future use in RAG-based conversations. Currently, this process supports only PDF files.
Click here to learn How to create a knowledge base
response = client.create_rag_knowledge_base('path/to/your/document.pdf', 'knowledge_base_name')
print(response)
# {'id': '66fa5bf022e73c17073768f0', 'name': 'test', 'files': '1727683567711_sample.pdf', 'userId': '66c4151c98bd0d3d47de682a'}
Adding documents to an existing knowledge base
To add a document to an existing knowledge base, use the add_document_to_knowledge_base
method. This method requires the knowledge base ID (from the knowledge base you’ve already created) and the file path of the new document.
response = client.add_document_to_knowledge_base('path/to/your/document.pdf', 'knowledge_base_id')
print(response)
Deleting documents from an existing knowledge base
To delete a document from an existing knowledge base, use the delete_document_from_knowledge_base
method with the knowledge base id and the document name.
response = client.delete_document_from_knowledge_base('document_id','knowledge_base_id',)
print(response)
Getting all knowledge bases
To get all knowledge bases, use the get_knowledge_bases
method.
response = client.get_knowledge_bases()
print(response)
Getting details of a knowledge base
To get details of a knowledge base, use the get_knowledge_base
method with the knowledge base id.
response = client.get_knowledge_base('knowledge_base_id')
print(response)
7. Conversations
Creating a conversation
To initiate a conversation in the RAG (Retrieval Augmented Generation) system, use the create_chat_session
method. This method requires the knowledge base ID (from your existing knowledge base) and the question you want to ask. The AI will return a response based on the context provided by the knowledge base, along with a chat session ID to continue the conversation if needed.
response = client.create_chat_session('knowledge_base_id', 'question')
print(response)
# {'answer': 'Hello! How can I help you today? \n', 'session_id': '66fa625fb58f5a4b9a30b983', 'userId': '66c4151c98bd0d3d47de682a'}
Adding a message to a conversation
To add a message to a conversation, use the same method create_chat_session
with the chat session id and the message.
response = client.create_chat_session('knowledge_base_id', 'message', 'chat_session_id')
print(response)
Getting all conversations
To get all conversations, use the get_chat_sessions
method.
response = client.get_chat_sessions()
print(response)
Getting details of a conversation
To get details of a conversation (which contains chat history for this conversation), use the get_chat_session
method with the chat session id.
response = client.get_chat_session('chat_session_id')
print(response)
Get uploaded documents
To get all uploaded documents, use the get_uploaded_documents
method.
response = client.get_uploaded_documents()
print(response)
Examples
Here is a complete example demonstrating how to use the rivalz-client
to create a simple RAG conversation based on a PDF document:
# main.py
import os
from dotenv import load_dotenv
from rivalz_client.client import RivalzClient
import time
def main():
# Load environment variables from .env file
load_dotenv()
# Get the secret token from environment variables
secret_token = os.getenv('SECRET_TOKEN')
if not secret_token:
raise ValueError("SECRET_TOKEN is not set in the environment variables.")
# Initialize the RivalzClient with the secret token
client = RivalzClient(secret_token)
# create knowledge base
knowledge_base = client.create_rag_knowledge_base('sample.pdf', 'knowledge_base_name')
knowledge_base_id = knowledge_base['id']
if knowledge_base['status'] == 'processing':
print('Knowledge base is processing')
#sleep for 5 seconds
time.sleep(5)
# create conversation
conversation = client.create_chat_session(knowledge_base_id, 'what is the document about?')
conversation_id = conversation['session_id']
# add message to conversation
conversation = client.create_chat_session(knowledge_base_id, 'what is the document about?', conversation_id)
print(conversation['answer'])
if __name__ == '__main__':
main()
Last updated