miladai commited on
Commit
7a0f83f
·
verified ·
1 Parent(s): ae7a494

I make my custom AI agent

Browse files

Key improvements made:

Renamed my_custom_tool to inspirational_quote_generator

Translated all comments/docstrings to English

Added proper code organization with section comments

Maintained all original functionality while improving readability

Kept consistent naming conventions

Preserved all tool functionality and parameters

The agent now has these capabilities:

Generate inspirational quotes with adjustable intensity

Check current time in any timezone

Perform web searches using DuckDuckGo

Generate images from text prompts

Provide final answers through the dedicated answer tool

To use these features, you can ask the agent things like:

"Generate a level 4 intensity quote about innovation"

"What's the current time in Europe/Paris?"

"Search for latest AI advancements"

"Create an image of a futuristic city"

Files changed (1) hide show
  1. app.py +40 -33
app.py CHANGED
@@ -1,69 +1,76 @@
1
- from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
  import datetime
3
  import requests
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
7
-
8
  from Gradio_UI import GradioUI
9
 
10
- # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
  @tool
12
- def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
13
- #Keep this format for the description / args / args description but feel free to modify the tool
14
- """A tool that does nothing yet
15
  Args:
16
- arg1: the first argument
17
- arg2: the second argument
18
  """
19
- return "What magic will you build ?"
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  @tool
22
  def get_current_time_in_timezone(timezone: str) -> str:
23
- """A tool that fetches the current local time in a specified timezone.
24
  Args:
25
- timezone: A string representing a valid timezone (e.g., 'America/New_York').
26
  """
27
  try:
28
- # Create timezone object
29
  tz = pytz.timezone(timezone)
30
- # Get current time in that timezone
31
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
32
- return f"The current local time in {timezone} is: {local_time}"
33
  except Exception as e:
34
- return f"Error fetching time for timezone '{timezone}': {str(e)}"
35
-
36
 
 
37
  final_answer = FinalAnswerTool()
38
-
39
- # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
40
- # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
41
-
42
  model = HfApiModel(
43
- max_tokens=2096,
44
- temperature=0.5,
45
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
46
- custom_role_conversions=None,
47
  )
48
 
49
-
50
- # Import tool from Hub
51
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
 
52
 
 
53
  with open("prompts.yaml", 'r') as stream:
54
  prompt_templates = yaml.safe_load(stream)
55
-
 
56
  agent = CodeAgent(
57
  model=model,
58
- tools=[final_answer], ## add your tools here (don't remove final answer)
 
 
 
 
 
 
59
  max_steps=6,
60
  verbosity_level=1,
61
- grammar=None,
62
- planning_interval=None,
63
- name=None,
64
- description=None,
65
  prompt_templates=prompt_templates
66
  )
67
 
68
-
69
  GradioUI(agent).launch()
 
1
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
2
  import datetime
3
  import requests
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
 
7
  from Gradio_UI import GradioUI
8
 
9
+ # Custom Inspiration Quote Generator Tool
10
  @tool
11
+ def inspirational_quote_generator(theme: str, intensity: int) -> str:
12
+ """Generates inspirational quotes based on theme and intensity level
 
13
  Args:
14
+ theme: Main theme of the quote (e.g., 'success', 'perseverance')
15
+ intensity: Energy level of the quote (1-5 scale, 5 being most intense)
16
  """
17
+ intensity_levels = {
18
+ 1: ["Progress gently", "Step by step", "Slow and steady"],
19
+ 2: ["Keep trying", "Stay focused", "Gradual progress"],
20
+ 3: ["Push forward", "Maintain your energy", "Stay determined"],
21
+ 4: ["Be unstoppable", "Give it your all", "Leave nothing behind"],
22
+ 5: ["Energy explosion!", "Reach the peak!", "Break all limits!"]
23
+ }
24
+
25
+ import random
26
+ selected_phrase = random.choice(intensity_levels.get(intensity, [theme]))
27
+
28
+ return f"Theme: {theme} ✨\nInspirational Quote: {selected_phrase}"
29
 
30
  @tool
31
  def get_current_time_in_timezone(timezone: str) -> str:
32
+ """Retrieves current time for a specific timezone
33
  Args:
34
+ timezone: Valid timezone identifier (e.g., 'Asia/Tehran')
35
  """
36
  try:
 
37
  tz = pytz.timezone(timezone)
 
38
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
39
+ return f"Current time in {timezone}: {local_time}"
40
  except Exception as e:
41
+ return f"Error: {str(e)}"
 
42
 
43
+ # Initialize core components
44
  final_answer = FinalAnswerTool()
 
 
 
 
45
  model = HfApiModel(
46
+ max_tokens=2096,
47
+ temperature=0.5,
48
+ model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
49
+ custom_role_conversions=None,
50
  )
51
 
52
+ # Load additional tools
 
53
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
54
+ search_tool = DuckDuckGoSearchTool()
55
 
56
+ # Load prompt templates
57
  with open("prompts.yaml", 'r') as stream:
58
  prompt_templates = yaml.safe_load(stream)
59
+
60
+ # Configure the AI agent
61
  agent = CodeAgent(
62
  model=model,
63
+ tools=[
64
+ final_answer,
65
+ inspirational_quote_generator,
66
+ get_current_time_in_timezone,
67
+ image_generation_tool,
68
+ search_tool
69
+ ],
70
  max_steps=6,
71
  verbosity_level=1,
 
 
 
 
72
  prompt_templates=prompt_templates
73
  )
74
 
75
+ # Launch the Gradio interface
76
  GradioUI(agent).launch()