ana-35 commited on
Commit
0dd0640
·
1 Parent(s): 8c360c1

Keeping the prompt

Browse files
Files changed (2) hide show
  1. app.py +27 -34
  2. requirements.txt +0 -2
app.py CHANGED
@@ -3,9 +3,11 @@ import gradio as gr
3
  import requests
4
  import pandas as pd
5
  from dotenv import load_dotenv
6
- from langchain_openai import ChatOpenAI
 
7
  from langchain.agents import create_react_agent, AgentExecutor
8
  from langchain_core.prompts import PromptTemplate
 
9
  from tools.web_searcher import web_search_tool
10
  from tools.calculator import calculator_tool
11
  from tools.file_reader import read_file_tool
@@ -25,27 +27,30 @@ tools = [
25
  code_reviewer,
26
  web_scraper_tool
27
  ]
28
- tool_names = ", ".join([tool.name for tool in tools])
29
 
30
  # === Model ===
31
  model = ChatOpenAI(model="gpt-4o", api_key=os.getenv("OPENAI_API_KEY"))
32
 
33
  # === ReAct Prompt ===
34
- template = """
35
- You are an expert AI assistant, capable of using tools when necessary to answer questions. Follow these strict rules:
36
-
37
- 1. For web-based questions (e.g., Wikipedia, IOC, search), use web_search_tool or web_scraper_tool.
38
- 2. For file-based questions (e.g., Excel, PDF, JSON), use file_reader.
39
- 3. For math or calculation questions, use calculator.
40
- 4. For code-related questions, use code_review.
41
- 5. If the question requires multiple tools, combine them logically.
42
- 6. Always answer exactly in the format requested (e.g., single name, city, number, or code). No explanations or extra text.
43
- 7. If you cannot answer, say: "No answer found."
44
- 8. Do not guess. Always verify using tools when available.
45
- 9. For YouTube/video links, use web_scraper_tool to access the content.
46
- 10. Follow the structure below to reason step by step.
47
-
48
- Available tools: {tools}
 
 
 
 
49
 
50
  Use the following format:
51
 
@@ -61,8 +66,7 @@ Final Answer: the final answer to the original input question
61
  Begin!
62
 
63
  Question: {input}
64
- {agent_scratchpad}
65
- """
66
 
67
  react_prompt = PromptTemplate.from_template(template)
68
 
@@ -99,24 +103,13 @@ def process_question(profile: gr.OAuthProfile | None):
99
 
100
  try:
101
  result = agent_executor.invoke({"input": question_text})
102
- output_text = result.get("output") if isinstance(result, dict) else result
103
- final_answer = None
104
- if isinstance(output_text, str):
105
- for line in output_text.strip().split("\n"):
106
- if line.strip().lower().startswith("final answer:"):
107
- final_answer = line.split(":", 1)[1].strip()
108
- break
109
- if not final_answer:
110
- final_answer = "No answer found."
111
-
112
  answers_payload.append({"task_id": task_id, "submitted_answer": final_answer})
113
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": final_answer})
114
-
115
  except Exception as e:
116
- error_msg = f"[ERROR: {e}]"
117
- answers_payload.append({"task_id": task_id, "submitted_answer": error_msg})
118
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": error_msg})
119
- time.sleep(1) # Rate limit safety
120
 
121
  submission_data = {"username": username, "agent_code": agent_code, "answers": answers_payload}
122
 
 
3
  import requests
4
  import pandas as pd
5
  from dotenv import load_dotenv
6
+ from langchain import hub
7
+ from langchain_openai import ChatOpenAI
8
  from langchain.agents import create_react_agent, AgentExecutor
9
  from langchain_core.prompts import PromptTemplate
10
+ from langchain_core.messages import AIMessage, HumanMessage
11
  from tools.web_searcher import web_search_tool
12
  from tools.calculator import calculator_tool
13
  from tools.file_reader import read_file_tool
 
27
  code_reviewer,
28
  web_scraper_tool
29
  ]
 
30
 
31
  # === Model ===
32
  model = ChatOpenAI(model="gpt-4o", api_key=os.getenv("OPENAI_API_KEY"))
33
 
34
  # === ReAct Prompt ===
35
+ template = '''Answer the following questions as best you can. You have access to the following tools:
36
+
37
+ {tools}
38
+
39
+ Strict rules to follow:
40
+ 1️⃣ Use tools when needed: web_search_tool for external information, file_reader for file data, calculator for math, code_reviewer for code questions, web_scraper for web content.
41
+ 2️⃣ Combine tools logically for multi-step problems.
42
+ 3️⃣ Format answers exactly as requested: single name, city, code, or number—no extra text.
43
+ 4️⃣ If a link is provided, use web_search_tool or web_scraper to extract the information.
44
+ 5️⃣ Do not guess; if information is unavailable, say: 'No answer found.'
45
+ 6️⃣ Be precise, factual, and avoid hallucination. Verify using tools.
46
+
47
+ Use the following format for every question:
48
+
49
+ Question: the input question you must answer
50
+ Thought: reasoning about what to do
51
+ Action: the action to take, one of [{tool_names}]
52
+ Action Input: the input to the action
53
+ Observation: the result of the action
54
 
55
  Use the following format:
56
 
 
66
  Begin!
67
 
68
  Question: {input}
69
+ Thought:{agent_scratchpad}'''
 
70
 
71
  react_prompt = PromptTemplate.from_template(template)
72
 
 
103
 
104
  try:
105
  result = agent_executor.invoke({"input": question_text})
106
+ final_answer = result["output"] if isinstance(result, dict) else result
 
 
 
 
 
 
 
 
 
107
  answers_payload.append({"task_id": task_id, "submitted_answer": final_answer})
108
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": final_answer})
 
109
  except Exception as e:
110
+ answers_payload.append({"task_id": task_id, "submitted_answer": f"[ERROR: {e}]"})
111
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"[ERROR: {e}]"})
112
+ time.sleep(1) # slight delay to avoid rate limits
 
113
 
114
  submission_data = {"username": username, "agent_code": agent_code, "answers": answers_payload}
115
 
requirements.txt CHANGED
@@ -8,5 +8,3 @@ langchain_openai
8
  langchain_community
9
  PyPDF2
10
  beautifulsoup4
11
- langchain-openai
12
- langchain-core
 
8
  langchain_community
9
  PyPDF2
10
  beautifulsoup4