ailia_llm  1.3.1.0
Classes | Macros | Typedefs | Functions
ailia_llm.h File Reference

LLM inference library. More...

Go to the source code of this file.

Classes

struct  _AILIALLMChatMessage
 

Macros

#define AILIA_LLM_API   __stdcall
 
#define AILIA_LLM_STATUS_SUCCESS   (0)
 Successful. More...
 
#define AILIA_LLM_STATUS_INVALID_ARGUMENT   (-1)
 Incorrect argument. More...
 
#define AILIA_LLM_STATUS_ERROR_FILE_API   (-2)
 File access failed. More...
 
#define AILIA_LLM_STATUS_INVALID_VERSION   (-3)
 Incorrect struct version. More...
 
#define AILIA_LLM_STATUS_BROKEN   (-4)
 A corrupt file was passed. More...
 
#define AILIA_LLM_STATUS_MEMORY_INSUFFICIENT   (-5)
 Insufficient memory. More...
 
#define AILIA_LLM_STATUS_THREAD_ERROR   (-6)
 Thread creation failed. More...
 
#define AILIA_LLM_STATUS_INVALID_STATE   (-7)
 The internal status is incorrect. More...
 
#define AILIA_LLM_STATUS_CONTEXT_FULL   (-8)
 Exceeded the context length. More...
 
#define AILIA_LLM_STATUS_UNIMPLEMENTED   (-15)
 Unimplemented error. More...
 
#define AILIA_LLM_STATUS_OTHER_ERROR   (-128)
 Unknown error. More...
 

Typedefs

typedef struct _AILIALLMChatMessage AILIALLMChatMessage
 

Functions

AILIA_LLM_API int ailiaLLMGetBackendCount (unsigned int *env_count)
 Gets the number of available computational environments (CPU, GPU). More...
 
AILIA_LLM_API int ailiaLLMGetBackendName (const char **env, unsigned int env_idx)
 Gets the list of computational environments. More...
 
AILIA_LLM_API int ailiaLLMCreate (struct AILIALLM **llm)
 Creates a LLM instance. More...
 
AILIA_LLM_API int ailiaLLMOpenModelFileA (struct AILIALLM *llm, const char *path, unsigned int n_ctx)
 Open model file. More...
 
AILIA_LLM_API int ailiaLLMOpenModelFileW (struct AILIALLM *llm, const wchar_t *path, unsigned int n_ctx)
 
AILIA_LLM_API int ailiaLLMGetContextSize (struct AILIALLM *llm, unsigned int *context_size)
 Gets the size of context. More...
 
AILIA_LLM_API int ailiaLLMSetSamplingParams (struct AILIALLM *llm, unsigned int top_k, float top_p, float temp, unsigned int dist)
 Set the sampling parameter. More...
 
AILIA_LLM_API int ailiaLLMSetPrompt (struct AILIALLM *llm, const AILIALLMChatMessage *message, unsigned int message_cnt)
 Set the prompt. More...
 
AILIA_LLM_API int ailiaLLMGenerate (struct AILIALLM *llm, unsigned int *done)
 Perform generate. More...
 
AILIA_LLM_API int ailiaLLMGetDeltaTextSize (struct AILIALLM *llm, unsigned int *buf_size)
 Gets the size of text. (Include null) More...
 
AILIA_LLM_API int ailiaLLMGetDeltaText (struct AILIALLM *llm, char *text, unsigned int buf_size)
 Gets the decoded text. More...
 
AILIA_LLM_API int ailiaLLMGetTokenCount (struct AILIALLM *llm, unsigned int *cnt, const char *text)
 Gets the count of token. More...
 
AILIA_LLM_API int ailiaLLMGetPromptTokenCount (struct AILIALLM *llm, unsigned int *cnt)
 Gets the count of prompt token. More...
 
AILIA_LLM_API int ailiaLLMGetGeneratedTokenCount (struct AILIALLM *llm, unsigned int *cnt)
 Gets the count of prompt token. More...
 
AILIA_LLM_API void ailiaLLMDestroy (struct AILIALLM *llm)
 It destroys the LLM instance. More...
 

Detailed Description

LLM inference library.

Date
September 27, 2024

Macro Definition Documentation

◆ AILIA_LLM_API

#define AILIA_LLM_API   __stdcall

◆ AILIA_LLM_STATUS_BROKEN

#define AILIA_LLM_STATUS_BROKEN   (-4)

A corrupt file was passed.

Remarks
Please check model file are correct or not, and please pass correct model.

