Spaces:
Sleeping
Sleeping
Keeping the prompt
Browse files- app.py +27 -34
- 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
|
|
|
|
| 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 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 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 |
-
|
| 117 |
-
|
| 118 |
-
|
| 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
|
|
|
|
|
|