Overview
Custom Code serves as a flexible extension point for your Prisme.ai solutions:Code Execution
Run custom JavaScript and Python code in a secure environment
Data Transformation
Transform data with complex logic and algorithms
Integration Bridge
Connect to systems without pre-built integrations
Advanced Processing
Implement sophisticated data processing and analytics
Key Features
A secure, controlled environment for running code:
- JavaScript Runtime: Execute JavaScript/TypeScript
- Python Runtime: Run Python 3.x code
- Isolation: Code runs in a containerized environment
- Resource Limits: CPU, memory, and execution time controls
- Version Management: Control runtime versions
How Custom Code Works
Custom Code functions are defined, managed, and executed through a structured process:1
Function Definition
Create a function with code, parameters, and metadata:
- Write the function code
- Define input parameters with types and validation
- Specify output format
- Add documentation
2
Storage and Management
Store functions in the Custom Code repository:
- Save to the function library
- Version and track changes
- Organize by category or purpose
- Control access permissions
3
Execution from Workflows
Call functions from automations:
- Reference the function by name
- Provide required parameters
- Map workflow data to parameters
- Capture the function output
4
Runtime Execution
Execute the function in a secure environment:
- Validate input parameters
- Run the code in an isolated container
- Monitor execution and resource usage
- Handle errors and exceptions
5
Result Processing
Return the result to the calling workflow:
- Format the output according to specifications
- Pass the result back to the workflow
- Log execution details
- Track performance metrics
JavaScript Functions
JavaScript functions in Custom Code allow you to implement a wide range of data processing and transformation tasks:Function Structure
Function Structure
The basic structure of a JavaScript function:This function takes a data parameter, transforms it, and returns the result.
Parameter Configuration
Parameter Configuration
Configure function parameters for validation and documentation:This configuration defines a
data
parameter of type array
with a description and default value.Advanced Example
Advanced Example
A more sophisticated JavaScript function:This function classifies text based on keyword matching against categories.
Available Libraries
Available Libraries
JavaScript functions have access to built-in libraries whitelisted by your orgnization registry.These libraries help you accomplish common tasks without reinventing the wheel.
Python Functions
Python functions in Custom Code provide access to Python’s rich ecosystem for data analysis, machine learning, and scientific computing:Function Structure
Function Structure
The basic structure of a Python function:This function takes a list of numeric values and returns basic statistical measures.
Parameter Configuration
Parameter Configuration
Configure Python function parameters:This configuration is similar to JavaScript function parameters, providing type information, description, and default values.
Advanced Example
Advanced Example
A more sophisticated Python function:This function performs K-means clustering on numeric data and returns the clustered data, cluster centers, and statistics.
Available Libraries
Available Libraries
Python functions have access to popular data science and utility libraries whitelisted by your DevSecOps Teams.These libraries enable sophisticated data analysis and machine learning within your workflows.
Integration with Prisme.ai Workflows
Custom Code is designed to integrate seamlessly with Prisme.ai automations:1
Calling Functions from Automations
Reference Custom Code functions in your automation YAML:This automation calls the
processData
function with input from the payload and emits the result as an event.2
Handling Function Results
Process the output from Custom Code functions:This example shows conditional logic based on the function’s output.
3
Error Handling
Implement robust error handling for Custom Code execution:This approach captures and logs errors from function execution.
Common Use Cases
Custom Code enables a wide range of use cases:Data Transformation
Implement complex data transformations:
- Format conversion
- Schema mapping
- Data normalization
- Content extraction
Advanced Analysis
Perform sophisticated data analysis:
- Statistical calculations
- Pattern recognition
- Trend identification
- Risk assessment
Integration Logic
Create custom integration components:
- API request formatting
- Response processing
- Protocol implementations
- Legacy system connections
Business Logic
Implement specialized business rules:
- Pricing calculations
- Eligibility determinations
- Approval workflows
- Validation logic
Example: Data Classification
A common use case for Custom Code is classifying data based on content. Here’s an example that classifies documents:Example: Advanced Data Analysis with Python
When you need sophisticated data analysis, Python’s ecosystem provides powerful capabilities:Best Practices
Follow these recommendations to get the most from Custom Code:Code Quality
Code Quality
Maintain high standards of code quality:
- Write clean, readable code with proper indentation
- Include comments to explain complex logic
- Use meaningful variable and function names
- Break complex functions into smaller, focused ones
- Add error handling for robustness
- Validate inputs and handle edge cases
Performance Optimization
Performance Optimization
Optimize code for efficient execution:
- Minimize external API calls
- Use efficient data structures and algorithms
- Avoid unnecessary loops and iterations
- Process only the data you need
- Use built-in functions and libraries when available
- Consider the impact of large datasets
Security Considerations
Security Considerations
Implement secure coding practices:
- Validate and sanitize all inputs
- Avoid hardcoded credentials
- Use platform security features
- Be cautious with external libraries
- Implement proper error handling
- Follow the principle of least privilege
Testing and Debugging
Testing and Debugging
Thoroughly test your functions:
- Test with various input scenarios
- Check edge cases (empty data, large data, etc.)
- Verify error handling
- Use logging for debugging
- Validate outputs against expected results
- Update tests when functions change
Limitations and Considerations
When using Custom Code, be aware of these considerations:- Execution Environment: Code runs in a controlled environment with resource limits. Very compute-intensive operations may not be suitable.
- External Access: For security reasons, network access is restricted. External API calls and file system access follow platform security policies.
- Runtime Duration: Functions have maximum execution times. Long-running operations should be designed with this in mind.
- Library Availability: While many common libraries are available, some specialized packages may not be pre-installed. Check documentation for the current list.
- State Management: Functions are stateless by default. Persistent state should be stored using platform services like Collection.
Integration with Other Prisme.ai Products
Custom Code works seamlessly with other Prisme.ai products:Enhance AI Knowledge with custom processing:
- Pre-process documents before ingestion
- Create custom embedding algorithms
- Implement specialized reranking logic
- Generate synthetic training data
- Analyze query patterns and performance