|
2 | 2 | CLI for gptme utility commands.
|
3 | 3 | """
|
4 | 4 |
|
| 5 | +import logging |
5 | 6 | import sys
|
6 | 7 | from pathlib import Path
|
7 | 8 |
|
|
14 | 15 |
|
15 | 16 |
|
16 | 17 | @click.group()
|
17 |
| -def main(): |
| 18 | +@click.option("-v", "--verbose", is_flag=True, help="Enable verbose output.") |
| 19 | +def main(verbose: bool = False): |
18 | 20 | """Utility commands for gptme."""
|
19 |
| - pass |
| 21 | + |
| 22 | + if verbose: |
| 23 | + logging.getLogger().setLevel(logging.DEBUG) |
20 | 24 |
|
21 | 25 |
|
22 | 26 | @main.group()
|
@@ -182,5 +186,51 @@ def tools_info(tool_name: str):
|
182 | 186 | print(tool.examples)
|
183 | 187 |
|
184 | 188 |
|
| 189 | +@tools.command("call") |
| 190 | +@click.argument("tool_name") |
| 191 | +@click.argument("function_name") |
| 192 | +@click.option( |
| 193 | + "--arg", |
| 194 | + "-a", |
| 195 | + multiple=True, |
| 196 | + help="Arguments to pass to the function. Format: key=value", |
| 197 | +) |
| 198 | +def tools_call(tool_name: str, function_name: str, arg: list[str]): |
| 199 | + """Call a tool with the given arguments.""" |
| 200 | + from ..tools import get_tool, init_tools, loaded_tools # fmt: skip |
| 201 | + |
| 202 | + # Initialize tools |
| 203 | + init_tools() |
| 204 | + |
| 205 | + tool = get_tool(tool_name) |
| 206 | + if not tool: |
| 207 | + print(f"Tool '{tool_name}' not found. Available tools:") |
| 208 | + for t in loaded_tools: |
| 209 | + print(f"- {t.name}") |
| 210 | + sys.exit(1) |
| 211 | + |
| 212 | + function = ( |
| 213 | + [f for f in tool.functions if f.__name__ == function_name] or None |
| 214 | + if tool.functions |
| 215 | + else None |
| 216 | + ) |
| 217 | + if not function: |
| 218 | + print(f"Function '{function_name}' not found in tool '{tool_name}'.") |
| 219 | + if tool.functions: |
| 220 | + print("Available functions:") |
| 221 | + for f in tool.functions: |
| 222 | + print(f"- {f.__name__}") |
| 223 | + else: |
| 224 | + print("No functions available for this tool.") |
| 225 | + sys.exit(1) |
| 226 | + else: |
| 227 | + # Parse arguments into a dictionary, ensuring proper typing |
| 228 | + kwargs = {} |
| 229 | + for arg_str in arg: |
| 230 | + key, value = arg_str.split("=", 1) |
| 231 | + kwargs[key] = value |
| 232 | + function[0](**kwargs) |
| 233 | + |
| 234 | + |
185 | 235 | if __name__ == "__main__":
|
186 | 236 | main()
|
0 commit comments