Brunwo commited on
Commit
60f49cc
·
1 Parent(s): 7f53c1a

updated smolagents dept : adapted to tools structure changes

Browse files
Files changed (5) hide show
  1. .vscode/launch.json +17 -0
  2. HFHub.py +34 -38
  3. README.md +15 -1
  4. app.py +31 -25
  5. requirements.txt +11 -2
.vscode/launch.json ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "version": "0.2.0",
3
+ "configurations": [
4
+
5
+ {
6
+ "name": "Python Debugger: Current File",
7
+ "type": "debugpy",
8
+ "request": "launch",
9
+ "program": "${file}",
10
+ "console": "integratedTerminal",
11
+ "pythonPath": "/usr/bin/python3" ,
12
+ "env": {
13
+ "PYTHONPATH": "/home/bruno/.local/lib/python3.12/site-packages"
14
+ }
15
+ }
16
+ ]
17
+ }
HFHub.py CHANGED
@@ -2,6 +2,7 @@
2
 
3
  #utility file
4
 
 
5
  from smolagents.default_tools import __all__ as dft_tools
6
  from huggingface_hub import login, InferenceClient
7
  import logging
@@ -11,33 +12,30 @@ from smolagents import ToolCollection
11
  from smolagents import HfApiModel, CodeAgent, Tool
12
 
13
  publishtoken = None;
14
-
15
  tool_Collections: dict[str, ToolCollection] = {}
 
16
 
17
-
18
- def all_tools() -> list[Tool]:
19
  all_tools = []
20
  for collection in tool_Collections.values():
21
  if isinstance(collection, ToolCollection):
22
  for tool in collection.tools:
23
- all_tools.append(tool)
24
  else:
25
- for tool in collection:
26
  all_tools.append(tool)
27
 
28
  return all_tools
29
 
30
  def filter_tools(tools):
31
  base_tools_names = ['web search']
32
- # dft_tools
33
- # logging.warning(f"Filtering tools, excluding: {exclude_tool}") # Log the exclusion
34
- filtered_tools = [tool for tool in tools if tool.name not in base_tools_names]
35
- logging.warning(f"Number of tools after filtering: {len(filtered_tools)}") # Log the result
36
  return filtered_tools
37
 
38
  additional_authorized_imports=["smolagents","subprocess","typing","os", "inspect","open", "requests"]
39
 
