Spaces:
Paused
Paused
File size: 2,935 Bytes
bc31a94 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | # 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)}" |