from langchain_google_genai import ChatGoogleGenerativeAI from tools import tools from langgraph.prebuilt import ToolNode, tools_condition from langgraph.graph import START, StateGraph, MessagesState from langchain_core.messages import HumanMessage import requests, base64 import os sys_prompt=""" You are a helpful assistant tasked with answering questions using a set of tools. Now, I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. 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. Your answer should only start with "FINAL ANSWER: ", then follows with the answer. """ class MyAgent: def __init__(self, hf_api_url: str): api_key = os.environ.get("GOOGLE_API_KEY") # self.llm = ChatGoogleGenerativeAI(model="gemini-2.5-flash-preview-04-17", temperature=0) self.llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash", temperature=0.1, google_api_key=api_key) self.llm_with_tools = self.llm.bind_tools(tools) self.hf_api_url = hf_api_url def instructor(state: MessagesState): """Instructor node""" return {"messages": [sys_prompt] + state["messages"]} def assistant(state: MessagesState): """Assistant node""" return {"messages": [self.llm_with_tools.invoke(state["messages"])]} builder = StateGraph(MessagesState) builder.add_node("instructor", instructor) builder.add_node("assistant", assistant) builder.add_node("tools", ToolNode(tools)) builder.add_edge(START, "instructor") builder.add_edge("instructor", "assistant") builder.add_conditional_edges( "assistant", tools_condition, ) builder.add_edge("tools", "assistant") self.graph = builder.compile() def __call__(self, question_item: dict) -> str: question = question_item.get("question") task_id = question_item.get("task_id") file_name = question_item.get("file_name") print(f"Agent received question: {question_item}") content = [{"type": "text", "text": question}] if file_name: print(f" [+] Attached file: {file_name}") file_url = f"{self.hf_api_url}/files/{task_id}" if file_name.endswith(".png"): content.append({"type": "image_url", "image_url": file_url}) else: resp_file = requests.get(file_url) encoded_file = base64.b64encode(resp_file.content).decode("utf-8") mime_type='audio/mp3' if file_name.endswith(".py"): mime_type='"application/x-python, text/x-python"' elif file_name.endswith(".xlsx"): mime_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' content.append( { "type": "media", "data": encoded_file, "mime_type": mime_type, } ) messages = [HumanMessage(content)] print(f"Agent messages: \n--- --- ---\n{messages}\n--- --- ---\n") response = self.graph.invoke({"messages": messages}) print(f"Graph response: {response['messages']}") response_text = response['messages'][-1].content print(f"Agent tmp response: \n--- --- ---\n{response_text}\n--- --- ---\n") if "FINAL ANSWER:" in response_text: buff = response_text.split("FINAL ANSWER:") response_text = buff[1].strip() print(f"Agent FINAL response: {response_text}") return response_text