Spaces:
Sleeping
Sleeping
Upload 5 files
Browse files- .gitattributes +1 -0
- app.py +18 -0
- faiss_index/index.faiss +3 -0
- faiss_index/index.pkl +3 -0
- rag_query.py +55 -0
- requirements.txt +6 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
faiss_index/index.faiss filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from rag_query import query_rag
|
| 3 |
+
|
| 4 |
+
st.title("United Methodist Church Discipline Assistant")
|
| 5 |
+
st.write("Ask questions about The Book of Discipline 2020-2024")
|
| 6 |
+
|
| 7 |
+
# User input
|
| 8 |
+
query = st.text_input("Enter your question:", "Can you tell me the history of United Methodist Church")
|
| 9 |
+
|
| 10 |
+
# Process query and display response
|
| 11 |
+
if st.button("Submit"):
|
| 12 |
+
with st.spinner("Fetching response..."):
|
| 13 |
+
response = query_rag(query)
|
| 14 |
+
st.markdown(response)
|
| 15 |
+
|
| 16 |
+
# Instructions
|
| 17 |
+
st.sidebar.header("Instructions")
|
| 18 |
+
st.sidebar.write("Enter a question about The United Methodist Church's Bood Of Discipline. The assistant will retrieve relevant sections and explain them in simple language, citing parts, paragraphs, and titles as needed.")
|
faiss_index/index.faiss
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2f1c3305c2d8a2de660469c9a995e5aef965fd87c98d66166c70bad06f164a3d
|
| 3 |
+
size 2267181
|
faiss_index/index.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4152c2facf6490f8497a0574b95b8815eb4c0aa19b081def5d952ddb7099bf81
|
| 3 |
+
size 570125
|
rag_query.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
|
| 6 |
+
# Initialize OpenAI client with Streamlit secrets
|
| 7 |
+
client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"])
|
| 8 |
+
|
| 9 |
+
# Load vector store
|
| 10 |
+
def load_vector_store():
|
| 11 |
+
embeddings = OpenAIEmbeddings(model="text-embedding-ada-002", openai_api_key=st.secrets["OPENAI_API_KEY"])
|
| 12 |
+
return LangChainFAISS.load_local(
|
| 13 |
+
folder_path="faiss_index",
|
| 14 |
+
embeddings=embeddings,
|
| 15 |
+
allow_dangerous_deserialization=True
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
vector_store = load_vector_store()
|
| 19 |
+
|
| 20 |
+
def query_rag(query, top_k=5):
|
| 21 |
+
results = vector_store.similarity_search(query, k=top_k)
|
| 22 |
+
context = ""
|
| 23 |
+
for i, doc in enumerate(results):
|
| 24 |
+
meta = doc.metadata
|
| 25 |
+
context += f"Source: {meta['source']}\n"
|
| 26 |
+
if meta["part"]:
|
| 27 |
+
context += f"Part: {meta['part']}\n"
|
| 28 |
+
context += f"Heading: {meta['heading']}\n"
|
| 29 |
+
if meta["title"]:
|
| 30 |
+
context += f"Title: {meta['title']}\n"
|
| 31 |
+
if meta["sub_title"]:
|
| 32 |
+
context += f"Sub-title: {meta['sub_title']}\n"
|
| 33 |
+
if meta["paragraph_number"]:
|
| 34 |
+
context += f"Paragraph {meta['paragraph_number']}"
|
| 35 |
+
if meta["paragraph_title"]:
|
| 36 |
+
context += f": {meta['paragraph_title']}\n"
|
| 37 |
+
elif meta["paragraph_number"]:
|
| 38 |
+
context += "\n"
|
| 39 |
+
if meta["sub_para_title"]:
|
| 40 |
+
context += f"Sub-paragraph: {meta['sub_para_title']}\n"
|
| 41 |
+
context += f"Text: {doc.page_content}\n\n"
|
| 42 |
+
|
| 43 |
+
prompt = f"""
|
| 44 |
+
User Query: {query}
|
| 45 |
+
Retrieved Context:
|
| 46 |
+
{context}
|
| 47 |
+
|
| 48 |
+
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.
|
| 49 |
+
"""
|
| 50 |
+
response = client.chat.completions.create(
|
| 51 |
+
model="gpt-4o-mini",
|
| 52 |
+
messages=[{"role": "user", "content": prompt}],
|
| 53 |
+
max_tokens=1000
|
| 54 |
+
)
|
| 55 |
+
return response.choices[0].message.content
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
openai
|
| 2 |
+
langchain
|
| 3 |
+
langchain-community
|
| 4 |
+
langchain-openai
|
| 5 |
+
faiss-cpu
|
| 6 |
+
streamlit
|