Local functions allow your AI agents to interact with your local system through predefined functions. This enables capabilities like file operations while maintaining security through controlled access.

Defining Local Tools

First, define your local tools with their specifications:

localTools = [
    {
        "type": "function",
        "function": {
            "name": "read-file",
            "description": "Reads file content from the local system within current directory",
            "parameters": {
                "type": "object",
                "properties": {
                    "bodyParams": {
                        "type": "object",
                        "properties": {
                            "filePath": {
                                "type": "string",
                                "description": "File name to read",
                                "examples": ["file.txt", "data.csv"]
                            },
                            "outputFormat": {
                                "type": "string",
                                "description": "Output format (json/text/xml/csv)",
                                "examples": ["json", "text", "xml", "csv"]
                            }
                        },
                        "required": ["filePath"]
                    }
                },
                "required": ["bodyParams"]
            }
        }
    },
    {
        "type": "function",
        "function": {
            "name": "write-file",
            "description": "Writes content to a file in current directory",
            "parameters": {
                "type": "object",
                "properties": {
                    "bodyParams": {
                        "type": "object",
                        "properties": {
                            "fileName": {
                                "type": "string",
                                "description": "File name to write"
                            },
                            "fileContent": {
                                "type": "string",
                                "description": "Content to write"
                            },
                            "fileType": {
                                "type": "string",
                                "description": "File format (json/txt/csv/xml)"
                            }
                        },
                        "required": ["fileName", "fileContent", "fileType"]
                    }
                },
                "required": ["bodyParams"]
            }
        }
    }
]

Implementation Examples

File Operation Functions

Here are the core file operation functions used by the local tools:

def write_file(file_path, content, fileType):
    """
    Writes content to a file in specified format
    
    Args:
        file_path (str): Target file path
        content (str/list): Content to write
        fileType (str): Format (json/txt/csv/xml)
    """
    # Implementation details...

def read_file(file_path, output_format=None):
    """
    Reads file content with optional format conversion
    
    Args:
        file_path (str): Source file path
        output_format (str): Desired output format
    """
    # Implementation details...

Security Considerations

  1. Path Validation: All file operations are restricted to the current working directory
  2. Format Validation: Only allowed file formats (json/txt/csv/xml) are processed
  3. Error Handling: Comprehensive error handling for all file operations
  4. Input Sanitization: All inputs are validated before processing

Best Practices

  1. Always validate file paths using is_within_current_directory()
  2. Handle file format conversions carefully
  3. Implement proper error handling for all operations
  4. Use appropriate content type validation
  5. Keep security considerations in mind when implementing new local functions