wt002 commited on
Commit
6646d0c
·
verified ·
1 Parent(s): a6da0a6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -48
app.py CHANGED
@@ -26,53 +26,63 @@ model = OpenAIServerModel(
26
  openAiClient = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
27
 
28
  @tool
29
- def tavily_search(query: str) -> str:
30
- """
31
- Perform a search using the Tavily API.
32
-
33
- Args:
34
- query: The search query string
35
-
36
- Returns:
37
- A string containing the search results
38
- """
39
- api_key = os.getenv("TAVILY_API_KEY")
40
- if not api_key:
41
- return "Error: TAVILY_API_KEY environment variable is not set"
42
-
43
- api_url = "https://api.tavily.com/search"
44
-
45
- headers = {
46
- "Content-Type": "application/json",
47
- }
48
-
49
- payload = {
50
- "api_key": api_key,
51
- "query": query,
52
- "search_depth": "advanced",
53
- "include_answer": True,
54
- "include_raw_content": False,
55
- "max_results": 5
56
  }
57
-
58
- try:
59
- response = requests.post(api_url, headers=headers, json=payload)
60
- response.raise_for_status()
61
- data = response.json()
62
-
63
- # Extract the answer and results
64
- result = []
65
- if "answer" in data:
66
- result.append(f"Answer: {data['answer']}")
67
-
68
- if "results" in data:
69
- result.append("\nSources:")
70
- for i, item in enumerate(data["results"], 1):
71
- result.append(f"{i}. {item.get('title', 'No title')}: {item.get('url', 'No URL')}")
72
-
73
- return "\n".join(result)
74
- except Exception as e:
75
- return f"Error performing Tavily search: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
 
77
  @tool
78
  def analyze_image(image_url: str) -> str:
@@ -292,7 +302,7 @@ def transcribe_youtube(youtube_url: str) -> str:
292
  except Exception as e:
293
  return f"Error processing YouTube video: {str(e)}"
294
 
295
- """
296
  @tool
297
  def process_file(task_id: str, file_name: str) -> str:
298
  """
@@ -342,7 +352,7 @@ def process_file(task_id: str, file_name: str) -> str:
342
  return f"File '{file_name}' of type '{mime_type or 'unknown'}' was fetched successfully. Content processing not implemented for this file type."
343
  except Exception as e:
344
  return f"Error processing file: {str(e)}"
345
- """
346
 
347
  class BasicAgent:
348
  """
 
26
  openAiClient = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
27
 
28
  @tool
29
+ def VisitWebpageTool(Tool):
30
+ name = "visit_webpage"
31
+ description = (
32
+ "Visits a webpage at the given url and reads its content as a markdown string. Use this to browse webpages."
33
+ )
34
+ inputs = {
35
+ "url": {
36
+ "type": "string",
37
+ "description": "The url of the webpage to visit.",
38
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  }
40
+ output_type = "string"
41
+
42
+ def __init__(self, max_output_length: int = 40000):
43
+ super().__init__()
44
+ self.max_output_length = max_output_length
45
+
46
+ def _truncate_content(self, content: str, max_length: int) -> str:
47
+ if len(content) <= max_length:
48
+ return content
49
+ return (
50
+ content[: max_length // 2]
51
+ + f"\n..._This content has been truncated to stay below {max_length} characters_...\n"
52
+ + content[-max_length // 2 :]
53
+ )
54
+
55
+ def forward(self, url: str) -> str:
56
+ try:
57
+ import re
58
+
59
+ import requests
60
+ from markdownify import markdownify
61
+ from requests.exceptions import RequestException
62
+ except ImportError as e:
63
+ raise ImportError(
64
+ "You must install packages `markdownify` and `requests` to run this tool: for instance run `pip install markdownify requests`."
65
+ ) from e
66
+ try:
67
+ # Send a GET request to the URL with a 20-second timeout
68
+ response = requests.get(url, timeout=20)
69
+ response.raise_for_status() # Raise an exception for bad status codes
70
+
71
+ # Convert the HTML content to Markdown
72
+ markdown_content = markdownify(response.text).strip()
73
+
74
+ # Remove multiple line breaks
75
+ markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)
76
+
77
+ return self._truncate_content(markdown_content, self.max_output_length)
78
+
79
+ except requests.exceptions.Timeout:
80
+ return "The request timed out. Please try again later or check the URL."
81
+ except RequestException as e:
82
+ return f"Error fetching the webpage: {str(e)}"
83
+ except Exception as e:
84
+ return f"An unexpected error occurred: {str(e)}"
85
+
86
 
87
  @tool
88
  def analyze_image(image_url: str) -> str:
 
302
  except Exception as e:
303
  return f"Error processing YouTube video: {str(e)}"
304
 
305
+
306
  @tool
307
  def process_file(task_id: str, file_name: str) -> str:
308
  """
 
352
  return f"File '{file_name}' of type '{mime_type or 'unknown'}' was fetched successfully. Content processing not implemented for this file type."
353
  except Exception as e:
354
  return f"Error processing file: {str(e)}"
355
+
356
 
357
  class BasicAgent:
358
  """