Update app.py
Browse files
app.py
CHANGED
|
@@ -84,19 +84,27 @@ all_splits = text_splitter.split_documents(docs)
|
|
| 84 |
# -------------------------------
|
| 85 |
|
| 86 |
# Create a Chroma vector store using the document splits
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
)
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
# Create a BM25 retriever from the document splits
|
| 94 |
bm25_retriever = BM25Retriever.from_documents(all_splits)
|
| 95 |
|
| 96 |
# Combine the retrievers using an ensemble approach
|
| 97 |
ensemble_retriever = EnsembleRetriever(
|
| 98 |
-
retrievers=[vectorstore.as_retriever(search_kwargs={"k":
|
| 99 |
-
weights=[0.
|
| 100 |
)
|
| 101 |
retriever = ensemble_retriever
|
| 102 |
|
|
@@ -124,21 +132,15 @@ rag_chain = (
|
|
| 124 |
| StrOutputParser()
|
| 125 |
)
|
| 126 |
system_prompt = (
|
| 127 |
-
"You are the LA2050 Navigator, an AI-powered chatbot designed to help users explore organizations
|
| 128 |
-
"
|
| 129 |
-
"
|
| 130 |
-
"
|
| 131 |
-
"
|
| 132 |
-
"
|
| 133 |
-
"
|
| 134 |
-
"
|
| 135 |
-
"
|
| 136 |
-
"Maintain a polite, helpful, respectful, and enthusiastic tone at all times to create a positive user experience."
|
| 137 |
-
"Politely redirect the user back to relevant topics if they ask unrelated questions, keeping the conversation aligned with your purpose."
|
| 138 |
-
"Use the following information about the data files to help you:"
|
| 139 |
-
"The “ideas” are community initiatives submitted by organizations to apply for grants and support from the Goldhirsh Foundation."
|
| 140 |
-
"There are currently no winners for the 2024 data, although this information can be found on the LA2050 Ideas Hub website. "
|
| 141 |
-
"Only projects labeled as “winner” in the Project Ranking column should be identified as winning projects."
|
| 142 |
"\n\n{context}"
|
| 143 |
)
|
| 144 |
prompt = ChatPromptTemplate.from_messages(
|
|
@@ -240,17 +242,18 @@ css = """""
|
|
| 240 |
"""
|
| 241 |
|
| 242 |
# setup gradio interface
|
| 243 |
-
with gr.Blocks(theme=green_theme, js=js_func, css=css) as block:
|
| 244 |
|
|
|
|
| 245 |
gr.HTML('<div class="chat-header"><h1>LA2050 Navigator</h1></div>')
|
| 246 |
-
|
| 247 |
-
chatbot = gr.Chatbot(
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
|
|
|
| 252 |
state = gr.State([])
|
| 253 |
-
|
| 254 |
message = gr.MultimodalTextbox(
|
| 255 |
interactive=True,
|
| 256 |
file_count="multiple",
|
|
@@ -258,8 +261,9 @@ with gr.Blocks(theme=green_theme, js=js_func, css=css) as block:
|
|
| 258 |
label="",
|
| 259 |
elem_classes="custom-textbox",
|
| 260 |
scale=3,
|
| 261 |
-
show_label=False
|
| 262 |
-
|
|
|
|
| 263 |
message.submit(
|
| 264 |
message_and_history,
|
| 265 |
inputs=[message, state],
|
|
@@ -267,5 +271,5 @@ with gr.Blocks(theme=green_theme, js=js_func, css=css) as block:
|
|
| 267 |
).then(
|
| 268 |
lambda: "", inputs=[], outputs=message
|
| 269 |
)
|
| 270 |
-
|
| 271 |
block.launch(debug=True, share=True)
|
|
|
|
| 84 |
# -------------------------------
|
| 85 |
|
| 86 |
# Create a Chroma vector store using the document splits
|
| 87 |
+
persist_directory = "./chroma_db"
|
| 88 |
+
if os.path.exists(persist_directory) and os.listdir(persist_directory):
|
| 89 |
+
vectorstore = Chroma(
|
| 90 |
+
persist_directory=persist_directory,
|
| 91 |
+
embedding_function=OpenAIEmbeddings()
|
| 92 |
+
)
|
| 93 |
+
print("Loaded vector store from persist directory.")
|
| 94 |
+
else:
|
| 95 |
+
vectorstore = Chroma.from_documents(
|
| 96 |
+
documents=all_splits,
|
| 97 |
+
embedding=OpenAIEmbeddings(),
|
| 98 |
+
persist_directory=persist_directory
|
| 99 |
+
)
|
| 100 |
+
print("Created new vector store and persisted embeddings.")
|
| 101 |
# Create a BM25 retriever from the document splits
|
| 102 |
bm25_retriever = BM25Retriever.from_documents(all_splits)
|
| 103 |
|
| 104 |
# Combine the retrievers using an ensemble approach
|
| 105 |
ensemble_retriever = EnsembleRetriever(
|
| 106 |
+
retrievers=[vectorstore.as_retriever(search_kwargs={"k": 5}), bm25_retriever],
|
| 107 |
+
weights=[0.8, 0.3]
|
| 108 |
)
|
| 109 |
retriever = ensemble_retriever
|
| 110 |
|
|
|
|
| 132 |
| StrOutputParser()
|
| 133 |
)
|
| 134 |
system_prompt = (
|
| 135 |
+
"You are the LA2050 Navigator, an AI-powered chatbot designed to help users explore organizations "
|
| 136 |
+
"and community initiatives within the Goldhirsh Foundation’s LA2050 Ideas Hub. "
|
| 137 |
+
"Your role is to provide personalized recommendations, guide users toward supporting these organizations "
|
| 138 |
+
"and initiatives, and answer relevant questions about the Goldhirsh Foundation, LA2050, and its projects. "
|
| 139 |
+
"When answering, include the full name of the organization, a brief (1-2 sentence) description, a link to its website or social media, "
|
| 140 |
+
"and suggestions for engagement such as donating, volunteering, or learning more. "
|
| 141 |
+
"Prioritize nonprofit organizations awarded by the Goldhirsh Foundation (designated 'winner') and those with multiple proposal submissions. "
|
| 142 |
+
"Use the data files as your primary source of information. If information is unavailable, acknowledge it and guide the user to relevant resources. "
|
| 143 |
+
"Maintain a polite, helpful, respectful, and enthusiastic tone at all times. "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
"\n\n{context}"
|
| 145 |
)
|
| 146 |
prompt = ChatPromptTemplate.from_messages(
|
|
|
|
| 242 |
"""
|
| 243 |
|
| 244 |
# setup gradio interface
|
|
|
|
| 245 |
|
| 246 |
+
with gr.Blocks(theme=green_theme, js=js_func, css=css) as block:
|
| 247 |
gr.HTML('<div class="chat-header"><h1>LA2050 Navigator</h1></div>')
|
| 248 |
+
|
| 249 |
+
chatbot = gr.Chatbot(
|
| 250 |
+
value=[{"role": "assistant", "content": "<b>LA2050 Navigator:</b><br> Welcome to the LA2050 ideas hub! How can I help you today?"}],
|
| 251 |
+
type="messages",
|
| 252 |
+
bubble_full_width=False
|
| 253 |
+
)
|
| 254 |
+
|
| 255 |
state = gr.State([])
|
| 256 |
+
|
| 257 |
message = gr.MultimodalTextbox(
|
| 258 |
interactive=True,
|
| 259 |
file_count="multiple",
|
|
|
|
| 261 |
label="",
|
| 262 |
elem_classes="custom-textbox",
|
| 263 |
scale=3,
|
| 264 |
+
show_label=False
|
| 265 |
+
)
|
| 266 |
+
|
| 267 |
message.submit(
|
| 268 |
message_and_history,
|
| 269 |
inputs=[message, state],
|
|
|
|
| 271 |
).then(
|
| 272 |
lambda: "", inputs=[], outputs=message
|
| 273 |
)
|
| 274 |
+
|
| 275 |
block.launch(debug=True, share=True)
|