OceanBase
English日本語简体中文
Docs Home
Products
Integrations
Release Notes

Databases

OceanBase DatabaseOceanBase Cloud OceanBase Best Practices

Tools

OceanBase Cloud PlatformOceanBase Migration ServiceOceanBase Developer CenterOceanBase Migration AssessmentOceanBase Admin ToolOceanBase Loader and DumperOceanBase DeployerKubernetes operator for OceanBaseOceanBase Diagnostic Tool OceanBase Binlog Service

Connectors and Middleware

OceanBase Database Proxy OceanBase Connector/C OceanBase Connector/J OceanBase Connector/ODBC OceanBase Connector/NETOceanBase Command-Line Client

Databases

OceanBase DatabaseOceanBase Cloud OceanBase Best Practices

Connectors and Middleware

OceanBase Database Proxy OceanBase Connector/C OceanBase Connector/J OceanBase Connector/ODBC OceanBase Connector/NETOceanBase Command-Line Client

Tools

OceanBase Cloud PlatformOceanBase Migration ServiceOceanBase Developer CenterOceanBase Migration AssessmentOceanBase Admin ToolOceanBase Loader and DumperOceanBase DeployerKubernetes operator for OceanBaseOceanBase Diagnostic Tool OceanBase Binlog Service
OceanBase DatabaseOceanBase Cloud ToolsConnectors and MiddlewareLifecycle Support Policy

OceanBase Cloud

  • Overview
  • Schema evolution
    • Liquibase
    • Flyway
  • Data ingestion
    • Canal
    • dbt
    • Debezium
    • Flink
    • Glue
    • Informatica Cloud
    • Kafka
    • Maxwell
    • SeaTunnel
    • DataWorks
    • NiFi
  • SQL development
    • DataGrip
    • DBeaver
    • Navicat
    • TablePlus
  • Orchestration
    • DolphinScheduler
    • Linkis
    • Airflow
  • Visualization
    • Grafana
    • Power BI
    • Quick BI
    • Superset
    • Tableau
  • Observability
    • Datadog
    • Prometheus
  • Database management
    • Bytebase
  • AI
    • LlamaIndex
    • Dify
    • LangChain
    • Tongyi Qianwen
    • OpenAI
    • n8n
    • Trae
    • SpringAI
    • Cline
    • Cursor
    • Continue
    • Toolbox
    • CamelAI
    • Firecrawl
    • Hugging Face
    • Ollama
    • Google Gemini
    • Cloudflare Workers AI
    • Qoder
    • OpenCode
    • Claude Code
    • GitHub Copilot
    • Codex
    • Jina AI
    • Augment Code
    • Claude Code
    • Kiro
  • Development tools
    • Cloudflare Workers
    • Vercel

Download PDF

Document Feedback
  1. Documentation Center
  2. OceanBase Cloud

Integrate OceanBase Cloud Vector with Ollama

Last Updated:2026-09-04 07:03:30

Prerequisites
Step 1: Obtain the database connection information
Step 2: Build your AI assistant
Set environment variables
Install Ollama and pull the Qwen3-embedding model
Sample code

OceanBase Database V4.3.3 and later support vector storage, vector indexing, and embedding vector retrieval. You can store vectorized data in OceanBase Database for subsequent retrieval.

Ollama is an open-source tool that allows you to easily run and manage large language models (LLMs) such as gpt-oss, Gemma 3, DeepSeek-R1, and Qwen3 on your local machine.

This tutorial demonstrates how to integrate the vector retrieval feature of OceanBase Cloud with Ollama to perform similarity search and retrieval tasks.

Prerequisites

  • An OceanBase Cloud transactional instance is available in your environment. For instructions on how to create the instance, see Create an transactional instance.

  • You have created a MySQL-compatible tenant in the instance. For instructions on how to create the tenant, see Create a MySQL-compatible tenant.

  • You have a MySQL database and account available under the tenant, and you have granted read and write permissions to the database account. For more information, see Create an account and Create a database (MySQL only).

  • You are a project admin or instance admin and have the permissions required to read and write data in the instance. If not, contact your organization admin to grant the required permissions.

  • You have installed Python 3.11 or later.

  • You have installed the required dependencies.

    python3 -m pip install pyobvector requests sqlalchemy ollama tqdm
    

Step 1: Obtain the database connection information

  1. Log in to the OceanBase Cloud console.

  2. In the instance list page, expand the the information of the target instance.

  3. Select Connect > Get Connection String under the target tenant.

  4. In the pop-up window, select Public Network as the connection method.

  5. Follow the prompts in the pop-up window to obtain the public endpoint and the connection string.

