Spaces:
Sleeping
Sleeping
File size: 1,057 Bytes
0307f67 4867fcb 0307f67 4867fcb 0307f67 4867fcb | 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 | import os
from dotenv import load_dotenv
from langchain_groq import ChatGroq
from langchain_google_genai import ChatGoogleGenerativeAI
load_dotenv()
def get_llm(task_type: str = "general"):
"""
Factory to get the best LLM for the job.
- Planning/Logic -> Groq (Llama 3.3) for speed.
- Writing/Context -> Groq or Gemini Fallback.
"""
groq_key = os.getenv("GROQ_API_KEY")
gemini_key = os.getenv("GEMINI_API_KEY")
# PRIMARY: Groq (Llama 3.3 70B)
if groq_key:
try:
return ChatGroq(
temperature=0.7,
model_name="llama-3.3-70b-versatile",
api_key=groq_key
)
except Exception as e:
print(f"⚠️ Groq failed: {e}. Falling back...")
# FALLBACK: Gemini
if gemini_key:
return ChatGoogleGenerativeAI(
model="gemini-1.5-flash",
google_api_key=gemini_key,
temperature=0.7
)
raise ValueError("❌ No API Keys found! Please set GROQ_API_KEY or GEMINI_API_KEY.")
|