Skip to content

AI assistance

anonympins edited this page Jul 5, 2026 · 2 revisions

AI Assistance: Leverage Artificial Intelligence

The data-primals-engine integrates powerful generative AI capabilities directly into its core, allowing you to automate content creation, analyze data, and enrich your business processes.

Enabling the AI Assistant

To use the AI features, you must first provide API keys for one or more supported AI providers. The engine natively supports providers like OpenAI, Google (Gemini), DeepSeek, and Anthropic.

You can configure your keys in two ways:

  1. Environment Variables: Set the appropriate variable in your .env file. This is the recommended approach for production.
# Choose one or more providers
OPENAI_API_KEY=sk-xxxxxxxxxxxxxxxxxxxx
GOOGLE_API_KEY=AIzaSyxxxxxxxxxxxxxxxxxxxx
DEEPSEEK_API_KEY=sk-xxxxxxxxxxxxxxxxxxxx
ANTHROPIC_API_KEY=sk-xxxxxxxxxxxxxxxxxxxx
  1. env Model: For user-specific keys, you can store them in the env model within the application. The assistant will automatically look for a key belonging to the current user.

Once a key is configured, AI capabilities become available, most notably within the automation engine.

AI in Workflows: The GenerateAIContent Action

The most powerful way to use AI in data-primals-engine is by integrating it into your Workflows. The GenerateAIContent action type allows you to call an AI model as a step in any automated process.

This is perfect for tasks like:

  • Summarizing a new support ticket.
  • Generating a product description when a new product is added.
  • Classifying incoming data based on its content.
  • Translating text.

Example: Automatically Generating a Product Description

Let's create a complete workflow that automatically generates an SEO description whenever a new product is added without one.

This workflow can be created programmatically by making the following API calls from your JavaScript code.

1. The workflow

First, we define the main workflow. This document acts as a container for the steps and logic.

await insertData("workflow", {
    "name": "Generate Product SEO Description",
    "description": "When a new product is created without an SEO description, this workflow uses AI to generate one based on the product's name.",
    "startStep": { "$find": { "name": "Generate and Update Description" } }
});

2. The workflowAction documents

Next, we define the individual actions that will be performed. We need two actions: one to generate the content with AI, and another to update the product with that content. We use insertData to create both in a single call.

await insertData("workflowAction", [
  {
    "name": "Generate SEO Content",
    "type": "GenerateAIContent",
    "aiProvider": "OpenAI",
    "aiModel": "gpt-4o-mini",
    "prompt": "Write a short, engaging, and SEO-optimized product description (30-40 words) for the following product: '{triggerData.name}'. Return only the description text, without any introductory or concluding phrases."
  },
  {
    "name": "Update Product with AI Content",
    "type": "UpdateData",
    "targetModel": "product",
    "targetSelector": { "_id": "{triggerData._id}" },
    "fieldsToUpdate": { "seoDescription": "{context.aiContent}" }
  }
]);

Note: The targetSelector and fieldsToUpdate use placeholders. {triggerData._id} refers to the ID of the product that started the workflow, and {context.aiContent} refers to the text generated by the previous AI action.

3. The workflowStep

Now, we create a step that groups our actions together. A workflow can have multiple steps that execute in sequence. For this simple case, we only need one, which we create with insertData.

await insertData("workflowStep", {
  "name": "Generate and Update Description",
  "workflow": { "$find": { "name": "Generate Product SEO Description" } },
  "actions": [
    { "$find": { "name": "Generate SEO Content" } },
    { "$find": { "name": "Update Product with AI Content" } }
  ],
  "isTerminal": true
});

4. The workflowTrigger

Finally, we define the trigger that will start the workflow. This trigger will listen for new documents being added (DataAdded) to the product model. It also includes a dataFilter to ensure it only runs if the seoDescription field is empty. We create it with insertData.

await insertData("workflowTrigger", {
  "name": "On New Product Added without SEO Description",
  "workflow": { "$find": { "name": "Generate Product SEO Description" } },
  "type": "manual",
  "onEvent": "DataAdded",
  "targetModel": "product",
  "dataFilter": {
    "$or": [
      { "seoDescription": { "$exists": false } },
      { "seoDescription": "" }
    ]
  },
  "isActive": true
});

With these four API calls, your automated AI content generation workflow is now active. Any new product created without an SEO description will automatically have one generated and saved.

This integration of AI both as an interactive assistant and as an automation component makes data-primals-engine a powerful platform for building intelligent applications.

Clone this wiki locally