philincloud commited on
Commit
514b444
·
verified ·
1 Parent(s): e4b659c

Update langgraph_agent.py

Browse files
Files changed (1) hide show
  1. langgraph_agent.py +6 -85
langgraph_agent.py CHANGED
@@ -5,8 +5,8 @@ import pandas as pd
5
  from typing import Dict, List, Union
6
  import re
7
 
8
- from PIL import Image as PILImage # Keep PIL for potential future use or if other parts depend on it, but describe_image is removed.
9
- from huggingface_hub import InferenceClient # Keep InferenceClient for other potential HF uses, but describe_image is removed.
10
 
11
  from langgraph.graph import START, StateGraph, MessagesState
12
  from langgraph.prebuilt import tools_condition, ToolNode
@@ -73,8 +73,6 @@ def google_web_search(query: str) -> str:
73
  return f"Error occurred while searching the web for '{query}'. Details: {str(e)}"
74
 
75
 
76
- # HF_API_TOKEN is no longer directly needed for describe_image as that tool is removed.
77
- # But keeping InferenceClient initialization for completeness if other HF tools might be added later.
78
  HF_API_TOKEN = os.getenv("HF_API_TOKEN")
79
  MODEL = os.getenv("MODEL")
80
  HF_INFERENCE_CLIENT = None
@@ -133,50 +131,17 @@ def python_interpreter(code: str) -> Dict[str, str]:
133
  except Exception as e:
134
  return {"execution_error": str(e)}
135
 
136
- # --- Youtube Tool (Remains the same) ---
137
- @tool
138
- def Youtube(url: str, question: str) -> Dict[str, str]:
139
- """
140
- Tells about the YouTube video identified by the given URL, answering a question about it.
141
- Note: This is a simulated response. In a real application, this would interact with a YouTube API
142
- or a video analysis service to get actual video information and transcripts.
143
- """
144
- print(f"Youtube called with URL: {url}, Question: {question}")
145
-
146
- # Placeholder for actual YouTube API call.
147
- # In a real scenario, you'd use a library like `google-api-python-client` for YouTube Data API
148
- # or a dedicated video transcription/analysis service.
149
-
150
- # Simulating the previous video content for demonstration
151
- if "https://www.youtube.com/watch?v=1htKBjuUWec" in url or re.search(r'youtube\.com/watch\?v=|youtu\.be/', url):
152
- return {
153
- "video_url": url,
154
- "question_asked": question,
155
- "video_summary": "The video titled 'Teal'c coffee first time' shows a scene where several individuals are reacting to a beverage, presumably coffee, that Teal'c is trying for the first time. Key moments include: A person off-screen remarking, 'Wow this coffee's great'; another asking if it's 'cinnamon chicory tea oak'; and Teal'c reacting strongly to the taste or temperature, stating 'isn't that hot' indicating he finds it very warm.",
156
- "details": {
157
- "00:00:00": "Someone remarks, 'Wow this coffee's great I was just thinking that yeah is that cinnamon chicory tea oak'",
158
- "00:00:11": "Teal'c takes a large gulp from a black mug",
159
- "00:00:24": "Teal'c reacts strongly, someone asks 'isn't that hot'",
160
- "00:00:26": "Someone agrees, 'extremely'"
161
- }
162
- }
163
- else:
164
- return {"error": "Invalid or unrecognized YouTube URL.", "url": url}
165
-
166
- # --- END YOUTUBE TOOL ---
167
-
168
  API_KEY = os.getenv("GEMINI_API_KEY")
169
- HF_API_TOKEN = os.getenv("HF_SPACE_TOKEN") # Kept for potential future HF uses, but not for describe_image
170
  GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
171
 
172
- # Update the tools list (removed describe_image and arvix_search)
173
  tools = [
174
  multiply, add, subtract, divide, modulus,
175
  wiki_search,
176
  google_web_search,
177
  read_file_content,
178
  python_interpreter,
179
- Youtube,
180
  ]
181
 
182
  with open("prompt.txt", "r", encoding="utf-8") as f:
@@ -206,52 +171,8 @@ def build_graph(provider: str = "gemini"):
206
 
207
  def assistant(state: MessagesState):
208
  messages_to_send = [sys_msg] + state["messages"]
