ekabaruh commited on
Commit
083d433
·
verified ·
1 Parent(s): 4fa2cc2

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +65 -2
agent.py CHANGED
@@ -4,6 +4,8 @@ from typing import List, Dict, Any, Optional
4
  import tempfile
5
  import pandas as pd
6
  import numpy as np
 
 
7
 
8
  """Langraph"""
9
  from langgraph.graph import START, StateGraph, MessagesState
@@ -29,8 +31,17 @@ def duckduckgo_search(query: str) -> str:
29
  Args:
30
  query: The search query."""
31
  try:
32
- # Initialize DuckDuckGo search
33
- ddgs = DDGS()
 
 
 
 
 
 
 
 
 
34
 
35
  # Perform the search and get the first 5 results
36
  results = list(ddgs.text(query, max_results=5))
@@ -52,6 +63,57 @@ def duckduckgo_search(query: str) -> str:
52
  except Exception as e:
53
  return {"search_results": f"Error performing search: {str(e)}"}
54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  ### =============== DOCUMENT PROCESSING TOOLS =============== ###
56
  # File handling still requires external tools
57
 
@@ -87,6 +149,7 @@ sys_msg = SystemMessage(content=system_prompt)
87
  tools = [
88
  duckduckgo_search,
89
  save_and_read_file,
 
90
  ]
91
 
92
 
 
4
  import tempfile
5
  import pandas as pd
6
  import numpy as np
7
+ import requests
8
+ from pathlib import Path
9
 
10
  """Langraph"""
11
  from langgraph.graph import START, StateGraph, MessagesState
 
31
  Args:
32
  query: The search query."""
33
  try:
34
+ # Initialize DuckDuckGo search with appropriate user agent
35
+ ddgs_user_agent_header = "/".join(
36
+ [
37
+ "Mozilla",
38
+ "5.0 (Windows NT 10.0; Win64; x64; rv:124.0) Gecko",
39
+ "20100101 Firefox",
40
+ "124.0",
41
+ ]
42
+ )
43
+
44
+ ddgs = DDGS(headers={"User-Agent": ddgs_user_agent_header})
45
 
46
  # Perform the search and get the first 5 results
47
  results = list(ddgs.text(query, max_results=5))
 
63
  except Exception as e:
64
  return {"search_results": f"Error performing search: {str(e)}"}
65
 
66
+ @tool
67
+ def download_image(url: str, filename: Optional[str] = None) -> str:
68
+ """Download an image from a URL and save it to a file.
69
+ Args:
70
+ url: The URL of the image to download.
71
+ filename: Optional; The name to save the file as. If not provided, a name will be derived from the URL.
72
+ """
73
+ try:
74
+ # Send a GET request to the URL
75
+ response = requests.get(url, stream=True)
76
+ response.raise_for_status() # Raise an exception for HTTP errors
77
+
78
+ # Determine the file extension from the content type
79
+ content_type = response.headers.get('Content-Type', '')
80
+ ext = '.jpg' # Default extension
81
+ if 'png' in content_type:
82
+ ext = '.png'
83
+ elif 'jpeg' in content_type or 'jpg' in content_type:
84
+ ext = '.jpg'
85
+ elif 'gif' in content_type:
86
+ ext = '.gif'
87
+ elif 'webp' in content_type:
88
+ ext = '.webp'
89
+
90
+ # Create a filename if not provided
91
+ if not filename:
92
+ # Use the last part of the URL or a timestamp
93
+ url_path = Path(url.split('?')[0])
94
+ url_filename = url_path.name
95
+ if url_filename:
96
+ filename = url_filename
97
+ if not Path(filename).suffix:
98
+ filename = f"{filename}{ext}"
99
+ else:
100
+ import time
101
+ filename = f"image_{int(time.time())}{ext}"
102
+
103
+ # Ensure the file has an extension
104
+ if not Path(filename).suffix:
105
+ filename = f"{filename}{ext}"
106
+
107
+ # Save the file
108
+ filepath = os.path.join(tempfile.gettempdir(), filename)
109
+ with open(filepath, 'wb') as f:
110
+ for chunk in response.iter_content(chunk_size=8192):
111
+ f.write(chunk)
112
+
113
+ return f"Image downloaded and saved to {filepath}"
114
+ except Exception as e:
115
+ return f"Error downloading image: {str(e)}"
116
+
117
  ### =============== DOCUMENT PROCESSING TOOLS =============== ###
118
  # File handling still requires external tools
119
 
 
149
  tools = [
150
  duckduckgo_search,
151
  save_and_read_file,
152
+ download_image,
153
  ]
154
 
155