Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

3.5 KB
Raw Blame
import { createAction, Property } from '@activepieces/pieces-framework';
import { GoogleGenAI } from '@google/genai';
import { googleGeminiAuth } from '../auth';
import { getGeminiEmbeddingModelOptions } from '../common/common';
import { generateEmbeddingsActionOutputSchema } from '../output-schemas';

export const generateEmbeddingsAction = createAction({
  audience: 'both',
  name: 'generate_embeddings',
  classification: 'READ',
  auth: googleGeminiAuth,
  displayName: 'Generate Embeddings',
  description: 'Turns text into a list of numbers for search and similarity.',
  aiMetadata: {
    description:
      'Converts input text into a numerical embedding vector for semantic search, similarity comparison, clustering, or classification. Set Task Type to the intended use (e.g. retrieval_document when indexing content, retrieval_query when searching it) for a better-tuned vector. Idempotent: the same text and settings always produce the same vector.',
    idempotent: true,
  },
  propertyGroups: [
    { key: 'content', display: 'section', label: 'Content', icon: 'text', props: ['text'] },
    { key: 'settings', display: 'section', label: 'Settings', icon: 'sliders', props: ['model', 'taskType'] },
  ],
  props: {
    text: Property.LongText({
      displayName: 'Text',
      required: true,
      description: 'The text to generate an embedding for.',
    }),
    model: Property.Dropdown({
      displayName: 'Model',
      description: 'Embedding model that turns the text into numbers.',
      required: true,
      auth: googleGeminiAuth,
      refreshers: [],
      defaultValue: 'gemini-embedding-001',
      options: async ({ auth }) => getGeminiEmbeddingModelOptions({ auth }),
    }),
    taskType: Property.StaticDropdown({
      displayName: 'Task Type',
      description: 'Tunes the embedding for how it will be used.',
      required: false,
      options: {
        disabled: false,
        options: [
          { label: 'Retrieval Query (searching)', value: 'RETRIEVAL_QUERY' },
          { label: 'Retrieval Document (indexing)', value: 'RETRIEVAL_DOCUMENT' },
          { label: 'Semantic Similarity', value: 'SEMANTIC_SIMILARITY' },
          { label: 'Classification', value: 'CLASSIFICATION' },
          { label: 'Clustering', value: 'CLUSTERING' },
          { label: 'Question Answering', value: 'QUESTION_ANSWERING' },
        ],
      },
    }),
    title: Property.ShortText({
      displayName: 'Document Title',
      description: 'Only used when Task Type is Retrieval Document.',
      required: false,
      advanced: true,
    }),
    outputDimensionality: Property.Number({
      displayName: 'Vector Dimensions',
      description: 'Shorten the vector to this many numbers. Empty: full length.',
      required: false,
      advanced: true,
    }),
  },
  outputSchema: generateEmbeddingsActionOutputSchema,
  async run({ auth, propsValue }) {
    const { text, model, taskType, title, outputDimensionality } = propsValue;

    const genAI = new GoogleGenAI({ apiKey: auth.secret_text });

    const response = await genAI.models.embedContent({
      model,
      contents: text,
      config: {
        taskType,
        ...(taskType === 'RETRIEVAL_DOCUMENT' ? { title } : {}),
        outputDimensionality,
      },
    });

    const embedding = response.embeddings?.[0];

    if (!embedding?.values) {
      throw new Error('No embedding returned from model response.');
    }

    return {
      embedding: embedding.values,
      dimensions: embedding.values.length,
    };
  },
});