◆ AILIA_LLM_STATUS_CONTEXT_FULL

#define AILIA_LLM_STATUS_CONTEXT_FULL   (-8)

Exceeded the context length.

Remarks
Please reduce the context length given to SetPrompt.

◆ AILIA_LLM_STATUS_ERROR_FILE_API

#define AILIA_LLM_STATUS_ERROR_FILE_API   (-2)

File access failed.

Remarks
Please check file is exist or not, and check access permission.

◆ AILIA_LLM_STATUS_INVALID_ARGUMENT

#define AILIA_LLM_STATUS_INVALID_ARGUMENT   (-1)

Incorrect argument.

Remarks
Please check argument of called API.

◆ AILIA_LLM_STATUS_INVALID_STATE

#define AILIA_LLM_STATUS_INVALID_STATE   (-7)

The internal status is incorrect.

Remarks
Please check API document and API call steps.

◆ AILIA_LLM_STATUS_INVALID_VERSION

#define AILIA_LLM_STATUS_INVALID_VERSION   (-3)

Incorrect struct version.

Remarks
Please check struct version that passed with API and please pass correct struct version.

◆ AILIA_LLM_STATUS_MEMORY_INSUFFICIENT

#define AILIA_LLM_STATUS_MEMORY_INSUFFICIENT   (-5)

Insufficient memory.

Remarks
Please check usage of main memory and VRAM. And please call API after free memory.

◆ AILIA_LLM_STATUS_OTHER_ERROR

#define AILIA_LLM_STATUS_OTHER_ERROR   (-128)

Unknown error.

Remarks
The misc error has been occurred.

◆ AILIA_LLM_STATUS_SUCCESS

#define AILIA_LLM_STATUS_SUCCESS   (0)

Successful.

◆ AILIA_LLM_STATUS_THREAD_ERROR

#define AILIA_LLM_STATUS_THREAD_ERROR   (-6)

Thread creation failed.

Remarks
Please check usage of system resource (e.g. thread). And please call API after release system resources.

◆ AILIA_LLM_STATUS_UNIMPLEMENTED

#define AILIA_LLM_STATUS_UNIMPLEMENTED   (-15)

Unimplemented error.

Remarks
The called API are not available on current environment. Please contact support desk that described on document.

Typedef Documentation

◆ AILIALLMChatMessage

Function Documentation

◆ ailiaLLMCreate()

AILIA_LLM_API int ailiaLLMCreate ( struct AILIALLM **  llm)

Creates a LLM instance.

Parameters
llmA pointer to the LLM instance pointer
Returns
If this function is successful, it returns AILIA_LLM_STATUS_SUCCESS , or an error code otherwise.

Creates a LLM instance.

◆ ailiaLLMDestroy()

AILIA_LLM_API void ailiaLLMDestroy ( struct AILIALLM *  llm)

It destroys the LLM instance.

Parameters
llmA LLM instance pointer

◆ ailiaLLMGenerate()

AILIA_LLM_API int ailiaLLMGenerate ( struct AILIALLM *  llm,
unsigned int *  done 
)

Perform generate.

Parameters
llmA LLM instance pointer
doneGeneration complete?
Returns
If this function is successful, it returns AILIA_LLM_STATUS_SUCCESS , or an error code otherwise.

The decoded result is obtained through the ailiaLLMGetDeltaText API. Each call to ailiaLLMGenerate decodes one token at a time. The value of done is 0 or 1. If done is 1, the generation is complete.

◆ ailiaLLMGetBackendCount()

AILIA_LLM_API int ailiaLLMGetBackendCount ( unsigned int *  env_count)

Gets the number of available computational environments (CPU, GPU).

Parameters
env_countThe storage location of the number of computational environment information
Returns
If this function is successful, it returns AILIA_LLM_STATUS_SUCCESS , or an error code otherwise.

◆ ailiaLLMGetBackendName()

AILIA_LLM_API int ailiaLLMGetBackendName ( const char **  env,
unsigned int  env_idx 
)

Gets the list of computational environments.

Parameters
envThe storage location of the computational environment information (valid until the AILIANetwork instance is destroyed)
env_idxThe index of the computational environment information (0 to ailiaLLMGetBackendCount() -1)
Returns
If this function is successful, it returns AILIA_LLM_STATUS_SUCCESS , or an error code otherwise.

◆ ailiaLLMGetContextSize()

AILIA_LLM_API int ailiaLLMGetContextSize ( struct AILIALLM *  llm,
unsigned int *  context_size 
)

