# chat_handler.py updates from groq import Groq import os from dotenv import load_dotenv class YogaChatHandler: def __init__(self): load_dotenv() self.GROQ_API_KEY = os.getenv("GROQ_API_KEY") if self.GROQ_API_KEY: self.groq_client = Groq(api_key=self.GROQ_API_KEY) def get_streaming_response(self, user_query, yoga_context): try: messages = [ { "role": "system", "content": """ You are an experienced yoga instructor providing concise guidance. Key instructions: - Limit responses to maximum 200 words - Be direct and specific - Focus on the most relevant information - Only include essential details - Skip general advice unless specifically asked - Maintain a supportive but concise tone - If the question can be answered briefly, do so """ }, { "role": "user", "content": f""" Context: {yoga_context} Question: {user_query} Remember to provide a focused response in 200 words or less. """ } ] # Create streaming response stream = self.groq_client.chat.completions.create( messages=messages, model="llama3-8b-8192", max_tokens=300, temperature=0.7, top_p=0.9, stream=True # Enable streaming ) collected_response = "" for chunk in stream: if chunk.choices[0].delta.content is not None: chunk_content = chunk.choices[0].delta.content collected_response += chunk_content yield chunk_content # Add disclaimer if needed if any(word in user_query.lower() for word in ['practice', 'safe', 'risk', 'hurt', 'pain', 'modify']): disclaimer = ( "\n\n---\n" "*Note: Please practice with proper instruction and consult a qualified instructor for personalized guidance.*" ) yield disclaimer # Verify response length if self.count_words(collected_response) > 200: words = collected_response.split() truncated = ' '.join(words[:200]) last_sentence = truncated.rsplit('.', 1)[0] + '.' return last_sentence except Exception as e: yield f"Unable to generate response: {str(e)}"