-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.py
66 lines (52 loc) · 1.5 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
import sys
from colorama import Fore
from random import randint
sys.path.append("./build/")
import pyllama_cpu as llama
MODEL_PATH = "./models/7B/ggml-model-q4_0.bin"
def flush_output(token: str) -> None:
"""
Prints the given token in green color and flushes the output stream.
Args:
token (str): The token to be printed.
Returns:
None
"""
print(Fore.GREEN + token, end='', flush=True)
# Instantiate the model object
model = llama.Model(
path=MODEL_PATH,
num_threads=10,
n_ctx=512,
last_n_size=64,
seed=randint(0, 10e5)
)
# Print the welcome message
print(Fore.YELLOW + "--------------------------------------------")
print(Fore.YELLOW + "----LLaMa text completion running on CPU----")
print(Fore.YELLOW + "-------Type 'exit' to close the loop--------")
print(Fore.YELLOW + "--------------------------------------------")
# Start the main loop
while True:
# Prompt the user for input
print(Fore.RED + '\n[Prompt]: ' + Fore.WHITE, end='')
user_input = input()
# Check if the user wants to exit
if user_input == "exit":
break
# Ingest the user input
res = model.ingest(user_input)
# Check if ingestion was successful
if res is not True:
break
# Print the user input
print(Fore.WHITE + user_input, end='')
# Generate text using the model
res = model.generate(
num_tokens=130,
top_p=0.95,
temp=0.8,
repeat_penalty=1.0,
streaming_fn=flush_output,
)
print("\n")