palltaruo commited on
Commit
2463ddc
·
verified ·
1 Parent(s): be8e2cc

Update tools/recipe_tools.py

Browse files
Files changed (1) hide show
  1. tools/recipe_tools.py +30 -19
tools/recipe_tools.py CHANGED
@@ -1,43 +1,54 @@
1
  from typing import Dict, List, Optional
2
  from smolagents.tools import Tool, tool
3
- from smolagents import DuckDuckGoSearchTool
4
- import requests
5
  from bs4 import BeautifulSoup
6
  import re
7
  import json
8
 
9
  @tool
10
- def search_recipe(query: str) -> List[Dict[str, str]]:
11
- """Searches for recipes using DuckDuckGo
12
  Args:
13
  query: search query for the recipe (e.g., 'chicken pasta recipe')
 
 
14
  Returns:
15
- List of dictionaries containing recipe titles and URLs
16
  """
17
- search_tool = DuckDuckGoSearchTool()
18
- results = search_tool.search(f"{query} recipe")
19
 
20
- # Filter and format results
21
  recipes = []
22
- for result in results[:5]: # Limit to top 5 results
23
- if any(domain in result['link'] for domain in ['allrecipes.com', 'food.com', 'foodnetwork.com']):
24
- recipes.append({
25
- 'title': result['title'],
26
- 'url': result['link']
27
- })
28
- return recipes
 
 
 
 
 
 
 
29
 
30
  @tool
31
- def parse_recipe(url: str) -> Dict:
32
  """Extracts recipe information from a webpage
33
  Args:
34
  url: URL of the recipe page
 
35
  Returns:
36
  Dictionary containing recipe details (ingredients, instructions)
37
  """
38
  try:
39
- response = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'})
40
- soup = BeautifulSoup(response.text, 'html.parser')
 
 
 
41
 
42
  # Initialize recipe data
43
  recipe_data = {
@@ -63,7 +74,7 @@ def parse_recipe(url: str) -> Dict:
63
  return recipe_data
64
 
65
  except Exception as e:
66
- return {'error': str(e)}
67
 
68
  @tool
69
  def generate_shopping_list(ingredients: List[str]) -> Dict[str, List[str]]:
 
1
  from typing import Dict, List, Optional
2
  from smolagents.tools import Tool, tool
 
 
3
  from bs4 import BeautifulSoup
4
  import re
5
  import json
6
 
7
  @tool
8
+ def search_recipe(query: str, web_search, visit_webpage) -> List[Dict[str, str]]:
9
+ """Searches for recipes using web search and retrieves recipe details
10
  Args:
11
  query: search query for the recipe (e.g., 'chicken pasta recipe')
12
+ web_search: DuckDuckGoSearchTool instance
13
+ visit_webpage: VisitWebPageTool instance
14
  Returns:
15
+ List of dictionaries containing recipe information
16
  """
17
+ # Use the web_search tool to find recipes
18
+ search_results = web_search.forward(f"{query} recipe best rated")
19
 
20
+ # Parse the markdown formatted results to extract URLs
21
  recipes = []
22
+ lines = search_results.split('\n')
23
+ for line in lines:
24
+ if line.startswith('[') and '](' in line and ')' in line:
25
+ # Extract title and URL from markdown link
26
+ title = line[1:line.index(']')]
27
+ url = line[line.index('(')+1:line.index(')')]
28
+ if any(domain in url.lower() for domain in ['allrecipes.com', 'food.com', 'foodnetwork.com']):
29
+ recipes.append({
30
+ 'title': title,
31
+ 'url': url
32
+ })
33
+
34
+ # Limit to top 3 recipes
35
+ return recipes[:3]
36
 
37
  @tool
38
+ def parse_recipe(url: str, visit_webpage) -> Dict:
39
  """Extracts recipe information from a webpage
40
  Args:
41
  url: URL of the recipe page
42
+ visit_webpage: VisitWebPageTool instance
43
  Returns:
44
  Dictionary containing recipe details (ingredients, instructions)
45
  """
46
  try:
47
+ # Use visit_webpage tool to get the page content
48
+ page_content = visit_webpage.forward(url)
49
+
50
+ # Parse the HTML content
51
+ soup = BeautifulSoup(page_content, 'html.parser')
52
 
53
  # Initialize recipe data
54
  recipe_data = {
 
74
  return recipe_data
75
 
76
  except Exception as e:
77
+ return {'error': f"Error parsing recipe: {str(e)}"}
78
 
79
  @tool
80
  def generate_shopping_list(ingredients: List[str]) -> Dict[str, List[str]]: