Skip to main content
The following is a SQL-managed UDF written in the Python UDF API that performs a semantic search for a given sentence on a baseline set of sentences, ranking the baseline sentences on their similarity to the given one. This will demonstrate the UDF being registered as a standard UDF and as a table function (UDTF). While it is only necessary to use one or the other, both are demonstrated here. Which UDF type to use depends on the use case:
  • 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
After copying these scripts to a local directory, the example can be run as follows, using the KiSQL SQL client, specifying the database URL, username, & password (when prompted):
Run Example
The script should output the sentences with the top 5 closest match scores to the given sentence for both the UDF & UDTF executions.
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-transformers package 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 UDF output table, into which the UDF will write the semantic search scores:
Create Output Table

Load UDF & Data Files

Create a KiFS directory into which the UDF’s files will be uploaded:
Create KiFS Directory
Upload Python UDF script, the path of which assumes the file is co-located with the SQL script running the UPLOAD command:
Upload Python UDF
Upload sentence data file, which assumes the same co-location:
Upload Sentence File
Load sentences from the file uploaded to KiFS into the input table:
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 the dependent libraries the Python UDF will need to run:
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 the Python UDF as an executable UDF:
Register Python UDF
Register the Python UDF as an executable table function:
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 the UDF:
Run UDF
Display the sentences and resulting semantic search scores by joining the input and output tables together:
Display UDF Results
Run the UDTF, joining its results to the input table, allowing the display of sentences & search scores in one SQL statement:
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 the ProcData 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
Call 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
Calculate the embeddings for the baseline sentences in the input table, using the SentenceTransformer 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
Calculate the embedding for the sentence passed in to the UDF; parameters can be accessed using the params map of parameter names to parameter values:
Calculate Embeddings for Sentence to Compare
Calculate semantic search scores between the given sentence and baseline sentence embeddings using the cosine similarity function:
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.
Acquire references to the input column being read from and the output columns being written to:
Handles to Input/Output Table Columns
Loop over the baseline sentences, writing the ID and corresponding score for each sentence to the output table:
Output Scores
Call complete() to let the database know the process completed successfully and is ready for clean-up:
UDF as Complete