you need to download a model gpt4all model the default one is "ggml-gpt4all-j-v1.3-groovy.bin"
path.s = "C:\Users\idle\AppData\Local\nomic.ai\GPT4All\ggml-gpt4all-j-v1.3-groovy.bin
it can't do multiple clients the c lib needs adapting so the call backs can accept a user data pointer.
Code: Select all
Structure llmodel_error
*message; // Human readable error description; Thread-local; guaranteed to survive until next llmodel C API call
code.l; // errno; 0 if none
EndStructure ;
Structure arFloat
e.f[0]
EndStructure
Structure arLong
e.l[0]
EndStructure
Structure llmodel_prompt_context Align #PB_Structure_AlignC
*logits.arfloat; // logits of current context
logits_size.i; // the size of the raw logits vector
*tokens.arlong; // current tokens in the context window
tokens_size.i; // the size of the raw tokens vector
n_past.l; // number of tokens in past conversation
n_ctx.l; // number of tokens possible in context window
n_predict.l; // number of tokens to predict
top_k.l; // top k logits to sample from
top_p.f; // nucleus sampling probability threshold
temp.f; // temperature to adjust model's output distribution
n_batch.l; // number of predictions to generate in parallel
repeat_penalty.f; // penalty factor for repeated tokens
repeat_last_n.f; // last n tokens to penalize
context_erase.f; // percent of context to erase if we exceed the context window
EndStructure
PrototypeC llmodel_prompt_callback(token_id.l);
PrototypeC llmodel_response_callback(token_id.l,*response);
PrototypeC llmodel_recalculate_callback(is_recalculating.l);
ImportC "libllmodel.dll.a"
llmodel_model_create(model_path.p-utf8);
llmodel_model_create2(model_path.p-utf8,build_variant.p-utf8,*error.llmodel_error);
llmodel_model_destroy(model.i);
llmodel_loadModel(model.i,model_path.p-utf8);
llmodel_isModelLoaded(model.i);
llmodel_get_state_size(model.i);
llmodel_save_state_data(model.i,*dest.Ascii);
llmodel_restore_state_data(model.i,*src.Ascii);
llmodel_prompt(model,prompt.p-utf8,*prompt_callback,*response_callback,*recalculate_callback,*ctx.llmodel_prompt_context);
llmodel_setThreadCount(model,n_threads.l);
llmodel_threadCount.l(model.i);
llmodel_set_implementation_search_path(path.p-utf8);
llmodel_get_implementation_search_path();string
EndImport
EnableExplicit
Global gModel,gCtx.llmodel_prompt_context
Global gErr.llmodel_error
Global path.s,gResponce.s,Quit
OpenConsole()
ProcedureC CBResponse(token_id.l,*response);
Print(PeekS(*response,-1,#PB_UTF8))
ProcedureReturn #True
EndProcedure
ProcedureC CBNetResponse(token_id.l,*response);
gResponce + PeekS(*response,-1,#PB_UTF8)
ProcedureReturn #True
EndProcedure
ProcedureC CBPrompt(token.l)
ProcedureReturn #True
EndProcedure
ProcedureC CBRecalc(is_recalculating.l)
ProcedureReturn is_recalculating
EndProcedure
Procedure Process(client)
Protected length,*buffer,*mem,res,pos,input.s,prompt.s,content.s
*buffer = AllocateMemory($FFFF)
length = ReceiveNetworkData(client,*buffer,$ffff)
If PeekS(*buffer,3,#PB_UTF8) = "GET"
input.s = PeekS(*buffer+5,-1,#PB_UTF8)
pos = FindString(input,"HTTP/1.1")-1
input = Left(input,pos)
input = ReplaceString(input,"%20"," ")
input = ReplaceString(input,"+"," ")
pos = FindString(input,"query?fquery=")
If pos
input = Right(input,Len(input)-(pos+12))
PrintN("input")
PrintN(input)
PrintN("end input")
prompt.s = "### Human " + input + " ### Assistant"
llmodel_prompt(gmodel,prompt,@CBPrompt(),@CBNetResponse(),@CBRecalc(),@gctx);
EndIf
content.s = "<!DOCTYPE html>" +#CRLF$
content + "<html><head>" +#CRLF$
content + "<title>Gpt4all PB</title>" +#CRLF$
content + "</head><body>" + #CRLF$
content + "<form action='/query'>" + #CRLF$
content + "<label for='fquery'>Query:</label><br>" + #CRLF$
content + "<input type='text' id='fquery' name='fquery' value=''><br>" + #CRLF$
content + "<input type='submit' value='submit'>" + #CRLF$
content + "<p>" + gResponce + "</p>" + #CRLF$
content + "</body></html>"
*mem = UTF8(content)
Length = PokeS(*Buffer, "HTTP/1.1 200 OK"+#CRLF$, -1, #PB_UTF8)
Length + PokeS(*Buffer+Length, "Date: Wed, 11 Fec 2017 11:15:43 GMT"+#CRLF$, -1, #PB_UTF8)
Length + PokeS(*Buffer+Length, "Server: Atomic Web Server 0.2b"+#CRLF$, -1, #PB_UTF8)
Length + PokeS(*Buffer+Length, "Content-Length: "+Str(MemorySize(*mem))+#CRLF$, -1, #PB_UTF8)
Length + PokeS(*Buffer+Length, "Content-Type: text/html"+#CRLF$, -1, #PB_UTF8)
Length + PokeS(*Buffer+Length, #CRLF$, -1, #PB_UTF8)
CopyMemory(*mem,*buffer+Length,MemorySize(*mem))
Length + MemorySize(*mem)
FreeMemory(*mem)
SendNetworkData(client,*buffer,length)
PrintN("output")
PrintN(gResponce)
CloseNetworkConnection(client)
gResponce = ""
EndIf
FreeMemory(*buffer)
EndProcedure
; = { "### Instruction", "### Prompt", "### Response", "### Human", "### Assistant", "### Context" };
;### Instruction:
;%1
;### Input:
;You are an Elf.
;### Response:
gctx\n_ctx = 2048 ;maxiumum number of tokens in context windows
gctx\n_predict = 128 ;number of tokens to predict
gctx\top_k = 40 ;top k logits
gctx\top_p = 0.965 ;nuclus sampling probabnility threshold
gctx\temp = 0.28 ;temperature to adjust model's output distribution
gctx\n_batch = 12 ;number of predictions to generate in parallel
gctx\repeat_penalty = 1.2 ;penalty factor for repeated tokens
gctx\repeat_last_n = 10 ;last n tokens To penalize
gctx\context_erase = 0.5 ; percent of context to erase if we exceed the context window
Global Event,ServerEvent,ClientID,gbusy
path.s = "C:\Users\OEM\AppData\Local\nomic.ai\GPT4All\ggml-gpt4all-j-v1.3-groovy.bin"
gmodel = llmodel_model_create2(path,"auto",@gerr);
If gmodel
llmodel_setThreadCount(gmodel,12)
If llmodel_loadModel(gmodel,path)
If CreateNetworkServer(0,80, #PB_Network_IPv4 | #PB_Network_TCP);
OpenWindow(0, 100, 200, 320, 50, "gtp4all")
Repeat
Repeat
Event = WaitWindowEvent(20)
Select Event
Case #PB_Event_CloseWindow
Quit = 1
Case #PB_Event_Gadget
If EventGadget() = 0
Quit = 1
EndIf
EndSelect
Until Event = 0
ServerEvent = NetworkServerEvent()
If ServerEvent
ClientID = EventClient()
Select ServerEvent
Case #PB_NetworkEvent_Connect
If gbusy = 0
gbusy = 1
EndIf
Case #PB_NetworkEvent_Data
If gbusy
Process(clientid)
gbusy=0
EndIf
EndSelect
EndIf
Until Quit = 1
CloseNetworkServer(0)
EndIf
llmodel_model_destroy(gModel)
EndIf
EndIf