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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# First, here's the integrated Streamlit interface with RAG implementation:
|
| 2 |
+
|
| 3 |
+
import streamlit as st
|
| 4 |
+
import requests
|
| 5 |
+
import json
|
| 6 |
+
import time
|
| 7 |
+
from langchain_community.document_loaders import PyPDFLoader
|
| 8 |
+
from langchain_community.embeddings import OllamaEmbeddings
|
| 9 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 10 |
+
from langchain_community.vectorstores import Chroma
|
| 11 |
+
from langchain.prompts import ChatPromptTemplate, PromptTemplate
|
| 12 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 13 |
+
from langchain_community.chat_models import ChatOllama
|
| 14 |
+
from langchain_core.runnables import RunnablePassthrough
|
| 15 |
+
from langchain.retrievers.multi_query import MultiQueryRetriever
|
| 16 |
+
import logging
|
| 17 |
+
import os
|
| 18 |
+
from datetime import datetime
|
| 19 |
+
from PIL import Image
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
# Configure logging
|
| 23 |
+
logging.basicConfig(
|
| 24 |
+
level=logging.INFO,
|
| 25 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
| 26 |
+
handlers=[
|
| 27 |
+
logging.FileHandler('ai_buddy_streamlit.log'),
|
| 28 |
+
logging.StreamHandler()
|
| 29 |
+
]
|
| 30 |
+
)
|
| 31 |
+
logger = logging.getLogger(__name__)
|
| 32 |
+
|
| 33 |
+
# Configuration
|
| 34 |
+
class Config:
|
| 35 |
+
LOCAL_PATH = "AI-BUDDY.pdf" # Adjust path as needed
|
| 36 |
+
PERSIST_DIRECTORY = "rag_data"
|
| 37 |
+
COLLECTION_NAME = "local-rags"
|
| 38 |
+
CHUNK_SIZE = 500
|
| 39 |
+
CHUNK_OVERLAP = 200
|
| 40 |
+
LOCAL_MODEL = "llama3.2"
|
| 41 |
+
|
| 42 |
+
# Initialize RAG Components
|
| 43 |
+
@st.cache_resource
|
| 44 |
+
def initialize_rag():
|
| 45 |
+
try:
|
| 46 |
+
embeddings = OllamaEmbeddings(model="nomic-embed-text", show_progress=True)
|
| 47 |
+
|
| 48 |
+
# Initialize or load vector store
|
| 49 |
+
if os.path.exists(Config.PERSIST_DIRECTORY) and os.listdir(Config.PERSIST_DIRECTORY):
|
| 50 |
+
vector_db = Chroma(
|
| 51 |
+
persist_directory=Config.PERSIST_DIRECTORY,
|
| 52 |
+
embedding_function=embeddings,
|
| 53 |
+
collection_name=Config.COLLECTION_NAME
|
| 54 |
+
)
|
| 55 |
+
else:
|
| 56 |
+
loader = PyPDFLoader(Config.LOCAL_PATH)
|
| 57 |
+
data = loader.load()
|
| 58 |
+
text_splitter = RecursiveCharacterTextSplitter(
|
| 59 |
+
chunk_size=Config.CHUNK_SIZE,
|
| 60 |
+
chunk_overlap=Config.CHUNK_OVERLAP
|
| 61 |
+
)
|
| 62 |
+
chunks = text_splitter.split_documents(data)
|
| 63 |
+
|
| 64 |
+
vector_db = Chroma.from_documents(
|
| 65 |
+
documents=chunks,
|
| 66 |
+
embedding=embeddings,
|
| 67 |
+
persist_directory=Config.PERSIST_DIRECTORY,
|
| 68 |
+
collection_name=Config.COLLECTION_NAME
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
# Initialize LLM and prompts
|
| 72 |
+
llm = ChatOllama(model=Config.LOCAL_MODEL, streaming=True)
|
| 73 |
+
|
| 74 |
+
query_prompt = PromptTemplate(
|
| 75 |
+
input_variables=["question"],
|
| 76 |
+
template="""You are AI Buddy, an AI assistant and counsellor. Generate diverse versions of the user's
|
| 77 |
+
question to retrieve comprehensive information about your capabilities and relevant details. Focus on:
|
| 78 |
+
1. Core question intent
|
| 79 |
+
2. Related capabilities
|
| 80 |
+
3. Specific services mentioned
|
| 81 |
+
4. Similar use cases
|
| 82 |
+
5. Contextual information
|
| 83 |
+
|
| 84 |
+
Original question: {question}
|
| 85 |
+
|
| 86 |
+
Generate three distinct questions that capture different aspects of the query."""
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
response_prompt = ChatPromptTemplate.from_template("""You are AI Buddy, an AI assistant and counsellor for the AI Buddy platform.
|
| 90 |
+
Respond in first person as AI Buddy, using "I" and "me". Always maintain this persona.
|
| 91 |
+
|
| 92 |
+
Context: {context}
|
| 93 |
+
Question: {question}
|
| 94 |
+
|
| 95 |
+
Guidelines for response:
|
| 96 |
+
1. Only answer based on the provided context
|
| 97 |
+
2. For course/bootcamp inquiries, only suggest from our official list
|
| 98 |
+
3. Verify names and services exist in the context before mentioning them
|
| 99 |
+
4. Be clear and direct in your responses
|
| 100 |
+
5. Provide a simple and short answers only which is required to answer the question""")
|
| 101 |
+
|
| 102 |
+
# Initialize retriever
|
| 103 |
+
retriever = MultiQueryRetriever.from_llm(
|
| 104 |
+
retriever=vector_db.as_retriever(search_kwargs={"k": 3}),
|
| 105 |
+
llm=llm,
|
| 106 |
+
prompt=query_prompt,
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
# Define the chain
|
| 110 |
+
chain = (
|
| 111 |
+
{"context": retriever, "question": RunnablePassthrough()}
|
| 112 |
+
| response_prompt
|
| 113 |
+
| llm
|
| 114 |
+
| StrOutputParser()
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
return chain
|
| 118 |
+
|
| 119 |
+
except Exception as e:
|
| 120 |
+
logger.error(f"Error initializing RAG components: {str(e)}")
|
| 121 |
+
st.error("Failed to initialize AI components. Please check the logs.")
|
| 122 |
+
return None
|
| 123 |
+
|
| 124 |
+
# Streamlit interface setup
|
| 125 |
+
st.set_page_config(page_title="AI-Buddy Assistant", page_icon="AI-Buddy.png", layout="centered")
|
| 126 |
+
|
| 127 |
+
# Load and resize the image
|
| 128 |
+
img = Image.open("AI Buddy Green Logo.png")
|
| 129 |
+
resized_img = img.resize((400, 150)) # Set width=400px and height=200px
|
| 130 |
+
|
| 131 |
+
# Display the resized image
|
| 132 |
+
st.image(resized_img, caption="AI-Buddy Assistant")
|
| 133 |
+
|
| 134 |
+
# Set the title
|
| 135 |
+
# st.title("AI-Buddy Assistant")
|
| 136 |
+
# Custom CSS (keeping your original styling)
|
| 137 |
+
st.markdown("""
|
| 138 |
+
<style>
|
| 139 |
+
.sidebar .sidebar-content {
|
| 140 |
+
font-size: 1.3rem;
|
| 141 |
+
color: #333333;
|
| 142 |
+
background-color: #f9f9f9;
|
| 143 |
+
padding: 20px;
|
| 144 |
+
border-radius: 10px;
|
| 145 |
+
}
|
| 146 |
+
.sidebar .sidebar-content h1 {
|
| 147 |
+
font-size: 1.8rem;
|
| 148 |
+
font-weight: bold;
|
| 149 |
+
color: #ff4b4b;
|
| 150 |
+
margin-bottom: 15px;
|
| 151 |
+
}
|
| 152 |
+
.sidebar .sidebar-content select, .sidebar .sidebar-content textarea, .sidebar .sidebar-content input {
|
| 153 |
+
border: 2px solid #ff4b4b;
|
| 154 |
+
padding: 12px;
|
| 155 |
+
font-size: 1.2rem;
|
| 156 |
+
margin-bottom: 15px;
|
| 157 |
+
border-radius: 5px;
|
| 158 |
+
}
|
| 159 |
+
.sidebar .sidebar-content button {
|
| 160 |
+
background-color: #ff4b4b;
|
| 161 |
+
color: white;
|
| 162 |
+
font-size: 1.2rem;
|
| 163 |
+
padding: 10px 20px;
|
| 164 |
+
border-radius: 5px;
|
| 165 |
+
border: none;
|
| 166 |
+
cursor: pointer;
|
| 167 |
+
transition: background-color 0.3s ease;
|
| 168 |
+
}
|
| 169 |
+
.sidebar .sidebar-content button:hover {
|
| 170 |
+
background-color: #ff6565;
|
| 171 |
+
}
|
| 172 |
+
.st-chat-message p {
|
| 173 |
+
font-size: 1.3rem;
|
| 174 |
+
}
|
| 175 |
+
</style>
|
| 176 |
+
""", unsafe_allow_html=True)
|
| 177 |
+
|
| 178 |
+
# Initialize RAG chain
|
| 179 |
+
chain = initialize_rag()
|
| 180 |
+
|
| 181 |
+
# Initialize chat history
|
| 182 |
+
if "messages" not in st.session_state:
|
| 183 |
+
st.session_state.messages = [
|
| 184 |
+
{"role": "assistant", "content": "Hello! I'm AI-Buddy. How can I assist you today?"}
|
| 185 |
+
]
|
| 186 |
+
|
| 187 |
+
# Sidebar for profession selection (keeping your original implementation)
|
| 188 |
+
st.sidebar.title("Want to know how AI helps in your profession and the role of AI-Buddy?")
|
| 189 |
+
professions = ["Software Engineer", "Data Scientist", "Marketing Specialist", "Financial Analyst", "Teacher", "Doctor", "Project Manager", "Consultant", "Business Analyst", "Other"]
|
| 190 |
+
fields = ["IT", "Healthcare", "Education", "Finance", "Marketing", "Engineering", "Sales", "Human Resources", "Consulting", "Other"]
|
| 191 |
+
|
| 192 |
+
profession = st.sidebar.selectbox("Choose Your Profession", professions)
|
| 193 |
+
field = st.sidebar.selectbox("Choose Your Field/Domain", fields)
|
| 194 |
+
|
| 195 |
+
if profession == "Other":
|
| 196 |
+
profession = st.sidebar.text_input("Please specify your profession")
|
| 197 |
+
if field == "Other":
|
| 198 |
+
field = st.sidebar.text_input("Please specify your field")
|
| 199 |
+
|
| 200 |
+
description = st.sidebar.text_area("About you (a short description)", placeholder="Briefly describe your role")
|
| 201 |
+
|
| 202 |
+
# Submit button for profession info
|
| 203 |
+
if st.sidebar.button("Submit"):
|
| 204 |
+
prompt = f"My profession is {profession} in the {field} field. Here's a bit about me: {description}. Tell me how AI and AI-Buddy can help me."
|
| 205 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 206 |
+
|
| 207 |
+
# Function to display streaming response
|
| 208 |
+
def display_response_streaming(response_iterator):
|
| 209 |
+
response_placeholder = st.empty()
|
| 210 |
+
full_response = ""
|
| 211 |
+
for chunk in response_iterator:
|
| 212 |
+
full_response += chunk
|
| 213 |
+
response_placeholder.write(full_response)
|
| 214 |
+
time.sleep(0.05)
|
| 215 |
+
return full_response
|
| 216 |
+
|
| 217 |
+
# Chat interface
|
| 218 |
+
if prompt := st.chat_input("Type your message"):
|
| 219 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 220 |
+
|
| 221 |
+
# Display chat history and generate responses
|
| 222 |
+
for message in st.session_state.messages:
|
| 223 |
+
with st.chat_message(message["role"]):
|
| 224 |
+
st.write(message["content"])
|
| 225 |
+
|
| 226 |
+
# Generate new response if last message is from user
|
| 227 |
+
if st.session_state.messages[-1]["role"] == "user":
|
| 228 |
+
with st.chat_message("assistant"):
|
| 229 |
+
with st.spinner("Thinking..."):
|
| 230 |
+
try:
|
| 231 |
+
# Use RAG chain to generate response
|
| 232 |
+
response_iterator = chain.stream({"question": st.session_state.messages[-1]["content"]})
|
| 233 |
+
response = display_response_streaming(response_iterator)
|
| 234 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 235 |
+
except Exception as e:
|
| 236 |
+
logger.error(f"Error generating response: {str(e)}")
|
| 237 |
+
st.error("I apologize, but I encountered an error. Please try again or rephrase your question.")
|
| 238 |
+
|
| 239 |
+
if __name__ == "__main__":
|
| 240 |
+
logging.info("AI-Buddy Streamlit Interface Started")
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
requests
|
| 3 |
+
pillow
|
| 4 |
+
langchain
|
| 5 |
+
langchain-community
|
| 6 |
+
chromadb
|
| 7 |
+
pypdf
|
| 8 |
+
ollama
|
| 9 |
+
|