40
-
41
  litellm_api_keys = {
42
  'xai': '',
43
  'HF': '',
@@ -54,58 +52,56 @@ def login_for_publication() :
54
  login(publishtoken)
55
 
56
 
57
- def load_collection_from_space(agent : CodeAgent, collection_slug: str = "Mightypeacock/agent-tools-6777c9699c231b7a1e87fa31" ) -> ToolCollection:
58
 
59
- if collection_slug not in tool_Collections:
60
  tool_collection = ToolCollection(
61
  collection_slug=collection_slug,
62
  # token=publishtoken,
63
  trust_remote_code=True
64
  )
65
- tool_Collections[collection_slug] = tool_collection
 
 
 
 
66
 
67
  for tool in tool_collection.tools:
68
- if agent.toolbox.tools.get(tool.name) is None:
69
- agent.toolbox.add_tool(tool)
 
 
 
70
  else:
71
- agent.toolbox.update_tool(tool)
72
 
73
- return all_tools()
74
 
75
 
76
- def createAgent():
77
 
78
- agent = CodeAgent(
79
- tools=filter_tools(all_tools()),
80
- # all_tools(),#[tool for col in tool_Collections.values() for tool in col.tools], # need to flatmap those probably
81
- # tools=[],
82
- # model=HfApiModel("microsoft/phi-4"),
83
- # model=HfApiModel("Leonuraht/Phi-4"), #space
84
-
85
- # model = LiteLLMModel(model_id=
86
- # # "gpt-4o"
87
- # "anthropic/claude-3-5-sonnet-20240620"
88
- # # xai/<any-model-on-xai> : grok-beta
89
- # ),
90
 
 
 
 
91
  model=HfApiModel(
92
- # # "lmstudio-community/phi-4-GGUF" #working
93
- ),
94
-
95
  additional_authorized_imports=additional_authorized_imports,
96
  add_base_tools=True,
97
  planning_interval=None,
98
- # use_e2b_executor=True,
99
- verbose=True
100
-
101
- )
102
 
103
- for tool in agent.toolbox.tools.values():
 
104
  if tool not in all_tools():
105
  if "base tools" not in tool_Collections:
106
  tool_Collections["base tools"] = []
107
 
108
  tool_Collections["base tools"].append(tool)
 
 
109
 
110
  return agent
111
 
 
2
 
3
  #utility file
4
 
5
+ from typing import List, Set
6
  from smolagents.default_tools import __all__ as dft_tools
7
  from huggingface_hub import login, InferenceClient
8
  import logging
 
12
  from smolagents import HfApiModel, CodeAgent, Tool
13
 
14
  publishtoken = None;
 
15
  tool_Collections: dict[str, ToolCollection] = {}
16
+ loaded_tools: Set[Tool] = set()
17
 
18
+ #kueep separate list, maybe should only refer to agent tools merged list ?
19
+ def all_tools_names2() -> list[Tool]:
20
  all_tools = []
21
  for collection in tool_Collections.values():
22
  if isinstance(collection, ToolCollection):
23
  for tool in collection.tools:
24
+ all_tools.append(tool.name) #new we return key, not the tool
25
  else:
26
+ for tool in collection: # base tools : only keys str
27
  all_tools.append(tool)
28
 
29
  return all_tools
30
 
31
  def filter_tools(tools):
32
  base_tools_names = ['web search']
33
+ filtered_tools = [tool for tool in tools if tool not in base_tools_names]
34
+ logging.warning(f"Number of tools after filtering: {len(filtered_tools)}") # Log the result
 
 
35
  return filtered_tools
36
 
37
  additional_authorized_imports=["smolagents","subprocess","typing","os", "inspect","open", "requests"]
38
 
 
39
  litellm_api_keys = {
40
  'xai': '',
41
  'HF': '',
 
52
  login(publishtoken)
53
 
54
 
55
+ def load_collection_from_space(agent :CodeAgent, collection_slug: str = "Mightypeacock/agent-tools-6777c9699c231b7a1e87fa31" ) -> ToolCollection:
56
 
57
+ if collection_slug not in tool_Collections: #or overwite ?
58
  tool_collection = ToolCollection(
59
  collection_slug=collection_slug,
60
  # token=publishtoken,
61
  trust_remote_code=True
62
  )
63
+
64
+ # tool_Collections[collection_slug] = tool_collection
65
+
66
+ #replace this by slug > tools names:
67
+ tool_Collections[collection_slug] = [tool.name for tool in tool_collection.tools]
68
 
69
  for tool in tool_collection.tools:
70
+ if agent.tools.get(tool.name) is None: #or overwrite ?
71
+
72
+ agent.tools[tool.name]=tool
73
+ loaded_tools.add(tool)
74
+
75
  else:
76
+ agent.tools[tool.name]=tool
77
 
 
78
 
79
 
80
+ return all_tools_names2()
81
 
82
+ def all_tools() -> List[Tool]:
83
+ return list(loaded_tools)
 
 
 
 
 
 
 
 
 
 
84
 
85
+ def createAgent():
86
+ agent = CodeAgent(
87
+ tools=filter_tools(all_tools()),
88
  model=HfApiModel(
89
+ # Model configuration can be added here
90
+ ),
 
91
  additional_authorized_imports=additional_authorized_imports,
92
  add_base_tools=True,
93
  planning_interval=None,
94
+ )
 
 
 
95
 
96
+ # Update the base tools collection
97
+ for tool in agent.tools: # Changed from agent.toolbox.tools.values() to agent.tools
98
  if tool not in all_tools():
99
  if "base tools" not in tool_Collections:
100
  tool_Collections["base tools"] = []
101
 
102
  tool_Collections["base tools"].append(tool)
103
+ logging.debug(f"Base tool added: {tool}") # Debug line
104
+
105
 
106
  return agent
107
 
README.md CHANGED
@@ -11,4 +11,18 @@ license: apache-2.0
11
  short_description: testing smolagent tools creation
12
  ---
13
 
14
- An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  short_description: testing smolagent tools creation
12
  ---
13
 
14
+ An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
15
+
16
+
17
+ accelerate required for base tools (pipeline tool)
18
+
19
+ #pip install -r requirements.txt --ignore-installed torchaudio
20
+ #pip install -r requirements.txt --no-deps
21
+
22
+ #without torchaudio:
23
+ pip install -e ../smolagents/
24
+
25
+
26
+
27
+ #launch in dev (hotreload )
28
+ gradio app.py
app.py CHANGED
@@ -12,10 +12,8 @@ from typing import Dict
12
  import os
13
  import logging
14
 
15
-
16
- def dropdown_update_choices(elt, choices):
17
- print(f"updating dropdown: {elt} with {choices}")
18
- return gr.update(elt, choices=choices, value=None)
19
 
20
  # Configure logging
21
  logging.basicConfig(level=logging.INFO) # not working (prints warnings tho)
@@ -25,6 +23,12 @@ logging.basicConfig(level=logging.INFO) # not working (prints warnings tho)
25
 
26
  logging.warning('start')
27
 
 
 
 
 
 
 
28
 
29
  def process_logs(agent):
30
  logs = ""
@@ -37,7 +41,7 @@ def process_logs(agent):
37
  return "The agent object does not have a valid 'logs' attribute or the attribute is not a list."
38
 
39
  def get_tools():
40
- return [{"name": tool.name, "description": tool.description} for tool in agent.toolbox.tools.values()]
41
 
42
  def get_functions():
43
  return agent.python_executor.custom_tools
@@ -60,37 +64,37 @@ def get_tool_description(selected_tool_name, tools):
60
  return tool["description"]
61
  return "No description available."
62
 
63
-
64
- def refresh_ui_elements():
 
65
 
66
- updated_tools = get_tools()
 
67
  updated_functions = get_functions()
68
 
69
  tool_names = [tool["name"] for tool in updated_tools]
70
  function_names = list(updated_functions.keys())
71
 
72
- print(function_names)
73
-
74
  current_tool = tool_names[0] if tool_names else None
75
  current_function = function_names[0] if function_names else None
76
 
77
  tool_description = get_tool_description(current_tool, updated_tools)
78
-
79
  function_code = get_function_code(current_function) if current_function else ""
80
 
81
- dropdown_update_choices(function_dropdown,function_names)
82
- dropdown_update_choices(tool_dropdown,tool_names)
83
-
84
  return (
85
- tool_dropdown,
86
- function_dropdown,
87
  tool_description,
88
  function_code
89
  )
90
 
91
-
92
  def load_collection_andUploadUI(collection_slug: str ):
93
- load_collection_from_space(agent,collection_slug)
94
  refresh_ui_elements()
95
 
96
  with gr.Blocks() as demo:
@@ -101,8 +105,7 @@ with gr.Blocks() as demo:
101
  chatbot = gr.Chatbot(type="messages")
102
  msg = gr.Textbox()
103
 
104
- send_button = gr.Button("Send") # Send button
105
-
106
  # send_button.click(respond, [msg, chatbot], [msg, chatbot])
107
 
108
  clear = gr.ClearButton([msg, chatbot])
@@ -167,10 +170,7 @@ with gr.Blocks() as demo:
167
 
168
  slug = gr.Textbox(label="collection slug",value="Mightypeacock/agent-tools-6777c9699c231b7a1e87fa31")
169
  greet_btn = gr.Button("Load")
170
- greet_btn.click(fn=load_collection_from_space, inputs=slug,
171
- # outputs=tool_dropdown,
172
- outputs=None,
173
- api_name="load_HF_Collection")
174
 
175
 
176
  gr.Markdown("<center><h2>Functions</h2></center>")
@@ -196,6 +196,13 @@ with gr.Blocks() as demo:
196
  outputs=code,
197
  )
198
 
 
 
 
 
 
 
 
199
  def respond(message, console_output, chat_history):
200
  try:
201
  print(f"Received message: {message}")
@@ -230,5 +237,4 @@ with gr.Blocks() as demo:
230
  )
