Function import¶
When working with transforms in Alan AI, you can import functions to process input data and shape it into the desired output. Alan AI offers two ways to import functions:
Using the import statement: you can pull in functions defined in the dialog script. Imported functions can be used to process data or apply specific logic as part of the query transformation.
Using EJS (Embedded JavaScript): you can embed a function output directly in the transform instruction using EJS.
Import statement¶
Assume you want to import a function that returns a list of all cloud services from the database. To do this, you need to:
Define the
getServices()
function with the JSDOC description in the dialog script:async function getData() { // Make the GET request to fetch virtual infrastructure data from the database // ... } /** * @tool: Returns an array containing cloud services * @param: * @return: An array with a short description of objects with the following fields: * [ * { * "errorRate": "Percentage representing the error rate", * "id": "Unique identifier for the cloud service", * "lastCheck": "ISO formatted date and time of the last status check", * "name": "Name of the cloud service", * "responseTime": "Average response time in milliseconds", * "status": "Current status of the cloud service (e.g., operational, degraded)", * "uptime": "Percentage representing the service uptime" * }, * ... * ] */ async function getServices() { const data = await getData(); console.log(data.cloudServices); return data.cloudServices; }
Import the
getServices()
function in the general instruction of the query transform:Provide a description in natural language how to use this function to get the desired output:
Add a dynamic corpus to use the created query transform:
corpus({ title: `Infrastructure requests`, query: transforms.query, priority: 1 });
EJS¶
Alan AI supports embedding function outputs directly into the transform instruction using EJS. To do this, use the <%- %>
tag, which inserts content directly into the output without escaping.
Assume you want the AI agent to answer questions about common cloud abbreviations. You can:
Add a function to return a list of abbreviations and their definitions:
function getCloudAbbreviations() { return ` API, or Application Programming Interface, is a set of protocols and tools that allow software applications to communicate with each other. In cloud computing, APIs enable developers to programmatically manage and integrate cloud services, automate infrastructure tasks, and interact with cloud-based resources. CDN, or Content Delivery Network, is a distributed network of servers that helps deliver web content faster by caching it in multiple geographic locations. CDNs are crucial for improving the performance of cloud-hosted websites and applications by reducing latency for users around the world. DNS, or Domain Name System, is a service that translates human-readable domain names, like www.example.com, into IP addresses that computers use. DNS plays a vital role in cloud computing by directing user traffic to the correct servers and supporting the scalability of global applications. IaaS, or Infrastructure as a Service, is a cloud computing model that provides on-demand access to essential computing, networking, and storage resources. IaaS allows businesses to run applications without investing in physical hardware, offering a flexible and scalable cloud infrastructure. SLA, or Service Level Agreement, is a formal contract between a cloud provider and a customer that outlines the expected level of service. It defines measurable performance metrics such as uptime, response time, and support availability. For instance, a common SLA might guarantee 99.9% service uptime. VM, or Virtual Machine, is a software-based emulation of a physical computer. It allows multiple operating systems to run on a single physical machine by partitioning resources like CPU, memory, and storage. VMs are often used in cloud computing to deploy scalable and isolated workloads. VPC, or Virtual Private Cloud, is a virtual network dedicated to your cloud account. It allows you to define custom network configurations, including subnets, IP address ranges, and routing. This creates a secure environment for running cloud resources isolated from other networks.` }
Import the
getCloudAbbreviations()
function in the general instruction of the query transform using the EJS syntax:<%- getCloudAbbreviations() %>
Provide a description in natural language how to use this function to get the desired output:
Add a dynamic corpus to use the created query transform:
corpus({ title: `Abbreviation requests`, query: transforms.query, priority: 1 });