Skip to content

xAI

The UC AI xAI package integrates xAI’s Grok models into your Oracle database applications.

  • Support for the current Grok models (Grok 4.6, Grok 4.5, Grok 4.3, Grok 4.20)
  • Full function calling (tools) support
  • Reasoning capabilities with reasoning effort control
  • Multi-modal support (text, images)
  • Structured output support
  1. xAI API key
  2. Oracle database with internet access to xAI’s API endpoints (see Network Setup (ACL & Wallet))
  3. UC AI package installed
  4. Set up API key (guide)

You can get an API key by signing up at xAI Console.

To use your xAI key with APEX Web Credentials, create a new web credential in your APEX workspace (Workspace Utilities → Web Credentials) with the following configuration:

  • Authentication Type: HTTP Header
  • Credential Name: Authorization
  • Credential Secret: Bearer <your_api_key>

Also set Valid for URLs to the xAI API (https://api.x.ai/) to limit the scope of the credential.

To use the web credential in your PL/SQL code, set the package variable uc_ai_xai.g_apex_web_credential to the static ID of your web credential before calling any UC AI functions:

uc_ai_xai.g_apex_web_credential := 'XAI';

xAI offers a range of Grok models optimized for different use cases. The UC AI xAI package provides constants for the current models:

The latest and most capable models in the Grok family:

  • uc_ai_xai.c_model_grok_4_7 - Most capable Grok model
  • uc_ai_xai.c_model_grok_4_20_multi_agent - Grok 4.20 with a multi-agent architecture

This list shows the newest model of each family. The package keeps the constants of older models for code that still references them.

  • uc_ai_xai.c_model_grok_build_0_1 - Coding model for agentic development tasks

xAI no longer lists the Grok 2, Grok 3, Grok 4, and Grok 4.1 models, and grok-code-fast-1. The package keeps their constants for code that still references them, but requests with these models can fail.

See xAI’s models documentation for the latest model information and capabilities.

declare
l_result json_object_t;
begin
l_result := uc_ai.generate_text(
p_user_prompt => 'What is Oracle APEX?',
p_provider => uc_ai.c_provider_xai,
p_model => uc_ai_xai.c_model_grok_4_6
);
dbms_output.put_line('AI Response: ' || l_result.get_string('final_message'));
end;
/
declare
l_result json_object_t;
begin
l_result := uc_ai.generate_text(
p_user_prompt => 'I have tomatoes, salad, potatoes, olives, and cheese. What can I cook with that?',
p_system_prompt => 'You are an assistant helping users to get recipes. Please answer in short sentences.',
p_provider => uc_ai.c_provider_xai,
p_model => uc_ai_xai.c_model_grok_4_6
);
dbms_output.put_line('Recipe: ' || l_result.get_string('final_message'));
end;
/

All Grok models support tools/function calling. You can define tools in your application and Grok will call them as needed.

declare
l_result json_object_t;
begin
-- Ensure tools are set up in UC_AI_TOOLS table
uc_ai.g_enable_tools := true;
l_result := uc_ai.generate_text(
p_user_prompt => 'What is the email address of Jim?',
p_system_prompt => 'You are an assistant to a time tracking system. Your tools give you access to user, project and timetracking information. Answer concise and short.',
p_provider => uc_ai.c_provider_xai,
p_model => uc_ai_xai.c_model_grok_4_6
);
dbms_output.put_line('AI Response: ' || l_result.get_string('final_message'));
end;
/

See the tools guide for details on how to set up and use tools.

Grok models support vision capabilities for image analysis. PDF analysis is not implemented in UC AI yet, because xAI uses a different approach for it.

declare
l_messages json_array_t := json_array_t();
l_content json_array_t := json_array_t();
l_result json_object_t;
begin
l_messages.append(uc_ai_message_api.create_system_message(
'You are an image analysis assistant.'));
l_content.append(uc_ai_message_api.create_file_content(
p_media_type => 'image/webp',
p_data_blob => your_image_blob,
p_filename => 'image.webp'
));
l_content.append(uc_ai_message_api.create_text_content(
'What is depicted in the attached image?'
));
l_messages.append(uc_ai_message_api.create_user_message(l_content));
l_result := uc_ai.generate_text(
p_messages => l_messages,
p_provider => uc_ai.c_provider_xai,
p_model => uc_ai_xai.c_model_grok_4_6
);
dbms_output.put_line('Analysis: ' || l_result.get_string('final_message'));
end;
/

xAI’s Grok models support reasoning. You can control the reasoning effort level to balance speed and cost:

declare
l_result json_object_t;
begin
uc_ai.g_enable_reasoning := true;
uc_ai_xai.g_reasoning_effort := 'high'; -- 'low', 'high'
l_result := uc_ai.generate_text(
p_user_prompt => 'Answer in one sentence. If there is a great filter, are we before or after it and why.',
p_provider => uc_ai.c_provider_xai,
p_model => uc_ai_xai.c_model_grok_4_6
);
dbms_output.put_line('AI Response: ' || l_result.get_string('final_message'));
end;
/

You can also use the main package reasoning level setting. As xAI does not have a medium setting it will get remapped to low.

declare
l_result json_object_t;
begin
uc_ai.g_enable_reasoning := true;
uc_ai.g_reasoning_level := 'low';
l_result := uc_ai.generate_text(
p_user_prompt => 'Your complex question here',
p_provider => uc_ai.c_provider_xai,
p_model => uc_ai_xai.c_model_grok_4_6
);
dbms_output.put_line('AI Response: ' || l_result.get_string('final_message'));
end;
/

The reasoning effort parameter controls how many reasoning tokens are generated before creating a response. Higher effort levels result in more thorough reasoning but take longer and cost more.

Refer to xAI’s reasoning guide for advanced usage and best practices.

xAI supports structured output with JSON schemas:

declare
l_result json_object_t;
l_schema json_object_t;
l_final_message clob;
l_structured_output json_object_t;
begin
-- Define your JSON schema
l_schema := json_object_t('{
"name": "confidence_response",
"strict": true,
"schema": {
"type": "object",
"properties": {
"response": {
"type": "string",
"description": "The answer to the question"
},
"confidence": {
"type": "number",
"description": "Confidence level between 0 and 1"
}
},
"required": ["response", "confidence"],
"additionalProperties": false
}
}');
l_result := uc_ai.generate_text(
p_user_prompt => 'What is the capital of France? Please respond with confidence.',
p_system_prompt => 'You are a helpful assistant that provides accurate information.',
p_provider => uc_ai.c_provider_xai,
p_model => uc_ai_xai.c_model_grok_4_6,
p_response_json_schema => l_schema
);
l_final_message := l_result.get_clob('final_message');
l_structured_output := json_object_t(l_final_message);
dbms_output.put_line('Response: ' || l_structured_output.get_string('response'));
dbms_output.put_line('Confidence: ' || l_structured_output.get_number('confidence'));
end;
/