Skip to content

Conversation

@manel1874
Copy link
Contributor

@manel1874 manel1874 commented Jan 8, 2025

This adds the ability for nilAI to perform the first version of RAG with nilDB.

Solution

  • Only touches the chat completion endpoint
  • Adds a key value in the api endpoint parameters called nilrag. This holds a dictionary which is empty by default. In case it is not empty we need to provide a list of nilDB nodes with the following elements: nodes url, jwt for each node, schema id and query id.
  • If nilrag dictionary is not empty, it runs nilRAG with nilDB instances. To do so, the message sent by the user is used for RAG and the RAG output is appended to the system messages sent to the LLM.

Testing

  1. Through API:
    Update the bearer token for each node and use the following set of parameters:
 {
  "model": "Llama-3.2-1B-Instruct",
  "messages": [
    {
      "role": "system",
      "content": "You are a helpful assistant."
    },
    {
      "role": "user",
      "content": "What is your name?"
    }
  ],
  "temperature": 0.2,
  "max_tokens": 2048,
  "stream": false,
  "nilrag": {
            "nodes": [
                {
                    "url": "https://nildb-node-a50d.sandbox.app-cluster.sandbox.nilogy.xyz/api/v1",
                    "bearer_token": "ADD_HERE_TOKEN_FOR_NODE_1",
                    "schema_id": "6aa651af-7762-4aaa-9089-82f8eab16201",
                    "diff_query_id": "dfcee886-231d-4a9d-9bdd-857f74a72964"
                },
                {
                    "url": "https://nildb-node-dvml.sandbox.app-cluster.sandbox.nilogy.xyz/api/v1",
                    "bearer_token": "ADD_HERE_TOKEN_FOR_NODE_2",
                    "schema_id": "6aa651af-7762-4aaa-9089-82f8eab16201",
                    "diff_query_id": "dfcee886-231d-4a9d-9bdd-857f74a72964"
                },
                {
                    "url": "https://nildb-node-guue.sandbox.app-cluster.sandbox.nilogy.xyz/api/v1",
                    "bearer_token": "eADD_HERE_TOKEN_FOR_NODE_3",
                    "schema_id": "6aa651af-7762-4aaa-9089-82f8eab16201",
                    "diff_query_id": "dfcee886-231d-4a9d-9bdd-857f74a72964"
                }
            ]
        }
}
  1. Through nilrag library
  • Install nilrag through pip:
pip install nilrag
  • Update the bearer tokens and save to a file with name query_nildb_config.json:
{
    "nodes": [
        {
            "url": "https://nildb-node-a50d.sandbox.app-cluster.sandbox.nilogy.xyz/api/v1",
            "bearer_token": "ADD_HERE_TOKEN_FOR_NODE_1",
            "schema_id": "6aa651af-7762-4aaa-9089-82f8eab16201",
            "diff_query_id": "dfcee886-231d-4a9d-9bdd-857f74a72964"
        },
        {
            "url": "https://nildb-node-dvml.sandbox.app-cluster.sandbox.nilogy.xyz/api/v1",
            "bearer_token": "ADD_HERE_TOKEN_FOR_NODE_2",
            "schema_id": "6aa651af-7762-4aaa-9089-82f8eab16201",
            "diff_query_id": "dfcee886-231d-4a9d-9bdd-857f74a72964"
        },
        {
            "url": "https://nildb-node-guue.sandbox.app-cluster.sandbox.nilogy.xyz/api/v1",
            "bearer_token": "ADD_HERE_TOKEN_FOR_NODE_3",
            "schema_id": "6aa651af-7762-4aaa-9089-82f8eab16201",
            "diff_query_id": "dfcee886-231d-4a9d-9bdd-857f74a72964"
        }
    ]
}
  • Run the below example (or take from here) with the token passed to nilai_chat_completion updated:
"""
Example of querying nilDB with NilAI using nilRAG.
"""

import os
import sys
import json
from nilrag.nildb_requests import NilDB, Node


JSON_FILE = "examples/query_nildb_config.json"

# Load NilDB from JSON file if it exists
if os.path.exists(JSON_FILE):
    print("Loading NilDB configuration from file...")
    with open(JSON_FILE, "r", encoding="utf-8") as f:
        data = json.load(f)
        nodes = []
        for node_data in data["nodes"]:
            nodes.append(
                Node(
                    url=node_data["url"],
                    node_id=None,
                    org=None,
                    bearer_token=node_data.get("bearer_token"),
                    schema_id=node_data.get("schema_id"),
                    diff_query_id=node_data.get("diff_query_id"),
                )
            )
        nilDB = NilDB(nodes)
else:
    print("Error: NilDB configuration file not found.")
    sys.exit(1)

print("NilDB instance:", nilDB)
print()

print('Query nilAI with nilRAG...')
response = nilDB.nilai_chat_completion(
    nilai_url="http://127.0.0.1:8080/",
    token="1770c101-dd83-4fbc-b996-ef8121889172",
    messages=[
        {"role": "user", "content": "Tell me about Asia."}
    ],
    temperature=0.2,
    max_tokens=2048,
    stream=False,
)
print(response)

@manel1874 manel1874 requested review from jcabrero and jimouris January 8, 2025 11:55

# Initialize secret keys
num_parties = len(nilDB.nodes)
additive_key = nilql.secret_key(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this key None or something not relevant for secret sharing? I do not fully understand the key's role in secret shared RAG.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its part of nilql logic of abstracting away the difference between shares and homomorphic encryption. For secret sharing we need to create kind of a phantom key

@jcabrero jcabrero linked an issue Jan 8, 2025 that may be closed by this pull request
@jcabrero jcabrero merged commit 8b00031 into main Jan 14, 2025
1 check passed
@jcabrero jcabrero deleted the feat/nilrag branch March 25, 2025 08:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Integration with nilRAG

4 participants