Isateles commited on
Commit
9c9affd
Β·
1 Parent(s): 8239e51

Updated LLMs

Browse files
Files changed (4) hide show
  1. app.py +234 -53
  2. requirements.txt +5 -3
  3. test_hf_space.py +1 -1
  4. tools.py +1 -1
app.py CHANGED
@@ -29,45 +29,133 @@ PASSING_SCORE = 30 # Need this to get my certificate!
29
 
30
  def setup_llm():
31
  """
32
- Setting up the LLM - trying OpenAI first since it usually works better,
33
- but falling back to HuggingFace if I don't have OpenAI credits
 
 
 
 
 
34
  """
35
- logger.info("Setting up LLM...")
36
 
37
- # Try OpenAI first (better performance but costs money)
38
- openai_key = os.getenv("OPENAI_API_KEY")
39
- if openai_key:
40
  try:
41
- from llama_index.llms.openai import OpenAI
42
- llm = OpenAI(
43
- api_key=openai_key,
44
- model="gpt-4o-mini", # Good balance of performance and cost
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  max_tokens=1024,
46
- temperature=0.1 # Low temp for more consistent answers
47
  )
48
- logger.info("Got OpenAI working!")
49
  return llm
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  except Exception as e:
51
- logger.warning(f"OpenAI didn't work: {e}")
52
 
53
- # Fallback to HuggingFace (free but maybe not as good)
54
  hf_token = os.getenv("HF_TOKEN")
55
  if hf_token:
56
  try:
57
  from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
58
  llm = HuggingFaceInferenceAPI(
59
- model_name="Qwen/Qwen2.5-Coder-32B-Instruct",
60
  token=hf_token,
61
  max_new_tokens=512,
62
  temperature=0.1
63
  )
64
- logger.info("Using HuggingFace LLM")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  return llm
66
  except Exception as e:
67
- logger.error(f"HuggingFace also failed: {e}")
68
 
69
  # If we get here, nothing worked
70
- raise RuntimeError("No LLM available! Need either OPENAI_API_KEY or HF_TOKEN")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
 
72
  class MyGAIAAgent:
73
  """
@@ -347,7 +435,7 @@ Required to pass: {PASSING_SCORE}%
347
  logger.error(error_msg)
348
  return error_msg, pd.DataFrame(results)
349
 
350
- # Create the Gradio interface (template pattern)
351
  with gr.Blocks(title="My GAIA Agent") as demo:
352
  gr.Markdown("# πŸ€– My GAIA Benchmark Agent")
353
  gr.Markdown("""
@@ -360,37 +448,109 @@ with gr.Blocks(title="My GAIA Agent") as demo:
360
  - πŸ‘₯ Query a database of personas
361
 
362
  **Goal:** Score 30%+ on GAIA benchmark to pass the course!
363
-
364
- **Instructions:**
365
- 1. **Login** to HuggingFace using the button below
366
- 2. **Click "Run GAIA Evaluation"** and wait (takes 5-10 minutes)
367
- 3. **See your results** and hopefully pass with 30%+!
368
  """)
369
 
370
  # Login button (template pattern)
371
  gr.LoginButton()
372
 
373
- gr.Markdown("### Step 2: Run the Evaluation")
374
- gr.Markdown("⏰ This might take 5-10 minutes...")
375
-
376
- run_btn = gr.Button("πŸš€ Run GAIA Evaluation", variant="primary", size="lg")
377
-
378
- gr.Markdown("### Step 3: Results")
379
-
380
- status_text = gr.Textbox(
381
- label="πŸ“Š My Results",
382
- lines=10,
383
- interactive=False,
384
- placeholder="Results will show here..."
385
- )
386
-
387
- results_df = gr.DataFrame(label="πŸ“ Question by Question Results", wrap=True)
388
-
389
- # Button connection (template pattern - this is the key!)
390
- run_btn.click(
391
- fn=run_gaia_evaluation,
392
- outputs=[status_text, results_df]
393
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
394
 
395
  gr.Markdown("---")
396
  gr.Markdown("🀞 Fingers crossed I pass this course!")
@@ -399,18 +559,39 @@ if __name__ == "__main__":
399
  print("🎯 My GAIA Agent - Final Course Project")
400
  print("=" * 50)
401
 
402
- # Check my environment
403
- openai_key = os.getenv("OPENAI_API_KEY")
 
 
 
404
  hf_token = os.getenv("HF_TOKEN")
 
405
 
406
- if openai_key:
407
- print("βœ… OpenAI key found")
 
 
 
 
 
 
408
  if hf_token:
409
- print("βœ… HuggingFace token found")
410
- if not openai_key and not hf_token:
411
- print("⚠️ No API keys! Add OPENAI_API_KEY or HF_TOKEN to secrets")
 
 
 
 
 
 
 
 
 
 
 
412
 
413
- print(f"🎯 Need {PASSING_SCORE}% to pass the course")
414
  print("πŸš€ Starting my agent...")
415
 
416
  demo.launch(debug=True, share=False, show_error=True)
 
29
 
30
  def setup_llm():
31
  """
