Python SDK
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 for interacting with the Rivalz API. It allows you to upload files, download files,
and manage files on the Rivalz platform using IPFS.
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:
pip install rivalz-client
Usage
Here is a detailed guide 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')
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 knowledge base from a document
To vectorize a document (which will be used as embedding for the RAG) and create a knowledge base, use the
create_rag_knowledge_base
method with the path to the document. This
method returns the knowledge base id which can be used to create a conversation.
We now only support PDF files for creating knowledge bases.
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 document to an existed knowledge base
To add document to existed knowledge base, use the add_document_to_knowledge_base
method with the knowledge base id
and the path to the document.
response = client.add_document_to_knowledge_base('knowledge_base_id', 'path/to/your/document.pdf')
print(response)
Deleting document from an existed knowledge base
To delete document from existed 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('knowledge_base_id', 'document_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)
Creating a conversation
To create a conversation, use the create_chat_session
method with the knowledge base id and the question. This will
return the AI response along with the chat session id.
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
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']
# 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()