Discussions

Ask a Question
Back to All

using Rag Collection in Python code

import gradientai
import os
from gradientai import Gradient

os.environ['GRADIENT_ACCESS_TOKEN'] = ""
os.environ['GRADIENT_WORKSPACE_ID'] = ""

Initialize your Gradient instance

gradient = Gradient()

def run_answer_example_with_rag_collection(*, gradient: Gradient) -> None:
question = "What are the titles of the books you got for knowledge"

print("==== Q & A ====")
print(f"Question: {question}\n")

print("Answering question using RAG collection...")
try:
    # Include the RAG collection ID in the source parameter, according to the error message format
    result = gradient.answer(
        question=question,
        source={
            "type": "document",  # Adjusted to 'document' as per error message
            "value": "Salesman" # Speculative inclusion of collection ID
        },
    )
    print(f"Answer: {result['answer']}")
except Exception as e:  # Broad exception to catch any errors from the method call
    print(f"An error occurred: {e}")
print("================\n")

Run the function

run_answer_example_with_rag_collection(gradient=gradient)

How can i make that it uses my RAG Collection to answer?