NatalieCheong commited on
Commit
8502d9b
·
verified ·
1 Parent(s): 49c0f04

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -30
app.py CHANGED
@@ -16,45 +16,40 @@ def my_custom_tool(search_query: str, generate_image: bool = False) -> str:
16
  search_query: The search query to look up
17
  generate_image: Whether to also generate an image based on the search results
18
  """
19
- print("\n=== DEBUG: my_custom_tool started ===")
20
- print(f"Received query: {search_query}")
21
-
22
  try:
23
- # Initialize the search tool
24
- print("Initializing DuckDuckGoSearchTool...")
25
- search_tool = DuckDuckGoSearchTool()
26
-
27
- # Perform the search
28
- print("Performing search...")
29
- results = search_tool(search_query)
30
-
31
- print(f"Search complete. Got {len(results) if results else 0} results")
32
-
 
 
 
 
 
 
 
 
33
  if not results:
34
- return "No results found for the query."
35
-
36
- # Get the first result
37
  first_result = results[0]
38
-
39
- # Create formatted result
40
  result_text = (
41
- f"Latest AI News:\n"
42
- f"Title: {first_result['title']}\n"
43
- f"Summary: {first_result['snippet']}"
44
  )
45
 
46
  print("Successfully created result text")
47
- print("=== DEBUG: my_custom_tool completed ===\n")
48
-
49
  return result_text
50
-
51
  except Exception as e:
52
- error_message = f"ERROR in my_custom_tool: {str(e)}\n"
53
- error_message += f"Error type: {type(e)}\n"
54
- error_message += f"Error location: {e.__traceback__.tb_frame.f_code.co_filename}, line {e.__traceback__.tb_lineno}"
55
- print(error_message)
56
- print("=== DEBUG: my_custom_tool failed ===\n")
57
- return error_message
58
 
59
 
60
  @tool
 
16
  search_query: The search query to look up
17
  generate_image: Whether to also generate an image based on the search results
18
  """
 
 
 
19
  try:
20
+ # First try to create the search tool
21
+ print("Attempting to create DuckDuckGoSearchTool...")
22
+ search_tool = None
23
+ try:
24
+ search_tool = DuckDuckGoSearchTool()
25
+ print("Successfully created DuckDuckGoSearchTool")
26
+ except Exception as search_tool_error:
27
+ return f"Failed to create search tool: {str(search_tool_error)}"
28
+
29
+ # Then try to perform the search
30
+ print(f"Attempting search for: {search_query}")
31
+ try:
32
+ results = search_tool(search_query)
33
+ print(f"Search completed with {len(results) if results else 0} results")
34
+ except Exception as search_error:
35
+ return f"Failed to perform search: {str(search_error)}"
36
+
37
+ # Process the results
38
  if not results:
39
+ return "Search completed but no results found."
40
+
 
41
  first_result = results[0]
 
 
42
  result_text = (
43
+ f"Here are the latest findings about artificial intelligence:\n\n"
44
+ f"Title: {first_result.get('title', 'No title available')}\n"
45
+ f"Summary: {first_result.get('snippet', 'No summary available')}"
46
  )
47
 
48
  print("Successfully created result text")
 
 
49
  return result_text
50
+
51
  except Exception as e:
52
+ return f"Unexpected error in my_custom_tool: {str(e)}"
 
 
 
 
 
53
 
54
 
55
  @tool