32
+ Setting up the LLM - trying multiple free/cheap providers since OpenAI is expensive!
33
+
34
+ Priority order:
35
+ 1. Groq (fast and often has generous free tier)
36
+ 2. Together AI (good open models, reasonable pricing)
37
+ 3. HuggingFace (free fallback)
38
+ 4. OpenAI (if I have credits)
39
  """
40
+ logger.info("Setting up LLM with multiple provider options...")
41
 
42
+ # Try Groq first (often has generous free tier and is very fast)
43
+ groq_key = os.getenv("GROQ_API_KEY")
44
+ if groq_key:
45
  try:
46
+ # Try the official Groq import
47
+ from llama_index.llms.groq import Groq
48
+ llm = Groq(
49
+ api_key=groq_key,
50
+ model="llama3-groq-70b-8192-tool-use-preview", # Known working Groq model
51
+ max_tokens=1024,
52
+ temperature=0.1
53
+ )
54
+ logger.info("πŸš€ Got Groq working!")
55
+ return llm
56
+ except ImportError:
57
+ logger.warning("Groq LlamaIndex integration not available, trying generic OpenAI-compatible...")
58
+ try:
59
+ # Fallback: Use OpenAI client with Groq endpoint
60
+ from llama_index.llms.openai import OpenAI
61
+ llm = OpenAI(
62
+ api_key=groq_key,
63
+ model="llama3-groq-70b-8192-tool-use-preview",
64
+ api_base="https://api.groq.com/openai/v1",
65
+ max_tokens=1024,
66
+ temperature=0.1
67
+ )
68
+ logger.info("πŸš€ Got Groq working via OpenAI-compatible API!")
69
+ return llm
70
+ except Exception as e:
71
+ logger.warning(f"Groq didn't work: {e}")
72
+ except Exception as e:
73
+ logger.warning(f"Groq didn't work: {e}")
74
+
75
+ # Try Together AI (good selection of open models)
76
+ together_key = os.getenv("TOGETHER_API_KEY")
77
+ if together_key:
78
+ try:
79
+ # Try the official Together import
80
+ from llama_index.llms.together import Together
81
+ llm = Together(
82
+ api_key=together_key,
83
+ model="meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo", # Known working Together model
84
  max_tokens=1024,
85
+ temperature=0.1
86
  )
87
+ logger.info("🀝 Got Together AI working!")
88
  return llm
89
+ except ImportError:
90
+ logger.warning("Together AI LlamaIndex integration not available, trying generic OpenAI-compatible...")
91
+ try:
92
+ # Fallback: Use OpenAI client with Together endpoint
93
+ from llama_index.llms.openai import OpenAI
94
+ llm = OpenAI(
95
+ api_key=together_key,
96
+ model="meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo",
97
+ api_base="https://api.together.xyz/v1",
98
+ max_tokens=1024,
99
+ temperature=0.1
100
+ )
101
+ logger.info("🀝 Got Together AI working via OpenAI-compatible API!")
102
+ return llm
103
+ except Exception as e:
104
+ logger.warning(f"Together AI didn't work: {e}")
105
  except Exception as e:
106
+ logger.warning(f"Together AI didn't work: {e}")
107
 
108
+ # Fallback to HuggingFace (free but slower)
109
  hf_token = os.getenv("HF_TOKEN")
110
  if hf_token:
111
  try:
112
  from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
113
  llm = HuggingFaceInferenceAPI(
114
+ model_name="meta-llama/Llama-3.1-70B-Instruct", # Good HF model
115
  token=hf_token,
116
  max_new_tokens=512,
117
  temperature=0.1
118
  )
119
+ logger.info("πŸ€— Using HuggingFace as fallback")
120
+ return llm
121
+ except Exception as e:
122
+ logger.warning(f"HuggingFace failed: {e}")
123
+
124
+ # Try OpenAI last (in case I get more credits)
125
+ openai_key = os.getenv("OPENAI_API_KEY")
126
+ if openai_key:
127
+ try:
128
+ from llama_index.llms.openai import OpenAI
129
+ llm = OpenAI(
130
+ api_key=openai_key,
131
+ model="gpt-4o-mini",
132
+ max_tokens=1024,
133
+ temperature=0.1
134
+ )
135
+ logger.info("πŸ”„ Trying OpenAI...")
136
  return llm
137
  except Exception as e:
138
+ logger.warning(f"OpenAI still having issues: {e}")
139
 
140
  # If we get here, nothing worked
141
+ error_msg = """
142
+ No LLM available! Please set one of these API keys in your Space secrets:
143
+
144
+ 🎯 RECOMMENDED (Free/Cheap):
145
+ - GROQ_API_KEY (Fast, generous free tier)
146
+ - TOGETHER_API_KEY (Good open models)
147
+
148
+ πŸ”„ ALTERNATIVES:
149
+ - HF_TOKEN (Free but slower)
150
+ - OPENAI_API_KEY (If you get more credits)
151
+
152
+ Get keys from:
153
+ - Groq: https://console.groq.com/
154
+ - Together: https://api.together.xyz/
155
+ """
156
+
157
+ logger.error(error_msg)
158
+ raise RuntimeError(error_msg)
159
 
160
  class MyGAIAAgent:
161
  """
 
