|
a |
|
b/src/llama-main/example_text_completion.py |
|
|
1 |
# Copyright (c) Meta Platforms, Inc. and affiliates. |
|
|
2 |
# This software may be used and distributed according to the terms of the Llama 2 Community License Agreement. |
|
|
3 |
|
|
|
4 |
import fire |
|
|
5 |
|
|
|
6 |
from llama import Llama |
|
|
7 |
from typing import List |
|
|
8 |
|
|
|
9 |
def main( |
|
|
10 |
ckpt_dir: str, |
|
|
11 |
tokenizer_path: str, |
|
|
12 |
temperature: float = 0.6, |
|
|
13 |
top_p: float = 0.9, |
|
|
14 |
max_seq_len: int = 128, |
|
|
15 |
max_gen_len: int = 64, |
|
|
16 |
max_batch_size: int = 4, |
|
|
17 |
): |
|
|
18 |
""" |
|
|
19 |
Entry point of the program for generating text using a pretrained model. |
|
|
20 |
|
|
|
21 |
Args: |
|
|
22 |
ckpt_dir (str): The directory containing checkpoint files for the pretrained model. |
|
|
23 |
tokenizer_path (str): The path to the tokenizer model used for text encoding/decoding. |
|
|
24 |
temperature (float, optional): The temperature value for controlling randomness in generation. |
|
|
25 |
Defaults to 0.6. |
|
|
26 |
top_p (float, optional): The top-p sampling parameter for controlling diversity in generation. |
|
|
27 |
Defaults to 0.9. |
|
|
28 |
max_seq_len (int, optional): The maximum sequence length for input prompts. Defaults to 128. |
|
|
29 |
max_gen_len (int, optional): The maximum length of generated sequences. Defaults to 64. |
|
|
30 |
max_batch_size (int, optional): The maximum batch size for generating sequences. Defaults to 4. |
|
|
31 |
""" |
|
|
32 |
generator = Llama.build( |
|
|
33 |
ckpt_dir=ckpt_dir, |
|
|
34 |
tokenizer_path=tokenizer_path, |
|
|
35 |
max_seq_len=max_seq_len, |
|
|
36 |
max_batch_size=max_batch_size, |
|
|
37 |
) |
|
|
38 |
|
|
|
39 |
prompts: List[str] = [ |
|
|
40 |
# For these prompts, the expected answer is the natural continuation of the prompt |
|
|
41 |
"I believe the meaning of life is", |
|
|
42 |
"Simply put, the theory of relativity states that ", |
|
|
43 |
"""A brief message congratulating the team on the launch: |
|
|
44 |
|
|
|
45 |
Hi everyone, |
|
|
46 |
|
|
|
47 |
I just """, |
|
|
48 |
# Few shot prompt (providing a few examples before asking model to complete more); |
|
|
49 |
"""Translate English to French: |
|
|
50 |
|
|
|
51 |
sea otter => loutre de mer |
|
|
52 |
peppermint => menthe poivrée |
|
|
53 |
plush girafe => girafe peluche |
|
|
54 |
cheese =>""", |
|
|
55 |
] |
|
|
56 |
results = generator.text_completion( |
|
|
57 |
prompts, |
|
|
58 |
max_gen_len=max_gen_len, |
|
|
59 |
temperature=temperature, |
|
|
60 |
top_p=top_p, |
|
|
61 |
) |
|
|
62 |
for prompt, result in zip(prompts, results): |
|
|
63 |
print(prompt) |
|
|
64 |
print(f"> {result['generation']}") |
|
|
65 |
print("\n==================================\n") |
|
|
66 |
|
|
|
67 |
|
|
|
68 |
if __name__ == "__main__": |
|
|
69 |
fire.Fire(main) |