Spaces:
Sleeping
Sleeping
Nguyen Nhu Trung
commited on
Commit
·
8d1f665
1
Parent(s):
1bf4291
update prompt and tools
Browse files
agent.py
CHANGED
|
@@ -2,6 +2,8 @@ import os
|
|
| 2 |
from dotenv import load_dotenv
|
| 3 |
import pandas as pd
|
| 4 |
import json
|
|
|
|
|
|
|
| 5 |
from langgraph.graph import START, StateGraph, MessagesState
|
| 6 |
from langgraph.prebuilt import tools_condition
|
| 7 |
from langgraph.prebuilt import ToolNode
|
|
@@ -42,6 +44,32 @@ tavily_search_tool = TavilySearch(
|
|
| 42 |
# Initialize Tavily Extract Tool
|
| 43 |
tavily_extract_tool = TavilyExtract()
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
@tool
|
| 47 |
def read_excel_file(file_name: str) -> str:
|
|
@@ -96,7 +124,7 @@ def wiki_search(query: str) -> str:
|
|
| 96 |
return {"wiki_results": formatted_search_docs}
|
| 97 |
|
| 98 |
@tool
|
| 99 |
-
def
|
| 100 |
"""Solve logic or math problem.
|
| 101 |
|
| 102 |
Args:
|
|
@@ -132,6 +160,7 @@ def solve_logic_or_math_problem(problem: str) -> str:
|
|
| 132 |
|
| 133 |
system_prompt = """
|
| 134 |
You are a helpful assistant tasked with answering questions using a set of tools.
|
|
|
|
| 135 |
Now, I will ask you a question. Report your thoughts, and finish your answer with the following template:
|
| 136 |
FINAL ANSWER: [YOUR FINAL ANSWER].
|
| 137 |
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
|
|
@@ -141,13 +170,15 @@ sys_msg = SystemMessage(content=system_prompt)
|
|
| 141 |
|
| 142 |
|
| 143 |
tools = [
|
|
|
|
| 144 |
wiki_search,
|
|
|
|
| 145 |
tavily_search_tool,
|
| 146 |
tavily_extract_tool,
|
| 147 |
repl_tool,
|
| 148 |
read_excel_file,
|
| 149 |
transcribe_audio,
|
| 150 |
-
|
| 151 |
]
|
| 152 |
|
| 153 |
|
|
|
|
| 2 |
from dotenv import load_dotenv
|
| 3 |
import pandas as pd
|
| 4 |
import json
|
| 5 |
+
import base64
|
| 6 |
+
|
| 7 |
from langgraph.graph import START, StateGraph, MessagesState
|
| 8 |
from langgraph.prebuilt import tools_condition
|
| 9 |
from langgraph.prebuilt import ToolNode
|
|
|
|
| 44 |
# Initialize Tavily Extract Tool
|
| 45 |
tavily_extract_tool = TavilyExtract()
|
| 46 |
|
| 47 |
+
@tool
|
| 48 |
+
def describe_image(file_name: str) -> str:
|
| 49 |
+
"""Describe the image.
|
| 50 |
+
|
| 51 |
+
Args:
|
| 52 |
+
file_name: name of image file
|
| 53 |
+
"""
|
| 54 |
+
with open(file_name, "rb") as image_file:
|
| 55 |
+
encoded_image = base64.b64encode(image_file.read()).decode("utf-8")
|
| 56 |
+
message_local = HumanMessage(
|
| 57 |
+
content=[
|
| 58 |
+
{"type": "text", "text": "Describe the local image."},
|
| 59 |
+
{"type": "image_url", "image_url": f"data:image/png;base64,{encoded_image}"},
|
| 60 |
+
]
|
| 61 |
+
)
|
| 62 |
+
llm = ChatGoogleGenerativeAI(
|
| 63 |
+
model="gemini-2.0-flash",
|
| 64 |
+
temperature=0.1,
|
| 65 |
+
max_tokens=None,
|
| 66 |
+
timeout=None,
|
| 67 |
+
max_retries=2,
|
| 68 |
+
# other params...
|
| 69 |
+
)
|
| 70 |
+
result_local = llm.invoke([message_local])
|
| 71 |
+
return "Response for local image: {result_local.content}"
|
| 72 |
+
|
| 73 |
|
| 74 |
@tool
|
| 75 |
def read_excel_file(file_name: str) -> str:
|
|
|
|
| 124 |
return {"wiki_results": formatted_search_docs}
|
| 125 |
|
| 126 |
@tool
|
| 127 |
+
def solve_math_problem(problem: str) -> str:
|
| 128 |
"""Solve logic or math problem.
|
| 129 |
|
| 130 |
Args:
|
|
|
|
| 160 |
|
| 161 |
system_prompt = """
|
| 162 |
You are a helpful assistant tasked with answering questions using a set of tools.
|
| 163 |
+
If the question is related to math or logic or a puzzle, ALWAYS USE a tool and NOT trying to answer by yourself.
|
| 164 |
Now, I will ask you a question. Report your thoughts, and finish your answer with the following template:
|
| 165 |
FINAL ANSWER: [YOUR FINAL ANSWER].
|
| 166 |
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
|
|
|
|
| 170 |
|
| 171 |
|
| 172 |
tools = [
|
| 173 |
+
solve_math_problem,
|
| 174 |
wiki_search,
|
| 175 |
+
describe_image,
|
| 176 |
tavily_search_tool,
|
| 177 |
tavily_extract_tool,
|
| 178 |
repl_tool,
|
| 179 |
read_excel_file,
|
| 180 |
transcribe_audio,
|
| 181 |
+
|
| 182 |
]
|
| 183 |
|
| 184 |
|
app.py
CHANGED
|
@@ -79,11 +79,12 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 79 |
for item in questions_data:
|
| 80 |
task_id = item.get("task_id")
|
| 81 |
question_text = item.get("question")
|
|
|
|
| 82 |
if not task_id or question_text is None:
|
| 83 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 84 |
continue
|
| 85 |
try:
|
| 86 |
-
submitted_answer = agent(question_text)
|
| 87 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 88 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 89 |
except Exception as e:
|
|
|
|
| 79 |
for item in questions_data:
|
| 80 |
task_id = item.get("task_id")
|
| 81 |
question_text = item.get("question")
|
| 82 |
+
file_name = item.get("file_name")
|
| 83 |
if not task_id or question_text is None:
|
| 84 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 85 |
continue
|
| 86 |
try:
|
| 87 |
+
submitted_answer = agent(question_text + f"(file_name: '{file_name}')" if len(file_name)>0 else "")
|
| 88 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 89 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 90 |
except Exception as e:
|