435
  logger.error(error_msg)
436
  return error_msg, pd.DataFrame(results)
437
 
438
+ # Create the Gradio interface with chat + GAIA evaluation
439
  with gr.Blocks(title="My GAIA Agent") as demo:
440
  gr.Markdown("# πŸ€– My GAIA Benchmark Agent")
441
  gr.Markdown("""
 
448
  - πŸ‘₯ Query a database of personas
449
 
450
  **Goal:** Score 30%+ on GAIA benchmark to pass the course!
 
 
 
 
 
451
  """)
452
 
453
  # Login button (template pattern)
454
  gr.LoginButton()
455
 
456
+ # Create tabs for different functionalities
457
+ with gr.Tabs():
458
+
459
+ # Tab 1: GAIA Evaluation (main functionality)
460
+ with gr.TabItem("🎯 GAIA Evaluation"):
461
+ gr.Markdown("### Run the Official GAIA Evaluation")
462
+ gr.Markdown("⏰ This might take 5-10 minutes...")
463
+
464
+ run_btn = gr.Button("πŸš€ Run GAIA Evaluation", variant="primary", size="lg")
465
+
466
+ status_text = gr.Textbox(
467
+ label="πŸ“Š My Results",
468
+ lines=10,
469
+ interactive=False,
470
+ placeholder="Results will show here..."
471
+ )
472
+
473
+ results_df = gr.DataFrame(label="πŸ“ Question by Question Results", wrap=True)
474
+
475
+ # Button connection (template pattern)
476
+ run_btn.click(
477
+ fn=run_gaia_evaluation,
478
+ outputs=[status_text, results_df]
479
+ )
480
+
481
+ # Tab 2: Chat Interface (for testing)
482
+ with gr.TabItem("πŸ’¬ Test Chat"):
483
+ gr.Markdown("### Chat with My Agent")
484
+ gr.Markdown("Test your agent here before running the official evaluation!")
485
+
486
+ # Simple chat interface
487
+ chatbot = gr.Chatbot(label="Chat with My Agent", height=400)
488
+ msg_input = gr.Textbox(
489
+ label="Your Message",
490
+ placeholder="Ask me anything! Try: 'What is 15% of 847?' or 'Search for recent AI news'",
491
+ lines=2
492
+ )
493
+
494
+ with gr.Row():
495
+ send_btn = gr.Button("Send", variant="primary")
496
+ clear_btn = gr.Button("Clear Chat")
497
+
498
+ # Chat functionality
499
+ def chat_with_agent(message, history):
500
+ """Simple chat function to test my agent"""
501
+ if not message.strip():
502
+ return history, ""
503
+
504
+ try:
505
+ # Create agent if needed (cache it)
506
+ if not hasattr(chat_with_agent, 'agent'):
507
+ logger.info("Creating agent for chat...")
508
+ chat_with_agent.agent = MyGAIAAgent()
509
+ logger.info("Chat agent ready!")
510
+
511
+ # Get response from agent
512
+ response = chat_with_agent.agent(message)
513
+
514
+ # Add to chat history
515
+ history.append((message, response))
516
+
517
+ except Exception as e:
518
+ error_response = f"Sorry, I had an error: {str(e)}"
519
+ history.append((message, error_response))
520
+
521
+ return history, "" # Return updated history and clear input
522
+
523
+ def clear_chat():
524
+ """Clear the chat history"""
525
+ return [], ""
526
+
527
+ # Connect chat functions
528
+ send_btn.click(
529
+ fn=chat_with_agent,
530
+ inputs=[msg_input, chatbot],
531
+ outputs=[chatbot, msg_input]
532
+ )
533
+
534
+ msg_input.submit( # Allow Enter key to send
535
+ fn=chat_with_agent,
536
+ inputs=[msg_input, chatbot],
537
+ outputs=[chatbot, msg_input]
538
+ )
539
+
540
+ clear_btn.click(
541
+ fn=clear_chat,
542
+ outputs=[chatbot, msg_input]
543
+ )
544
+
545
+ # Some example questions
546
+ gr.Markdown("""
547
+ **Try these example questions:**
548
+ - `What is 25 * 17?`
549
+ - `Search for recent news about AI`
550
+ - `Find creative people in the persona database`
551
+ - `What's the weather in Paris?`
552
+ - `Analyze this CSV: name,age\\nAlice,25\\nBob,30`
553
+ """)
554
 
