NgBaoAnn commited on
Commit
99f90fd
·
1 Parent(s): 0e5461b

feat: add Gemini 2.0 Flash as primary LLM (native vision+audio) — Groq/HF fallback

Browse files
Files changed (2) hide show
  1. app.py +38 -8
  2. requirements.txt +1 -0
app.py CHANGED
@@ -437,6 +437,20 @@ _tools = [
437
  ]
438
 
439
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
440
  def _build_groq_llm():
441
  """Build Groq LLM (fast, free, excellent tool calling)."""
442
  from langchain_groq import ChatGroq
@@ -469,24 +483,40 @@ def _build_hf_llm():
469
 
470
 
471
  def build_graph():
472
- """Build and return a compiled LangGraph ReAct agent. Tries Groq first, then HF."""
473
- # Try Groq first (better tool calling, free)
474
  llm = None
475
  provider_used = None
 
 
476
  try:
477
- llm = _build_groq_llm()
478
- provider_used = "Groq (llama-3.3-70b-versatile)"
479
  print(f"✅ Using LLM: {provider_used}")
480
  except Exception as e:
481
- print(f"⚠️ Groq not available: {e}. Trying HuggingFace...")
 
 
 
 
 
 
 
 
 
 
 
 
482
  try:
483
  llm = _build_hf_llm()
484
  provider_used = "HuggingFace (Qwen2.5-Coder-32B)"
485
  print(f"✅ Using LLM: {provider_used}")
486
- except Exception as e2:
487
  raise RuntimeError(
488
- f"No LLM available. Set GROQ_API_KEY or HF_TOKEN.\n"
489
- f"Groq error: {e}\nHF error: {e2}"
 
 
 
490
  )
491
 
492
  llm_with_tools = llm.bind_tools(_tools)
 
437
  ]
438
 
439
 
440
+ def _build_gemini_llm():
441
+ """Build Gemini 2.0 Flash — multimodal, free, best for vision+audio+text."""
442
+ from langchain_google_genai import ChatGoogleGenerativeAI
443
+ google_key = os.environ.get("GOOGLE_API_KEY")
444
+ if not google_key:
445
+ raise ValueError("GOOGLE_API_KEY not set")
446
+ return ChatGoogleGenerativeAI(
447
+ model="gemini-2.0-flash",
448
+ temperature=0,
449
+ google_api_key=google_key,
450
+ max_tokens=8192,
451
+ )
452
+
453
+
454
  def _build_groq_llm():
455
  """Build Groq LLM (fast, free, excellent tool calling)."""
456
  from langchain_groq import ChatGroq
 
483
 
484
 
485
  def build_graph():
486
+ """Build LangGraph ReAct agent. Priority: Gemini Groq → HuggingFace."""
 
487
  llm = None
488
  provider_used = None
489
+
490
+ # 1st choice: Gemini 2.0 Flash (multimodal — handles images + audio natively)
491
  try:
492
+ llm = _build_gemini_llm()
493
+ provider_used = "Gemini 2.0 Flash (multimodal)"
494
  print(f"✅ Using LLM: {provider_used}")
495
  except Exception as e:
496
+ print(f"⚠️ Gemini not available: {e}")
497
+
498
+ # 2nd choice: Groq Llama-3.3-70B (excellent tool calling, free)
499
+ if llm is None:
500
+ try:
501
+ llm = _build_groq_llm()
502
+ provider_used = "Groq (llama-3.3-70b-versatile)"
503
+ print(f"✅ Using LLM: {provider_used}")
504
+ except Exception as e:
505
+ print(f"⚠️ Groq not available: {e}")
506
+
507
+ # 3rd choice: HuggingFace endpoint
508
+ if llm is None:
509
  try:
510
  llm = _build_hf_llm()
511
  provider_used = "HuggingFace (Qwen2.5-Coder-32B)"
512
  print(f"✅ Using LLM: {provider_used}")
513
+ except Exception as e:
514
  raise RuntimeError(
515
+ "No LLM available. Please set one of:\n"
516
+ " GOOGLE_API_KEY (recommended aistudio.google.com)\n"
517
+ " GROQ_API_KEY (groq.com)\n"
518
+ " HF_TOKEN (huggingface.co)\n"
519
+ f"Last error: {e}"
520
  )
521
 
522
  llm_with_tools = llm.bind_tools(_tools)
requirements.txt CHANGED
@@ -10,6 +10,7 @@ langchain-core>=0.3.0
10
  langchain-community>=0.3.0
11
  langchain-huggingface>=0.1.0
12
  langchain-groq>=0.2.0
 
13
  langgraph>=0.2.0
14
 
15
  # HuggingFace
 
10
  langchain-community>=0.3.0
11
  langchain-huggingface>=0.1.0
12
  langchain-groq>=0.2.0
13
+ langchain-google-genai>=2.0.0
14
  langgraph>=0.2.0
15
 
16
  # HuggingFace