PyGPT-J API Reference
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 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 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 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 pygptj/model.py
300 301 302 303 304 305 306 |
|
reset
reset()
Resets the context
Returns:
Type | Description |
---|---|
None
|
None |
Source code in 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 pygptj/model.py
318 319 320 321 322 323 324 325 326 327 328 329 |
|