555
  gr.Markdown("---")
556
  gr.Markdown("🀞 Fingers crossed I pass this course!")
 
559
  print("🎯 My GAIA Agent - Final Course Project")
560
  print("=" * 50)
561
 
562
+ # Check my environment and available LLM providers
563
+ print("\nπŸ” Available LLM Providers:")
564
+
565
+ groq_key = os.getenv("GROQ_API_KEY")
566
+ together_key = os.getenv("TOGETHER_API_KEY")
567
  hf_token = os.getenv("HF_TOKEN")
568
+ openai_key = os.getenv("OPENAI_API_KEY")
569
 
570
+ providers_found = []
571
+
572
+ if groq_key:
573
+ providers_found.append("Groq")
574
+ print("βœ… GROQ_API_KEY found - Groq available!")
575
+ if together_key:
576
+ providers_found.append("Together AI")
577
+ print("βœ… TOGETHER_API_KEY found - Together AI available!")
578
  if hf_token:
579
+ providers_found.append("HuggingFace")
580
+ print("βœ… HF_TOKEN found - HuggingFace available!")
581
+ if openai_key:
582
+ providers_found.append("OpenAI")
583
+ print("βœ… OPENAI_API_KEY found - OpenAI available!")
584
+
585
+ if providers_found:
586
+ print(f"\nπŸŽ‰ Found {len(providers_found)} LLM provider(s): {', '.join(providers_found)}")
587
+ print(f" Will use: {providers_found[0]} (highest priority)")
588
+ else:
589
+ print("\n⚠️ No API keys found! Add at least one to Space secrets:")
590
+ print(" - GROQ_API_KEY (recommended - fast & often free)")
591
+ print(" - TOGETHER_API_KEY (good open models)")
592
+ print(" - HF_TOKEN (free fallback)")
593
 
594
+ print(f"\n🎯 Need {PASSING_SCORE}% to pass the course")
595
  print("πŸš€ Starting my agent...")
596
 
597
  demo.launch(debug=True, share=False, show_error=True)
requirements.txt CHANGED
@@ -9,9 +9,11 @@ pandas>=1.5.0
9
  # Main LlamaIndex stuff - this is the core framework we learned about
10
  llama-index-core>=0.10.0
11
 
12
- # Different LLM options - trying both OpenAI and HuggingFace
13
- llama-index-llms-openai
14
- llama-index-llms-huggingface-api
 
 
15
 
16
  # For the RAG part with embeddings and vector search
17
  llama-index-retrievers-bm25
 
9
  # Main LlamaIndex stuff - this is the core framework we learned about
10
  llama-index-core>=0.10.0
11
 
12
+ # Multiple LLM options - using correct package names
13
+ llama-index-llms-openai # OpenAI (if I have credits)
14
+ llama-index-llms-huggingface-api # HuggingFace (free option)
15
+ llama-index-llms-groq # Groq (fast and often free)
16
+ llama-index-llms-together # Together AI (good models)
17
 
18
  # For the RAG part with embeddings and vector search
19
  llama-index-retrievers-bm25
test_hf_space.py CHANGED
@@ -137,7 +137,7 @@ def test_my_persona_database():
137
  print("\nπŸ‘₯ Testing My Persona Database...")
138
 
139
  try:
140
- from my_retriever import test_my_personas
141
 
142
  # Run the built-in test
143
  success = test_my_personas()
 
137
  print("\nπŸ‘₯ Testing My Persona Database...")
138
 
139
  try:
140
+ from retriever import test_my_personas
141
 
142
  # Run the built-in test
143
  success = test_my_personas()
tools.py CHANGED
@@ -236,7 +236,7 @@ def create_persona_tool(llm=None):
236
  try:
237
  # Try to load the persona data first
238
  try:
239
- from my_retriever import get_persona_query_engine
240
  query_engine = get_persona_query_engine(llm=llm)
241
  except ImportError:
242
  # Fallback if my_retriever doesn't exist
 
236
  try:
237
  # Try to load the persona data first
238
  try:
239
+ from retriever import get_persona_query_engine
240
  query_engine = get_persona_query_engine(llm=llm)
241
  except ImportError:
242
  # Fallback if my_retriever doesn't exist