OceanBase Database supports the vector data type starting from V4.3.3 and continues to enhance its AI capabilities in subsequent versions, gradually building an AI database capability that integrates vector storage, vector indexes, full-text indexes, AI function services, and hybrid search. By directly invoking Embedding Models and Large Language Models (LLMs) within the database, developers no longer need to transfer data back and forth between the database and external AI services. They can complete the entire process—from data vectorization, semantic search, to answer generation—using only SQL.
This topic covers the following experiences:
- Create a vector table and a full-text index: Create a single table that contains both
VECTORtype fields and aFULLTEXTindex, laying the foundation for hybrid search. - Insert document data and generate vectors: Use the
AI_EMBED()function to directly convert text into vectors and insert them into the database, experiencing the level of integration of OceanBase as an "AI database". - Execute a hybrid search query: Use hybrid retrieval that combines vector search and full-text search to experience OceanBase's integrated search capability.
- Experience RAG search: Complete the entire RAG process within the database: "Retrieve Top-K segments → Reorder → Call LLM to generate answer".
Note
The commands and feature descriptions in this topic apply to V5.0.1. This topic uses a single-node environment deployed with the All-in-One package in Deploy OceanBase as an example. You must have created the test tenant oltp_tenant as described in Experience high-concurrency OLTP scenarios. AI function services are available starting from V4.4.1. Creating vector indexes and full-text indexes on the same table requires V4.4.0 or later. V5.0.1 used in this topic supports all these features.
Prerequisites
Before you perform the operations described in this topic, ensure that the following conditions are met:
Project |
Description |
|---|---|
| OceanBase Database | OceanBase Database has been deployed and started successfully. For more information, see Deploy OceanBase Database. We recommend V4.4.1 or later. This topic uses V5.0.1 as an example. |
| Test tenant | You have created a MySQL-compatible business tenant (for example, oltp_tenant). For more information, see Experience high-concurrency OLTP scenarios. |
| Vector memory configuration | Vector search is enabled by default in V4.4.1 and later, including V5.0.1, and requires no manual configuration of ob_vector_memory_limit_percentage. For versions earlier than V4.4.1, we recommend setting it to 30. |
| AI model | You have obtained the API key for the AI model service (Alibaba Cloud Bailian is used in this example). For how to register the embedding, text generation, and reranking models, see "Register AI models" below. |
| obclient | obclient has been installed and can connect to the business tenant. |
| Network | Make sure that the default port 2881 of OceanBase Database is not blocked by the firewall and that the database nodes can access the AI model service endpoint. |
Configure vector memory
Starting from V4.4.1, the vector search feature is enabled by default. Setting ob_vector_memory_limit_percentage to its default value 0 enables adaptive mode, where the system automatically adjusts the memory usage percentage for vector indexes (adapting to 40 for tenants with 8 GB or less of actual memory, and 50 for those above 8 GB). No manual configuration is required. For versions prior to V4.4.1, you must manually set this parameter before using HNSW series indexes; otherwise, creating an HNSW index will result in an error. If you need to explicitly specify the memory percentage, execute the following command in the sys tenant:
-- Log in to the sys tenant as the root user.
obclient -h127.0.0.1 -uroot@sys -P2881 -p -A oceanbase
-- Manually set the vector memory percentage to 30.
obclient [oceanbase]> ALTER SYSTEM SET ob_vector_memory_limit_percentage = 30;
Notice
IVF series indexes do not occupy resident memory, so you do not need to pay attention to this parameter. For more information, see ob_vector_memory_limit_percentage.
Register AI models
OceanBase Database manages AI models and access endpoints through the DBMS_AI_SERVICE system package. Before using AI functions such as AI_EMBED, AI_COMPLETE, and AI_RERANK, you must register the corresponding models and access endpoints.
The following example registers three sets of models and endpoints on Alibaba Cloud Bailian: an embedding model, a text generation model, and a reranking model. Replace access_key with your actual API key:
-- Log in to the business tenant
obclient -h127.0.0.1 -uroot@oltp_tenant -P2881 -p -A test
-- Register the embedding model (for the AI_EMBED function)
obclient [test]> CALL DBMS_AI_SERVICE.CREATE_AI_MODEL(
'ob_embed', '{
"type": "dense_embedding",
"model_name": "BAAI/bge-m3"
}');
obclient [test]> CALL DBMS_AI_SERVICE.CREATE_AI_MODEL_ENDPOINT(
'ob_embed_endpoint', '{
"ai_model_name": "ob_embed",
"url": "https://dashscope.aliyuncs.com/compatible-mode/v1/embeddings",
"access_key": "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxx",
"provider": "aliyun-openAI"
}');
-- Register the text generation model (for the AI_COMPLETE function)
obclient [test]> CALL DBMS_AI_SERVICE.CREATE_AI_MODEL(
'ob_complete', '{
"type": "completion",
"model_name": "THUDM/GLM-4-9B-0414"
}');
obclient [test]> CALL DBMS_AI_SERVICE.CREATE_AI_MODEL_ENDPOINT(
'ob_complete_endpoint', '{
"ai_model_name": "ob_complete",
"url": "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions",
"access_key": "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxx",
"provider": "aliyun-openAI"
}');
-- Register the reranking model (for the AI_RERANK function)
obclient [test]> CALL DBMS_AI_SERVICE.CREATE_AI_MODEL(
'ob_rerank', '{
"type": "rerank",
"model_name": "BAAI/bge-reranker-v2-m3"
}');
obclient [test]> CALL DBMS_AI_SERVICE.CREATE_AI_MODEL_ENDPOINT(
'ob_rerank_endpoint', '{
"ai_model_name": "ob_rerank",
"url": "https://dashscope.aliyuncs.com/api/v1/services/rerank/text-rerank/text-rerank",
"access_key": "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxx",
"provider": "aliyun-dashscope"
}');
Note
typeindicates the model type:dense_embeddingfor embedding models,completionfor text generation models, andrerankfor reranking models. Note that the type of an embedding model isdense_embedding. If it is mistakenly written asembeddings, an errorERROR 11116 (HY000): The value of param 'type' is invalidis reported.- The output dimensionality of the embedding model must match the dimensionality of the vector column created later: The BAAI/bge-m3 model used in this example outputs vectors of dimension 1024, corresponding to
VECTOR(1024). If they do not match, an error will occur when writing vector data. - The endpoint URL and
providerof the reranking model differ from those of the other two model types (they areapi/v1/services/rerank/...andaliyun-dashscope). Use the values shown in the example. - The example above uses the model service of Alibaba Cloud Bailian as the AI model endpoint. You can also register other model services. For details, see Register an AI model. For the complete quick start process, see Quick start with AI function services.
Execute the following query to verify that the model and endpoint have been successfully registered:
obclient [test]> SELECT NAME, TYPE FROM oceanbase.DBA_OB_AI_MODELS;
obclient [test]> SELECT ENDPOINT_NAME, AI_MODEL_NAME, PROVIDER FROM oceanbase.DBA_OB_AI_MODEL_ENDPOINTS;
The returned result should show the three registered models and three endpoints, indicating successful registration:
+-------------+-----------------+
| NAME | TYPE |
+-------------+-----------------+
| ob_embed | DENSE_EMBEDDING |
| ob_complete | COMPLETION |
| ob_rerank | RERANK |
+-------------+-----------------+
3 rows in set
+----------------------+---------------+------------------+
| ENDPOINT_NAME | AI_MODEL_NAME | PROVIDER |
+----------------------+---------------+------------------+
| ob_embed_endpoint | ob_embed | aliyun-openAI |
| ob_complete_endpoint | ob_complete | aliyun-openAI |
| ob_rerank_endpoint | ob_rerank | aliyun-dashscope |
+----------------------+---------------+------------------+
3 rows in set
Create a vector table and a full-text index
OceanBase Database can create both a vector index and a full-text index on the same table at the same time, which is the basis for implementing hybrid search. Vector indexes support semantic similarity search, while full-text indexes support exact keyword matching. Combining the two provides more accurate and comprehensive search results.
Storage format description
Index type |
Keyword |
Use cases |
|---|---|---|
| Vector index | VECTOR INDEX |
Semantic search: finds semantically similar content by calculating vector distances. |
| Full-text index | FULLTEXT INDEX |
Exact keyword matching, implemented through word segmentation and inverted indexes |
| Coexistence in one table | Created together | Hybrid search, which ranks results by combining semantic search and keyword search |
Procedure
Log in to the business tenant:
obclient -h127.0.0.1 -uroot@oltp_tenant -P2881 -p -A testCreate a knowledge base table that includes both vector columns and full-text index columns. Taking a document knowledge base as an example:
obclient [test]> CREATE TABLE knowledge_base ( id INT AUTO_INCREMENT PRIMARY KEY, title VARCHAR(255) DEFAULT NULL COMMENT 'Document title', content TEXT DEFAULT NULL COMMENT 'Document content', embedding VECTOR(1024) DEFAULT NULL COMMENT 'Content vector', FULLTEXT INDEX ft_title (title) WITH PARSER ngram, FULLTEXT INDEX ft_content (content) WITH PARSER ngram, VECTOR INDEX vec_idx (embedding) WITH (distance=cosine, type=hnsw, lib=vsag) ) ORGANIZATION HEAP;Note
VECTOR(1024)declares a 1024-dimensional vector column. The dimensionality must match the output dimensionality of the embedding model. OceanBase supports vectors with up to 16,000 dimensions.FULLTEXT INDEXcreates single-column full-text indexes on thetitleandcontentcolumns, respectively, and usesWITH PARSER ngramto specify the ngram tokenizer. The ngram tokenizer splits text into fixed-length segments (2 characters by default), which suits Chinese content. The default space tokenizer splits words by spaces and cannot handle continuous Chinese sentences (it treats an entire sentence as a single word), which can result in no results for Chinese full-text searches.- Hybrid search requires single-column full-text indexes on the full-text columns. Combined full-text indexes (such as
(title, content)) are invalid for hybrid search. Therefore, single-column full-text indexes are created separately for each column here. VECTOR INDEX ... WITH (distance=cosine, type=hnsw, lib=vsag)creates an HNSW vector index using the cosine distance algorithm.ORGANIZATION HEAPspecifies the heap table organization. We recommend using heap tables for hybrid search.- The length of an HNSW index name currently does not support exceeding 25 characters.
Verify that the table and indexes were created successfully:
obclient [test]> SHOW CREATE TABLE knowledge_base\GThe return result should show both
VECTOR KEYandFULLTEXT KEYindex types, indicating that the vector index and full-text index have been successfully created on the same table.
Insert document data and generate vectors
OceanBase Database provides the AI_EMBED() function, which allows you to directly call registered embedding models in SQL statements to convert text data into vector data. This eliminates the cumbersome process of generating vectors using languages like Python externally and then importing them into the database, demonstrating OceanBase's integration as an "AI database".
About the AI_EMBED function
The AI_EMBED(model_key, input[, dim_or_parameters]) function specifies a registered embedding model via model_key and converts the user-provided text data into vector data.
Parameter |
Description |
|---|---|
model_key |
The name of the model registered in the database, for example, ob_embed. |
input |
The text data to be converted into a vector can be a string constant or a column from a table. |
dim_or_parameters |
An optional third parameter. When you embed text, you can specify the dimensionality of the output vector (some models support multi-dimensional output). Starting from V5.0.1, you can also pass in a JSON parameter such as {"type":"image"} to specify an image input for image embedding. |
Procedure
Insert document data and use the
AI_EMBED()function to generate vectors simultaneously:obclient [test]> INSERT INTO knowledge_base (title, content, embedding) VALUES ('Introduction to OceanBase', 'OceanBase is a powerful native distributed database system that supports vector search and AI function services.', AI_EMBED('ob_embed', 'OceanBase is a powerful native distributed database system that supports vector search and AI function services.')), ('Principle of Vector Search', 'Vector search finds semantically similar content by calculating the distance between vectors and is commonly used in semantic search and recommendation systems.', AI_EMBED('ob_embed', 'Vector search finds semantically similar content by calculating the distance between vectors and is commonly used in semantic search and recommendation systems.')), ('Introduction to Full-text Indexing', 'OceanBase full-text indexing supports keyword search for text data and achieves efficient retrieval through word segmentation and inverted indexes.', AI_EMBED('ob_embed', 'OceanBase full-text indexing supports keyword search for text data and achieves efficient retrieval through word segmentation and inverted indexes.')), ('AI Function Services', 'The AI_EMBED function can directly call embedding models to generate vectors in SQL, while the AI.Complete function can call large language models to generate text.', AI_EMBED('ob_embed', 'The AI_EMBED function can directly call embedding models to generate vectors in SQL, while the AI.Complete function can call large language models to generate text.')), ('RAG Applications', 'RAG (Retrieval-Augmented Generation) improves the accuracy and reliability of large model responses by first retrieving relevant documents and then generating answers.', AI_EMBED('ob_embed', 'RAG (Retrieval-Augmented Generation) improves the accuracy and reliability of large model responses by first retrieving relevant documents and then generating answers.'));After the command is successfully executed, obclient returns the insertion result:
Query OK, 5 rows affected Records: 5 Duplicates: 0 Warnings: 0Note
By directly calling the
AI_EMBED()function in anINSERTstatement, the database automatically sends the text to the registered embedding model. After obtaining the vector, it writes it into theembeddingcolumn. The entire process is completed at the SQL layer without requiring external code.If an error
ERROR 4216 (HY000): http request to AI service failedoccurs during execution, the database failed to call the AI model service. Check whether the API key is valid, whether the database node can access the model service endpoint (see the network requirement in Prerequisites), and whether the model and endpoint registration parameters are correct.You can also use the
UPDATEstatement to batch generate vectors for existing data:obclient [test]> UPDATE knowledge_base SET embedding = AI_EMBED('ob_embed', content) WHERE embedding IS NULL;Verify that vectors were generated successfully:
obclient [test]> SELECT id, title, vector_dims(embedding) AS dim_count FROM knowledge_base;The return result is as follows;
dim_countshould display a value consistent with the model output dimension (for example, 1024):+----+------------------+-----------+ | id | title | dim_count | +----+------------------+-----------+ | 1 | Introduction to OceanBase Database | 1024 | | 2 | Principle of vector search | 1024 | | 3 | Introduction to full-text index | 1024 | | 4 | AI Function Service | 1024 | | 5 | RAG Application | 1024 | +----+------------------+-----------+ 5 rows in setNote
The
vector_dims()function returns the dimensionality of a vector. If the return value isNULL, it indicates that the vector for that row was not generated. Check whether the AI model endpoint is registered successfully and whether the network is connected.
Execute hybrid search queries
Hybrid search combines semantic search based on vectors with keyword search based on full-text indexes. Vector search excels at approximate semantic matching but is weak at matching exact keywords, numbers, and proper nouns, while full-text search effectively compensates for this weakness. Starting from V4.5.0, OceanBase provides the DBMS_HYBRID_SEARCH system package (PL interface). The current version also provides the HYBRID_SEARCH table function (SQL interface). Both can combine vector search and full-text search in a single SQL statement, and both automatically merge and rank the results with fusion algorithms such as RRF (Reciprocal Rank Fusion). This topic uses the PL interface for demonstration.
Hybrid search principle
Search method |
Use cases |
Limitations |
|---|---|---|
| Vector search | Semantic similarity matching, understands synonyms and related concepts | Weak at matching exact keywords, numbers, and proper nouns |
| Full-text search | Exact keyword matching, supports boolean operations and phrase matching | Cannot understand semantic similarity |
| Hybrid search | Combines semantic search and keyword search and fuses the rankings with RRF | — |
Procedure
1. Pure vector search
Use ORDER BY with a distance function and the APPROXIMATE keyword to perform an approximate nearest neighbor search with vectors:
obclient [test]> SET @query_vector = AI_EMBED('ob_embed', 'Database search functionality');
obclient [test]> SELECT id, title, cosine_distance(embedding, @query_vector) AS distance
FROM knowledge_base
WHERE embedding IS NOT NULL
ORDER BY cosine_distance(embedding, @query_vector) APPROXIMATE LIMIT 3;
The returned results are sorted by distance, from closest to farthest. A smaller distance means greater semantic similarity (the following is sample output; actual values depend on the model and data used):
+----+------------------+----------------------+
| id | title | distance |
+----+------------------+----------------------+
| 2 | Principle of vector search | 0.012345678901234567 |
| 3 | Introduction to Full-text Indexes | 0.056789012345678901 |
| 1 | Introduction to OceanBase | 0.089012345678901234 |
+----+------------------+----------------------+
3 rows in set
Note
- The
APPROXIMATE(orAPPROX) keyword indicates using a vector index for an approximate search. Without this keyword, a full table scan is performed. cosine_distance()calculates the cosine distance. You can also usel2_distance()(Euclidean distance) orvector_distance(v1, v2, metric)(unified distance function). A smaller distance value indicates greater similarity.AI_EMBED()returns a string in vector format, which can be directly used in distance function calculations or concatenated into the JSON parameters for the subsequent hybrid search.- Vector search is an approximate nearest neighbor search and does not guarantee 100% accuracy. Search accuracy is measured by recall. You can adjust the size of the HNSW index's search candidate set using the session variable
ob_hnsw_ef_search. A larger value results in higher recall but slower search.
2. Pure full-text search
Use the MATCH() AGAINST() syntax for full-text search:
obclient [test]> SELECT id, title, MATCH(content) AGAINST('Vector Search' IN NATURAL LANGUAGE MODE) AS score
FROM knowledge_base
WHERE MATCH(content) AGAINST('Vector Search' IN NATURAL LANGUAGE MODE);
The returned results are sorted by relevance score (the following output was produced in our test environment; the exact values depend on the data distribution):
+----+------------------+---------------------+
| id | title | score |
+----+------------------+---------------------+
| 2 | Principle of vector search | 0.7060192883272365 |
| 1 | Introduction to OceanBase | 0.5315473209814723 |
| 3 | Introduction to Full-text Indexes | 0.24894465290806755 |
| 4 | AI Function Service | 0.22518031395842178 |
+----+------------------+---------------------+
4 rows in set
Note
MATCH(column) AGAINST(expr)is the full-text search syntax.IN NATURAL LANGUAGE MODEis the default mode, which supports natural language fuzzy matching;IN BOOLEAN MODEsupports boolean operators (+represents AND and-represents NOT);IN MATCH PHRASE MODEsupports phrase exact matching.- The column list in
MATCHmust exactly match the columns of a full-text index. In this example, single-column full-text indexes are created on thetitleandcontentcolumns respectively. Therefore,MATCH(content)is used to search the body; to search the title, useMATCH(title). - Query terms are split using the ngram tokenizer specified during table creation and then matched against the index. If the default space tokenizer is used and the content consists of consecutive Chinese characters, the query may return no results.
scoreis the relevance score of the full-text search. A higher value means a better match.- Full-text search excels at exact keyword matching but struggles with matching synonyms or semantically related content that differs in wording.
3. Hybrid vector and full-text search
Use the DBMS_HYBRID_SEARCH.SEARCH function to initiate both vector search and full-text search in a single SQL statement, and fuse and sort the results using the RRF algorithm.
First, generate the query vector:
obclient [test]> SET @query_vector = AI_EMBED('ob_embed', 'Search functionality of a database');
Since the AI_EMBED() function cannot be called directly within JSON parameters, you need to concatenate the query vector to the JSON query parameter using CONCAT():
obclient [test]> SET @hybrid_query = CONCAT('{
"query": {
"bool": {
"should": [
{"match": {"title": "Database Search"}},
{"match": {"content": "Vector Search"}}
]
}
},
"knn": {
"field": "embedding",
"k": 5,
"query_vector": ', @query_vector, '
},
"rank": {
"rrf": {
"rank_window_size": 10,
"rank_constant": 60
}
},
"_source": ["id", "title", "content"]
}');
obclient [test]> SELECT json_pretty(DBMS_HYBRID_SEARCH.SEARCH('knowledge_base', @hybrid_query));
The returned results are sorted by the RRF-fusion relevance (the following is a sample output; actual values depend on the model and data used):
[
{
"id": 2,
"_score": 0.0325,
"title": "Principle of Vector Search",
"content": "Vector search finds semantically similar content by calculating vector distances. It is commonly used in semantic search and recommendation systems."
},
{
"id": 3,
"_score": 0.0323,
"title": "Introduction to Full-text Indexes",
"content": "OceanBase full-text indexes support keyword search for text data, achieving efficient retrieval through word segmentation and inverted indexes."
}
]
Note
- The
querypart specifies the full-text search conditions, and theknnpart specifies the vector search conditions. The results of both are fused and sorted usingrank.rrf. - The full-text part of hybrid search requires the target column to use a single-column full-text index. A composite full-text index is invalid for hybrid search (this is also why two single-column full-text indexes were used when creating the table earlier).
- The RRF (Reciprocal Rank Fusion) algorithm calculates the final score by fusing the rankings of multiple subquery result sets:
score = Σ 1/(k + rank(result, doc)), wherekis the value of therank_constantparameter. _sourcespecifies the range of columns to return. It is strongly recommended to specify this field. If not specified, all columns (including vector columns) will be returned by default, significantly increasing response time.- The dimensionality of
knn.query_vectormust match the definition of the vector column (1024 dimensions in this example). Mismatched dimensions result in anERROR 5874error. In the example, the query vector is first saved to a variable withSET @query_vector = AI_EMBED(...)(AI_EMBED()returns a string in vector format) and then concatenated into the JSON parameter withCONCAT().
Performance evaluation
When evaluating the performance of hybrid search, you can focus on the following metrics:
Metric |
Description |
|---|---|
| Recall | The proportion of relevant results that the vector search actually returns. You can increase it by adjusting the ob_hnsw_ef_search parameter. A larger value yields higher recall but slower search. |
| Response time (RT) | The execution time of the search query. By default, obclient displays the execution time after the query results (N rows in set (X.XX sec)), which you can use to compare the response times of pure vector search, pure full-text search, and hybrid search. |
_score correlation |
The _score value of each record in the hybrid search result, which reflects the relevance after combined ranking. The RRF score takes both the vector search rank and the full-text search rank into account. |
You can execute each search method separately. obclient displays the execution time (N rows in set (X.XX sec)) after each query result, allowing you to compare the differences in response time and result quality.
Experience RAG search
Retrieval-Augmented Generation (RAG) is a technique that enhances the accuracy and reliability of large language model responses by first retrieving relevant documents and then generating an answer. OceanBase Database supports the complete RAG process within the database using three built-in AI functions: AI_EMBED (vectorization), AI_RERANK (re-ranking), and AI_COMPLETE (text generation), eliminating the need for external code.
RAG process
Input question → AI_EMBED generates query vector → Vector search for Top-K documents → AI_RERANK re-ranks → AI_COMPLETE generates answer
Procedure
The following example demonstrates a simplified RAG process completed within the database.
Input a question and generate a query vector:
obclient [test]> SET @query = 'What search capabilities does OceanBase support?'; obclient [test]> SET @query_vector = AI_EMBED('ob_embed', @query);Vector search for Top-K documents: Retrieve the Top 3 documents most semantically similar to the query:
obclient [test]> SET @candidate_docs = ( SELECT JSON_ARRAYAGG(content) FROM ( SELECT content FROM knowledge_base WHERE embedding IS NOT NULL ORDER BY cosine_distance(embedding, @query_vector) APPROXIMATE LIMIT 3 ) AS t );Note
JSON_ARRAYAGG()aggregates the retrieved document content into a JSON array format for use by the subsequentAI_RERANKfunction.Re-ranking with AI_RERANK (Optional step; requires a registered re-ranking model):
obclient [test]> SELECT AI_RERANK('ob_rerank', @query, @candidate_docs) AS ranked_results;The returned result is as follows, where
indexis the document index andrelevance_scoreis the relevance score, sorted in descending order (schematic output; actual values depend on the model):+-------------------------------------------------------------------------------------------------------------+ | ranked_results | +-------------------------------------------------------------------------------------------------------------+ | [{"index": 0, "relevance_score": 0.9904329776763916}, {"index": 2, "relevance_score": 0.16993996500968933}] | +-------------------------------------------------------------------------------------------------------------+ 1 row in setNote
The
AI_RERANK(model_key, query, documents)function sorts a document list by similarity based on the query terms using a re-ranking model. The re-ranking model can more accurately determine the relevance of documents to the query, improving the quality of answers from the RAG system. If no re-ranking model is registered, you can skip this step and directly use the Top-K results from the vector search.Call AI_COMPLETE to generate an answer: Based on the retrieved document content, call a large language model to generate an answer:
obclient [test]> SELECT AI_COMPLETE('ob_complete', CONCAT('Based on the following document content, answer the user's question. User question:', @query, ' Related documents:', CAST(JSON_EXTRACT(@candidate_docs, '$[0]') AS CHAR), ' Please answer the user's question concisely and accurately based on the above document content.')) AS answer;The return result is as follows (schematic output; the actual answer depends on the model):
+------------------------------------------------------------------------------------------------------------------------------------+ | answer | +------------------------------------------------------------------------------------------------------------------------------------+ | OceanBase supports vector search and full-text index search, and supports hybrid search through DBMS_HYBRID_SEARCH, combining semantic search with keyword search to provide more comprehensive retrieval capabilities. | +------------------------------------------------------------------------------------------------------------------------------------+ 1 row in setNote
The
AI_COMPLETE(model_key, prompt)function processes the prompt using a registered large language model and returns the generated text. TheCONCAT()function is used to concatenate the user's question and the retrieved document content into a complete prompt, guiding the large model to answer the question based on the document content.
Performance evaluation
The integrity and accuracy of the RAG process can be evaluated in the following ways:
Evaluation dimension |
Method |
|---|---|
| Search quality | Check whether the top K documents returned by vector search are relevant to the question. You can read the execution time shown after the query results and assess the relevance of the documents manually. |
| Reranking effect | Compare the document ranking before and after AI_RERANK, and check whether the relevance scores (relevance_score) show that the top-ranked document better matches the user's intent. |
| Answer accuracy | Check whether the answers generated by AI_COMPLETE stay faithful to the retrieved document content and whether any hallucinations exist. RAG reduces the risk of hallucinations in large models by constraining the scope of the context documents. |
| End-to-end latency | To observe the total response time from initiating a query to obtaining the answer, you can refer to the execution duration displayed after the query result. The latency in each stage mainly depends on the call duration of the AI model service. |
Next steps
After you complete the AI scenarios in this topic, you can explore the following:
- Experience advanced features of hybrid search: Explore advanced features such as multi-vector search, scalar filtering combined with hybrid search, and custom RRF parameter tuning. For details, see Hybrid search by index (SQL interface).
- Experience advanced OceanBase features: Explore enterprise-level capabilities such as multi-tenancy and resource isolation, high availability and disaster recovery, horizontal scaling, parallel import, data protection and recovery, and online DDL. For details, see Experience multi-tenancy and resource isolation.
- Deepen understanding of vector indexes: Explore the characteristics and application scenarios of different vector index types such as HNSW_SQ, HNSW_BQ, and IVF. For details, see Overview of vector indexes.
References
- For a complete description of vector data types, see Overview of vector data types. For a complete description of vector indexes, see Overview of vector indexes.
- For a complete description of vector functions (such as distance functions and similarity functions), see Use SQL functions.
- For the syntax and search patterns of full-text queries, see Full-text query.
- For the complete syntax and examples of AI function services (AI_EMBED, AI_COMPLETE, AI_RERANK), see Syntax and examples of AI function services.
- For the complete commands and templates for AI model registration, see AI model registration.
- For a complete description and more examples of hybrid search, see Hybrid search by index (PL interface). For the SQL interface, see Hybrid search by index (SQL interface).
- For an overview and methods of vector embedding technology, see Vector embedding technology.