2023-05-15 12:45:56 -04:00
|
|
|
#include "../../gpt4all-backend/llmodel_c.h"
|
|
|
|
#include "../../gpt4all-backend/llmodel.h"
|
|
|
|
#include "../../gpt4all-backend/llmodel_c.cpp"
|
|
|
|
|
|
|
|
#include "binding.h"
|
|
|
|
#include <cassert>
|
|
|
|
#include <cmath>
|
2023-06-01 08:37:14 -04:00
|
|
|
#include <cstddef>
|
2023-05-15 12:45:56 -04:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
|
|
|
#include <fstream>
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <iostream>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2023-06-01 10:09:06 -04:00
|
|
|
void* load_model(const char *fname, int n_threads) {
|
2023-05-15 12:45:56 -04:00
|
|
|
// load the model
|
2023-11-07 11:20:14 -05:00
|
|
|
const char *new_error;
|
2023-06-01 10:09:06 -04:00
|
|
|
auto model = llmodel_model_create2(fname, "auto", &new_error);
|
2023-11-07 11:20:14 -05:00
|
|
|
if (model == nullptr) {
|
|
|
|
fprintf(stderr, "%s: error '%s'\n", __func__, new_error);
|
2023-05-15 12:45:56 -04:00
|
|
|
return nullptr;
|
|
|
|
}
|
2023-12-16 17:58:15 -05:00
|
|
|
if (!llmodel_loadModel(model, fname, 2048)) {
|
2023-06-12 12:41:22 -04:00
|
|
|
llmodel_model_destroy(model);
|
2023-05-15 12:45:56 -04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2023-06-05 15:35:40 -04:00
|
|
|
llmodel_setThreadCount(model, n_threads);
|
2023-06-01 10:09:06 -04:00
|
|
|
return model;
|
2023-05-15 12:45:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string res = "";
|
|
|
|
void * mm;
|
|
|
|
|
2023-06-01 10:09:06 -04:00
|
|
|
void model_prompt( const char *prompt, void *m, char* result, int repeat_last_n, float repeat_penalty, int n_ctx, int tokens, int top_k,
|
2023-05-15 12:45:56 -04:00
|
|
|
float top_p, float temp, int n_batch,float ctx_erase)
|
|
|
|
{
|
|
|
|
llmodel_model* model = (llmodel_model*) m;
|
|
|
|
|
|
|
|
// std::string res = "";
|
|
|
|
|
2023-05-22 11:43:07 -04:00
|
|
|
auto lambda_prompt = [](int token_id) {
|
2023-05-15 12:45:56 -04:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
mm=model;
|
|
|
|
res="";
|
|
|
|
|
|
|
|
auto lambda_response = [](int token_id, const char *responsechars) {
|
|
|
|
res.append((char*)responsechars);
|
|
|
|
return !!getTokenCallback(mm, (char*)responsechars);
|
|
|
|
};
|
|
|
|
|
|
|
|
auto lambda_recalculate = [](bool is_recalculating) {
|
|
|
|
// You can handle recalculation requests here if needed
|
|
|
|
return is_recalculating;
|
|
|
|
};
|
|
|
|
|
|
|
|
llmodel_prompt_context* prompt_context = new llmodel_prompt_context{
|
|
|
|
.logits = NULL,
|
|
|
|
.logits_size = 0,
|
|
|
|
.tokens = NULL,
|
|
|
|
.tokens_size = 0,
|
|
|
|
.n_past = 0,
|
|
|
|
.n_ctx = 1024,
|
|
|
|
.n_predict = 50,
|
|
|
|
.top_k = 10,
|
|
|
|
.top_p = 0.9,
|
|
|
|
.temp = 1.0,
|
|
|
|
.n_batch = 1,
|
|
|
|
.repeat_penalty = 1.2,
|
|
|
|
.repeat_last_n = 10,
|
|
|
|
.context_erase = 0.5
|
|
|
|
};
|
|
|
|
|
|
|
|
prompt_context->n_predict = tokens;
|
|
|
|
prompt_context->repeat_last_n = repeat_last_n;
|
|
|
|
prompt_context->repeat_penalty = repeat_penalty;
|
|
|
|
prompt_context->n_ctx = n_ctx;
|
|
|
|
prompt_context->top_k = top_k;
|
|
|
|
prompt_context->context_erase = ctx_erase;
|
|
|
|
prompt_context->top_p = top_p;
|
|
|
|
prompt_context->temp = temp;
|
|
|
|
prompt_context->n_batch = n_batch;
|
|
|
|
|
|
|
|
llmodel_prompt(model, prompt,
|
|
|
|
lambda_prompt,
|
|
|
|
lambda_response,
|
|
|
|
lambda_recalculate,
|
|
|
|
prompt_context );
|
|
|
|
|
|
|
|
strcpy(result, res.c_str());
|
|
|
|
|
|
|
|
free(prompt_context);
|
|
|
|
}
|
|
|
|
|
2023-06-01 10:09:06 -04:00
|
|
|
void free_model(void *state_ptr) {
|
2023-05-15 12:45:56 -04:00
|
|
|
llmodel_model* ctx = (llmodel_model*) state_ptr;
|
2023-06-01 08:37:14 -04:00
|
|
|
llmodel_model_destroy(*ctx);
|
2023-05-15 12:45:56 -04:00
|
|
|
}
|
|
|
|
|