Step 2: Build your AI assistant

Set environment variables

Obtain the OceanBase connection information and configure it to the environment variables.

export OCEANBASE_DATABASE_URL=YOUR_OCEANBASE_DATABASE_URL
export OCEANBASE_DATABASE_USER=YOUR_OCEANBASE_DATABASE_USER
export OCEANBASE_DATABASE_DB_NAME=YOUR_OCEANBASE_DATABASE_DB_NAME
export OCEANBASE_DATABASE_PASSWORD=YOUR_OCEANBASE_DATABASE_PASSWORD

Install Ollama and pull the Qwen3-embedding model

curl -fsSL https://ollama.ai/install.sh | sh
export PATH="/usr/local/bin:$PATH"
ollama --version
ollama pull qwen3-embedding:latest

Sample code

Load data

Here, we use the Qwen3-embedding embedding model as an example. We treat the vector retrieval common questions of OceanBase Vector as private knowledge and separate the content in the file with "#".

import requests,os,json,ollama
from tqdm import tqdm

from sqlalchemy import Column, Integer, String
from pyobvector import ObVecClient, VECTOR, IndexParam, l2_distance

# text URL
url = "https://raw.githubusercontent.com/oceanbase/oceanbase-doc/refs/heads/V4.3.5/en-US/640.ob-vector-search/800.ob-vector-search-faq.md"
response = requests.get(url)
text_lines = []
file_text = response.text
text_lines += file_text.split("# ")

def emb_text(text):
    response = ollama.embeddings(model="qwen3-embedding", prompt=text)
    return response["embedding"]


data = []
for i, line in enumerate(tqdm(text_lines, desc="Creating embeddings")):
    data.append({"text": line, "embedding": emb_text(line)})

Define the vector table structure and store the vectors in OceanBase

Create a table named ollama_oceanbase_demo_documents that contains a text column for storing text, an embedding column for storing embedding vectors, and a vector index. Use the Qwen3-embedding model to generate embedding vectors for each text segment and store the vectors in OceanBase:

OCEANBASE_DATABASE_URL = os.getenv('OCEANBASE_DATABASE_URL')
OCEANBASE_DATABASE_USER = os.getenv('OCEANBASE_DATABASE_USER')
OCEANBASE_DATABASE_DB_NAME = os.getenv('OCEANBASE_DATABASE_DB_NAME')
OCEANBASE_DATABASE_PASSWORD = os.getenv('OCEANBASE_DATABASE_PASSWORD')

client = ObVecClient(uri=OCEANBASE_DATABASE_URL, user=OCEANBASE_DATABASE_USER,password=OCEANBASE_DATABASE_PASSWORD,db_name=OCEANBASE_DATABASE_DB_NAME)
table_name = "ollama_oceanbase_demo_documents"
client.drop_table_if_exist(table_name)

cols = [
    Column("id", Integer, primary_key=True, autoincrement=True),
    Column("text", String(255), nullable=False),
    Column("embedding", VECTOR(4096))
]

# Create vector index
vector_index_params = IndexParam(
    index_name="idx_question_embedding",
    field_name="embedding",  
    index_type="HNSW",
    distance_metric="l2"
)

client.create_table_with_index_params(
    table_name=table_name,
    columns=cols, 
    vidxs=[vector_index_params]
)

print('- Inserting Data to OceanBase...')
client.insert(table_name, data=data)

Perform semantic search

Generate an embedding vector for the query text using the Qwen3-embedding model. Then, search for the most relevant documents based on the l2 distance between the query text's embedding vector and each embedding vector in the vector table:

question = "Which mode supports vector search?"
search_res = client.ann_search(
    table_name,
    vec_data=emb_text(question),
    vec_column_name="embedding",
    distance_func=l2_distance,
    with_dist=True,
    topk=1, 
    output_column_names=["id","text"], 
)

print('- The Most Relevant Document and Its Distance to the Query:')
for row in search_res.fetchall():
    print(f'  - ID: {row[0]}\n'
          f'    content: {row[1]}\n'
          f'    distance: {row[2]}')

Expected results

- ID: 3
  content: Which mode supports vector search?

  Vector search is supported only in the MySQL mode of OceanBase Database.

  distance: 0.509979758468682

Previous topic

Hugging Face
Last

Next topic

Google Gemini
Next
on this page
Prerequisites
Step 1: Obtain the database connection information
Step 2: Build your AI assistant
Set environment variables
Install Ollama and pull the Qwen3-embedding model
Sample code