https://www.npmjs.com/package/rivalz-client

Rivalz client Node.js SDK

This is a TypeScript library that provides functionalities for Rivazl Ai

Installation

Before you start, make sure you have Node.js and npm/yarn installed on your machine.

  npm install rivalz-client
  

or

  yarn add rivalz-client
  

Usage

  1. Creates key in Rivalz Dashboard and use it to initialize the RivalzClient class. After installing the package, Please come in the Rivalz Dashboard to create a encrypted key and secret key.

    • Encrypt key used for encrypting the file
    • Secret key used for authentication
  2. Import and use the RivalzClient class in your TypeScript/JavaScript code:

      import RivalzClient from 'rivalz-client';
    
    const rivalzClient = new RivalzClient('your-secret-key');
      

RAG (Retrieval-Augmented Generation) API

conversation.png

Prerequisites

Before using the rivalz RAG API, you need 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 createRagKnowledgeBase 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.

  const response = await client.createRagKnowledgeBase('path/to/your/document.pdf', 'knowledge_base_name')
console.log(response)
// {'id': '66fa5bf022e73c17073768f0', 'name': 'test', 'files': '1727683567711_sample.pdf', 'userId': '66c4151c98bd0d3d47de682a', 'status': 'processing'}
  

The file will be processed in the background, and the status will be ready when the process is done.

Adding document to an existed knowledge base

To add document to existed knowledge base, use the addDocumentToKnowledgeBase method with the knowledge base id and the path to the document.

  const response = await client.addDocumentToKnowledgeBase('path/to/your/document.pdf', 'knowledge_base_id')
console.log(response)
  

All the documents will be processed in the background, and the status will be ready when the process is done.

Deleting document from an existed knowledge base

To delete document from existed knowledge base, use the deleteDocumentFromKnowledgeBase method with the knowledge base id and the document name.

  const response = await client.deleteDocumentFromKnowledgeBase('document_id', 'knowledge_base_id')
console.log(response)
  

Getting all knowledge bases

To get all knowledge bases, use the getKnowledgeBases method.

  const response = await client.getKnowledgeBases()
console.log(response)
  

Getting details of a knowledge base

To get details of a knowledge base, use the getKnowledgeBase method with the knowledge base id.

  const response = await client.getKnowledgeBase('knowledge_base_id')
console.log(response)
  

Creating a conversation

To create a conversation, use the createChatSession method with the knowledge base id and the question. This will return the AI response along with the chat session id.

  const response = await client.createChatSession('knowledge_base_id', 'question')
console.log(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 createChatSession with the chat session id and the message.

  const response = await client.createChatSession('knowledge_base_id', 'message', 'chat_session_id')
console.log(response)
  

Getting all conversations

To get all conversations, use the getChatSessions method.

  const response = await client.getChatSessions()
console.log(response)
  

Getting details of a conversation

To get details of a conversation (which contains chat history for this conversation), use the getChatSession method with the chat session id.

  const response = client.getChatSession('chat_session_id')
console.log(response)
  

Get uploaded documents

To get all uploaded documents, use the getUploadedDocuments method.

  const response = await client.getUploadedDocuments()
console.log(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.ts
 */

import RivalzClient from 'rivalz-client';

const main = async () => {
  // Initialize the RivalzClient with the secret token
  const client = new RivalzClient('your-secret-key');
  // create knowledge base
  const knowledgeBase = await client.createRagKnowledgeBase('sample.pdf', 'knowledge_base_name');
  const knowledgeBaseId = knowledgeBase.id;
  if(knowledgeBase.status !== 'ready') {
    console.log('Knowledge base is still processing');
    // wait for 5 seconds to allow the process to finish
    await new Promise(resolve => setTimeout(resolve, 5000));
  }
  // create conversation
  let conversation = await client.createChatSession(knowledgeBaseId, 'what is the document about?');
  const conversationId = conversation.session_id;
  // add message to conversation
  conversation = await client.createChatSession(knowledgeBaseId, 'What is a RAG application ?', conversationId);
  console.log(conversation.answer);
  
}
main()