-
Notifications
You must be signed in to change notification settings - Fork 0
/
assistant.py
57 lines (44 loc) · 1.52 KB
/
assistant.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
from transformers import pipeline, TextStreamer
import warnings
warnings.filterwarnings("ignore", category=UserWarning)
def start_chat_session(query, relevant_content):
pipe = pipeline(
"text-generation",
"./Qwen2-1.5B-Instruct",
torch_dtype="auto",
device_map="auto",
)
streamer = TextStreamer(
pipe.tokenizer,
skip_prompt=True,
skip_special_tokens=True,
)
instructions = (
"You provide accurate, helpful, and consise responses based on DOCUMENT. "
+ "Reference the DOCUMENTS you found the information from. "
+ "If unsure, admit uncertainty. Do not make up information. "
+ "Maintain a neutal and professional tone."
)
context = (
"DOCUMENT1:\n"
+ relevant_content[0]
+ "DOCUMENT2:\n"
+ relevant_content[0]
+ "DOCUMENT3:\n"
+ relevant_content[0]
)
init_messages = [
{"role": "system", "content": instructions},
{"role": "system", "content": context},
{"role": "user", "content": query},
]
while True:
print("\nAssistant: ", end="")
output = pipe(init_messages, max_new_tokens=512, streamer=streamer)
answer = output[0]["generated_text"][-1]["content"].strip()
if query.lower() == "goodbye":
break
init_messages.append({"role": "assistant", "content": answer})
print("\nYou: ", end="")
query = input()
init_messages.append({"role": "user", "content": query})