2023-05-15 12:45:56 -04:00
|
|
|
package gpt4all
|
|
|
|
|
|
|
|
type PredictOptions struct {
|
2024-04-29 08:49:26 -04:00
|
|
|
ContextSize, RepeatLastN, Tokens, TopK, Batch, Special int
|
|
|
|
TopP, MinP, Temperature, ContextErase, RepeatPenalty float64
|
2023-05-15 12:45:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type PredictOption func(p *PredictOptions)
|
|
|
|
|
|
|
|
var DefaultOptions PredictOptions = PredictOptions{
|
|
|
|
Tokens: 200,
|
|
|
|
TopK: 10,
|
|
|
|
TopP: 0.90,
|
2024-04-29 08:49:26 -04:00
|
|
|
MinP: 0.0,
|
2023-05-15 12:45:56 -04:00
|
|
|
Temperature: 0.96,
|
|
|
|
Batch: 1,
|
2024-04-29 08:49:26 -04:00
|
|
|
Special: 0,
|
2023-05-15 12:45:56 -04:00
|
|
|
ContextErase: 0.55,
|
|
|
|
ContextSize: 1024,
|
|
|
|
RepeatLastN: 10,
|
|
|
|
RepeatPenalty: 1.2,
|
|
|
|
}
|
|
|
|
|
|
|
|
var DefaultModelOptions ModelOptions = ModelOptions{
|
2023-06-01 08:37:14 -04:00
|
|
|
Threads: 4,
|
2023-05-15 12:45:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type ModelOptions struct {
|
2023-06-14 10:27:19 -04:00
|
|
|
Threads int
|
|
|
|
LibrarySearchPath string
|
2023-05-15 12:45:56 -04:00
|
|
|
}
|
|
|
|
type ModelOption func(p *ModelOptions)
|
|
|
|
|
|
|
|
// SetTokens sets the number of tokens to generate.
|
|
|
|
func SetTokens(tokens int) PredictOption {
|
|
|
|
return func(p *PredictOptions) {
|
|
|
|
p.Tokens = tokens
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTopK sets the value for top-K sampling.
|
|
|
|
func SetTopK(topk int) PredictOption {
|
|
|
|
return func(p *PredictOptions) {
|
|
|
|
p.TopK = topk
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTopP sets the value for nucleus sampling.
|
|
|
|
func SetTopP(topp float64) PredictOption {
|
|
|
|
return func(p *PredictOptions) {
|
|
|
|
p.TopP = topp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-24 17:51:34 -05:00
|
|
|
// SetMinP sets the value for min p sampling
|
|
|
|
func SetMinP(minp float64) PredictOption {
|
|
|
|
return func(p *PredictOptions) {
|
|
|
|
p.MinP = minp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-15 12:45:56 -04:00
|
|
|
// SetRepeatPenalty sets the repeat penalty.
|
|
|
|
func SetRepeatPenalty(ce float64) PredictOption {
|
|
|
|
return func(p *PredictOptions) {
|
|
|
|
p.RepeatPenalty = ce
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetRepeatLastN sets the RepeatLastN.
|
|
|
|
func SetRepeatLastN(ce int) PredictOption {
|
|
|
|
return func(p *PredictOptions) {
|
|
|
|
p.RepeatLastN = ce
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetContextErase sets the context erase %.
|
|
|
|
func SetContextErase(ce float64) PredictOption {
|
|
|
|
return func(p *PredictOptions) {
|
|
|
|
p.ContextErase = ce
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTemperature sets the temperature value for text generation.
|
|
|
|
func SetTemperature(temp float64) PredictOption {
|
|
|
|
return func(p *PredictOptions) {
|
|
|
|
p.Temperature = temp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetBatch sets the batch size.
|
|
|
|
func SetBatch(size int) PredictOption {
|
|
|
|
return func(p *PredictOptions) {
|
|
|
|
p.Batch = size
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-29 08:49:26 -04:00
|
|
|
// SetSpecial is true if special tokens in the prompt should be processed, false otherwise.
|
|
|
|
func SetSpecial(special bool) PredictOption {
|
|
|
|
return func(p *PredictOptions) {
|
|
|
|
if special {
|
|
|
|
p.Special = 1
|
|
|
|
} else {
|
|
|
|
p.Special = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-15 12:45:56 -04:00
|
|
|
// Create a new PredictOptions object with the given options.
|
|
|
|
func NewPredictOptions(opts ...PredictOption) PredictOptions {
|
|
|
|
p := DefaultOptions
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(&p)
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetThreads sets the number of threads to use for text generation.
|
|
|
|
func SetThreads(c int) ModelOption {
|
|
|
|
return func(p *ModelOptions) {
|
|
|
|
p.Threads = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-14 10:27:19 -04:00
|
|
|
// SetLibrarySearchPath sets the dynamic libraries used by gpt4all for the various ggml implementations.
|
|
|
|
func SetLibrarySearchPath(t string) ModelOption {
|
|
|
|
return func(p *ModelOptions) {
|
|
|
|
p.LibrarySearchPath = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-15 12:45:56 -04:00
|
|
|
// Create a new PredictOptions object with the given options.
|
|
|
|
func NewModelOptions(opts ...ModelOption) ModelOptions {
|
|
|
|
p := DefaultModelOptions
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(&p)
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|