Skip to content
This repository has been archived by the owner on Dec 9, 2024. It is now read-only.

Commit

Permalink
Merge pull request #196 from DrAlzahraniProjects/Dev
Browse files Browse the repository at this point in the history
Merge Dev into Main
  • Loading branch information
ACraig7 authored Nov 22, 2024
2 parents d3fdac3 + 4fcdb18 commit af85794
Show file tree
Hide file tree
Showing 12 changed files with 929 additions and 332 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.env
.env
.log
10 changes: 5 additions & 5 deletions .streamlit/config.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[theme]
# base = "light"
primaryColor = '#21af85'
backgroundColor = '#ffffff'
secondaryBackgroundColor = '#f9f9f9'
textColor = '#0d0d0d'
font = "sans serif"
#primaryColor = '#21af85'
#backgroundColor = '#ffffff'
#secondaryBackgroundColor = '#f9f9f9'
#textColor = '#0d0d0d'
#font = "sans serif"
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ RUN mamba install --yes --file requirements.txt && mamba clean --all -f -y
# Install Python packages not on Mamba DB
RUN pip install -qU langchain_milvus


# Copy the current directory contents into the container at /app
COPY . /app

# Remove old logs that persist through previous run
RUN rm /app/logs/app.log

# Set the StreamLit ENV for configuration
ENV STREAMLIT_SERVER_BASEURLPATH=/team1
ENV STREAMLIT_SERVER_PORT=5001
Expand Down
41 changes: 26 additions & 15 deletions RAG.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
CORPUS_SOURCE = 'https://www.csusb.edu/its'

import os
import streamlit as st
import time
import re
from dotenv import load_dotenv
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain.schema import Document
Expand All @@ -13,7 +15,6 @@
from langchain_community.document_loaders import WebBaseLoader, RecursiveUrlLoader
from bs4 import BeautifulSoup
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain.chains import create_retrieval_chain
from langchain_huggingface import HuggingFaceEmbeddings
from pymilvus import connections, utility
from requests.exceptions import HTTPError
Expand Down Expand Up @@ -57,12 +58,11 @@ def query_rag(query):
# Create the prompt and components for the RAG model
prompt = create_prompt()
vector_store = load_existing_db(uri=MILVUS_URI)
retriever = ScoreThresholdRetriever(vector_store=vector_store, score_threshold=0.2, k=3)
retriever = ScoreThresholdRetriever(vector_store=vector_store, score_threshold=0.2, k=5)
document_chain = create_stuff_documents_chain(model, prompt)
retrieval_chain = create_retrieval_chain(retriever, document_chain)

# Retrieve the most relevant document based on the query
retrieved_documents = retriever.get_relevant_documents(query)
retrieved_documents = retriever.get_related_documents(query)

if not retrieved_documents:
print("No Relevant Documents Retrieved, so sending default response")
Expand All @@ -76,15 +76,18 @@ def query_rag(query):
print("Most Relevant Document Retrieved")

# Generate a response using retrieval chain
response = retrieval_chain.invoke({"input": f"{query}"})
response_text = response.get("answer", "I couldn't generate a response.")
response = document_chain.invoke({
"input": query,
"context": retrieved_documents
})
# response_text = response.get("answer", "I couldn't generate a response.")

# Add the source to the response if available
if isinstance(source, str) and source != "Unknown":
response_text += f"\n\nSource: [{title}]({source})"
response += f"\n\nSource: [{title}]({source})"
print("Response Generated")

return response_text, source
return response, source

except HTTPStatusError as e:
print(f"HTTPStatusError: {e}")
Expand Down Expand Up @@ -243,7 +246,7 @@ def vector_store_check(uri):
connections.connect("default",uri=uri)

# Return True if exists, False otherwise
return utility.has_collection("IT_support")
return utility.has_collection(re.sub(r'\W+', '', CORPUS_SOURCE))

def create_vector_store(docs, embeddings, uri):
"""
Expand All @@ -261,7 +264,7 @@ def create_vector_store(docs, embeddings, uri):
vector_store = Milvus.from_documents(
documents=docs,
embedding=embeddings,
collection_name="IT_support",
collection_name=re.sub(r'\W+', '', CORPUS_SOURCE),
connection_args={"uri": uri},
drop_old=True,
)
Expand All @@ -280,11 +283,19 @@ def load_existing_db(uri=MILVUS_URI):
vector_store: The vector store created
"""
# Load an existing vector store
vector_store = Milvus(
collection_name="IT_support",
embedding_function = get_embedding_function(),
connection_args={"uri": uri},
)
# vector_store = Milvus(
# collection_name="IT_support",
# embedding_function = get_embedding_function(),
# connection_args={"uri": uri},
# )
# print("Vector Store Loaded")
vector_store = st.session_state.get("vector_store", None)
if vector_store is None:
vector_store = Milvus(
collection_name=re.sub(r'\W+', '', CORPUS_SOURCE),
embedding_function = get_embedding_function(),
connection_args={"uri": uri},
)
print("Vector Store Loaded")
return vector_store

Expand Down
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,28 @@ Before you begin, ensure you have the following installed on your machine:

6. **Run the Docker container**

### Windows PS or Docker Desktop Terminal

Run the Docker container with the following command:

```bash
docker run -v $pwd/milvus:/app/milvus -v $pwd/logs:/app/logs -d -p 5001:5001 -p 6001:6001 team1_app
```

### Windows CMD

Run the Docker container with the following command:

```bash
docker run -v %cd%/milvus:/app/milvus -v %cd%/logs:/app/logs -d -p 5001:5001 -p 6001:6001 team1_app
```

### Linux CLI

Run the Docker container with the following command:

```bash
docker run -d -p 5001:5001 -p 6001:6001 team1_app
docker run -v $PWD/milvus:/app/milvus -v $PWD/logs:/app/logs -d -p 5001:5001 -p 6001:6001 team1_app
```

## Accessing the Application
Expand Down
Loading

0 comments on commit af85794

Please sign in to comment.