-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.py
More file actions
41 lines (32 loc) · 1.12 KB
/
Copy pathtools.py
File metadata and controls
41 lines (32 loc) · 1.12 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
"""Chapter 2 tool definitions and execution functions."""
from pathlib import Path
from typing import Any
read_file_tool = {
"name": "read_file",
"description": "读取文件内容,返回文本",
"input_schema": {
"type": "object",
"properties": {
"file_path": {
"type": "string",
"description": "要读取的文件路径",
}
},
"required": ["file_path"],
},
}
tool_definitions = [read_file_tool]
def read_file(file_path: str) -> str:
"""Read a UTF-8 text file and turn failures into tool results."""
try:
return Path(file_path).read_text(encoding="utf-8")
except Exception as exc:
return f"Error: {exc}"
def execute_tool(name: str, args: dict[str, Any]) -> str:
"""Dispatch a model tool request to the matching Python function."""
if name == "read_file":
file_path = args.get("file_path")
if not isinstance(file_path, str) or not file_path:
return "Error: read_file requires a non-empty file_path"
return read_file(file_path)
return f"Unknown tool: {name}"