Spaces:
Sleeping
Sleeping
Vlad Bastina
commited on
Commit
·
3d01d70
1
Parent(s):
879871c
chat history
Browse files- app.py +1 -0
- query_chat.py +38 -32
app.py
CHANGED
|
@@ -63,6 +63,7 @@ with col2:
|
|
| 63 |
# Clear chat history
|
| 64 |
if clear_button:
|
| 65 |
st.session_state.messages = []
|
|
|
|
| 66 |
st.rerun()
|
| 67 |
|
| 68 |
# Handle user input
|
|
|
|
| 63 |
# Clear chat history
|
| 64 |
if clear_button:
|
| 65 |
st.session_state.messages = []
|
| 66 |
+
chatbot.clear_conv_history()
|
| 67 |
st.rerun()
|
| 68 |
|
| 69 |
# Handle user input
|
query_chat.py
CHANGED
|
@@ -2,15 +2,18 @@ import google.generativeai as genai
|
|
| 2 |
import os
|
| 3 |
|
| 4 |
class GeminiQanA:
|
| 5 |
-
def __init__(self,text1:str='',text2:str=''):
|
| 6 |
-
"""Initializes the Gemini question
|
| 7 |
self.api_key = os.getenv("GOOGLE_API_KEY")
|
| 8 |
genai.configure(api_key=self.api_key)
|
| 9 |
-
self.
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
def _load_model(self
|
| 12 |
-
"""Loads the generative AI model
|
| 13 |
-
|
| 14 |
You are a sales agent responsible for assisting customers by answering questions about our team’s capabilities and the projects we offer. You have access to two brochures that detail the available projects and their features. Your goal is to provide accurate and honest responses based solely on the information within these brochures.
|
| 15 |
|
| 16 |
Guidelines for Responses:
|
|
@@ -31,21 +34,6 @@ Guidelines for Responses:
|
|
| 31 |
-Do not create new information or assume additional capabilities.
|
| 32 |
-Do not make guarantees beyond what is stated in the brochures.
|
| 33 |
-Do not offer speculative solutions that are not explicitly supported by the documents.
|
| 34 |
-
Example Interactions:
|
| 35 |
-
Scenario 1: Customer Asks About a Team Capability
|
| 36 |
-
✅ Customer: "Does your team specialize in AI-powered automation?"
|
| 37 |
-
✅ Agent: "According to our brochure, our team specializes in [list relevant capabilities]. While AI-powered automation is not specifically mentioned, we do offer [related project] which may align with your needs."
|
| 38 |
-
|
| 39 |
-
Scenario 2: Customer Has a Specific Problem
|
| 40 |
-
✅ Customer: "I need a system to manage logistics for my e-commerce business. Do you have a solution?"
|
| 41 |
-
✅ Agent: "Yes, we offer [Project Name], which is designed for logistics management. It provides [brief relevant details from the brochure]. Would you like more information on its features?"
|
| 42 |
-
|
| 43 |
-
Scenario 3: No Matching Solution Available
|
| 44 |
-
✅ Customer: "Do you have a tool for automating customer sentiment analysis?"
|
| 45 |
-
✅ Agent: "I'm sorry, but our current projects do not include a tool specifically for sentiment analysis. However, we do have [Project Name], which provides [related functionality]. Would that be of interest?"
|
| 46 |
-
|
| 47 |
-
✅ Customer: "I need a blockchain-based security system. Can your team provide one?"
|
| 48 |
-
✅ Agent: "Unfortunately, we do not have a blockchain-based security system in our current offerings. I'm happy to help with any other inquiries regarding our available solutions."
|
| 49 |
|
| 50 |
Tone & Style:
|
| 51 |
Maintain a professional, helpful, and customer-focused tone.
|
|
@@ -54,20 +42,38 @@ If a solution exists, explain how it meets the customer's needs without oversell
|
|
| 54 |
If no solution exists, remain polite and transparent.
|
| 55 |
|
| 56 |
First Brochure:
|
| 57 |
-
{text1}
|
| 58 |
|
| 59 |
Second Brochure:
|
| 60 |
-
{text2}
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
def
|
| 66 |
-
|
| 67 |
-
response = self.model.generate_content(question)
|
| 68 |
-
return response.text
|
| 69 |
|
| 70 |
if __name__ == "__main__":
|
| 71 |
-
analyzer = GeminiQanA()
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
| 2 |
import os
|
| 3 |
|
| 4 |
class GeminiQanA:
|
| 5 |
+
def __init__(self, text1: str = '', text2: str = ''):
|
| 6 |
+
"""Initializes the Gemini question-answering model with brochures and conversation history."""
|
| 7 |
self.api_key = os.getenv("GOOGLE_API_KEY")
|
| 8 |
genai.configure(api_key=self.api_key)
|
| 9 |
+
self.text1 = text1
|
| 10 |
+
self.text2 = text2
|
| 11 |
+
self.conversation_history = [] # Store previous exchanges
|
| 12 |
+
self.model = self._load_model()
|
| 13 |
|
| 14 |
+
def _load_model(self):
|
| 15 |
+
"""Loads the generative AI model without the conversation history (history will be passed dynamically)."""
|
| 16 |
+
system_instruction = f'''Role:
|
| 17 |
You are a sales agent responsible for assisting customers by answering questions about our team’s capabilities and the projects we offer. You have access to two brochures that detail the available projects and their features. Your goal is to provide accurate and honest responses based solely on the information within these brochures.
|
| 18 |
|
| 19 |
Guidelines for Responses:
|
|
|
|
| 34 |
-Do not create new information or assume additional capabilities.
|
| 35 |
-Do not make guarantees beyond what is stated in the brochures.
|
| 36 |
-Do not offer speculative solutions that are not explicitly supported by the documents.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
Tone & Style:
|
| 39 |
Maintain a professional, helpful, and customer-focused tone.
|
|
|
|
| 42 |
If no solution exists, remain polite and transparent.
|
| 43 |
|
| 44 |
First Brochure:
|
| 45 |
+
{self.text1}
|
| 46 |
|
| 47 |
Second Brochure:
|
| 48 |
+
{self.text2}
|
| 49 |
+
'''
|
| 50 |
+
return genai.GenerativeModel("gemini-1.5-pro", system_instruction=system_instruction)
|
| 51 |
+
|
| 52 |
+
def answer_question(self, question: str) -> str:
|
| 53 |
+
"""Generates a response by including conversation history in the prompt."""
|
| 54 |
+
|
| 55 |
+
# Format conversation history as a chat transcript
|
| 56 |
+
history_text = "\n".join([f"Customer: {q}\nAgent: {a}" for q, a in self.conversation_history])
|
| 57 |
|
| 58 |
+
# Create a dynamic prompt with history + current question
|
| 59 |
+
dynamic_prompt = f"""{history_text}
|
| 60 |
+
Customer: {question}
|
| 61 |
+
Agent:"""
|
| 62 |
+
|
| 63 |
+
response = self.model.generate_content(dynamic_prompt)
|
| 64 |
+
answer = response.text.strip()
|
| 65 |
+
|
| 66 |
+
# Save this exchange in the history
|
| 67 |
+
self.conversation_history.append((question, answer))
|
| 68 |
+
|
| 69 |
+
return answer
|
| 70 |
|
| 71 |
+
def clear_conv_history(self) -> None:
|
| 72 |
+
self.conversation_history.clear()
|
|
|
|
|
|
|
| 73 |
|
| 74 |
if __name__ == "__main__":
|
| 75 |
+
analyzer = GeminiQanA("Example text from first brochure", "Example text from second brochure")
|
| 76 |
+
|
| 77 |
+
# Example conversation
|
| 78 |
+
print(analyzer.answer_question("What AI solutions do you offer?"))
|
| 79 |
+
print(analyzer.answer_question("Do you have a project for logistics automation?"))
|