Spaces:
Paused
Paused
Update that oly allows the RAG model to get context from the provided database
Browse files
app.py
CHANGED
|
@@ -1,73 +1,88 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import ollama
|
| 3 |
-
import chromadb
|
| 4 |
-
from chromadb.utils import embedding_functions
|
| 5 |
-
import os
|
| 6 |
-
import time
|
| 7 |
-
|
| 8 |
-
# --- SETUP ---
|
| 9 |
-
DB_PATH = "./legal_db"
|
| 10 |
-
COLLECTION_NAME = "legal_docs"
|
| 11 |
-
EMBEDDING_MODEL = 'hf.co/CompendiumLabs/bge-base-en-v1.5-gguf'
|
| 12 |
-
LANGUAGE_MODEL = 'llama3.2:3b'
|
| 13 |
-
|
| 14 |
-
st.set_page_config(page_title="Legal Assistant AI")
|
| 15 |
-
st.title("⚖️ Kenya Law RAG Bot")
|
| 16 |
-
|
| 17 |
-
# --- INITIALIZE DATABASE ---
|
| 18 |
-
@st.cache_resource
|
| 19 |
-
def get_collection():
|
| 20 |
-
client = chromadb.PersistentClient(path=DB_PATH)
|
| 21 |
-
ollama_ef = embedding_functions.OllamaEmbeddingFunction(
|
| 22 |
-
model_name=EMBEDDING_MODEL,
|
| 23 |
-
url="http://localhost:11434/api/embeddings"
|
| 24 |
-
)
|
| 25 |
-
return client.get_or_create_collection(name=COLLECTION_NAME, embedding_function=ollama_ef)
|
| 26 |
-
|
| 27 |
-
try:
|
| 28 |
-
collection = get_collection()
|
| 29 |
-
except Exception as e:
|
| 30 |
-
st.error(f"Could not connect to database: {e}")
|
| 31 |
-
st.stop()
|
| 32 |
-
|
| 33 |
-
# --- CHAT INTERFACE ---
|
| 34 |
-
if "messages" not in st.session_state:
|
| 35 |
-
st.session_state.messages = []
|
| 36 |
-
|
| 37 |
-
# Display history
|
| 38 |
-
for message in st.session_state.messages:
|
| 39 |
-
with st.chat_message(message["role"]):
|
| 40 |
-
st.markdown(message["content"])
|
| 41 |
-
|
| 42 |
-
# Handle Input
|
| 43 |
-
if prompt := st.chat_input("Ask a legal question..."):
|
| 44 |
-
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 45 |
-
with st.chat_message("user"):
|
| 46 |
-
st.markdown(prompt)
|
| 47 |
-
|
| 48 |
-
# RAG Logic
|
| 49 |
-
with st.chat_message("assistant"):
|
| 50 |
-
with st.spinner("Searching legal documents..."):
|
| 51 |
-
results = collection.query(query_texts=[prompt], n_results=3)
|
| 52 |
-
|
| 53 |
-
context_str = ""
|
| 54 |
-
if results['documents'] and results['documents'][0]:
|
| 55 |
-
for i, doc in enumerate(results['documents'][0]):
|
| 56 |
-
meta = results['metadatas'][0][i]
|
| 57 |
-
context_str += f"[Source: {meta.get('source', 'unknown')}]\n{doc}\n\n"
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import ollama
|
| 3 |
+
import chromadb
|
| 4 |
+
from chromadb.utils import embedding_functions
|
| 5 |
+
import os
|
| 6 |
+
import time
|
| 7 |
+
|
| 8 |
+
# --- SETUP ---
|
| 9 |
+
DB_PATH = "./legal_db"
|
| 10 |
+
COLLECTION_NAME = "legal_docs"
|
| 11 |
+
EMBEDDING_MODEL = 'hf.co/CompendiumLabs/bge-base-en-v1.5-gguf'
|
| 12 |
+
LANGUAGE_MODEL = 'llama3.2:3b'
|
| 13 |
+
|
| 14 |
+
st.set_page_config(page_title="Legal Assistant AI")
|
| 15 |
+
st.title("⚖️ Kenya Law RAG Bot")
|
| 16 |
+
|
| 17 |
+
# --- INITIALIZE DATABASE ---
|
| 18 |
+
@st.cache_resource
|
| 19 |
+
def get_collection():
|
| 20 |
+
client = chromadb.PersistentClient(path=DB_PATH)
|
| 21 |
+
ollama_ef = embedding_functions.OllamaEmbeddingFunction(
|
| 22 |
+
model_name=EMBEDDING_MODEL,
|
| 23 |
+
url="http://localhost:11434/api/embeddings"
|
| 24 |
+
)
|
| 25 |
+
return client.get_or_create_collection(name=COLLECTION_NAME, embedding_function=ollama_ef)
|
| 26 |
+
|
| 27 |
+
try:
|
| 28 |
+
collection = get_collection()
|
| 29 |
+
except Exception as e:
|
| 30 |
+
st.error(f"Could not connect to database: {e}")
|
| 31 |
+
st.stop()
|
| 32 |
+
|
| 33 |
+
# --- CHAT INTERFACE ---
|
| 34 |
+
if "messages" not in st.session_state:
|
| 35 |
+
st.session_state.messages = []
|
| 36 |
+
|
| 37 |
+
# Display history
|
| 38 |
+
for message in st.session_state.messages:
|
| 39 |
+
with st.chat_message(message["role"]):
|
| 40 |
+
st.markdown(message["content"])
|
| 41 |
+
|
| 42 |
+
# Handle Input
|
| 43 |
+
if prompt := st.chat_input("Ask a legal question..."):
|
| 44 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 45 |
+
with st.chat_message("user"):
|
| 46 |
+
st.markdown(prompt)
|
| 47 |
+
|
| 48 |
+
# RAG Logic
|
| 49 |
+
with st.chat_message("assistant"):
|
| 50 |
+
with st.spinner("Searching legal documents..."):
|
| 51 |
+
results = collection.query(query_texts=[prompt], n_results=3)
|
| 52 |
+
|
| 53 |
+
context_str = ""
|
| 54 |
+
if results['documents'] and results['documents'][0]:
|
| 55 |
+
for i, doc in enumerate(results['documents'][0]):
|
| 56 |
+
meta = results['metadatas'][0][i]
|
| 57 |
+
context_str += f"[Source: {meta.get('source', 'unknown')}]\n{doc}\n\n"
|
| 58 |
+
|
| 59 |
+
# --- STRICT PROMPT LOGIC ---
|
| 60 |
+
system_msg = f"""
|
| 61 |
+
You are a strict specialized assistant. You verify facts against the provided database extracts.
|
| 62 |
+
|
| 63 |
+
DATABASE EXTRACTS:
|
| 64 |
+
{context_str}
|
| 65 |
+
|
| 66 |
+
RULES:
|
| 67 |
+
1. You must ONLY answer using the information in the 'DATABASE EXTRACTS' above.
|
| 68 |
+
2. If the answer is not explicitly in the extracts, you MUST say: "The provided documents do not contain information about this."
|
| 69 |
+
3. Do not use outside knowledge. Do not make up laws or facts.
|
| 70 |
+
4. Cite the source file names provided in the extracts.
|
| 71 |
+
"""
|
| 72 |
+
|
| 73 |
+
if not context_str:
|
| 74 |
+
context_str = "No relevant documents found."
|
| 75 |
+
# Override system msg to force a "not found" response if no context exists
|
| 76 |
+
system_msg = "The database contained no relevant information. Inform the user you cannot answer based on the available documents."
|
| 77 |
+
|
| 78 |
+
# Streaming Response
|
| 79 |
+
stream = ollama.chat(
|
| 80 |
+
model=LANGUAGE_MODEL,
|
| 81 |
+
messages=[{'role': 'system', 'content': system_msg}, {'role': 'user', 'content': prompt}],
|
| 82 |
+
stream=True
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
# Uses st.write_stream as requested in your snippet
|
| 86 |
+
response = st.write_stream(chunk['message']['content'] for chunk in stream)
|
| 87 |
+
|
| 88 |
st.session_state.messages.append({"role": "assistant", "content": response})
|