Gets the size of context.

Parameters
llmA LLM instance pointer
lenThe length of context
Returns
If this function is successful, it returns AILIA_LLM_STATUS_SUCCESS , or an error code otherwise.

◆ ailiaLLMGetDeltaText()

AILIA_LLM_API int ailiaLLMGetDeltaText ( struct AILIALLM *  llm,
char *  text,
unsigned int  buf_size 
)

Gets the decoded text.

Parameters
llmA LLM instance pointer
textText(UTF8)
buf_sizeBuffer size
Returns
If this function is successful, it returns AILIA_LLM_STATUS_SUCCESS , or an error code otherwise.

If ailiaLLMGenerate() is not run at all, the function returns AILIA_LLM_STATUS_INVALID_STATE .

◆ ailiaLLMGetDeltaTextSize()

AILIA_LLM_API int ailiaLLMGetDeltaTextSize ( struct AILIALLM *  llm,
unsigned int *  buf_size 
)

Gets the size of text. (Include null)

Parameters
llmA LLM instance pointer
buf_sizeThe length of text
Returns
If this function is successful, it returns AILIA_LLM_STATUS_SUCCESS , or an error code otherwise.

◆ ailiaLLMGetGeneratedTokenCount()

AILIA_LLM_API int ailiaLLMGetGeneratedTokenCount ( struct AILIALLM *  llm,
unsigned int *  cnt 
)

Gets the count of prompt token.

Parameters
llmA LLM instance pointer
cntThe count of generated token
Returns
If this function is successful, it returns AILIA_LLM_STATUS_SUCCESS , or an error code otherwise.

It can be called after calling ailiaLLMGenerate.

◆ ailiaLLMGetPromptTokenCount()

AILIA_LLM_API int ailiaLLMGetPromptTokenCount ( struct AILIALLM *  llm,
unsigned int *  cnt 
)

Gets the count of prompt token.

Parameters
llmA LLM instance pointer
cntThe count of prompt token
Returns
If this function is successful, it returns AILIA_LLM_STATUS_SUCCESS , or an error code otherwise.

It can be called after calling ailiaLLMSetPrompt.

◆ ailiaLLMGetTokenCount()

AILIA_LLM_API int ailiaLLMGetTokenCount ( struct AILIALLM *  llm,
unsigned int *  cnt,
const char *  text 
)

Gets the count of token.

Parameters
llmA LLM instance pointer
cntThe count of token
textText(UTF8)
Returns
If this function is successful, it returns AILIA_LLM_STATUS_SUCCESS , or an error code otherwise.

◆ ailiaLLMOpenModelFileA()

AILIA_LLM_API int ailiaLLMOpenModelFileA ( struct AILIALLM *  llm,
const char *  path,
unsigned int  n_ctx 
)

Open model file.

Parameters
llmA pointer to the LLM instance pointer
pathPath for GGUF
n_ctxContext length for model (0 is model default)
Returns
If this function is successful, it returns AILIA_LLM_STATUS_SUCCESS , or an error code otherwise.

Open a model file for GGUF.

◆ ailiaLLMOpenModelFileW()

AILIA_LLM_API int ailiaLLMOpenModelFileW ( struct AILIALLM *  llm,
const wchar_t *  path,
unsigned int  n_ctx 
)

◆ ailiaLLMSetPrompt()

AILIA_LLM_API int ailiaLLMSetPrompt ( struct AILIALLM *  llm,
const AILIALLMChatMessage message,
unsigned int  message_cnt 
)

Set the prompt.

Parameters
llmA pointer to the LLM instance pointer
messageArray of messages
message_cntNumber of messages
Returns
If this function is successful, it returns AILIA_LLM_STATUS_SUCCESS , or an error code otherwise.

Set the prompt to query the LLM. Please include ChatHistory in the message as well.

◆ ailiaLLMSetSamplingParams()

AILIA_LLM_API int ailiaLLMSetSamplingParams ( struct AILIALLM *  llm,
unsigned int  top_k,
float  top_p,
float  temp,
unsigned int  dist 
)

Set the sampling parameter.

Parameters
llmA pointer to the LLM instance pointer
top_kSampling probability value's top number, default 40
top_pSampling probability value range, default 0.9 (0.9 to 1.0)
tempTemperature parameter, default 0.4
distSeed, default 1234
Returns
If this function is successful, it returns AILIA_LLM_STATUS_SUCCESS , or an error code otherwise.

Set LLM sampling parameters. Must be run before ailiaLLMSetPrompt.