cgoncalves commited on
Commit
480a5f3
·
verified ·
1 Parent(s): a65f0b8

Update agents.py

Browse files
Files changed (1) hide show
  1. agents.py +133 -123
agents.py CHANGED
@@ -1,4 +1,3 @@
1
- import os
2
  import re
3
  from datetime import datetime
4
  from typing import Annotated
@@ -28,19 +27,6 @@ from prompts import WEB_SEARCH_PROMPT, YOUTUBE_PROMPT, MULTIMODAL_PROMPT
28
  # Load environment variables from .env file
29
  load_dotenv()
30
 
31
- # Initialize OpenAI LLM (gpt-4o) for general and web search tasks
32
- openai_llm = ChatOpenAI(
33
- model="gpt-4o",
34
- use_responses_api=True,
35
- api_key=os.getenv("OPENAI_API_KEY")
36
- )
37
-
38
- # Initialize Google Gemini LLM for YouTube and multimodal tasks
39
- google_llm = ChatGoogleGenerativeAI(
40
- model="gemini-2.5-flash-preview-04-17",
41
- google_api_key=os.getenv("GOOGLE_API_KEY"),
42
- )
43
-
44
  class AgentState(MessagesState):
45
  """
46
  State class for agent workflows, tracks the message history.
@@ -85,133 +71,157 @@ def youtube_transcript(video_url: str, raw: bool = False) -> str:
85
  except Exception as e:
86
  return f"An error occurred while fetching the transcript: {e}"
87
 
88
- # List of available tools for the agent (currently only YouTube transcript)
89
- tools = [youtube_transcript]
90
-
91
- def create_web_search_graph() -> StateGraph:
92
  """
93
- Create the web search agent graph.
 
 
 
 
94
 
95
  Returns:
96
- StateGraph: The compiled web search agent workflow.
97
  """
98
- web_search_preview = [{"type": "web_search_preview"}]
99
- # Bind the web search tool to the OpenAI LLM
100
- llm_with_tools = openai_llm.bind_tools(web_search_preview)
 
 
 
101
 
102
- def agent_node(state: AgentState) -> dict:
103
- """
104
- Node function for handling web search queries.
 
 
105
 
106
- Args:
107
- state (AgentState): The current agent state.
 
 
 
108
 
109
  Returns:
110
- dict: Updated state with the LLM response.
111
  """
112
- current_date = datetime.now().strftime("%B %d, %Y")
113
- # Format the system prompt with the current date
114
- system_message = SystemMessage(content=WEB_SEARCH_PROMPT.format(current_date=current_date))
115
- # Re-bind tools for each invocation (defensive)
116
  web_search_preview = [{"type": "web_search_preview"}]
117
- response = llm_with_tools.bind_tools(web_search_preview).invoke(
118
- [system_message] + state.get("messages")
119
- )
120
- return {"messages": state.get("messages") + [response]}
121
-
122
- # Build the workflow graph
123
- workflow = StateGraph(AgentState)
124
- workflow.add_node("agent", agent_node)
125
- workflow.add_edge(START, "agent")
126
- workflow.add_edge("agent", END)
127
- return workflow.compile(name="web_search_agent")
128
-
129
- def create_youtube_viwer_graph() -> StateGraph:
130
- """
131
- Create the YouTube viewer agent graph.
132
-
133
- Returns:
134
- StateGraph: The compiled YouTube viewer agent workflow.
135
- """
136
- def agent_node(state: AgentState) -> dict:
 
 
 
 
 
 
 
 
 
 
 
137
  """
138
- Node function for handling YouTube-related queries.
139
-
140
- Args:
141
- state (AgentState): The current agent state.
142
 
143
  Returns:
144
- dict: Updated state with the LLM response.
145
  """
146
- current_date = datetime.now().strftime("%B %d, %Y")
147
- # Format the system prompt with the current date
148
- system_message = SystemMessage(content=YOUTUBE_PROMPT.format(current_date=current_date))
149
- # Bind the YouTube transcript tool to the Gemini LLM
150
- llm_with_tools = google_llm.bind_tools(tools)
151
- response = llm_with_tools.invoke([system_message] + state.get("messages"))
152
- return {"messages": state.get("messages") + [response]}
153
-
154
- # Build the workflow graph with tool node and conditional routing
155
- workflow = StateGraph(AgentState)
156
- workflow.add_node("llm", agent_node)
157
- workflow.add_node("tools", ToolNode(tools))
158
- workflow.set_entry_point("llm")
159
- workflow.add_conditional_edges(
160
- "llm",
161
- tools_condition,
162
- {
163
- "tools": "tools", # If tool is needed, go to tools node
164
- "__end__": END, # Otherwise, end the workflow
165
- },
166
- )
167
- workflow.add_edge("tools", "llm") # After tool, return to LLM node
168
- return workflow.compile(name="youtube_viwer_agent")
169
-
170
- def create_multimodal_agent_graph() -> StateGraph:
171
- """
172
- Create the multimodal agent graph using Gemini for best multimodal support.
 
 
 
 
 
 
173
 