231
 
232
  if __name__ == "__main__":
233
- agent = createAgent()
234
  demo.launch(show_error=True, debug=True)
 
12
  import os
13
  import logging
14
 
15
+ def dropdown_update_choices(choices):
16
+ return gr.update(choices=choices, value=None)
 
 
17
 
18
  # Configure logging
19
  logging.basicConfig(level=logging.INFO) # not working (prints warnings tho)
 
23
 
24
  logging.warning('start')
25
 
26
+ agent = createAgent()
27
+
28
+ def update_agent(collection_slug: str):
29
+ load_collection_from_space(agent, collection_slug=collection_slug)
30
+ return refresh_ui_elements() # Update the UI elements after loading the collection
31
+
32
 
33
  def process_logs(agent):
34
  logs = ""
 
41
  return "The agent object does not have a valid 'logs' attribute or the attribute is not a list."
42
 
43
  def get_tools():
44
+ return [{"name": tool.name, "description": tool.description} for tool in agent.tools.values()]
45
 
46
  def get_functions():
47
  return agent.python_executor.custom_tools
 
64
  return tool["description"]
65
  return "No description available."
66
 
67
+ def get_tools_name():
68
+ return [tool["name"] for tool in get_tools()]
69
+
70
 
71
+ def refresh_ui_elements():
72
+ updated_tools = get_tools() # Get the updated tools after loading the collection
73
  updated_functions = get_functions()
