tinycodinglm / agentic /aiagent-0015.jsonl
winapiadmin's picture
Upload folder using huggingface_hub
1f3a244 verified
|
Raw History Blame Contribute Delete
2.46 kB
{"type": "session", "harness": "opencode", "id": "aiagent-0015", "name": "tool use"}
{"type": "message", "message": {"role": "system", "content": "You are an expert software engineer and technical assistant. You provide accurate, detailed, and practical answers. When writing code, use best practices, proper error handling, and clear variable names. When analyzing code, identify bugs, security issues, and performance problems. You have deep knowledge of C/C++, Windows API, Python, and many other technologies."}}
{"type": "message", "message": {"role": "user", "content": "How should I define tools for an LLM agent securely in Python? Show JSON-schema tool definitions with allowlisting and sandboxed file execution, not raw exec. Include code examples."}}
{"type": "message", "message": {"role": "assistant", "content": "**Define tools as JSON schemas + an allowlisted dispatcher. Never `exec()` model output.**\n\n```python\nimport json\nimport pathlib\n\n# Allowlist: only these files may be read\nALLOWED_DIR = pathlib.Path(\"./data\").resolve()\n\ndef read_file(path: str) -> str:\n p = (ALLOWED_DIR / path).resolve()\n if ALLOWED_DIR not in p.parents and p != ALLOWED_DIR:\n raise ValueError(\"path escapes allowed dir\")\n return p.read_text(encoding=\"utf-8\")\n\ndef search_web(query: str) -> str:\n # stub: call your search API here with timeout + validation\n if not query or len(query) > 300:\n raise ValueError(\"bad query\")\n return f\"results for: {query}\"\n\nTOOLS = {\n \"web_search\": ( # (name, desc, props, required) for schema builder\n \"web_search\", \"Search the web for information\",\n {\"query\": {\"type\": \"string\"}}, [\"query\"],\n ),\n \"read_file\": (\n \"read_file\", \"Read a file under ./data\",\n {\"path\": {\"type\": \"string\"}}, [\"path\"],\n ),\n}\n\nDISPATCH = {\"web_search\": lambda a: search_web(a[\"query\"]),\n \"read_file\": lambda a: read_file(a[\"path\"])}\n\ndef execute_tool(name: str, arguments_json: str) -> str:\n if name not in DISPATCH:\n raise KeyError(f\"unknown tool: {name}\")\n args = json.loads(arguments_json or \"{}\")\n return str(DISPATCH[name](args))\n```\n\n**Best practices:**\n1. Clear descriptions so the model picks correctly\n2. Minimal tools - too many confuse routing\n3. Validate args, sandbox I/O, timeouts, no raw `exec`/`eval`\n4. Verify tool output before acting on it"}}