174
- Returns:
175
- StateGraph: The compiled multimodal agent workflow.
176
- """
177
- def agent_node(state: AgentState) -> dict:
178
  """
179
- Node function for handling multimodal queries.
180
-
181
- Args:
182
- state (AgentState): The current agent state.
183
 
184
  Returns:
185
- dict: Updated state with the LLM response.
186
  """
187
- current_date = datetime.now().strftime("%B %d, %Y")
188
- # Compose the system message with the multimodal prompt and current date
189
- system_message = SystemMessage(content=MULTIMODAL_PROMPT + f" Today's date: {current_date}.")
190
- messages = [system_message] + state.get("messages")
191
- # Invoke Gemini LLM for multimodal reasoning
192
- response = google_llm.invoke(messages)
193
- return {"messages": state.get("messages") + [response]}
194
-
195
- # Build the workflow graph
196
- workflow = StateGraph(AgentState)
197
- workflow.add_node("agent", agent_node)
198
- workflow.add_edge(START, "agent")
199
- workflow.add_edge("agent", END)
200
- return workflow.compile(name="multimodal_agent")
201
-
202
- # Instantiate the agent graphs
203
- multimodal_agent = create_multimodal_agent_graph()
204
- web_search_agent = create_web_search_graph()
205
- youtube_agent = create_youtube_viwer_graph()
206
-
207
- # Create the supervisor workflow to route queries to the appropriate sub-agent
208
- supervisor_workflow = create_supervisor(
209
- [web_search_agent, youtube_agent, multimodal_agent],
210
- model=openai_llm,
211
- prompt=(
212
- "You are a supervisor. For each question, call one of your sub-agents and return their answer directly to the user. Do not modify, summarize, or rephrase the answer."
 
 
 
 
 
 
 
 
 
 
 
213
  )
214
- )
215
 
216
- # Compile the supervisor agent for use in the application
217
- supervisor_agent = supervisor_workflow.compile(name="supervisor_agent")
 
 
 
1
  import re
2
  from datetime import datetime
3
  from typing import Annotated
 
27
  # Load environment variables from .env file
28
  load_dotenv()
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  class AgentState(MessagesState):
31
  """
32
  State class for agent workflows, tracks the message history.
 
71
  except Exception as e:
72
  return f"An error occurred while fetching the transcript: {e}"
73
 
74
+ def build_supervisor_agent(openai_key, google_key):
 
 
 
75
  """
76
+ Build the supervisor agent with the provided API keys.
77
+
78
+ Args:
79
+ openai_key (str): OpenAI API key.
80
+ google_key (str): Google API key.
81
 
82
  Returns:
83
+ supervisor_agent: The compiled supervisor agent.
84
  """
85
+ # Initialize OpenAI LLM (gpt-4o) for general and web search tasks
86
+ openai_llm = ChatOpenAI(
87
+ model="gpt-4o",
88
+ use_responses_api=True,
89
+ api_key=openai_key
90
+ )
91
 
92
+ # Initialize Google Gemini LLM for YouTube and multimodal tasks
93
+ google_llm = ChatGoogleGenerativeAI(
94
+ model="gemini-2.5-flash-preview-04-17",
95
+ google_api_key=google_key,
96
+ )
97
 
98
+ tools = [youtube_transcript]
99
+
100
+ def create_web_search_graph() -> StateGraph:
101
+ """
102
+ Create the web search agent graph.
103
 
104
  Returns:
105
+ StateGraph: The compiled web search agent workflow.
106
  """
 
 
 
 
107
  web_search_preview = [{"type": "web_search_preview"}]
