Skip to content

Ollama

The UC AI Ollama package integrates the open-source language models of a locally running Ollama instance into your Oracle database applications.

  • Support for popular open-source models (OpenAI GPT-OSS, Facebook Llama, Mistral, Alibaba Qwen, DeepSeek, Google Gemma, Microsoft Phi, CodeLlama)
  • Full function calling (tools) support
  • Reasoning capabilities with models that support it
  • Multi-modal support (text and images) with models that support it. Ollama cannot read a PDF: it answers HTTP 400: file inputs are not currently supported. Convert each page to a PNG file and send one image for each page.
  • No API keys required (runs locally)

As of v26.2, UC AI routes Ollama calls through Ollama’s OpenAI-compatible Responses API endpoint by default. To fall back to the legacy chat endpoint, set:

uc_ai_ollama.g_use_responses_api := false;
  1. Install and run Ollama on your local machine or server
  2. Download the models you want to use
  3. Make sure that the Oracle database can access the Ollama API endpoint — you still need a network ACL for the Ollama host (e.g. localhost), see Network Setup (ACL & Wallet)

You can follow the Ollama installation guide for detailed instructions on setting up Ollama. You can run it containerized or directly on your machine, or install the MacOS/Windows GUI application.

By default, the package connects to http://localhost:11434/api/chat. If your Ollama instance runs on a different host or port, set the uc_ai.g_base_url global variable before you call uc_ai.generate_text.

-- In my case the database is running in a Docker container, so I use the host's internal address
uc_ai.g_base_url := 'host.containers.internal:11434/api';
-- instead you can use any ip or hostname
uc_ai.g_base_url := 'example-ollama-server.com:11434/api';

Ollama supports a wide range of open-source models, you can browse them on the Ollama website. The UC AI Ollama package does not come with pre-defined model constants as you can only use models that you have downloaded and installed locally. So instead create your own constants like qwen3:8b for the 8 billion parameter Qwen 3 model.

declare
l_result json_object_t;
begin
uc_ai.g_base_url := 'host.containers.internal:11434/api';
l_result := uc_ai.generate_text(
p_user_prompt => 'What is Oracle APEX?',
p_provider => uc_ai.c_provider_ollama,
p_model => 'qwen3:8b'
);
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 => 'Write a SQL query to find all employees hired this year',
p_system_prompt => 'You are a helpful SQL expert. Write clean, efficient queries.',
p_provider => uc_ai.c_provider_ollama,
p_model => 'gpt-oss:20b'
);
dbms_output.put_line('SQL Query: ' || l_result.get_string('final_message'));
end;
/

Refer to the list of models that support tools to find models that can use function calling. Small models can support tools, but they are less capable in complex tasks. If you enable reasoning, the model can call the tools more effectively.

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

Refer to the list of models that support vision to find models that can use to analyze images.

Refer to the file analysis guide for examples on how to analyze images.

Make sure that you use a model that supports reasoning, such as qwen3:8b or deepseek-r1:8b. Here is a list of models that support reasoning.

Make sure to set the uc_ai.g_enable_reasoning variable to true in the package specification.

declare
l_result json_object_t;
begin
uc_ai.g_base_url := 'host.containers.internal:11434/api';
uc_ai.g_enable_reasoning := true;
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_ollama,
p_model => 'qwen3:8b'
);
dbms_output.put_line('AI Response: ' || l_result.get_string('final_message'));
end;
/

UC AI supports embedding generation with Ollama. You first have to find and download one of the embedding models which are listed here. Then you can start generating embeddings:

declare
l_result json_array_t;
l_array_clob clob;
begin
uc_ai.g_base_url := 'host.containers.internal:11434/api';
uc_ai.g_enable_tools := false; -- disable tools for this test
uc_ai.g_enable_reasoning := false; -- disable reasoning for this test
l_result := uc_ai.generate_embeddings(
p_input => json_array_t('["APEX Office Print lets you create and manage print jobs directly from your APEX applications.", "UC AI allows you to easily use AI from PL/SQL"]'),
p_provider => uc_ai.c_provider_ollama,
p_model => 'granite-embedding:30m'
);
-- l_result.get_size → 2 (one embedding per input string)
-- l_result.get(0) → JSON array of embedding values for first input
end;

For more information about the generate_embeddings function, visit its documentation page.

uc_ai.ocr with uc_ai.c_provider_ollama reads an image with a vision model. Ollama has no default OCR model, so p_model is required. Use a model that supports vision, for example gemma4:26b. Set uc_ai.g_base_url as described in Configuration.

Ollama reads images only. A PDF raises ORA-20508, because PL/SQL cannot make an image of a PDF page. Use an image with an opaque background. Ollama flattens a transparent image on a black background and returns no error. The result has one page, no blocks and no confidence, and usage holds the token counts.

declare
l_text clob;
begin
l_text := uc_ai.ocr_text(
p_document => your_jpeg_blob,
p_media_type => 'image/jpeg',
p_provider => uc_ai.c_provider_ollama,
p_model => 'gemma4:26b'
);
end;
/

The OCR reference describes the options such as prompt and system, the result and the errors.