Spaces:
Sleeping
Sleeping
Update app.py
Browse files1503 unmodified lines15041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538178 unmodified lines1717171817191691172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511697169817521753175417551756175717581759176017611762176317644 unmodified lines1769177017711772177317741775177617131777177817791503 unmodified lines compute_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_output missing_buckets = []
# Check if all relevant buckets are included for bucket, builders in proof_points.items(): if bucket not in ai_output: missing_buckets.append(bucket)
# Add missing buckets and builders if any if 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 lines selected_dataset_ai = read_ai_dataset_selection()
# Recompute DATAFRAME_PROOF_POINT based on the selected dataset df_builder_pivot_str = compute_dataframe_proof_point() df_builder_pivot = compute_dataframe_proof_point()
# Filter and format the trust buckets and builders def 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 buckets formatted_buckets = format_trust_buckets(df_builder_pivot)
# Generate response using the agent executor output = agent_executor.invoke({"input": message, "chat_history": chat_history})
# Get Trust Tip and Suggestion trust_tip, suggestion = get_trust_tip_and_suggestion()
# Prepend the selected dataset to the response response = f"**Selected Dataset: {selected_dataset_ai}**\n\n{output['output']}\n\n---\n\n**Trust Tip**: {trust_tip}\n\n**Suggestion**: {suggestion}" # Compile the response response = ( 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 context chat_history.extend(4 unmodified lines )
return response
except 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_ai return selected_dataset_aiapp.py+67-4