108
+ # Bind the web search tool to the OpenAI LLM
109
+ llm_with_tools = openai_llm.bind_tools(web_search_preview)
110
+
111
+ def agent_node(state: AgentState) -> dict:
112
+ """
113
+ Node function for handling web search queries.
114
+
115
+ Args:
116
+ state (AgentState): The current agent state.
117
+
118
+ Returns:
119
+ dict: Updated state with the LLM response.
120
+ """
121
+ current_date = datetime.now().strftime("%B %d, %Y")
122
+ # Format the system prompt with the current date
123
+ system_message = SystemMessage(content=WEB_SEARCH_PROMPT.format(current_date=current_date))
124
+ # Re-bind tools for each invocation (defensive)
125
+ web_search_preview = [{"type": "web_search_preview"}]
126
+ response = llm_with_tools.bind_tools(web_search_preview).invoke(
127
+ [system_message] + state.get("messages")
128
+ )
129
+ return {"messages": state.get("messages") + [response]}
130
+
131
+ # Build the workflow graph
132
+ workflow = StateGraph(AgentState)
133
+ workflow.add_node("agent", agent_node)
134
+ workflow.add_edge(START, "agent")
135
+ workflow.add_edge("agent", END)
136
+ return workflow.compile(name="web_search_agent")
137
+
138
+ def create_youtube_viwer_graph() -> StateGraph:
139
  """
140
+ Create the YouTube viewer agent graph.
 
 
 
141
 
142
  Returns:
143
+ StateGraph: The compiled YouTube viewer agent workflow.
144
  """
145
+ def agent_node(state: AgentState) -> dict:
146
+ """
147
+ Node function for handling YouTube-related queries.
148
+
149
+ Args:
150
+ state (AgentState): The current agent state.
151
+
152
+ Returns:
153
+ dict: Updated state with the LLM response.
154
+ """
155
+ current_date = datetime.now().strftime("%B %d, %Y")
156
+ # Format the system prompt with the current date
157
+ system_message = SystemMessage(content=YOUTUBE_PROMPT.format(current_date=current_date))
158
+ # Bind the YouTube transcript tool to the Gemini LLM
159
+ llm_with_tools = google_llm.bind_tools(tools)
160
+ response = llm_with_tools.invoke([system_message] + state.get("messages"))
161
+ return {"messages": state.get("messages") + [response]}
162
+
163
+ # Build the workflow graph with tool node and conditional routing
164
+ workflow = StateGraph(AgentState)
165
+ workflow.add_node("llm", agent_node)
166
+ workflow.add_node("tools", ToolNode(tools))
167
+ workflow.set_entry_point("llm")
168
+ workflow.add_conditional_edges(
169
+ "llm",
170
+ tools_condition,
171
+ {
172
+ "tools": "tools", # If tool is needed, go to tools node
173
+ "__end__": END, # Otherwise, end the workflow
174
+ },
175
+ )
176
+ workflow.add_edge("tools", "llm") # After tool, return to LLM node
177
+ return workflow.compile(name="youtube_viwer_agent")
178
 
179
+ def create_multimodal_agent_graph() -> StateGraph:
 
 
 
180
  """
181
+ Create the multimodal agent graph using Gemini for best multimodal support.
 
 
 
182
 
183
  Returns:
184
+ StateGraph: The compiled multimodal agent workflow.
185
  """
186
+ def agent_node(state: AgentState) -> dict:
187
+ """
188
+ Node function for handling multimodal queries.
189
+
190
+ Args:
191
+ state (AgentState): The current agent state.
192
+
193
+ Returns:
194
+ dict: Updated state with the LLM response.
195
+ """
196
+ current_date = datetime.now().strftime("%B %d, %Y")
197
+ # Compose the system message with the multimodal prompt and current date
198
+ system_message = SystemMessage(content=MULTIMODAL_PROMPT + f" Today's date: {current_date}.")
199
+ messages = [system_message] + state.get("messages")
200
+ # Invoke Gemini LLM for multimodal reasoning
201
+ response = google_llm.invoke(messages)
202
+ return {"messages": state.get("messages") + [response]}
203
+
204
+ # Build the workflow graph
205
+ workflow = StateGraph(AgentState)
206
+ workflow.add_node("agent", agent_node)
207
+ workflow.add_edge(START, "agent")
208
+ workflow.add_edge("agent", END)
209
+ return workflow.compile(name="multimodal_agent")
210
+
211
+ # Instantiate the agent graphs
212
+ multimodal_agent = create_multimodal_agent_graph()
213
+ web_search_agent = create_web_search_graph()
214
+ youtube_agent = create_youtube_viwer_graph()
215
+
216
+ # Create the supervisor workflow to route queries to the appropriate sub-agent
217
+ supervisor_workflow = create_supervisor(
218
+ [web_search_agent, youtube_agent, multimodal_agent],
219
+ model=openai_llm,
220
+ prompt=(
221
+ "You are a supervisor. For each question, call one of your sub-agents and return their answer directly to the user. Do not modify, summarize, or rephrase the answer."
222
+ )
223
  )
 
224
 
225
+ # Compile the supervisor agent for use in the application
226
+ supervisor_agent = supervisor_workflow.compile(name="supervisor_agent")
227
+ return supervisor_agent