- UDF - Can be executed once and have results repeatedly queried when needed
- UDTF - Must be executed every time results are needed, but can be used within a query—execution & results in one step
Download & Run
This example will contain the following files (click to download):- udf_st.kisql - UDF management script, in SQL, which creates the input & output tables, uploads & loads the baseline data, and creates and executes the UDF as both a UDF & UDTF
- udf_st.py - the UDF, written using the Python UDF API, which contains the semantic search example
- sentences.txt - the data file containing the set of sentences that will be used as the baseline for the semantic search
Run Example
Semantic Search Results
UDF Detail
The example UDF uses a single table,udf_st_in, as input and a corresponding
table, udf_st_out, for output.
The input table will contain a string column and be populated with a set of
sentences. The output table will contain a float column for each sentence’s
search score. Both tables will also contain an int column that is the
sentence identifier, allowing the input sentences to be matched up with the
output scores after the UDF has run.
The UDF will compare a given sentence to each of the sentences in the input
table baseline data set and insert the distance scores into the output table.
The management script will then output the sentences & scores from both the UDF
& UDTF executions.
SQL Management Script
The SQL management script runs a sequence of commands to setup and execute the UDF:- Creates input & output tables
- Creates a KiFS directory, uploads data file & UDF to it, and loads the data into the input table
- Creates a Python UDF
function environment for the
UDF to run in and installs the
sentence-transformerspackage into it - Registers both the UDF and the table function (UDTF) variant of it, making both available for subsequent execution
- Executes the UDF and queries for the results, and then executes the UDTF, displaying its results
Create Tables
Create UDF input table, into which set of baseline sentences will be loaded:Create Input Table
Create Output Table
Load UDF & Data Files
Create a KiFS directory into which the UDF’s files will be uploaded:Create KiFS Directory
UPLOAD command:
Upload Python UDF
Upload Sentence File
Load Sentence Data
Create a Python UDF Environment
Create the Python UDF environment in which the Python UDF will be run:Create UDF Environment
Install Libraries
Register UDF & UDTF
In this step, several aspects of the UDF are configured:- operating mode (distributed)
- command to execute (
python, as this is a Python UDF) - name and location of the Python UDF to run
- Python UDF execution environment to run the UDF in
Register Python UDF
Register Python UDTF
Execute UDF & UDTF
In this step, the UDF parameters are passed in:- input table of sentences to read from
- output table to write semantic search score results to
- sentence to compare to the baseline set of sentences
Run UDF
Display UDF Results
Run UDTF & Display Results
Python UDF Script
When executed, each Python UDF process performs several operations on the segment of data available to it:- Initialize the UDF’s dependencies and data access handlers
- Perform the semantic search of the given sentence vs. the baseline sentences
- Write the search scores to the output table and mark the process complete
Initialize UDF
Import theProcData class from the kinetica_proc module to use the API;
also, load dependent packages and configure the environment for their use prior
to loading:
Imports & Environment Configuration
ProcData() to access the input & output tables; input_data is a
list of input tables and output_data is a list of output tables:
Get Handles to Input/Output Tables
Perform Semantic Search
Calculate the embeddings for the baseline sentences in the input table, using theSentenceTransformer library’s all-MiniLM-L6-v2 model; the sentences
in the sentence column are stored in the input table map of column name to
column values:
Calculate Embeddings for Baseline Sentences
params map of parameter names to parameter values:
Calculate Embeddings for Sentence to Compare
Perform Semantic Search Scoring
Write Output Data
Set the size of the output table segment appropriately before writing to it; there should be one output score for each input sentence, so the output table should be the same size as the input table:Output Table
Resizing the output table during or after record-writing will decrease
performance.
Handles to Input/Output Table Columns
Output Scores
complete() to let the database know the process completed successfully
and is ready for clean-up:
UDF as Complete