cowrycode commited on
Commit
238ce25
·
verified ·
1 Parent(s): 8a98cbf

Upload my_agent.py

Browse files
Files changed (1) hide show
  1. langgraph_impl/my_agent.py +94 -0
langgraph_impl/my_agent.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from langchain.agents import initialize_agent, AgentType
3
+ from langchain.tools import Tool
4
+ #from langchain_community.llms import OpenAI
5
+ from tools.wikipedia_tool import wiki_search
6
+ from tools.vegetable_classifier_tool import vegetable_classifier_2022
7
+ from tools.vegetable_classifier_tool import vegetable_classifier_2022
8
+ from tools.excel_sum_tool import excel_food_sales_sum
9
+ from tools.audio_transcriber import transcribe_audio
10
+ from tools.file_parser import parse_file_and_summarize
11
+ from tools.image_chess_solver import solve_chess_image
12
+ from tools.youtube_tool import extract_video_id, get_youtube_transcript
13
+ from langchain.agents.agent import AgentExecutor
14
+ from langchain_community.chat_models import ChatOpenAI
15
+
16
+ def create_langchain_agent() -> AgentExecutor:
17
+ llm = ChatOpenAI(
18
+ model_name="gpt-4o",
19
+ temperature=0.1,
20
+ openai_api_key=os.getenv("OPENAI_API_KEY"),
21
+ )
22
+
23
+ tools = [
24
+ Tool(name="wikipedia_search", func=wiki_search, description="Use to get factual info from Wikipedia.", return_direct=False),
25
+ Tool(name="youtube_transcript", func=get_youtube_transcript, description="Extract transcripts from YouTube videos.", return_direct=False),
26
+ Tool(name="audio_transcriber", func=transcribe_audio, description="Transcribe uploaded audio files."),
27
+ Tool(name="chess_image_solver", func=solve_chess_image, description="Solve chess puzzles from images."),
28
+ Tool(name="file_parser", func=parse_file_and_summarize, description="Parse and analyze Excel or CSV files."),
29
+ 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."),
30
+ Tool(name="excel_food_sales_sum", func=excel_food_sales_sum, description="Parse uploaded Excel file and return total food-related sales."),
31
+ ]
32
+
33
+ agent_kwargs = {
34
+ "prefix": (
35
+ "You are a helpful AI assistant completing GAIA benchmark tasks.\n"
36
+ "You MUST use the tools provided to answer the user's question. Do not answer from your own knowledge.\n"
37
+ "Carefully analyze the question to determine the most appropriate tool to use.\n"
38
+ "Here are guidelines for using the tools:\n"
39
+ "- Use 'wikipedia_search' to find factual information about topics, events, people, etc. (e.g., 'Use wikipedia_search to find the population of France').\n"
40
+ "- 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"
41
+ "- Use 'audio_transcriber' to transcribe uploaded audio files. (e.g., 'Use audio_transcriber to get the text from this audio recording').\n"
42
+ "- 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"
43
+ "- 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"
44
+ "- 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"
45
+ "- 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"
46
+ "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"
47
+ ),
48
+ "suffix": (
49
+ "Use the tools to research or calculate the answer.\n"
50
+ "If a tool fails, explain the reason for the failure instead of hallucinating an answer.\n"
51
+ "Provide concise and direct answers as requested in the questions. Do not add extra information unless explicitly asked for.\n"
52
+ "For example, if asked for a number, return only the number. If asked for a list, return only the list.\n"
53
+ ),
54
+ "input_variables": ["input", "agent_scratchpad"]
55
+ }
56
+
57
+ agent = initialize_agent(
58
+ tools=tools,
59
+ llm=llm,
60
+ agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION,
61
+ handle_parsing_errors=True,
62
+ verbose=True,
63
+ max_iterations=10,
64
+ max_execution_time=60,
65
+ agent_kwargs=agent_kwargs # Place the agent_kwargs here
66
+ )
67
+
68
+ return agent
69
+
70
+
71
+ '''
72
+ agent = initialize_agent(
73
+ tools=tools,
74
+ llm=llm,
75
+ agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
76
+ handle_parsing_errors=True,
77
+ verbose=True,
78
+ max_iterations=10,
79
+ max_execution_time=60,
80
+ agent_kwargs={
81
+ "prefix": (
82
+ "You are a helpful AI assistant completing GAIA benchmark tasks.\n"
83
+ "You must ALWAYS use the tools provided to answer the user's question.\n"
84
+ "Do NOT guess or respond directly from your own knowledge.\n"
85
+ ),
86
+ "suffix": (
87
+ "Use the tools to research or calculate the answer.\n"
88
+ "If a tool fails, explain that instead of hallucinating an answer.\n"
89
+ "Return only the final answer as a short response (e.g., a number, name, or sentence).\n"
90
+ ),
91
+ }
92
+ )
93
+ '''
94
+ return agent