Skip to content

Functions:generativeAI

Emiel Wit edited this page Nov 9, 2023 · 20 revisions

generativeAI Function

Description

The generativeAI function is an asynchronous function that interacts with a Generative AI model.

Parameters

The helper function expects GenerativeAIArguments as an argument.

const largeLanguageModels = [
  'gpt-4',
  'gpt-4-16k',
  'gpt-3.5-turbo',
  'gpt-3.5-turbo-16k',
] as const;

interface GrimoireAuth {
  apiKey: string;
}

type LargeLanguageModel = (typeof largeLanguageModels)[number];

type Agent =
  | 'text-to-text'
  | 'text-to-object'
  | 'text-to-choice'
  | 'text-to-array';

interface Choice {
  choice: string;
  description: string;
}

interface Model {
  model: LargeLanguageModel;
  settings: {
    maxNewTokens: number;
    temperature: number;
  };
}

interface GenerativeAIResult {
  result: string | null;
}

interface GenerativeAIArguments {
  agent?: Agent;
  authorization: GrimoireAuth;
  params: {
    variables?: Record<string, string>;
    prompt: string;
    model: Model;
    choices?: Choice[];
  };
}
  • agent: This is a string that represents the type of agent. The default value is text-to-text.
  • authorization: This is an object that contains the apiKey. The default value is an empty string.
  • params: This is an object that contains the following properties:
    • prompt: This is a string that represents the prompt for the AI model. The default value is an empty string.
    • variables: This is an object that represents the values that are used inside the prompt with {{question}}.
    • model: This is an object that contains the following properties:
      • model: This is a string that represents the model to be used.
      • settings: This is an object that contains the following properties:
        • maxNewTokens: This is a number that represents the maximum number of new tokens. The default value is 1024.
        • temperature: This is a number that represents the temperature for the AI model. The default value is 0.2.

Real life example

In this example we want to translate text to French.

const options = {
  agent: 'text-to-text',
  authorization: {
    apiKey: 'your-api-key',
  },
  params: {
    prompt: 'Translate the following English text to French: {{text_to_translate}}',
    variables: {
      text_to_translate: "Hello, how are you?"
    }
    model: {
      model: 'gpt-4',
      settings: {
        maxNewTokens: 1024,
        temperature: 0.5,
      },
    },
  },
};

const { result } = await generativeAI(options);  

Returns

The function returns a Promise that resolves to a GenerativeAIResult object.

interface GenerativeAIResult {
  result: string | null;
}

Errors

The function throws an error if:

  • The provided model is not included in the largeLanguageModels array.
  • The provided agent is not a key in the agents object.
  • No API key is provided.

Example

const options = {
  agent: 'text-to-text',
  authorization: {
    apiKey: 'your-api-key',
  },
  params: {
    prompt: 'your-prompt',
    model: {
      model: 'your-model',
      settings: {
        maxNewTokens: 1024,
        temperature: 0.2,
      },
    },
  },
};

const { result } = await generativeAI(options)

Note

Ensure that you have the necessary permissions and valid API key to interact with the Generative AI model.

Clone this wiki locally