This is a TypeScript library that provides functionalities for Rivazl Ai
npm install rivalz-clientbash
npm install rivalz-client
`
or
`bash
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:
`typescript
import RivalzClient from 'rivalz-client';
const rivalzClient = new RivalzClient('your-secret-key');
`
API:
##### 1 Upload File
`typescript
rivalzClient.uploadFile(file, fileName)
`
- file: Buffer of the file to be uploaded.
- Returns a promise that resolves to the IPFS hash of the uploaded file.
##### 2. Upload passport file
`typescript
rivalzClient.uploadPassport(file)
`
- file: Buffer of the file to be uploaded.
- Returns a promise that resolves to the IPFS hash of the uploaded file.
##### 3. Download File and save it to the local file system (Node.js only)
`typescript
rivalzClient.downloadFile(ipfsHash, savePath)
`
- ipfsHash: The IPFS hash of the file to be downloaded.
- savePath: The path where the downloaded file will be saved.
- Returns a promise that resolves to the path of the saved file.
##### 4. Download File and return it as buffer
`typescript
rivalzClient.download(ipfsHash)
`
- ipfsHash: The IPFS hash of the file to be downloaded.
- Returns a promise that resolves to a buffer containing the downloaded file.
##### 5. Delete File
`typescript
rivalzClient.deleteFile(ipfsHash)
`
- ipfsHash: The IPFS hash of the file to be deleted.
- Returns a promise that resolves to the IPFS hash of the deleted file.
##### 6. Get Uploaded History
`typescript
rivalzClient.getUploadedHistory(page, size)
`
- page: The page number of the uploaded history.
- size: The number of items per page.
- Returns a promise that resolves to an array of uploaded files.
Please replace 'your-secret', file, passport, ipfsHash, and savePath with actual values when using the RivalzClient class.
`
Example
Example upload file for node.js project:
`typescript
const RivalzClient = require('rivalz-client');
// import RivalzClient from 'rivalz-client';
const fs = require('fs');
// import fs from 'fs';
const file = fs.readFileSync('file_path');
const rivalzClient = new RivalzClient('your-secret-key');
rivalzClient.uploadFile(file, 'file_name);'
`
Example download file for node.js project:
`typescript
const RivalzClient = require('rivalz-client');
const rivalzClient = new RivalzClient('your-secret-key');
rivalzClient.downloadFile('ipfs_hash_uploaded', 'save_path')
`
RAG (Retrieval-Augmented Generation) API
!rag-flow
$3
Before using the RAG API, you need some rivalz credits. Claim for free now here
$3
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.
`typescript
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'}
`
$3
To add document to existed knowledge base, use the addDocumentToKnowledgeBase method with the knowledge base id
and the path to the document.
`typescript
const response = await client.addDocumentToKnowledgeBase('knowledge_base_id', 'path/to/your/document.pdf')
console.log(response)
`
$3
To delete document from existed knowledge base, use the deleteDocumentFromKnowledgeBase method with the knowledge
base id and the document name.
`typescript
const response = await client.deleteDocumentFromKnowledgeBase('knowledge_base_id', 'document_id')
console.log(response)
`
$3
To get all knowledge bases, use the getKnowledgeBases method.
`typescript
const response = await client.getKnowledgeBases()
console.log(response)
`
$3
To get details of a knowledge base, use the getKnowledgeBase method with the knowledge base id.
`typescript
const response = await client.getKnowledgeBase('knowledge_base_id')
console.log(response)
`
$3
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.
`typescript
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'}
`
$3
To add a message to a conversation, use the same method createChatSession with the chat session id and the message.
`typescript
const response = await client.createChatSession('knowledge_base_id', 'message', 'chat_session_id')
console.log(response)
`
$3
To get all conversations, use the getChatSessions method.
`typescript
const response = await client.getChatSessions()
console.log(response)
`
$3
To get details of a conversation (which contains chat history for this conversation), use the getChatSession method
with the chat session id.
`typescript
const response = client.getChatSession('chat_session_id')
console.log(response)
`
$3
To get all uploaded documents, use the getUploadedDocuments method.
`typescript
const response = await client.getUploadedDocuments()
console.log(response)
`
$3
Here is a complete example demonstrating how to use the rivalz-client to create a simple RAG conversation based on a
PDF document:
`typescript
/*
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;
// 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()
``