Allow to pass arguments to aiprompt programatically

Created on 23 April 2024, 8 months ago
Updated 30 July 2024, 5 months ago

Problem/Motivation

Passing arguments to the prompt can enable to use prompts contextually.
With arguments developer will be able to:

  • Fill various prompt segment configuration dynamically based on context where prompt is being used.
  • Create "Retrieval augmented generation" (RAG) applications by passing user input
  • Provide entity token by passing entity object

Proposed resolution

Create user interface (UI) to pre-define what arguments this prompt entity accepts.

Having this definition would make clear what arguments can be provided and what are available. Well defined tokens could be generated accordingly.

Having UI also means user can provide default values, meaning arguments are always useful (and can be used and referenced) and prompt is always usable with these defaults.

Remaining tasks

1. Implement plugin system for arguments. Create argument plugins for each argument type: string, integer, boolean, date, entity (reference)... Plugins will provide widget to set argument default value and validate that form element.

2. Create routes and forms for argument operations (add, edit, remove).

3. Create UI: arguments table below segments table.

Table columns:

  • name
  • type (string/integer/boolean/entity/etc)
  • default_value
  • description
  • [Operation "Edit"]
  • [Operation "Remove"]

Below a table, have button named "Add new".

Remove operation have confirmation form.

4. Improve UX further by editing arguments on the same page. When clicking add / edit / remove operation:

  • A) A new form could expand below with ajax (these inline forms would have good UX, so this would be prefered, but I am not aware of established Drupal pattern embedding forms within forms inline).
  • B) Another approach is to implement with modal or off-canvas dialog (this is common and documented workflow)

5. Define new API to set arguments:

$prompt->setArgument($key, $value);
$prompt->getArgument($key);
$prompt->getArguments();

Example:

$entity = \Drupal::entityTypeManager()->getStorage('aiprompt')->load('[enter_prompt_id]');

// pass variable example
$entity->setArgument('user_chat_message_string', $chat_user_message_string);
$entity->setArgument('user_input', $user_input);
$entity->setArgument('some_node', $node);

// get prompt output
$string = $entity->toString();

6. Do ✨ Implement tokens support Active [done]

7. Generate argument tokens to be used when user configures prompt segments. Example:

  [argument:user_input]
  [argument:some_node:title]

User interface changes

API changes

Data model changes

✨ Feature request
Status

Active

Version

1.0

Component

Code

Created by

🇱🇹Lithuania mindaugasd

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024