Previous Next JavaScript must be enabled to correctly display this content

## 18.9.4.5 Creating a Dynamic Action to Generate Text with AI

Create a dynamic action to generate a text response from a Generative AI Service.

Note:

Before you can add a dynamic action to generate text with AI, you must complete the steps described in About Including Generative AI in Applications. Failure to create a Generative AI Service or edit the application, AI attributes results in an error.

The following simple example creates a dynamic action hooked to a button that invokes an AI and generates a one-time text response based on user content. Note this example only describes the limited number of attributes. This example assumes you have already created an application and page.

To add a dynamic action to generate one-time text response from an AI Service:

1. Navigate to the application page.
   1. On the Workspace home page, click the App Builder icon.
   2. Select an application.
   3. Select a page.
   Page Designer appears.
2. Create a region named `AI` to contain the button and items:
   1. Right-click Body and select Create Region from the context menu.
      In Page Designer, edit the Region attributes.
   2. Identification, Name - Enter a region name. The region name is used as the runtime region title if the title attribute is empty. For example, enter: `AI`
3. Create a button:
   1. Right-click the region you just created and select Create Button from the context menu.
      In Page Designer, edit the Button attributes.
   2. Identification, Button Name - Enter the name used to reference the button being clicked. This name is used to reference the button. For example, enter: `GENERATE_TEXT`
4. Create the Input Value item:
   1. Right-click the region you just created and select Create Page Item from the context menu.
      In Page Designer, edit the Item attributes.
   2. Identification, Item Name - Enter the name used to reference the button being clicked. This name is used to reference the button. For example, enter: `P2_REQUEST`
5. Create the Use Response item:
   1. Right-click the region you just created and select Create Page Item from the context menu.
      In Page Designer, edit the Item attributes.
   2. Identification, Item Name - Enter the name used to reference the button being clicked. This name is used to reference the button. For example, enter: `P2_RESPONSE`
6. Create a dynamic action that fires when the user clicks a button:
   1. Click the Dynamic Actions tab in the left pane.
   2. Under Events, right-click Click and select Create Dynamic Action.

      A dynamic action named New appears in the left pane. The Property Editor displays Dynamic Action attributes.

7. In the Property Editor, edit the Dynamic Action attributes:
   1. Identification, Name - Enter the name of the dynamic action. For example, enter: `Generate Text with AI`
   2. When, Event - Specify the event that causes the dynamic action to fire. For this example, accept the default, Click
   3. When, Selection Type - Select the type of page element or construct to be used to trigger the event. For this example, select Button.
   4. When, Button - Select the type of page element or construct to trigger the event. For this example, select the button you previously created, GENERATE_TEXT.
      Description of the illustration da_generate_text_with_ai-png.png
8. Edit the True action, Show. In the Dynamic Action tab, click the Show action.

   Action attributes displays in the Property Editor.

9. Identification, Action - Select action you want to perform. Select Generate Text with AI.
10. Generative AI attributes:
    1. Generative AI, Configuration - Select the AI Configuration to use for this component.

       Note that only AI Configurations with Response Format Text are currently supported by this component. To generate a JSON object, use the `APEX_AI.GENERATE` PL/SQL API .

    2. Generative AI, Service - Select the Generative AI Service to be used by the AI Assistant. Application Default uses the Generative AI Service specified on the AI page in the application attributes. See Configuring AI Attributes for an Application.
    3. Generative AI, System Prompt - Specify the system prompt to be used by the AI Assistant. The System Prompt gives the AI a context.

       For this example, enter:

       `Given a user input with a recipe name, reply by just providing a formatted shopping list`

    4. Generative AI, Items to Submit - Enter items to be set into session state when the dynamic action is executed.

       For multiple items, separate each item name with a comma. Type in the name or pick from the list of available items. If you pick from the list and there is already text entered, then a comma is placed at the end of the existing text, followed by the item name returned from the list. The item values are then available for use in AI backend processing, including within the System Prompt or RAG Sources.

11. Input Value attributes:
    1. Input Value, Type - Select how the input value to the AI will be provided. For this example, select Item.
    2. Input Value, Item - Select P2_REQUEST.
12. Use Response attributes:
    1. Use Response, Type - Select where the text generated by the AI should be returned to. For this example, select Item.
    2. Use Response, Item - Select P2_RESPONSE.
       Description of the illustration da_generate_input_response.png
13. View the page. Click Save and Run Page..
    The AI page appears.
14. Test the page:
    1. Request - Enter `Tacos`
    2. Click the Generate Text button.

       A shopping list for tacos appears on the Response field.

       Description of the illustration ai_response.png

Parent topic: Including Generative AI in Applications