Update app.py
Browse files
app.py
CHANGED
@@ -49,6 +49,50 @@ class MyApp:
|
|
49 |
app = MyApp()
|
50 |
|
51 |
def respond(message: str, history: List[Tuple[str, str]]):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
system_message = "You are a supportive and empathetic Dialectical Behaviour Therapist assistant. You politely guide users through DBT exercises based on the given DBT book. You must say one thing at a time and ask follow-up questions to continue the chat."
|
53 |
messages = [{"role": "system", "content": system_message}]
|
54 |
|
|
|
49 |
app = MyApp()
|
50 |
|
51 |
def respond(message: str, history: List[Tuple[str, str]]):
|
52 |
+
system_message = (
|
53 |
+
"You are a supportive and empathetic Dialectical Behaviour Therapist assistant. "
|
54 |
+
"You politely guide users through DBT exercises based on the given DBT book. "
|
55 |
+
"You must say one thing at a time and ask follow-up questions to continue the chat."
|
56 |
+
)
|
57 |
+
messages = [{"role": "system", "content": system_message}]
|
58 |
+
|
59 |
+
for val in history:
|
60 |
+
if val[0]:
|
61 |
+
messages.append({"role": "user", "content": val[0]})
|
62 |
+
if val[1]:
|
63 |
+
messages.append({"role": "assistant", "content": val[1]})
|
64 |
+
|
65 |
+
messages.append({"role": "user", "content": message})
|
66 |
+
|
67 |
+
# RAG - Retrieve relevant documents if the query suggests exercises or specific information
|
68 |
+
if any(
|
69 |
+
keyword in message.lower()
|
70 |
+
for keyword in ["exercise", "technique", "information", "guide", "help", "how to"]
|
71 |
+
):
|
72 |
+
retrieved_docs = app.search_documents(message)
|
73 |
+
context = "\n".join(retrieved_docs)
|
74 |
+
if context.strip():
|
75 |
+
messages.append({"role": "system", "content": "Relevant documents: " + context})
|
76 |
+
|
77 |
+
# Generate response using the generative model
|
78 |
+
model = GenerativeModel("gemini-1.5-pro-latest")
|
79 |
+
generation_config = types.GenerationConfig(
|
80 |
+
temperature=0.7,
|
81 |
+
max_output_tokens=1024,
|
82 |
+
)
|
83 |
+
|
84 |
+
try:
|
85 |
+
response = model.generate_content([message], generation_config=generation_config)
|
86 |
+
# Properly access the response content
|
87 |
+
response_content = response.text if hasattr(response, "text") else "No response generated."
|
88 |
+
except Exception as e:
|
89 |
+
response_content = f"An error occurred while generating the response: {str(e)}"
|
90 |
+
|
91 |
+
# Append the message and generated response to the chat history
|
92 |
+
history.append((message, response_content))
|
93 |
+
return history, ""
|
94 |
+
|
95 |
+
def old_respond(message: str, history: List[Tuple[str, str]]):
|
96 |
system_message = "You are a supportive and empathetic Dialectical Behaviour Therapist assistant. You politely guide users through DBT exercises based on the given DBT book. You must say one thing at a time and ask follow-up questions to continue the chat."
|
97 |
messages = [{"role": "system", "content": system_message}]
|
98 |
|