Spaces:
Running
Running
import os | |
from langchain.agents import initialize_agent, AgentType | |
from langchain.tools import Tool | |
#from langchain_community.llms import OpenAI | |
from tools.wikipedia_tool import wiki_search | |
from tools.vegetable_classifier_tool import vegetable_classifier_2022 | |
from tools.vegetable_classifier_tool import vegetable_classifier_2022 | |
from tools.excel_sum_tool import excel_food_sales_sum | |
from tools.audio_transcriber import transcribe_audio | |
from tools.file_parser import parse_file_and_summarize | |
from tools.image_chess_solver import solve_chess_image | |
from tools.youtube_tool import extract_video_id, get_youtube_transcript | |
from langchain.agents.agent import AgentExecutor | |
from langchain_community.chat_models import ChatOpenAI | |
def create_langchain_agent() -> AgentExecutor: | |
llm = ChatOpenAI( | |
model_name="gpt-4o", | |
temperature=0.1, | |
openai_api_key=os.getenv("OPENAI_API_KEY"), | |
) | |
tools = [ | |
Tool(name="wikipedia_search", func=wiki_search, description="Use to get factual info from Wikipedia.", return_direct=False), | |
Tool(name="youtube_transcript", func=get_youtube_transcript, description="Extract transcripts from YouTube videos.", return_direct=False), | |
Tool(name="audio_transcriber", func=transcribe_audio, description="Transcribe uploaded audio files."), | |
Tool(name="chess_image_solver", func=solve_chess_image, description="Solve chess puzzles from images."), | |
Tool(name="file_parser", func=parse_file_and_summarize, description="Parse and analyze Excel or CSV files."), | |
Tool(name="vegetable_classifier_2022", func=vegetable_classifier_2022, description="Classify and extract only vegetables, excluding botanical fruits, based on a comma-separated list of food items."), | |
Tool(name="excel_food_sales_sum", func=excel_food_sales_sum, description="Parse uploaded Excel file and return total food-related sales."), | |
] | |
agent_kwargs = { | |
"prefix": ( | |
"You are a helpful AI assistant completing GAIA benchmark tasks.\n" | |
"You MUST use the tools provided to answer the user's question. Do not answer from your own knowledge.\n" | |
"Carefully analyze the question to determine the most appropriate tool to use.\n" | |
"Here are guidelines for using the tools:\n" | |
"- Use 'wikipedia_search' to find factual information about topics, events, people, etc. (e.g., 'Use wikipedia_search to find the population of France').\n" | |
"- Use 'youtube_transcript' to extract transcripts from YouTube videos when the question requires understanding the video content. (e.g., 'Use youtube_transcript to summarize the key points of this video').\n" | |
"- Use 'audio_transcriber' to transcribe uploaded audio files. (e.g., 'Use audio_transcriber to get the text from this audio recording').\n" | |
"- Use 'chess_image_solver' to analyze and solve chess puzzles from images. (e.g., 'Use chess_image_solver to determine the best move in this chess position').\n" | |
"- Use 'file_parser' to parse and analyze data from Excel or CSV files. (e.g., 'Use file_parser to calculate the average sales from this data').\n" | |
"- Use 'vegetable_classifier_2022' to classify a list of food items and extract only the vegetables. (e.g., 'Use vegetable_classifier_2022 to get a list of the vegetables in this grocery list').\n" | |
"- Use 'excel_food_sales_sum' to extract total food sales from excel files. (e.g., 'Use excel_food_sales_sum to calculate the total food sales').\n" | |
"Do NOT guess or make up answers. If a tool cannot provide the answer, truthfully respond that you were unable to find the information.\n" | |
), | |
"suffix": ( | |
"Use the tools to research or calculate the answer.\n" | |
"If a tool fails, explain the reason for the failure instead of hallucinating an answer.\n" | |
"Provide concise and direct answers as requested in the questions. Do not add extra information unless explicitly asked for.\n" | |
"For example, if asked for a number, return only the number. If asked for a list, return only the list.\n" | |
), | |
"input_variables": ["input", "agent_scratchpad"] | |
} | |
agent = initialize_agent( | |
tools=tools, | |
llm=llm, | |
agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION, | |
handle_parsing_errors=True, | |
verbose=True, | |
max_iterations=10, | |
max_execution_time=60, | |
agent_kwargs=agent_kwargs # Place the agent_kwargs here | |
) | |
return agent | |
''' | |
agent = initialize_agent( | |
tools=tools, | |
llm=llm, | |
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, | |
handle_parsing_errors=True, | |
verbose=True, | |
max_iterations=10, | |
max_execution_time=60, | |
agent_kwargs={ | |
"prefix": ( | |
"You are a helpful AI assistant completing GAIA benchmark tasks.\n" | |
"You must ALWAYS use the tools provided to answer the user's question.\n" | |
"Do NOT guess or respond directly from your own knowledge.\n" | |
), | |
"suffix": ( | |
"Use the tools to research or calculate the answer.\n" | |
"If a tool fails, explain that instead of hallucinating an answer.\n" | |
"Return only the final answer as a short response (e.g., a number, name, or sentence).\n" | |
), | |
} | |
) | |
''' | |
return agent | |