74
 
75
  tool_names = [tool["name"] for tool in updated_tools]
76
  function_names = list(updated_functions.keys())
77
 
78
+ print(f"function_names: {function_names}")
79
+
80
  current_tool = tool_names[0] if tool_names else None
81
  current_function = function_names[0] if function_names else None
82
 
83
  tool_description = get_tool_description(current_tool, updated_tools)
 
84
  function_code = get_function_code(current_function) if current_function else ""
85
 
86
+ # Update dropdown choices
87
+ tool_dropdown_update = dropdown_update_choices(tool_names)
88
+ function_dropdown_update = dropdown_update_choices(function_names)
89
  return (
90
+ tool_dropdown_update,
91
+ function_dropdown_update,
92
  tool_description,
93
  function_code
94
  )
95
 
 
96
  def load_collection_andUploadUI(collection_slug: str ):
97
+ # load_collection_from_space(agent,collection_slug)
98
  refresh_ui_elements()
99
 
100
  with gr.Blocks() as demo:
 
105
  chatbot = gr.Chatbot(type="messages")
106
  msg = gr.Textbox()
107
 
108
+ # send_button = gr.Button("Send") # Send button
 
109
  # send_button.click(respond, [msg, chatbot], [msg, chatbot])
110
 
111
  clear = gr.ClearButton([msg, chatbot])
 
170
 
171
  slug = gr.Textbox(label="collection slug",value="Mightypeacock/agent-tools-6777c9699c231b7a1e87fa31")
172
  greet_btn = gr.Button("Load")
173
+
 
 
 
174
 
175
 
176
  gr.Markdown("<center><h2>Functions</h2></center>")
 
196
  outputs=code,
197
  )
198
 
199
+ greet_btn.click(fn=update_agent, inputs=slug,
200
+
201
+ outputs=[tool_dropdown, function_dropdown, description_textbox, code],
202
+ # outputs=tool_dropdown,
203
+ # outputs=None,
204
+ api_name="load_HF_Collection")
205
+
206
  def respond(message, console_output, chat_history):
207
  try:
208
  print(f"Received message: {message}")
 
237
  )
238
 
239
  if __name__ == "__main__":
 
240
  demo.launch(show_error=True, debug=True)
requirements.txt CHANGED
@@ -1,5 +1,14 @@
1
- huggingface_hub==0.25.2
 
2
  git+https://github.com/Brunwo/smolagents
 
 
3
  huggingface_hub
 
 
 
 
4
 
5
- litellm
 
 
 
1
+ #huggingface_hub==0.25.2
2
+ #huggingface_hub==0.27.1 # = new current V on HF
3
  git+https://github.com/Brunwo/smolagents
4
+ git+https://github.com/huggingface/transformers
5
+
6
  huggingface_hub
7
+ litellm
8
+
9
+ #for specific (base) tools
10
+ accelerate
11
 
12
+ # for e2b
13
+ e2b-code-interpreter
14
+ python-dotenv