File size: 7,559 Bytes
f4325b0 795a09f 7bf46e1 7dfe08e bd17375 88ae537 7bf46e1 9b0b7f6 531d1d4 7bf46e1 531d1d4 d1aaa5c 1c7a9a0 929e339 ec0de3f bbba039 929e339 1c7a9a0 ee405d5 929e339 1c7a9a0 ee405d5 b49d1d5 929e339 d63189c ee405d5 90f4ddb 6614d6e 1c7a9a0 ee405d5 929e339 1c7a9a0 929e339 d63189c bbba039 ec0de3f b49d1d5 6614d6e 0538479 6614d6e d1aaa5c ee405d5 0ee0061 1c7a9a0 ee405d5 1c7a9a0 929e339 d63189c 254c111 929e339 254c111 ee405d5 f4325b0 b49d1d5 f4325b0 795a09f 7bf46e1 531d1d4 6614d6e ee405d5 795a09f 531d1d4 7dfe08e 7f9f8ca 7bf46e1 e017862 7bf46e1 254c111 f4325b0 7f9f8ca 7bf46e1 7f9f8ca 929e339 7f9f8ca 1c7a9a0 929e339 1c7a9a0 929e339 7bf46e1 f4325b0 7f9f8ca 7bf46e1 7f9f8ca 7bf46e1 7f9f8ca f4325b0 7f9f8ca 7bf46e1 7f9f8ca bd17375 1c7a9a0 bbba039 ec0de3f bbba039 ec0de3f bbba039 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | import mimetypes
import os
from typing import Annotated, TypedDict
import requests
from langchain_community.tools.google_serper import GoogleSerperResults
from langchain_core.messages import AnyMessage, HumanMessage
from langchain_openai import ChatOpenAI
from langchain_together import ChatTogether
from langgraph.graph import START, StateGraph
from langgraph.graph.message import add_messages
from langgraph.prebuilt import create_react_agent
import questions
from tools import (
GetYouTubeTranscript,
add,
categorize_foods,
check_commutativity,
divide,
execute_python_code,
find_wiki_page,
floor_divide,
get_text_file,
get_wiki_page_section_by_title,
handle_image,
load_excel_file,
modulus,
multiply,
power,
subtract,
sum_rows_cols,
)
tools = [
GetYouTubeTranscript(),
GoogleSerperResults(),
execute_python_code,
find_wiki_page,
get_wiki_page_section_by_title,
load_excel_file,
get_text_file,
add,
subtract,
multiply,
divide,
power,
modulus,
floor_divide,
sum_rows_cols,
check_commutativity,
categorize_foods,
handle_image,
]
mode = os.getenv("MODE", "free").lower()
model = (
ChatOpenAI(
model="gpt-4.1",
temperature=0.0,
max_retries=3,
)
if mode == "paid"
else ChatTogether(
model="meta-llama/Llama-3.3-70B-Instruct-Turbo-Free",
temperature=0.0,
max_retries=3,
)
).bind_tools(tools)
prompt = (
"You are an agent that will undergo the GAIA test.\n"
"You will be given a question and you will have to answer it using the tools provided.\n"
"Respond in the requested format, if specified, otherwise use the short answer style. I.E. Question: What is the capital of France? Answer: Paris.\n"
"When searching for information, always follow this specific sequence:\n"
" a. Use the 'google_serper_results_json' tool to search for relevant information.\n"
" Provide the query as input to the tool.\n"
" The tool will return a JSON string with the search results.\n"
" b. If the required information is not found in the search results, use the find_wiki_page tool to locate the relevant Wikipedia page.\n"
" The response will be a JSON string with the page's summary and sections.\n"
" c. If the Wikipedia page exists, and the summary does not contain the required info, use get_wiki_page_section_by_title tool to get the content of the desired section.\n"
"If the user asks for an Excel file, use the load_excel_file tool.\n"
"If you need to sum rows and columns in an Excel file, use the sum_rows_cols tool.\n"
"If the user provides a Python code snippet, follow this sequence:\n"
" a. Use the 'get_text_file' tool to download the code file if a file name or URL is provided.\n"
" b. Use the 'execute_python_code' tool to run the Python code.\n"
" Provide the code as input to the tool.\n"
"For reasoning tasks, adopt a step-by-step approach, but only output the final result.\n"
"Do not explain your reasoning, just provide the answer!\n"
"Do not generate any code, only use the tools provided!\n"
)
class GraphState(TypedDict):
messages: Annotated[list[AnyMessage], add_messages]
is_reverse: bool
file_name: str | None
question: questions.Question
def validate_state(state: GraphState):
if not state.get("question") or not state["question"].get("question"):
raise ValueError("Question is not provided or is empty.")
file_name = state["question"].get("file_name")
if file_name and not _is_supported_file_type(file_name):
raise ValueError(f"File name '{file_name}' is not a supported file type.")
return {
"messages": [HumanMessage(content=state["question"]["question"])],
"file_name": state["question"].get("file_name"),
}
def _is_supported_file_type(file_name: str) -> bool:
response = mimetypes.guess_type(file_name)
if (
not response
or not response[0]
or any(response[0].startswith(prefix) for prefix in ["audio", "video"])
):
return False
return True
log_level = os.getenv("LOG_LEVEL", "info").lower()
react_agent = create_react_agent(
model=model,
tools=tools,
prompt=prompt,
debug=log_level == "debug",
)
def detect_reverse(state: GraphState):
question = state["messages"][-1]
model_ = ChatTogether(
model="meta-llama/Llama-3.3-70B-Instruct-Turbo-Free",
temperature=0,
max_tokens=2000,
max_retries=3,
)
prompt = f"""Detect if the following text is written backwards.
If it is, respond with "yes", otherwise respond with "no".
Question: {question.content}"""
response = model_.invoke(
[
HumanMessage(content=prompt),
]
)
return {
"messages": [question],
"is_reverse": response.content.strip().lower() == "yes",
}
def prepare(state: GraphState):
question = state["messages"][-1].content
print(f"Preparing question: {question}")
if state.get("is_reverse", False):
print("Question is in reverse order, reversing it...")
question = question[::-1]
file_name = state.get("file_name")
if file_name:
return {
"messages": [
HumanMessage(content=f"Question: {question}\nFile name: {file_name}")
],
}
return {
"messages": [HumanMessage(content=question)],
}
graph = StateGraph(GraphState)
graph.add_node("validate_state", validate_state)
graph.add_node(
"detect_reverse",
detect_reverse,
)
graph.add_node(
"prepare",
prepare,
)
graph.add_node("react", react_agent)
graph.add_edge(START, "validate_state")
graph.add_edge("validate_state", "detect_reverse")
graph.add_edge("detect_reverse", "prepare")
graph.add_edge("prepare", "react")
agent = graph.compile().with_retry(
retry_if_exception_type=(requests.HTTPError,),
wait_exponential_jitter=True,
stop_after_attempt=3,
exponential_jitter_params={
"initial": 5.0,
"max": 60.0,
},
)
if __name__ == "__main__":
question = {
"task_id": "3cef3a44-215e-4aed-8e3b-b1e3f08063b7",
"question": "I'm making a grocery list for my mom, but she's a professor of botany and she's a real stickler when it comes to categorizing things. I need to add different foods to different categories on the grocery list, but if I make a mistake, she won't buy anything inserted in the wrong category. Here's the list I have so far:\n\nmilk, eggs, flour, whole bean coffee, Oreos, sweet potatoes, fresh basil, plums, green beans, rice, corn, bell pepper, whole allspice, acorns, broccoli, celery, zucchini, lettuce, peanuts\n\nI need to make headings for the fruits and vegetables. Could you please create a list of just the vegetables from my list? If you could do that, then I can figure out how to categorize the rest of the list into the appropriate categories. But remember that my mom is a real stickler, so make sure that no botanical fruits end up on the vegetable list, or she won't get them when she's at the store. Please alphabetize the list of vegetables, and place each item in a comma separated list.",
"file_name": "",
}
try:
result = agent.invoke(
{
"question": question,
}
)
print("Result:", result["messages"][-1].content)
except Exception as e:
print(f"Error: {e}")
|