Repository navigation
Expand file tree
/
Copy pathsmolagents_example.py
More file actions
59 lines (47 loc) · 1.86 KB
/
Copy pathsmolagents_example.py
File metadata and controls
59 lines (47 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# pytest: ollama, e2e
"""Example showing how to use pre-built Hugging Face smolagents tools with Mellea.
This demonstrates loading existing tools from the smolagents ecosystem,
similar to how you can use langchain tools with MelleaTool.from_langchain().
The smolagents library provides various pre-built tools like:
- PythonInterpreterTool for code execution
- DuckDuckGoSearchTool for web search (requires ddgs package)
- WikipediaSearchTool for Wikipedia queries
- And many others from the Hugging Face ecosystem
Prerequisites:
uv sync --extra tools
ollama pull granite4.2:3b
ollama serve
Usage:
uv run python docs/examples/tools/smolagents_example.py
"""
from mellea import start_session
from mellea.backends import ModelOption
from mellea.backends.tools import MelleaTool
try:
# Import a pre-built tool from smolagents
from smolagents import PythonInterpreterTool
# Create the smolagents tool instance
python_tool_hf = PythonInterpreterTool()
# Convert to Mellea tool - now you can use it with Mellea!
python_tool: MelleaTool = MelleaTool.from_smolagents(python_tool_hf)
# Use with Mellea session
m = start_session()
result = m.instruct(
"Calculate the sum of numbers from 1 to 10 using Python",
model_options={ModelOption.TOOLS: [python_tool]},
tool_calls=True,
)
print(f"Response: {result}")
if result.tool_calls:
try:
tool_call = next(
(tc for tc in result.tool_calls if tc.name == python_tool.name), None
)
if tool_call:
calc_result = tool_call.call_func()
print(f"\nCalculation result: {calc_result}")
except Exception as e:
print(f"\nTool execution failed: {e}")
except ImportError as e:
print("Please install smolagents: uv pip install 'mellea[tools]'")
print(f"Error: {e}")