pip install openai pydantic python-dotenv
Create the tool functions and their JSON schemas that the agent can call.
1 from openai import OpenAI 2 import json, os 3 4 client = OpenAI() 5 6 TOOLS = [ 7 { 8 "type": "function", 9 "function": { 10 "name": "search_docs", 11 "description": "Search internal documentation for relevant content. Returns top matching passages.", 12 "parameters": { 13 "type": "object", 14 "properties": { 15 "query": {"type": "string", "description": "The search query"}, 16 "max_results": {"type": "integer", "description": "Max results to return (1-10)", "default": 3} 17 }, 18 "required": ["query"] 19 } 20 } 21 }, 22 { 23 "type": "function", 24 "function": { 25 "name": "write_file", 26 "description": "Write content to a local file. Creates file if it doesn't exist.", 27 "parameters": { 28 "type": "object", 29 "properties": { 30 "filename": {"type": "string"}, 31 "content": {"type": "string"} 32 }, 33 "required": ["filename", "content"] 34 } 35 } 36 }, 37 { 38 "type": "function", 39 "function": { 40 "name": "read_file", 41 "description": "Read the contents of a local file.", 42 "parameters": { 43 "type": "object", 44 "properties": {"filename": {"type": "string"}}, 45 "required": ["filename"] 46 } 47 } 48 } 49 ] 50 51 # Tool implementations 52 def search_docs(query: str, max_results: int = 3) -> dict: 53 return {"results": [f"Doc {i}: content about {query}" for i in range(max_results)]} 54 55 def write_file(filename: str, content: str) -> dict: 56 with open(filename, "w") as f: 57 f.write(content) 58 return {"written": len(content), "filename": filename} 59 60 def read_file(filename: str) -> dict: 61 with open(filename) as f: 62 return {"content": f.read()} 63 64 TOOL_MAP = {"search_docs": search_docs, "write_file": write_file, "read_file": read_file} 65
TOOLS list with 3 function schemas passing schema validation
Sign in to share your feedback and join the discussion.