PyGPT4All API Reference
pygpt4all.models.gpt4all
GPT4ALL with llama.cpp
backend through pyllamacpp
GPT4All
GPT4All(
model_path,
prompt_context="",
prompt_prefix="",
prompt_suffix="",
log_level=logging.ERROR,
n_ctx=512,
seed=0,
n_parts=-1,
f16_kv=False,
logits_all=False,
vocab_only=False,
use_mlock=False,
embedding=False,
)
Bases: pyllamacpp.model.Model
GPT4All model
Base: pyllamacpp.model.Model
Example usage
from pygpt4all.models.gpt4all import GPT4All
model = GPT4All('path/to/gpt4all/model')
for token in model.generate("Tell me a joke ?"):
print(token, end='', flush=True)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_path |
str
|
the path to the gpt4all model |
required |
prompt_context |
str
|
the global context of the interaction |
''
|
prompt_prefix |
str
|
the prompt prefix |
''
|
prompt_suffix |
str
|
the prompt suffix |
''
|
log_level |
int
|
logging level, set to ERROR by default |
logging.ERROR
|
n_ctx |
int
|
LLaMA context |
512
|
seed |
int
|
random seed |
0
|
n_parts |
int
|
LLaMA n_parts |
-1
|
f16_kv |
bool
|
use fp16 for KV cache |
False
|
logits_all |
bool
|
the llama_eval() call computes all logits, not just the last one |
False
|
vocab_only |
bool
|
only load the vocabulary, no weights |
False
|
use_mlock |
bool
|
force system to keep model in RAM |
False
|
embedding |
bool
|
embedding mode only |
False
|
Source code in pygpt4all/models/gpt4all.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
|
pygpt4all.models.gpt4all_j
GPT4ALL with ggml
backend
GPT4All_J
GPT4All_J(
model_path,
prompt_context="",
prompt_prefix="",
prompt_suffix="",
log_level=logging.ERROR,
)
Bases: pygptj.model.Model
GPT4ALL-J model
Example usage
from pygpt4all.models.gpt4all_j import GPT4All_J
model = GPT4All_J('.path/to/gpr4all-j/model')
for token in model.generate("Tell me a joke ?"):
print(token, end='', flush=True)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_path |
str
|
The path to a gpt4all-j model |
required |
prompt_context |
str
|
the global context of the interaction |
''
|
prompt_prefix |
str
|
the prompt prefix |
''
|
prompt_suffix |
str
|
the prompt suffix |
''
|
log_level |
int
|
logging level, set to ERROR by default |
logging.ERROR
|
Source code in pygpt4all/models/gpt4all_j.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
|
Bases
pyllamacpp.model
This module contains a simple Python API around llama.cpp
Model
Model(
model_path,
prompt_context="",
prompt_prefix="",
prompt_suffix="",
log_level=logging.ERROR,
n_ctx=512,
seed=0,
n_parts=-1,
f16_kv=False,
logits_all=False,
vocab_only=False,
use_mlock=False,
embedding=False,
)
A simple Python class on top of llama.cpp
Example usage
from pyllamacpp.model import Model
model = Model(ggml_model='path/to/ggml/model')
for token in model.generate("Tell me a joke ?"):
print(token, end='', flush=True)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_path |
str
|
the path to the ggml model |
required |
prompt_context |
str
|
the global context of the interaction |
''
|
prompt_prefix |
str
|
the prompt prefix |
''
|
prompt_suffix |
str
|
the prompt suffix |
''
|
log_level |
int
|
logging level, set to INFO by default |
logging.ERROR
|
n_ctx |
int
|
LLaMA context |
512
|
seed |
int
|
random seed |
0
|
n_parts |
int
|
LLaMA n_parts |
-1
|
f16_kv |
bool
|
use fp16 for KV cache |
False
|
logits_all |
bool
|
the llama_eval() call computes all logits, not just the last one |
False
|
vocab_only |
bool
|
only load the vocabulary, no weights |
False
|
use_mlock |
bool
|
force system to keep model in RAM |
False
|
embedding |
bool
|
embedding mode only |
False
|
Source code in /opt/hostedtoolcache/Python/3.11.3/x64/lib/python3.11/site-packages/pyllamacpp/model.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
|
reset
reset()
Resets the context
Source code in /opt/hostedtoolcache/Python/3.11.3/x64/lib/python3.11/site-packages/pyllamacpp/model.py
106 107 108 109 110 111 112 |
|
tokenize
tokenize(text)
Returns a list of tokens for the text
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text |
str
|
text to be tokenized |
required |
Returns:
Type | Description |
---|---|
List of tokens |
Source code in /opt/hostedtoolcache/Python/3.11.3/x64/lib/python3.11/site-packages/pyllamacpp/model.py
114 115 116 117 118 119 120 |
|
detokenize
detokenize(tokens)
Returns a list of tokens for the text
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text |
text to be tokenized |
required |
Returns:
Type | Description |
---|---|
A string representing the text extracted from the tokens |
Source code in /opt/hostedtoolcache/Python/3.11.3/x64/lib/python3.11/site-packages/pyllamacpp/model.py
122 123 124 125 126 127 128 |
|
generate
generate(
prompt,
n_predict=None,
antiprompt=None,
infinite_generation=False,
n_threads=4,
repeat_last_n=64,
top_k=40,
top_p=0.95,
temp=0.8,
repeat_penalty=1.1,
)
Runs llama.cpp inference and yields new predicted tokens from the prompt provided as input
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt |
str
|
The prompt :) |
required |
n_predict |
Union[None, int]
|
if n_predict is not None, the inference will stop if it reaches |
None
|
antiprompt |
str
|
aka the stop word, the generation will stop if this word is predicted, keep it None to handle it in your own way |
None
|
infinite_generation |
bool
|
set it to |
False
|
n_threads |
int
|
The number of CPU threads |
4
|
repeat_last_n |
int
|
last n tokens to penalize |
64
|
top_k |
int
|
top K sampling parameter |
40
|
top_p |
float
|
top P sampling parameter |
0.95
|
temp |
float
|
temperature |
0.8
|
repeat_penalty |
float
|
repeat penalty sampling parameter |
1.1
|
Returns:
Type | Description |
---|---|
Generator
|
Tokens generator |
Source code in /opt/hostedtoolcache/Python/3.11.3/x64/lib/python3.11/site-packages/pyllamacpp/model.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
|
cpp_generate
cpp_generate(
prompt,
n_predict=128,
new_text_callback=None,
n_threads=4,
repeat_last_n=64,
top_k=40,
top_p=0.95,
temp=0.8,
repeat_penalty=1.1,
n_batch=8,
n_keep=0,
interactive=False,
antiprompt=[],
ignore_eos=False,
instruct=False,
verbose_prompt=False,
)
The generate function from llama.cpp
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt |
str
|
the prompt |
required |
n_predict |
int
|
number of tokens to generate |
128
|
new_text_callback |
Callable[[bytes], None]
|
a callback function called when new text is generated, default |
None
|
n_threads |
int
|
The number of CPU threads |
4
|
repeat_last_n |
int
|
last n tokens to penalize |
64
|
top_k |
int
|
top K sampling parameter |
40
|
top_p |
float
|
top P sampling parameter |
0.95
|
temp |
float
|
temperature |
0.8
|
repeat_penalty |
float
|
repeat penalty sampling parameter |
1.1
|
n_batch |
int
|
GPT params n_batch |
8
|
n_keep |
int
|
GPT params n_keep |
0
|
interactive |
bool
|
interactive communication |
False
|
antiprompt |
List
|
list of anti prompts |
[]
|
ignore_eos |
bool
|
Ignore LLaMA EOS |
False
|
instruct |
bool
|
Activate instruct mode |
False
|
verbose_prompt |
bool
|
verbose prompt |
False
|
Returns:
Type | Description |
---|---|
str
|
the new generated text |
Source code in /opt/hostedtoolcache/Python/3.11.3/x64/lib/python3.11/site-packages/pyllamacpp/model.py
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
|
get_params
staticmethod
get_params(params)
Returns a dict
representation of the params
Returns:
Type | Description |
---|---|
dict
|
params dict |
Source code in /opt/hostedtoolcache/Python/3.11.3/x64/lib/python3.11/site-packages/pyllamacpp/model.py
315 316 317 318 319 320 321 322 323 324 325 326 |
|
pygptj.model
This module contains a simple Python API around gpt-j
Model
Model(
model_path,
prompt_context="",
prompt_prefix="",
prompt_suffix="",
log_level=logging.ERROR,
)
GPT-J model
Example usage
from pygptj.model import Model
model = Model(ggml_model='path/to/ggml/model')
for token in model.generate("Tell me a joke ?"):
print(token, end='', flush=True)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_path |
str
|
The path to a gpt-j |
required |
prompt_context |
str
|
the global context of the interaction |
''
|
prompt_prefix |
str
|
the prompt prefix |
''
|
prompt_suffix |
str
|
the prompt suffix |
''
|
log_level |
int
|
logging level |
logging.ERROR
|
Source code in /opt/hostedtoolcache/Python/3.11.3/x64/lib/python3.11/site-packages/pygptj/model.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
|
generate
generate(
prompt,
n_predict=None,
antiprompt=None,
seed=None,
n_threads=4,
top_k=40,
top_p=0.9,
temp=0.9,
)
Runs GPT-J inference and yields new predicted tokens
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt |
str
|
The prompt :) |
required |
n_predict |
Union[None, int]
|
if n_predict is not None, the inference will stop if it reaches |
None
|
antiprompt |
str
|
aka the stop word, the generation will stop if this word is predicted, keep it None to handle it in your own way |
None
|
seed |
int
|
random seed |
None
|
n_threads |
int
|
The number of CPU threads |
4
|
top_k |
int
|
top K sampling parameter |
40
|
top_p |
float
|
top P sampling parameter |
0.9
|
temp |
float
|
temperature |
0.9
|
Returns:
Type | Description |
---|---|
Generator
|
Tokens generator |
Source code in /opt/hostedtoolcache/Python/3.11.3/x64/lib/python3.11/site-packages/pygptj/model.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
|
cpp_generate
cpp_generate(
prompt,
new_text_callback=None,
logits_callback=None,
n_predict=128,
seed=-1,
n_threads=4,
top_k=40,
top_p=0.9,
temp=0.9,
n_batch=8,
)
Runs the inference to cpp generate function
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt |
str
|
the prompt |
required |
new_text_callback |
Callable[[str], None]
|
a callback function called when new text is generated, default |
None
|
logits_callback |
Callable[[np.ndarray], None]
|
a callback function to access the logits on every inference |
None
|
n_predict |
int
|
number of tokens to generate |
128
|
seed |
int
|
The random seed |
-1
|
n_threads |
int
|
Number of threads |
4
|
top_k |
int
|
top_k sampling parameter |
40
|
top_p |
float
|
top_p sampling parameter |
0.9
|
temp |
float
|
temperature sampling parameter |
0.9
|
n_batch |
int
|
batch size for prompt processing |
8
|
Returns:
Type | Description |
---|---|
str
|
the new generated text |
Source code in /opt/hostedtoolcache/Python/3.11.3/x64/lib/python3.11/site-packages/pygptj/model.py
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
|
braindump
braindump(path)
Dumps the logits to .npy
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
Output path |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Source code in /opt/hostedtoolcache/Python/3.11.3/x64/lib/python3.11/site-packages/pygptj/model.py
300 301 302 303 304 305 306 |
|
reset
reset()
Resets the context
Returns:
Type | Description |
---|---|
None
|
None |
Source code in /opt/hostedtoolcache/Python/3.11.3/x64/lib/python3.11/site-packages/pygptj/model.py
308 309 310 311 312 313 314 315 316 |
|
get_params
staticmethod
get_params(params)
Returns a dict
representation of the params
Returns:
Type | Description |
---|---|
dict
|
params dict |
Source code in /opt/hostedtoolcache/Python/3.11.3/x64/lib/python3.11/site-packages/pygptj/model.py
318 319 320 321 322 323 324 325 326 327 328 329 |
|