Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -35,22 +35,8 @@ severities = {
|
|
| 35 |
"Best Practice": "Low"
|
| 36 |
}
|
| 37 |
|
| 38 |
-
# ----------
|
| 39 |
-
|
| 40 |
-
"governor limits soql": "In Salesforce, the governor limit for SOQL queries is 100 per synchronous transaction and 200 per asynchronous transaction.",
|
| 41 |
-
"governor limits dml": "The governor limit for DML statements is 150 per transaction.",
|
| 42 |
-
"bulkify apex trigger": "...",
|
| 43 |
-
"soql injection": "...",
|
| 44 |
-
"lwc best practices": "...",
|
| 45 |
-
"batch apex": "..."
|
| 46 |
-
}
|
| 47 |
-
|
| 48 |
-
# ---------- Load QnA Model ----------
|
| 49 |
-
try:
|
| 50 |
-
qa_pipeline = pipeline("text2text-generation", model="google/flan-t5-large")
|
| 51 |
-
except Exception as e:
|
| 52 |
-
print(f"Model loading error: {e}. Falling back to flan-t5-base.")
|
| 53 |
-
qa_pipeline = pipeline("text2text-generation", model="google/flan-t5-base")
|
| 54 |
|
| 55 |
# ---------- Logging ----------
|
| 56 |
def log_to_console(data, log_type):
|
|
@@ -154,7 +140,7 @@ def validate_metadata(metadata, admin_id=None):
|
|
| 154 |
|
| 155 |
return mtype, issue, recommendation
|
| 156 |
|
| 157 |
-
# ---------- Salesforce Chatbot (
|
| 158 |
conversation_history = []
|
| 159 |
|
| 160 |
def salesforce_chatbot(query, history=[]):
|
|
@@ -168,28 +154,20 @@ def salesforce_chatbot(query, history=[]):
|
|
| 168 |
]
|
| 169 |
|
| 170 |
if not any(keyword.lower() in query.lower() for keyword in salesforce_keywords):
|
| 171 |
-
return "Please ask a Salesforce-related question
|
| 172 |
-
|
| 173 |
-
query_key = query.lower().strip()
|
| 174 |
-
for kb_key, kb_answer in salesforce_knowledge_base.items():
|
| 175 |
-
if kb_key in query_key:
|
| 176 |
-
conversation_history.append((query, kb_answer))
|
| 177 |
-
conversation_history = conversation_history[-6:]
|
| 178 |
-
log_to_console({"Question": query, "Answer": kb_answer}, "Chatbot Query")
|
| 179 |
-
return kb_answer
|
| 180 |
|
| 181 |
history_summary = "\n".join([f"User: {q}\nAssistant: {a}" for q, a in conversation_history[-4:]])
|
| 182 |
|
| 183 |
prompt = f"""
|
| 184 |
-
You are
|
| 185 |
|
| 186 |
-
|
| 187 |
-
-
|
| 188 |
-
- Be
|
| 189 |
-
-
|
| 190 |
-
- Use bullet points or code snippets when
|
| 191 |
-
-
|
| 192 |
-
-
|
| 193 |
|
| 194 |
Conversation History:
|
| 195 |
{history_summary}
|
|
@@ -204,8 +182,8 @@ Assistant:
|
|
| 204 |
if output.startswith("Assistant:"):
|
| 205 |
output = output.replace("Assistant:", "").strip()
|
| 206 |
|
| 207 |
-
if len(output) <
|
| 208 |
-
output
|
| 209 |
|
| 210 |
conversation_history.append((query, output))
|
| 211 |
conversation_history = conversation_history[-6:]
|
|
@@ -236,7 +214,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 236 |
|
| 237 |
with gr.Tab("Salesforce Chatbot"):
|
| 238 |
chatbot_output = gr.Chatbot(label="Conversation History", height=400)
|
| 239 |
-
query_input = gr.Textbox(label="Your Question", placeholder="e.g.,
|
| 240 |
with gr.Row():
|
| 241 |
chatbot_button = gr.Button("Ask")
|
| 242 |
clear_button = gr.Button("Clear Chat")
|
|
|
|
| 35 |
"Best Practice": "Low"
|
| 36 |
}
|
| 37 |
|
| 38 |
+
# ---------- Load QnA Model (no fallback) ----------
|
| 39 |
+
qa_pipeline = pipeline("text2text-generation", model="google/flan-t5-large")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
# ---------- Logging ----------
|
| 42 |
def log_to_console(data, log_type):
|
|
|
|
| 140 |
|
| 141 |
return mtype, issue, recommendation
|
| 142 |
|
| 143 |
+
# ---------- Salesforce Chatbot (Improved Prompt) ----------
|
| 144 |
conversation_history = []
|
| 145 |
|
| 146 |
def salesforce_chatbot(query, history=[]):
|
|
|
|
| 154 |
]
|
| 155 |
|
| 156 |
if not any(keyword.lower() in query.lower() for keyword in salesforce_keywords):
|
| 157 |
+
return "Please ask a Salesforce-related question."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
history_summary = "\n".join([f"User: {q}\nAssistant: {a}" for q, a in conversation_history[-4:]])
|
| 160 |
|
| 161 |
prompt = f"""
|
| 162 |
+
You are a certified Salesforce developer and architect. Your role is to answer with 100% accurate and detailed technical explanations, especially about limits, code, and platform best practices.
|
| 163 |
|
| 164 |
+
Your answers MUST:
|
| 165 |
+
- Always be at least two lines long.
|
| 166 |
+
- Be correct, clear, and production-safe.
|
| 167 |
+
- Include official Salesforce governor limits when applicable.
|
| 168 |
+
- Use bullet points or code snippets when needed.
|
| 169 |
+
- Recommend Trailhead or official docs if the answer isn't definitive.
|
| 170 |
+
- Follow real-world practices (bulkification, error handling, etc).
|
| 171 |
|
| 172 |
Conversation History:
|
| 173 |
{history_summary}
|
|
|
|
| 182 |
if output.startswith("Assistant:"):
|
| 183 |
output = output.replace("Assistant:", "").strip()
|
| 184 |
|
| 185 |
+
if len(output.split()) < 15:
|
| 186 |
+
output += "\n\nRefer to: https://developer.salesforce.com/docs for more."
|
| 187 |
|
| 188 |
conversation_history.append((query, output))
|
| 189 |
conversation_history = conversation_history[-6:]
|
|
|
|
| 214 |
|
| 215 |
with gr.Tab("Salesforce Chatbot"):
|
| 216 |
chatbot_output = gr.Chatbot(label="Conversation History", height=400)
|
| 217 |
+
query_input = gr.Textbox(label="Your Question", placeholder="e.g., How many DML operations are allowed in Apex?")
|
| 218 |
with gr.Row():
|
| 219 |
chatbot_button = gr.Button("Ask")
|
| 220 |
clear_button = gr.Button("Clear Chat")
|