RAG QA Database using Embeddings

parmarjatin4911@gmail.com - Jan 28 - - Dev Community

RAG QA Database using Embeddings

from datasets import load_dataset
from txtai import Embeddings

Load dataset

ds = load_dataset("web_questions", split="train")

Initialize and index embeddings

embeddings = Embeddings(path="sentence-transformers/nli-mpnet-base-v2", content=True)
embeddings.index([(uid, {"url": row["url"], "text": row["question"],
"answer": ", ".join(row["answers"])}, None)
for uid, row in enumerate(ds)])

Save the embeddings

embeddings.save("questions.tar.gz")

Search embeddings

def question(text):
return embeddings.search(f"select text, answer, score from txtai where similar('{text}') limit 1")

print(question("Tell me an animal found offshore in Florida"))

User Interface

import gradio as gr
from txtai.app import Application

Initialize the saved embeddings

app = Application("path: questions.tar.gz")

def search_question(query):
results = app.search(f"select text, answer, score from txtai where similar('{query}') limit 1")[0]
return results['text'], results['answer'], results['score']

interface = gr.Interface(
fn=search_question,
inputs=gr.components.Textbox(label="Enter your query"),
outputs=[gr.components.Textbox(label="Question"),gr.components.Textbox(label="Answer"),gr.components.Number(label="Similarity")]
)

interface.launch()

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player