Rivalz hackathon
  • Developer Guides (Short Version)
    • Rivalz AI World Hackathon
      • Hackathon Ideas
    • Developer Overview
    • OCY - AI Data Storage and RAG
      • Rivalz Developer Console
        • How to signup an account
        • How to claim your free credit
        • Dashborad
        • Billing
        • Profile
        • Upload history
      • Access to the Rivalz Storage
      • Knowledge Base
      • Retrieval Augmented Generation (RAG)
      • SDK and APIs
        • PythonSDK
        • NodejsSDK
    • ADCS - AI/Data Oracle System
      • Data Providers
      • Adaptor Creator
      • Dapp Creator
        • Off-chain Components
          • Adaptor
          • ADCS Nodes
            • Fetcher Node
            • Data Storage
            • Data Providers
        • On-chain Components
          • CoordinatorBase
          • Consumer Contract
          • Oracle Router
          • Oracle
      • Dapp-Example
      • Embed a data provider to AI Agents
      • Self-Agent Deployment
    • Eliza ai16z
    • Project Submission (CLOSED)
Powered by GitBook
On this page
  • How to create your first knowledge base
  • 1. Prerequisites
  • 2. Setup the SDK
  • 3. Initialize the client
  • 4. Create a knowledge base
  • Full code example
Export as PDF
  1. Developer Guides (Short Version)
  2. OCY - AI Data Storage and RAG

Knowledge Base

PreviousAccess to the Rivalz StorageNextRetrieval Augmented Generation (RAG)

Last updated 5 months ago

In Rivalz, Knowledge is a collection of documents. A knowledge base can be integrated into an application as a retrieval context.

How to create your first knowledge base

1. Prerequisites

  • Saving vector data can be an expensive process, so credits are required to create a knowledge base. However, we’ve got you covered! When you create an account, you’ll receive 500 USD in free credits. Visit to learn How to claim your free credit.

  • Select the document you need to upload in PDF format.

2. Setup the SDK

To install the SDK, run the following command for your preferred language:

  • Python

pip install rivalz-client
  • Nodejs

npm install rivalz-client

3. Initialize the client

To initialize the client with your secret token, you have two options:

  1. Using a .env file with SECRET_TOKEN variable

  2. you can pass the secret token directly to the client during initialization

  • Python: Use the python-dotenv package to load the token from .env file

from dotenv import load_dotenv
from rivalz_client.client import RivalzClient
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)
  • Nodejs: Use the dotenv package to load the token from .env file

import RivalzClient from 'rivalz-client';
import dotenv from 'dotenv';

dotenv.config();

const rivalzClient = new RivalzClient(process.env.SECRET_TOKEN);

4. Create a knowledge base

Use the create_rag_knowledge_base method to create a knowledge base. This method takes the path to the PDF document and the name of the knowledge base as arguments.

# python
# create knowledge base
knowledge_base = client.create_rag_knowledge_base('sample.pdf', 'knowledge_base_name')
print(knowledge_base) # print the knowledge base details
// node.js
// create knowledge base
const knowledgeBase = await rivalzClient.createRagKnowledgeBase('sample.pdf', 'knowledge_base_name');
console.log(knowledgeBase); // print the knowledge base details

This method will return the knowledge base details as a JSON object, including the unique knowledge base ID. You can use this ID for future queries on the knowledge base.

The embedding process may take some time depending on the size of the document. This process will be processed off request and you can check when the process is done by checking the status of the knowledge base.

knowledge_base = client.create_rag_knowledge_base('sample.pdf', 'knowledge_base_name')
print(knowledge_base) # you will get the knowledge base id with the status 'processing'
# check the status of the knowledge base
knowledge_base = client.get_knowledge_base(knowledge_base['id'])
print(knowledge_base['status']) # you will get 'ready' when the process is done
// node.js
const knowledgeBase = await rivalzClient.createRagKnowledgeBase('sample.pdf', 'knowledge_base_name');
console.log(knowledgeBase); // print the knowledge base details at this point the status will be 'processing'
// check the status of the knowledge base
const knowledgeBaseStatus = await rivalzClient.getKnowledgeBase(knowledgeBase.id);
console.log(knowledgeBaseStatus.status); // you will get 'ready' when the process is done

Full code example

  • Python

# 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')
    print(knowledge_base) # print the knowledge base details
    #sleep for 5 seconds to allow the process to finish you can do this in a loop
    time.sleep(5)
    # check the status of the knowledge base
    knowledge_base = client.get_knowledge_base(knowledge_base['id'])
    print(knowledge_base['status']) # you will get 'ready' when the process is done
if __name__ == '__main__':
    main()
  • Nodejs

// main.js
import RivalzClient from 'rivalz-client';
import dotenv from 'dotenv';

dotenv.config();

async function main() {
  const rivalzClient = new RivalzClient(process.env.SECRET_TOKEN);
  const knowledgeBase = await rivalzClient.createRagKnowledgeBase('sample.pdf', 'knowledge_base_name');
  console.log(knowledgeBase); // print the knowledge base details
  // sleep for 5 seconds to allow the process to finish you can do this in a loop
  await new Promise(resolve => setTimeout(resolve, 5000));
  // check the status of the knowledge base
  const knowledgeBaseStatus = await rivalzClient.getKnowledgeBase(knowledgeBase.id);
}

main();

Congratulations! You have successfully created your first knowledge base.

API secret key to authenticate. If you don't have one, please refer to the "" section to obtain it.

here
Get API Secret key