Spaces:
Sleeping
Sleeping
Update app.py
Browse files1503 unmodified lines150415051506150715081509178 unmodified lines168816891690169116921693169416951696169716981699170017014 unmodified lines170617071708170917101711171217131714171517161503 unmodified linescompute_dataframe_proof_point_tool,]# Create the prompt templateprompt_message = """### Role178 unmodified linesselected_dataset_ai = read_ai_dataset_selection()# Recompute DATAFRAME_PROOF_POINT based on the selected datasetdf_builder_pivot_str = compute_dataframe_proof_point()# Generate response using the agent executoroutput = agent_executor.invoke({"input": message, "chat_history": chat_history})trust_tip, suggestion = get_trust_tip_and_suggestion()# Prepend the selected dataset to the responseresponse = f"**Selected Dataset: {selected_dataset_ai}**\n\n{output['output']}\n\n---\n\n**Trust Tip**: {trust_tip}\n\n**Suggestion**: {suggestion}"# Save the interaction contextchat_history.extend(4 unmodified lines)return responseexcept Exception as e:logger.error(f"Error generating chatbot response: {e}")return "Error occurred during response generation"def read_ai_dataset_selection():global selected_dataset_aireturn selected_dataset_ai1503 unmodified lines15041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538178 unmodified lines1717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317644 unmodified lines176917701771177217731774177517761777177817791503 unmodified linescompute_dataframe_proof_point_tool,]def validate_ai_output(ai_output, proof_points):"""Validates that the AI output includes all relevant Trust Buckets and Builders.Args:ai_output: The generated response from the AI.proof_points: The DATAFRAME_PROOF_POINT dictionary with Trust Buckets and Builders.Returns:Validated and corrected output."""validated_output = ai_outputmissing_buckets = []# Check if all relevant buckets are includedfor bucket, builders in proof_points.items():if bucket not in ai_output:missing_buckets.append(bucket)# Add missing buckets and builders if anyif missing_buckets:corrections = []for bucket in missing_buckets:corrections.append(f"**{bucket}**")for builder in proof_points[bucket]:corrections.append(f"- {builder['Trust Builder']} [{builder['Percentage']}%]")validated_output = f"{validated_output}\n\nMissing Data:\n" + "\n".join(corrections)return validated_output# Create the prompt templateprompt_message = """### Role178 unmodified linesselected_dataset_ai = read_ai_dataset_selection()# Recompute DATAFRAME_PROOF_POINT based on the selected datasetdf_builder_pivot = compute_dataframe_proof_point()# Filter and format the trust buckets and buildersdef format_trust_buckets(dataframe, threshold=18):"""Filters and formats Trust Buckets with top Trust Builders."""formatted_output = []for bucket in dataframe["Trust Buckets"].unique():bucket_data = dataframe[dataframe["Trust Buckets"] == bucket]top_builders = (bucket_data[bucket_data["Percentage"] >= threshold].nlargest(3, "Percentage").loc[:, ["Trust Builder", "Percentage"]])if not top_builders.empty:formatted_output.append(f"**{bucket}**\n" + "\n".join([f"- {row['Trust Builder']} [{row['Percentage']}%]" for _, row in top_builders.iterrows()]))return "\n\n".join(formatted_output)# Generate formatted trust bucketsformatted_buckets = format_trust_buckets(df_builder_pivot)# Generate response using the agent executoroutput = agent_executor.invoke({"input": message, "chat_history": chat_history})# Get Trust Tip and Suggestiontrust_tip, suggestion = get_trust_tip_and_suggestion()# Compile the responseresponse = (f"**Selected Dataset: {selected_dataset_ai}**\n\n"f"### Trust Buckets and Top Builders:\n\n{formatted_buckets}\n\n""---\n\n"f"**Chatbot Response:**\n\n{output['output']}\n\n""---\n\n"f"**Trust Tip**: {trust_tip}\n\n"f"**Suggestion**: {suggestion}")# Save the interaction contextchat_history.extend(4 unmodified lines)return responseexcept Exception as e:logger.error(f"Error generating chatbot response: {e}")return "Error occurred during response generation"def read_ai_dataset_selection():global selected_dataset_aireturn selected_dataset_ai
app.py+67-4