shabdham_backend / agents.py
akma01's picture
Upload 4 files
b04ae4b verified
Raw
History Blame Contribute Delete
2.19 kB
import json
import os
from groq import Groq
from tts_engine import synthesize_speech, play_audio
# Fallback to the key used in gen_voice.py if env var is missing
api_key = os.environ.get("GROQ_API_KEY", "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")
try:
client = Groq(api_key=api_key)
except Exception as e:
print(f"Failed to initialize Groq client in agents.py: {e}")
client = None
def generate_response(prompt: str) -> str:
if not client:
return "Groq client not initialized."
print("Generating LLM response...")
try:
response = client.chat.completions.create(
model='llama-3.3-70b-versatile',
messages=[
{"role": "system", "content": "You are a helpful voice assistant. Keep your response brief, natural, and conversational in Tamil or English."},
{"role": "user", "content": prompt}
]
)
return response.choices[0].message.content
except Exception as e:
print(f"Error calling LLM: {e}")
return "மன்னிக்கவும், ஒரு பிழை ஏற்பட்டுள்ளது." # Sorry, an error occurred.
def handle_agent_request(agent_name: str, entities: dict):
print(f"\n[{agent_name}] Handling request with entities: {json.dumps(entities, indent=2)}")
prompt = f"The user triggered the {agent_name} with these details: {json.dumps(entities)}. Please provide a short helpful response confirming this in Tamil."
response_text = generate_response(prompt)
print(f"[{agent_name} Response]: {response_text}")
# Synthesize and play
audio_path = synthesize_speech(response_text, f"{agent_name}_response.wav")
if audio_path:
play_audio(audio_path)
print(f"[{agent_name}] Done processing intent.\n")
def HEALTHCARE_AGENT(entities: dict):
handle_agent_request("HEALTHCARE_AGENT", entities)
def ECOMMERCE_AGENT(entities: dict):
handle_agent_request("ECOMMERCE_AGENT", entities)
def BANKING_AGENT(entities: dict):
handle_agent_request("BANKING_AGENT", entities)
def LMS_AGENT(entities: dict):
handle_agent_request("LMS_AGENT", entities)