i updated the conversation history and fixed some api errors
Browse files
app.py
CHANGED
|
@@ -17,6 +17,7 @@ from app.vectorstore import vectorstore
|
|
| 17 |
from app.tts import tts
|
| 18 |
from app.utils import remove_stars
|
| 19 |
|
|
|
|
| 20 |
# Set a custom user agent.
|
| 21 |
os.environ["USER_AGENT"] = "my-app/1.0"
|
| 22 |
|
|
@@ -30,6 +31,7 @@ class CancerApp:
|
|
| 30 |
# Run an initial search to load vectorstore contents.
|
| 31 |
self.vectorstore.search_vectorstore("cancer", 1)
|
| 32 |
self.tts = tts()
|
|
|
|
| 33 |
|
| 34 |
def text_to_speech(self, message, output_filename, Saved_response=""):
|
| 35 |
search_results = self.vectorstore.search_vectorstore(message, 5)
|
|
@@ -100,21 +102,53 @@ class CancerApp:
|
|
| 100 |
self.audio_processor.log_conversation(transcription, bot_text=gemini_response)
|
| 101 |
return gemini_response, Saved_response
|
| 102 |
|
| 103 |
-
def text_to_text(self, message, Saved_response=""):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
search_results = self.vectorstore.search_vectorstore(message, 5)
|
| 105 |
-
|
| 106 |
for i, result in enumerate(search_results, 1):
|
| 107 |
-
|
| 108 |
-
|
|
|
|
| 109 |
try:
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
except Exception as e:
|
| 113 |
print("Gemini error:", e)
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
# Create Flask API instance.
|
| 120 |
flask_app = Flask(__name__)
|
|
@@ -149,13 +183,33 @@ cancer_app_instance = CancerApp(vectordb_path)
|
|
| 149 |
|
| 150 |
|
| 151 |
# Endpoint for text-to-text processing.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
@flask_app.route('/text_to_text', methods=['POST'])
|
| 153 |
def api_text_to_text():
|
| 154 |
data = request.get_json()
|
| 155 |
message = data.get("message", "")
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
# Endpoint for text-to-speech processing.
|
| 161 |
@flask_app.route('/text_to_speech', methods=['POST'])
|
|
|
|
| 17 |
from app.tts import tts
|
| 18 |
from app.utils import remove_stars
|
| 19 |
|
| 20 |
+
|
| 21 |
# Set a custom user agent.
|
| 22 |
os.environ["USER_AGENT"] = "my-app/1.0"
|
| 23 |
|
|
|
|
| 31 |
# Run an initial search to load vectorstore contents.
|
| 32 |
self.vectorstore.search_vectorstore("cancer", 1)
|
| 33 |
self.tts = tts()
|
| 34 |
+
self.conversation_history = [] # Initialize an empty list for history
|
| 35 |
|
| 36 |
def text_to_speech(self, message, output_filename, Saved_response=""):
|
| 37 |
search_results = self.vectorstore.search_vectorstore(message, 5)
|
|
|
|
| 102 |
self.audio_processor.log_conversation(transcription, bot_text=gemini_response)
|
| 103 |
return gemini_response, Saved_response
|
| 104 |
|
| 105 |
+
# def text_to_text(self, message, Saved_response=""):
|
| 106 |
+
# search_results = self.vectorstore.search_vectorstore(message, 5)
|
| 107 |
+
# query = Saved_response + message
|
| 108 |
+
# for i, result in enumerate(search_results, 1):
|
| 109 |
+
# query += f"\n{i}. {result}"
|
| 110 |
+
|
| 111 |
+
# try:
|
| 112 |
+
# gemini_response = self.llm_processor.call_gemini_llm("gemini-2.5-flash-preview-04-17", query)
|
| 113 |
+
# gemini_response = remove_stars(gemini_response)
|
| 114 |
+
# except Exception as e:
|
| 115 |
+
# print("Gemini error:", e)
|
| 116 |
+
# gemini_response = f"Error: {str(e)}"
|
| 117 |
+
# Saved_response += "message: " + message + "\n" + "gemini_response: " + gemini_response + "\n"
|
| 118 |
+
# self.audio_processor.log_conversation(message, bot_text=gemini_response)
|
| 119 |
+
# return gemini_response, Saved_response
|
| 120 |
+
# 1. Add user's current message to history
|
| 121 |
+
def text_to_text(self, message): # Removed Saved_response from args, use self.conversation_history
|
| 122 |
+
# 1. Add user's current message to history
|
| 123 |
+
self.conversation_history.append({"role": "user", "parts": [message]})
|
| 124 |
+
|
| 125 |
+
# 2. Prepare RAG context
|
| 126 |
search_results = self.vectorstore.search_vectorstore(message, 5)
|
| 127 |
+
rag_context_for_query = ""
|
| 128 |
for i, result in enumerate(search_results, 1):
|
| 129 |
+
rag_context_for_query += f"\n{i}. {result}"
|
| 130 |
+
|
| 131 |
+
# 3. Call LLM with the full history and current RAG context
|
| 132 |
try:
|
| 133 |
+
# Pass self.conversation_history to the LLM call
|
| 134 |
+
# The LLM method will need to be adapted to take history
|
| 135 |
+
gemini_response_text = self.llm_processor.call_gemini_llm_with_history(
|
| 136 |
+
model_name="gemini-2.5-flash-preview-04-17", # Or your specific model
|
| 137 |
+
history=self.conversation_history,
|
| 138 |
+
rag_context=rag_context_for_query
|
| 139 |
+
)
|
| 140 |
+
gemini_response_text = remove_stars(gemini_response_text)
|
| 141 |
except Exception as e:
|
| 142 |
print("Gemini error:", e)
|
| 143 |
+
gemini_response_text = f"Error processing your request: {str(e)}"
|
| 144 |
+
|
| 145 |
+
# 4. Add bot's response to history
|
| 146 |
+
self.conversation_history.append({"role": "model", "parts": [gemini_response_text]})
|
| 147 |
+
|
| 148 |
+
# 5. Log (your existing log_conversation is fine for separate logging)
|
| 149 |
+
self.audio_processor.log_conversation(message, bot_text=gemini_response_text) # Logs current turn
|
| 150 |
+
|
| 151 |
+
return gemini_response_text # Return only the latest response
|
| 152 |
|
| 153 |
# Create Flask API instance.
|
| 154 |
flask_app = Flask(__name__)
|
|
|
|
| 183 |
|
| 184 |
|
| 185 |
# Endpoint for text-to-text processing.
|
| 186 |
+
# @flask_app.route('/text_to_text', methods=['POST'])
|
| 187 |
+
# def api_text_to_text():
|
| 188 |
+
# data = request.get_json()
|
| 189 |
+
# message = data.get("message", "")
|
| 190 |
+
# saved_response = data.get("Saved_response", "")
|
| 191 |
+
# response, saved_response = cancer_app_instance.text_to_text(message, Saved_response=saved_response)
|
| 192 |
+
# return jsonify({"gemini_response": response, "Saved_response": saved_response})
|
| 193 |
+
|
| 194 |
@flask_app.route('/text_to_text', methods=['POST'])
|
| 195 |
def api_text_to_text():
|
| 196 |
data = request.get_json()
|
| 197 |
message = data.get("message", "")
|
| 198 |
+
# Client would send the history, server appends and sends back updated history
|
| 199 |
+
# For simplicity, let's assume history is managed by cancer_app_instance for now
|
| 200 |
+
# If you want stateless, client sends history, method uses it, returns new history + response
|
| 201 |
+
|
| 202 |
+
if not message:
|
| 203 |
+
return jsonify({"error": "No message provided"}), 400
|
| 204 |
+
|
| 205 |
+
# The text_to_text method now manages its own history via self.conversation_history
|
| 206 |
+
response_text = cancer_app_instance.text_to_text(message)
|
| 207 |
+
|
| 208 |
+
return jsonify({
|
| 209 |
+
"gemini_response": response_text,
|
| 210 |
+
"conversation_history": cancer_app_instance.conversation_history # Send back updated history
|
| 211 |
+
})
|
| 212 |
+
|
| 213 |
|
| 214 |
# Endpoint for text-to-speech processing.
|
| 215 |
@flask_app.route('/text_to_speech', methods=['POST'])
|