Andrew Stanley Claude commited on
Commit
4efbaed
·
unverified ·
1 Parent(s): 0c6f69a

Refactor to use built-in FinalAnswerTool and add markdown writing capability

Browse files

- Switch from custom FinalAnswerTool to smolagents built-in version
- Remove custom tool implementations (final_answer.py, web_search.py)
- Add write_to_markdown tool for file output functionality
- Update .gitignore to exclude .claude directory

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

Files changed (4) hide show
  1. .gitignore +1 -0
  2. app.py +24 -2
  3. tools/final_answer.py +0 -14
  4. tools/web_search.py +0 -27
.gitignore CHANGED
@@ -151,3 +151,4 @@ flagged/
151
  Thumbs.db
152
 
153
  # Project specific
 
 
151
  Thumbs.db
152
 
153
  # Project specific
154
+ .claude
app.py CHANGED
@@ -1,9 +1,8 @@
1
- from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel, tool
2
  #import requests
3
  import pytz
4
  import yaml
5
  from datetime import datetime
6
- from tools.final_answer import FinalAnswerTool
7
 
8
  from Gradio_UI import GradioUI
9
 
@@ -21,6 +20,28 @@ def get_current_time_in_timezone(timezone: str) -> str:
21
  return f"The current local time in {timezone} is: {local_time}"
22
  except Exception as e:
23
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
  # Instantiate the DuckDuckGoSearchTool
26
  search_tool = DuckDuckGoSearchTool(max_results=5, rate_limit=2.0)
@@ -48,6 +69,7 @@ agent = CodeAgent(
48
  final_answer,
49
  search_tool,
50
  get_current_time_in_timezone,
 
51
  ], ## add your tools here (don't remove final answer)
52
  max_steps=6,
53
  verbosity_level=1,
 
1
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, FinalAnswerTool, InferenceClientModel, tool
2
  #import requests
3
  import pytz
4
  import yaml
5
  from datetime import datetime
 
6
 
7
  from Gradio_UI import GradioUI
8
 
 
20
  return f"The current local time in {timezone} is: {local_time}"
21
  except Exception as e:
22
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
23
+
24
+ @tool
25
+ def write_to_markdown(content: str, filename: str) -> None:
26
+ """
27
+ Write the given content to a markdown file.
28
+
29
+ Args:
30
+ content: The text content to write to the file
31
+ filename: The name of the markdown file (will automatically add .md extension if not present)
32
+
33
+ Example:
34
+ >>> write_to_markdown("# Hello World\\nThis is a test.", "output.md")
35
+ """
36
+ # Add .md extension if not present
37
+ if not filename.endswith('.md'):
38
+ filename += '.md'
39
+
40
+ # Write content to file
41
+ with open(filename, 'w', encoding='utf-8') as f:
42
+ f.write(content)
43
+
44
+ print(f"Content successfully written to {filename}")
45
 
46
  # Instantiate the DuckDuckGoSearchTool
47
  search_tool = DuckDuckGoSearchTool(max_results=5, rate_limit=2.0)
 
69
  final_answer,
70
  search_tool,
71
  get_current_time_in_timezone,
72
+ write_to_markdown,
73
  ], ## add your tools here (don't remove final answer)
74
  max_steps=6,
75
  verbosity_level=1,
tools/final_answer.py DELETED
@@ -1,14 +0,0 @@
1
- from typing import Any, Optional
2
- from smolagents.tools import Tool
3
-
4
- class FinalAnswerTool(Tool):
5
- name = "final_answer"
6
- description = "Provides a final answer to the given problem."
7
- inputs = {'answer': {'type': 'any', 'description': 'The final answer to the problem'}}
8
- output_type = "any"
9
-
10
- def forward(self, answer: Any) -> Any:
11
- return answer
12
-
13
- def __init__(self, *args, **kwargs):
14
- self.is_initialized = False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
tools/web_search.py DELETED
@@ -1,27 +0,0 @@
1
- from typing import Any, Optional
2
- from smolagents.tools import Tool
3
- import duckduckgo_search
4
-
5
- class DuckDuckGoSearchTool(Tool):
6
- name = "web_search"
7
- description = "Performs a duckduckgo web search based on your query (think a Google search) then returns the top search results."
8
- inputs = {'query': {'type': 'string', 'description': 'The search query to perform.'}}
9
- output_type = "string"
10
-
11
- def __init__(self, max_results=10, **kwargs):
12
- super().__init__()
13
- self.max_results = max_results
14
- try:
15
- from duckduckgo_search import DDGS
16
- except ImportError as e:
17
- raise ImportError(
18
- "You must install package `duckduckgo_search` to run this tool: for instance run `pip install duckduckgo-search`."
19
- ) from e
20
- self.ddgs = DDGS(**kwargs)
21
-
22
- def forward(self, query: str) -> str:
23
- results = self.ddgs.text(query, max_results=self.max_results)
24
- if len(results) == 0:
25
- raise Exception("No results found! Try a less restrictive/shorter query.")
26
- postprocessed_results = [f"[{result['title']}]({result['href']})\n{result['body']}" for result in results]
27
- return "## Search Results\n\n" + "\n\n".join(postprocessed_results)