Spaces:
Runtime error
Runtime error
Enrique Cardoza commited on
Commit ·
7bcdea6
1
Parent(s): 5a049e8
feat(tts): provide conversation history to text-to-speech tool
Browse files- Add history parameter to the should_use_tts_tool function
- Update system prompt to instruct Claude to extract content from prior messages
- Implement message formatting to include full conversation context
- Enable TTS tool to understand references to transcripts and prior messages
- Pass conversation history when calling TTS decision function
This change ensures the TTS tool has complete context when users ask to convert content from previous messages or transcripts to speech.
app.py
CHANGED
|
@@ -226,7 +226,7 @@ def should_use_transcription_tool(prompt, available_tools):
|
|
| 226 |
return False, f"Error: {str(e)}"
|
| 227 |
|
| 228 |
# Function to decide if TTS tool should be used
|
| 229 |
-
def should_use_tts_tool(prompt):
|
| 230 |
try:
|
| 231 |
if not client:
|
| 232 |
print("No Claude client available for TTS tool decision")
|
|
@@ -253,6 +253,8 @@ def should_use_tts_tool(prompt):
|
|
| 253 |
When deciding what text to convert:
|
| 254 |
- If the user provides specific text in quotes or after phrases like "convert this to speech:", use that exact text
|
| 255 |
- If the user asks to convert their previous message, use that as the text
|
|
|
|
|
|
|
| 256 |
- If the user doesn't specify text but clearly wants TTS, ask them to provide the text
|
| 257 |
|
| 258 |
Respond with false if:
|
|
@@ -260,12 +262,24 @@ def should_use_tts_tool(prompt):
|
|
| 260 |
- The user is asking about transcription (speech-to-text) instead
|
| 261 |
"""
|
| 262 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
# Ensure we're sending a valid message
|
| 264 |
response = client.messages.create(
|
| 265 |
model="claude-3-7-sonnet-latest",
|
| 266 |
max_tokens=500,
|
| 267 |
system=system_prompt,
|
| 268 |
-
messages=
|
| 269 |
)
|
| 270 |
|
| 271 |
response_content = response.content[0].text
|
|
@@ -321,7 +335,8 @@ def chat_with_tools(user_message, history, user_groq_api_key, user_anthropic_api
|
|
| 321 |
audio_url = extract_url(user_message)
|
| 322 |
|
| 323 |
# 2. Decision phase - First check if we should use the TTS tool
|
| 324 |
-
|
|
|
|
| 325 |
|
| 326 |
# 3a. Tool usage phase - Process TTS if decided to use that tool
|
| 327 |
if use_tts:
|
|
|
|
| 226 |
return False, f"Error: {str(e)}"
|
| 227 |
|
| 228 |
# Function to decide if TTS tool should be used
|
| 229 |
+
def should_use_tts_tool(prompt, history=None):
|
| 230 |
try:
|
| 231 |
if not client:
|
| 232 |
print("No Claude client available for TTS tool decision")
|
|
|
|
| 253 |
When deciding what text to convert:
|
| 254 |
- If the user provides specific text in quotes or after phrases like "convert this to speech:", use that exact text
|
| 255 |
- If the user asks to convert their previous message, use that as the text
|
| 256 |
+
- If the user refers to content from the conversation history, extract that content
|
| 257 |
+
- If the user asks to convert the transcript or other previous content, find that in the history
|
| 258 |
- If the user doesn't specify text but clearly wants TTS, ask them to provide the text
|
| 259 |
|
| 260 |
Respond with false if:
|
|
|
|
| 262 |
- The user is asking about transcription (speech-to-text) instead
|
| 263 |
"""
|
| 264 |
|
| 265 |
+
# Prepare the messages for Claude
|
| 266 |
+
messages = []
|
| 267 |
+
if history:
|
| 268 |
+
# Include history in the context to allow referencing previous messages
|
| 269 |
+
formatted_history = format_history(history)
|
| 270 |
+
messages = formatted_history
|
| 271 |
+
# Add the user's current prompt as the final message
|
| 272 |
+
messages.append({"role": "user", "content": prompt})
|
| 273 |
+
else:
|
| 274 |
+
# If no history, just use the current prompt
|
| 275 |
+
messages = [{"role": "user", "content": f"User message: {prompt}"}]
|
| 276 |
+
|
| 277 |
# Ensure we're sending a valid message
|
| 278 |
response = client.messages.create(
|
| 279 |
model="claude-3-7-sonnet-latest",
|
| 280 |
max_tokens=500,
|
| 281 |
system=system_prompt,
|
| 282 |
+
messages=messages
|
| 283 |
)
|
| 284 |
|
| 285 |
response_content = response.content[0].text
|
|
|
|
| 335 |
audio_url = extract_url(user_message)
|
| 336 |
|
| 337 |
# 2. Decision phase - First check if we should use the TTS tool
|
| 338 |
+
# Pass the full history to the TTS decision function
|
| 339 |
+
use_tts, tts_reasoning, text_to_convert = should_use_tts_tool(user_message, history[:-1]) # Exclude the current message
|
| 340 |
|
| 341 |
# 3a. Tool usage phase - Process TTS if decided to use that tool
|
| 342 |
if use_tts:
|