Spaces:
Sleeping
Sleeping
Created new rag_query.py
Browse files- rag_query.py +57 -0
rag_query.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from openai import OpenAI
|
| 2 |
+
from langchain_community.vectorstores import FAISS as LangChainFAISS
|
| 3 |
+
from langchain_openai import OpenAIEmbeddings
|
| 4 |
+
import streamlit as st
|
| 5 |
+
from functools import lru_cache
|
| 6 |
+
|
| 7 |
+
# Initialize OpenAI client with Streamlit secrets
|
| 8 |
+
client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"])
|
| 9 |
+
|
| 10 |
+
# Cache the vector store loading
|
| 11 |
+
@lru_cache(maxsize=1)
|
| 12 |
+
def load_vector_store():
|
| 13 |
+
embeddings = OpenAIEmbeddings(model="text-embedding-ada-002", openai_api_key=st.secrets["OPENAI_API_KEY"])
|
| 14 |
+
return LangChainFAISS.load_local(
|
| 15 |
+
folder_path="faiss_index",
|
| 16 |
+
embeddings=embeddings,
|
| 17 |
+
allow_dangerous_deserialization=True
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
vector_store = load_vector_store()
|
| 21 |
+
|
| 22 |
+
def query_rag(query, top_k=8):
|
| 23 |
+
results = vector_store.similarity_search(query, k=top_k)
|
| 24 |
+
context = ""
|
| 25 |
+
for i, doc in enumerate(results):
|
| 26 |
+
meta = doc.metadata
|
| 27 |
+
context += f"Source: {meta['source']}\n"
|
| 28 |
+
if meta["part"]:
|
| 29 |
+
context += f"Part: {meta['part']}\n"
|
| 30 |
+
context += f"Heading: {meta['heading']}\n"
|
| 31 |
+
if meta["title"]:
|
| 32 |
+
context += f"Title: {meta['title']}\n"
|
| 33 |
+
if meta["sub_title"]:
|
| 34 |
+
context += f"Sub-title: {meta['sub_title']}\n"
|
| 35 |
+
if meta["paragraph_number"]:
|
| 36 |
+
context += f"Paragraph {meta['paragraph_number']}"
|
| 37 |
+
if meta["paragraph_title"]:
|
| 38 |
+
context += f": {meta['paragraph_title']}\n"
|
| 39 |
+
elif meta["paragraph_number"]:
|
| 40 |
+
context += "\n"
|
| 41 |
+
if meta["sub_para_title"]:
|
| 42 |
+
context += f"Sub-paragraph: {meta['sub_para_title']}\n"
|
| 43 |
+
context += f"Text: {doc.page_content}\n\n"
|
| 44 |
+
|
| 45 |
+
prompt = f"""
|
| 46 |
+
User Query: {query}
|
| 47 |
+
Retrieved Context:
|
| 48 |
+
{context}
|
| 49 |
+
|
| 50 |
+
Provide a clear, easy-to-understand explanation based on the context. Include direct quotes with citations (e.g., 'Part I, Paragraph 1: Article I. Declaration of Union') where relevant. Structure the response by grouping information from the same 'part' together.
|
| 51 |
+
"""
|
| 52 |
+
response = client.chat.completions.create(
|
| 53 |
+
model="gpt-4o-mini",
|
| 54 |
+
messages=[{"role": "user", "content": prompt}],
|
| 55 |
+
max_tokens=3000
|
| 56 |
+
)
|
| 57 |
+
return response.choices[0].message.content
|