Maintain multi-turn context by passing conversation history:
self.history = []async def chat_loop(self): system = "You are a helpful assistant. Keep answers under 2 sentences." while True: user_input = await self.capability_worker.user_response() if "exit" in user_input.lower(): break # Add user message to history self.history.append({"role": "user", "content": user_input}) # Generate response with full history response = self.capability_worker.text_to_text_response( user_input, history=self.history, system_prompt=system ) # Add assistant response to history self.history.append({"role": "assistant", "content": response}) await self.capability_worker.speak(response) self.capability_worker.resume_normal_flow()
system = "You are a pirate captain. Speak like a pirate in all responses."response = self.capability_worker.text_to_text_response( "What's the weather today?", system_prompt=system)# Result: "Arr, the skies be clear and the winds fair, matey!"
async def summarize_conversation(self): """Summarize the current conversation history.""" # Get full message history from Agent history = self.capability_worker.get_full_message_history() # Convert to text conversation_text = "\n".join( [f"{msg['role']}: {msg['content']}" for msg in history] ) # Summarize summary = self.capability_worker.text_to_text_response( f"Summarize this conversation in 2-3 sentences:\n\n{conversation_text}" ) await self.capability_worker.speak(f"Here's a summary: {summary}")
def translate(self, text: str, target_language: str) -> str: """Translate text to target language.""" system = f"You are a translator. Translate all input to {target_language}. Return ONLY the translation." return self.capability_worker.text_to_text_response( text, system_prompt=system )# Usagespanish = self.translate("Hello, how are you?", "Spanish")await self.capability_worker.speak(spanish)
# ✅ Goodsystem = "Keep all responses under 2 sentences. Never use technical jargon."response = self.capability_worker.text_to_text_response(query, system_prompt=system)# ❌ Bad - constraints in every promptprompt = f"{query}. Keep response under 2 sentences and avoid jargon."
# ❌ Will failresponse = self.capability_worker.text_to_text_response( "Return JSON with name and age")data = json.loads(response) # Crashes if response is ```json\n{...}\n```# ✅ Correctresponse = self.capability_worker.text_to_text_response( "Return JSON with name and age")clean = response.replace("```json", "").replace("```", "").strip()data = json.loads(clean)
# ✅ Goodprompt = f"Respond to: {user_input}"# ❌ Unnecessarily verboseprompt = ( "The user has provided the following input and would like " "you to generate a helpful, friendly, and concise response " f"to their query which is: {user_input}")