209
-
210
- # --- IMPORTANT NOTE ON HANDLING BINARY BLOB DATA FOR MULTIMODAL LLMs ---
211
- # When read_file_content returns a file_type of "image" or "audio",
212
- # the agent should be able to send the actual binary data of that file
213
- # as part of the message to the LLM. LangChain's ChatGoogleGenerativeAI
214
- # supports this via content parts in HumanMessage.
215
- #
216
- # For this setup, we're assuming the framework (LangGraph/LangChain)
217
- # will correctly handle passing the actual file content when read_file_content
218
- # is called and its output indicates a media type.
219
- #
220
- # A more explicit implementation in the assistant node might look like this
221
- # for real binary file handling if the framework doesn't do it implicitly:
222
- #
223
- # new_messages_to_send = []
224
- # for msg in state["messages"]:
225
- # if isinstance(msg, HumanMessage) and msg.tool_calls:
226
- # # If a tool call to read_file_content happened in the previous turn
227
- # # and it returned a media type, we might need to get the file data
228
- # # and append it to the message parts. This logic is complex and
229
- # # depends heavily on how tool outputs are structured and passed.
230
- # # For simplicity in this template, we assume direct handling by the LLM
231
- # # if the tool output indicates media, and the file itself is accessible
232
- # # via the environment.
233
- # pass # Keep original message, tool output will follow
234
- # elif isinstance(msg, HumanMessage) and any(part.get("file_type") in ["image", "audio"] for part in msg.content if isinstance(part, dict)):
235
- # # This is a conceptual example for if the HumanMessage itself contains file data
236
- # # or a reference that needs to be resolved into data.
237
- # # You'd need to load the actual file bytes here.
238
- # # e.g., if msg.content was like: [{"type": "file_reference", "file_path": "image.png"}]
239
- # # with open(msg.content[0]["file_path"], "rb") as f:
240
- # # file_bytes = f.read()
241
- # # new_messages_to_send.append(
242
- # # HumanMessage(
243
- # # content=[
244
- # # {"type": "text", "text": "Here is the media content:"},
245
- # # {"type": "image_data" if "image" in msg.content[0]["file_type"] else "audio_data", "data": base64.b64encode(file_bytes).decode('utf-8'), "media_type": "image/png" if "image" in msg.content[0]["file_type"] else "audio/mp3"}
246
- # # ]
247
- # # )
248
- # # )
249
- # else:
250
- # new_messages_to_send.append(msg)
251
- # llm_response = llm_with_tools.invoke([sys_msg] + new_messages_to_send)
252
- # --- END IMPORTANT NOTE ---
253
 
254
- llm_response = llm_with_tools.invoke(messages_to_send,{"recursion_limit": 25}) # For now, keep as is, rely on framework
255
  print(f"LLM Raw Response: {llm_response}")
256
  return {"messages": [llm_response]}
257
 
@@ -265,4 +186,4 @@ def build_graph(provider: str = "gemini"):
265
  return builder.compile()
266
 
267
  if __name__ == "__main__":
268
- pass
 
5
  from typing import Dict, List, Union
6
  import re
7
 
8
+ from PIL import Image as PILImage # Keep PIL for potential future use or if other parts depend on it, but describe_image is removed.
9
+ from huggingface_hub import InferenceClient # Keep InferenceClient for other potential HF uses, but describe_image is removed.
10
 
11
  from langgraph.graph import START, StateGraph, MessagesState
12
  from langgraph.prebuilt import tools_condition, ToolNode
 
73
  return f"Error occurred while searching the web for '{query}'. Details: {str(e)}"
74
 
75
 
 
 
76
  HF_API_TOKEN = os.getenv("HF_API_TOKEN")
77
  MODEL = os.getenv("MODEL")
78
  HF_INFERENCE_CLIENT = None
 
131
  except Exception as e:
132
  return {"execution_error": str(e)}
133
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
  API_KEY = os.getenv("GEMINI_API_KEY")
135
+ HF_API_TOKEN = os.getenv("HF_SPACE_TOKEN") # Kept for potential future HF uses, but not for describe_image
136
  GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
137
 
138
+ # Updated tools list without Youtube
139
  tools = [
140
  multiply, add, subtract, divide, modulus,
141
  wiki_search,
142
  google_web_search,
143
  read_file_content,
144
  python_interpreter,
 
145
  ]
146
 
147
  with open("prompt.txt", "r", encoding="utf-8") as f:
 
171
 
172
  def assistant(state: MessagesState):
173
  messages_to_send = [sys_msg] + state["messages"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
174
 
175
+ llm_response = llm_with_tools.invoke(messages_to_send, {"recursion_limit": 25})
176
  print(f"LLM Raw Response: {llm_response}")
177
  return {"messages": [llm_response]}
178
 
 
186
  return builder.compile()
187
 
188
  if __name__ == "__main__":
189
+ pass