Update app.py
Browse files
app.py
CHANGED
|
@@ -14,8 +14,7 @@ import os
|
|
| 14 |
|
| 15 |
df_chunks = pd.read_pickle('Chunks_Complete.pkl')
|
| 16 |
|
| 17 |
-
placeHolderPersona1 = """
|
| 18 |
-
##Mission
|
| 19 |
Please create a highly targeted query for a semantic search engine. The query must represent the conversation to date.
|
| 20 |
** You will be given the converstaion to date in the user prompt.
|
| 21 |
** If no converstaion provided then this is the first converstaion
|
|
@@ -49,8 +48,8 @@ def format_elapsed_time(time):
|
|
| 49 |
# Format the elapsed time to two decimal places
|
| 50 |
return "{:.2f}".format(time)
|
| 51 |
|
| 52 |
-
def search_knowledgebase(query
|
| 53 |
-
results = retriever(query
|
| 54 |
return results
|
| 55 |
|
| 56 |
def process_search_results(search_results):
|
|
@@ -109,7 +108,7 @@ def lookup_related_chunks(df_chunks, chunk_id):
|
|
| 109 |
return df_chunks[(df_chunks['Title'] == title) & (df_chunks['PageNumber'].isin(page_range))]
|
| 110 |
|
| 111 |
|
| 112 |
-
def search_and_reconstruct(query, df_chunks
|
| 113 |
"""
|
| 114 |
Combines search, lookup of related chunks, and text reconstruction.
|
| 115 |
|
|
@@ -119,7 +118,7 @@ def search_and_reconstruct(query, df_chunks, k):
|
|
| 119 |
:param top_k: Number of top search results to retrieve.
|
| 120 |
:return: A list of dictionaries with document title, page number, and reconstructed text.
|
| 121 |
"""
|
| 122 |
-
search_results = search_knowledgebase(query
|
| 123 |
processed_results = process_search_results(search_results)
|
| 124 |
|
| 125 |
reconstructed_results = []
|
|
@@ -131,14 +130,14 @@ def search_and_reconstruct(query, df_chunks, k):
|
|
| 131 |
|
| 132 |
reconstructed_results.append({
|
| 133 |
"Title": result['Title'],
|
| 134 |
-
"
|
| 135 |
"PageNumber": result['PageNumber'],
|
| 136 |
"ReconstructedText": reconstructed_text
|
| 137 |
})
|
| 138 |
|
| 139 |
return reconstructed_results
|
| 140 |
|
| 141 |
-
def call_chat_api(data: ChatRequestClient
|
| 142 |
url = "https://agent-builder-api.greensea-b20be511.northeurope.azurecontainerapps.io/chat/"
|
| 143 |
# Validate and convert the data to a dictionary
|
| 144 |
validated_data = data.dict()
|
|
@@ -149,7 +148,7 @@ def call_chat_api(data: ChatRequestClient, k):
|
|
| 149 |
if response.status_code == 200:
|
| 150 |
body = response.json()
|
| 151 |
query = body.get("content")
|
| 152 |
-
final_results = search_and_reconstruct(query, df_chunks
|
| 153 |
return body, final_results # Return the JSON response if successful
|
| 154 |
else:
|
| 155 |
return "An error occured" # Return the raw response text if not successful
|
|
@@ -171,8 +170,7 @@ persona1SystemMessage = st.sidebar.text_area("Query Designer System Message", va
|
|
| 171 |
|
| 172 |
llm1 = st.sidebar.selectbox("Model Selection", ['GPT-4', 'GPT3.5'], key='persona1_size')
|
| 173 |
temp1 = st.sidebar.slider("Temperature", min_value=0.0, max_value=1.0, step=0.1, value=0.6, key='persona1_temp')
|
| 174 |
-
tokens1 = st.sidebar.slider("Tokens", min_value=
|
| 175 |
-
k = st.sidebar.slider("Returned Docs", min_value=1, max_value=10, step=1, value=3, key='k')
|
| 176 |
|
| 177 |
st.sidebar.caption(f"Session ID: {genuuid()}")
|
| 178 |
|
|
@@ -225,7 +223,7 @@ else:
|
|
| 225 |
temperature2=0.2
|
| 226 |
)
|
| 227 |
|
| 228 |
-
response, retrival = call_chat_api(data
|
| 229 |
agent_message = response.get("content", "No response received from the agent.")
|
| 230 |
elapsed_time = response.get("elapsed_time", 0)
|
| 231 |
st.session_state.messages.append({"role": "assistant", "content": agent_message})
|
|
@@ -238,14 +236,13 @@ else:
|
|
| 238 |
st.markdown(message["content"])
|
| 239 |
|
| 240 |
if response:
|
| 241 |
-
|
| 242 |
st.caption(f"##### Time taken: {format_elapsed_time(response.get('elapsed_time', 0))} seconds")
|
| 243 |
|
| 244 |
with col2:
|
| 245 |
for entry in retrival:
|
| 246 |
with st.container():
|
| 247 |
st.write(f"**Title:** {entry['Title']}")
|
|
|
|
| 248 |
st.write(f"**Page Number:** {entry['PageNumber']}")
|
| 249 |
-
st.
|
| 250 |
-
|
| 251 |
-
|
|
|
|
| 14 |
|
| 15 |
df_chunks = pd.read_pickle('Chunks_Complete.pkl')
|
| 16 |
|
| 17 |
+
placeHolderPersona1 = """##Mission
|
|
|
|
| 18 |
Please create a highly targeted query for a semantic search engine. The query must represent the conversation to date.
|
| 19 |
** You will be given the converstaion to date in the user prompt.
|
| 20 |
** If no converstaion provided then this is the first converstaion
|
|
|
|
| 48 |
# Format the elapsed time to two decimal places
|
| 49 |
return "{:.2f}".format(time)
|
| 50 |
|
| 51 |
+
def search_knowledgebase(query):
|
| 52 |
+
results = retriever(query)
|
| 53 |
return results
|
| 54 |
|
| 55 |
def process_search_results(search_results):
|
|
|
|
| 108 |
return df_chunks[(df_chunks['Title'] == title) & (df_chunks['PageNumber'].isin(page_range))]
|
| 109 |
|
| 110 |
|
| 111 |
+
def search_and_reconstruct(query, df_chunks):
|
| 112 |
"""
|
| 113 |
Combines search, lookup of related chunks, and text reconstruction.
|
| 114 |
|
|
|
|
| 118 |
:param top_k: Number of top search results to retrieve.
|
| 119 |
:return: A list of dictionaries with document title, page number, and reconstructed text.
|
| 120 |
"""
|
| 121 |
+
search_results = search_knowledgebase(query)
|
| 122 |
processed_results = process_search_results(search_results)
|
| 123 |
|
| 124 |
reconstructed_results = []
|
|
|
|
| 130 |
|
| 131 |
reconstructed_results.append({
|
| 132 |
"Title": result['Title'],
|
| 133 |
+
"score": result['score'],
|
| 134 |
"PageNumber": result['PageNumber'],
|
| 135 |
"ReconstructedText": reconstructed_text
|
| 136 |
})
|
| 137 |
|
| 138 |
return reconstructed_results
|
| 139 |
|
| 140 |
+
def call_chat_api(data: ChatRequestClient):
|
| 141 |
url = "https://agent-builder-api.greensea-b20be511.northeurope.azurecontainerapps.io/chat/"
|
| 142 |
# Validate and convert the data to a dictionary
|
| 143 |
validated_data = data.dict()
|
|
|
|
| 148 |
if response.status_code == 200:
|
| 149 |
body = response.json()
|
| 150 |
query = body.get("content")
|
| 151 |
+
final_results = search_and_reconstruct(query, df_chunks)
|
| 152 |
return body, final_results # Return the JSON response if successful
|
| 153 |
else:
|
| 154 |
return "An error occured" # Return the raw response text if not successful
|
|
|
|
| 170 |
|
| 171 |
llm1 = st.sidebar.selectbox("Model Selection", ['GPT-4', 'GPT3.5'], key='persona1_size')
|
| 172 |
temp1 = st.sidebar.slider("Temperature", min_value=0.0, max_value=1.0, step=0.1, value=0.6, key='persona1_temp')
|
| 173 |
+
tokens1 = st.sidebar.slider("Tokens", min_value=0, max_value=4000, step=100, value=500, key='persona1_tokens')
|
|
|
|
| 174 |
|
| 175 |
st.sidebar.caption(f"Session ID: {genuuid()}")
|
| 176 |
|
|
|
|
| 223 |
temperature2=0.2
|
| 224 |
)
|
| 225 |
|
| 226 |
+
response, retrival = call_chat_api(data)
|
| 227 |
agent_message = response.get("content", "No response received from the agent.")
|
| 228 |
elapsed_time = response.get("elapsed_time", 0)
|
| 229 |
st.session_state.messages.append({"role": "assistant", "content": agent_message})
|
|
|
|
| 236 |
st.markdown(message["content"])
|
| 237 |
|
| 238 |
if response:
|
| 239 |
+
st.chat_message("assistant").markdown(response.get("content", "No response"))
|
| 240 |
st.caption(f"##### Time taken: {format_elapsed_time(response.get('elapsed_time', 0))} seconds")
|
| 241 |
|
| 242 |
with col2:
|
| 243 |
for entry in retrival:
|
| 244 |
with st.container():
|
| 245 |
st.write(f"**Title:** {entry['Title']}")
|
| 246 |
+
st.write(f"**Score** {entry['score']}")
|
| 247 |
st.write(f"**Page Number:** {entry['PageNumber']}")
|
| 248 |
+
st.write("Grounding Text", entry['ReconstructedText'], height=150)
|
|
|
|
|
|