dawid-lorek commited on
Commit
e836bd4
·
verified ·
1 Parent(s): e103f3d

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +21 -7
agent.py CHANGED
@@ -1,6 +1,7 @@
1
- # agent.py — final version with working ReActAgent
2
 
3
  import asyncio
 
4
  from llama_index.llms.openai import OpenAI
5
  from llama_index.core.tools import FunctionTool
6
  from llama_index.core.agent.react.base import ReActAgent
@@ -9,6 +10,12 @@ from langchain_community.tools import DuckDuckGoSearchRun, WikipediaQueryRun
9
  from langchain_experimental.tools.python.tool import PythonREPLTool
10
  from langchain_community.document_loaders import YoutubeLoader
11
 
 
 
 
 
 
 
12
  # Tool wrappers
13
  def search_duckduckgo(query: str) -> str:
14
  return DuckDuckGoSearchRun().run(query)
@@ -36,26 +43,33 @@ llm = OpenAI(model="gpt-4")
36
  agent = ReActAgent.from_tools(
37
  tools=TOOLS,
38
  llm=llm,
39
- verbose=False,
40
  system_prompt="""
41
  You are an expert AI assistant participating in the GAIA benchmark.
42
 
43
  Your task is to answer questions as precisely and exactly as possible. Each answer is evaluated automatically—strict formatting matters.
44
 
45
  Rules:
46
- 1. Output ONLY the final answer—NO explanation or commentary.
47
  2. Format exactly as requested (lists, names, numbers, chess moves, currency).
48
- 3. Use tools as needed—no guessing.
49
- """,
50
  )
51
 
 
52
  def answer_question_sync(question: str) -> str:
53
  try:
54
  response = agent.chat(question)
55
- return response.response.content.strip()
 
 
 
 
 
56
  except Exception as e:
 
57
  return f"[ERROR] {e}"
58
 
 
59
  async def answer_question(question: str) -> str:
60
- # wrap synchronous call in asyncio to keep compatibility
61
  return answer_question_sync(question)
 
1
+ # agent.py — stable ReActAgent version with API key check and error logging
2
 
3
  import asyncio
4
+ import os
5
  from llama_index.llms.openai import OpenAI
6
  from llama_index.core.tools import FunctionTool
7
  from llama_index.core.agent.react.base import ReActAgent
 
10
  from langchain_experimental.tools.python.tool import PythonREPLTool
11
  from langchain_community.document_loaders import YoutubeLoader
12
 
13
+ # Optional log check for Hugging Face Secrets
14
+ if os.getenv("OPENAI_API_KEY"):
15
+ print("✅ Detected OPENAI_API_KEY in environment")
16
+ else:
17
+ print("⚠️ Missing OPENAI_API_KEY — LLM may fail")
18
+
19
  # Tool wrappers
20
  def search_duckduckgo(query: str) -> str:
21
  return DuckDuckGoSearchRun().run(query)
 
43
  agent = ReActAgent.from_tools(
44
  tools=TOOLS,
45
  llm=llm,
46
+ verbose=True,
47
  system_prompt="""
48
  You are an expert AI assistant participating in the GAIA benchmark.
49
 
50
  Your task is to answer questions as precisely and exactly as possible. Each answer is evaluated automatically—strict formatting matters.
51
 
52
  Rules:
53
+ 1. Output ONLY the final answer no explanation or commentary.
54
  2. Format exactly as requested (lists, names, numbers, chess moves, currency).
55
+ 3. Use tools as needed no guessing.
56
+ """
57
  )
58
 
59
+ # Safe sync wrapper
60
  def answer_question_sync(question: str) -> str:
61
  try:
62
  response = agent.chat(question)
63
+ if hasattr(response, "response") and hasattr(response.response, "content"):
64
+ return response.response.content.strip()
65
+ elif isinstance(response, str):
66
+ return response.strip()
67
+ else:
68
+ return str(response)
69
  except Exception as e:
70
+ print(f"❌ Exception while answering: {e}")
71
  return f"[ERROR] {e}"
72
 
73
+ # Async wrapper for FastAPI/Gradio
74
  async def answer_question(question: str) -> str:
 
75
  return answer_question_sync(question)