Build TOP Environments

For developer

C API

Debug family(C I/F)
Common Setting family(C I/F)
Initialization family(C I/F)
Attribute family(C I / F)
Operation family(C I/F)

C++ API

K2hVecDb class


C API

It is an API for C language.
Include the following header files when developing.

#include <k2hvec/k2hvec.h>

When linking please specify the following as an option.

-lk2hvec

The functions for C language are explained below.


Debug family(C I/F)

The K2HVEC library can output messages to check internal operation and API operation. This function group is a group of functions for controlling message output

Format

Description

Return Values

k2hvec_set_debug_file, k2hvec_unset_debug_file, k2hvec_load_debug_env will return true on success. If it fails, it returns false.

Note

For the environment variables VECDBGMODE and VECDBGFILE, see the Environments in the manual.

Examples

k2hvec_unset_debug_file("/var/log/k2hvec/error.log");
k2hvec_set_debug_level_dump();

Common Setting family(C I/F)

A set of functions for configuring common parameters governing the behavior of the K2HVEC library.

Format

Description

Parameters

Return Values

Examples

if(k2hvec_set_default_score_limit(0.9f)){
    return false;
}

size_t limit = k2hvec_get_default_neighbor_key_limit();

Initialization family(C I/F)

A set of functions for initializing K2Hash (database files and in-memory) used by the K2HVEC library.

Format

Description

Parameters

Return Values

Note

The returned K2HVEC handle (k2hvec_handle_t) after creation and attachment must be destroyed using k2hvec_delete when it is no longer needed.

Examples

k2hvec_handle_t hk2hvec = k2hvec_create("/tmp/my.k2h", true, 4, 8, 8, 1024);
if(!hk2hvec){
    return false;
}

k2hvec_delete(hk2hvec);

Attribute family(C I/F)

A set of functions for configuring and retrieving common parameters governing the attributes of the entire K2HVEC library.

Format

Description

Parameters

Return Values

These functions will return true on success. If it fails, it returns false.

Examples

bool        is_mtime   = true;
bool        is_defenc  = true;
const char* passfile   = "mypassphrase";
bool        is_history = true;
time_t      expire     = 120;
if(!k2hvec_set_attribute(hk2hvec, &is_mtime, &is_defenc, passfile, &is_history, &expire)){
    return false;
}

if(k2hvec_clear_attribute(hk2hvec)){
    return false;
}

Operation family(C I/F)

A set of functions for performing basic data operations, such as writing and reading, in K2HVEC (K2Hash).

Format

Description

Parameters

Return Values

Examples

k2hvec_handle_t hk2hvec = k2hvec_create("/tmp/my.k2h", true, 4, 8, 8, 1024);
if(!hk2hvec){
    return false;
}

float[] embedding = {...};
char[]  content   = "...";

if(!k2hvec_set(hk2hvec, embedding, sizeof(embedding) / sizeof(float), content, NULL, NULL, 0UL, NULL, NULL)){
    k2hvec_delete(hk2hvec);
    return false;
}

char* pResConent = k2hvec_get_embedding_content(hk2hvec, embedding, sizeof(embedding) / sizeof(float), content, NULL, NULL, 0UL, true, NULL);
if(!pResConent){
    k2hvec_delete(hk2hvec);
    return false;
}

char* pKey = k2hvec_search_key(hk2hvec, , embedding, sizeof(embedding) / sizeof(float), 0.9f, content, NULL, NULL, 0UL, true, NULL);
if(!pKey){
    free(pResConent);
    k2hvec_delete(hk2hvec);
    return false;
}

if(!k2hvec_remove_key(hk2hvec, pKey, false)){
    free(pKey);
    free(pResConent);
    k2hvec_delete(hk2hvec);
    return false;
}

free(pKey);
free(pResConent);
k2hvec_delete(hk2hvec);

return false;

C++ API

This is the I/F of the C ++ language API for using the K2HVEC library.

Include the following header file at development time.

#include <k2hvec/k2hvec.h>
#include <k2hvec/k2hvecdb.h>


When linking please specify the following as an option.

-lk2hvec


The functions for the C ++ language are explained below.


K2hVecDb class

Description

This class handles operations and implementation for K2HVEC data.
All K2HVEC data operations are performed through an instance of this class.
In C++, the basic workflow for managing K2HVEC data requires creating an instance of this class before carrying out any operations.
An instance can be created by opening (attaching to) a K2Hash file or in-memory storage.
Once operations are complete, ensure that you close (detach) the instance.

Class methods

Class Method Descriptions

Methods

Method Descriptions

Method Return Values

Refer to each function prototype. For prototypes returning a bool value, true is returned on success, and false is returned on failure.

Note

Refer to the corresponding C API functions for the parameters of each method.

Examples

K2hVec k2hvecobj;
if(!k2hvecobj.AttachK2hashMem()){
    exit(EXIT_FAILURE);
}

std::vector<float> embedding = {...};
std::string        content   = "...";
if(!k2hvec.Set(embedding. content)){
    exit(EXIT_FAILURE);
}

std::vector<float> sysem;
std::string        model;
std::string        strKeyContent;
std::string        strContent;
if(!k2hvec.Get(strKeyContent, embedding, sysem, strContent, model)){
    exit(EXIT_FAILURE);
}

std::vector<float> tgEmbedding = {...};
std::string        strFoundKey;
std::vector<float> strFoundEmbedding;
std::vector<float> FoundSysEmbedding;
std::string        strFoundContent;
std::string        strFoundModel;
if(!k2hvec.Search(tgEmbedding, strFoundKey, strFoundEmbedding, FoundSysEmbedding, strFoundContent, strFoundModel)){
    exit(EXIT_FAILURE);
    exit(-1);
}

if(!k2hvec.Remove(strFoundKey)){
    exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
Build TOP Environments