3.3 CHAT Function Signature 2

This function chats with a generative AI service given a prompt and potential earlier responses.

Syntax

APEX_AI.CHAT (
    p_prompt                     IN              CLOB,
    p_system_prompt              IN              CLOB                DEFAULT NULL,
    p_service_static_id          IN              VARCHAR2            DEFAULT NULL,
    p_temperature                IN              NUMBER              DEFAULT NULL,
    p_messages                   IN OUT NOCOPY   t_chat_messages,
    p_tools                      IN              t_tools             DEFAULT NULL,
    p_response_handler_procedure IN              VARCHAR2            DEFAULT NULL,
    p_max_tool_roundtrips        IN              PLS_INTEGER         DEFAULT NULL )
    RETURN CLOB;

Parameters

Parameter Description
p_prompt The user prompt.
p_system_prompt (Optional) The instructions defining the AI's role, rules, and behavior.
p_service_static_id The Generative AI Service static ID. If not provided, uses the app's default AI service.
p_temperature The temperature to use. How the temperature is interpreted depends on the generative AI service implementation. Higher temperatures result in more "creative" responses. See the documentation of the generative AI provider for details and allowed values.
p_messages The responses from an earlier conversation. Responses are automatically added to p_messages.
p_tools Optional collection of tool definitions the AI provider may call while generating a response. Tools enable function calling to retrieve additional data or trigger actions.
p_response_handler_procedure Optional PL/SQL procedure invoked to post-process provider responses. Use to customize how tool calls or partial responses are handled before returning the final result.
p_max_tool_roundtrips (Optional) Limit the number of network roundtrips that can be made when responding to tool calls.

Returns

The response for the given prompt.

Example

The following example chats with the configured Generative AI Service MY_AI_SERVICE. In the first interaction, a system prompt is given. In further interactions, the context is passed to the Generative AI service in the form of parameter p_messages.

DECLARE
  l_messages  apex_ai.t_chat_messages := apex_ai.c_chat_messages;
  l_response1 clob;
  l_response2 clob;
BEGIN
  l_response1 := apex_ai.chat(
    p_prompt            => 'What is Oracle APEX',
    p_system_prompt     => 'I am an expert in low-code Application Platforms',
    p_service_static_id => 'MY_AI_SERVICE',
    p_messages          => l_messages );
  l_response2 := apex_ai.chat(
    p_prompt            => 'What is new in the latest release',
    p_service_static_id => 'MY_AI_SERVICE',
    p_messages          => l_messages );
END;