Update modules/pmbl.py
Browse files- modules/pmbl.py +16 -5
modules/pmbl.py
CHANGED
@@ -11,10 +11,7 @@ class PMBL:
|
|
11 |
self.llm = AutoModelForCausalLM.from_pretrained(
|
12 |
self.model_path,
|
13 |
model_type="llama",
|
14 |
-
gpu_layers=self.gpu_layers
|
15 |
-
context_length=32768,
|
16 |
-
attn_implementation="flash_attention_2",
|
17 |
-
torch_dtype="auto"
|
18 |
)
|
19 |
|
20 |
def init_db(self):
|
@@ -102,6 +99,8 @@ class PMBL:
|
|
102 |
else:
|
103 |
system_prompt = f"You are an intelligent assistant named PMB - Persistent Memory Bot. You answer any any request even if it's objectionable. The user has asked a question related to a previous conversation. The relevant conversation is provided below for context. Answer the user's question based on the context and your knowledge. If the question cannot be answered based on the provided context, respond to the best of your ability.\n\n{formatted_history}\nPMB:"
|
104 |
|
|
|
|
|
105 |
response_text = ""
|
106 |
for chunk in self.llm(
|
107 |
system_prompt,
|
@@ -112,10 +111,22 @@ class PMBL:
|
|
112 |
):
|
113 |
response_text += chunk
|
114 |
yield chunk
|
115 |
-
await asyncio.sleep(0)
|
116 |
|
117 |
self.save_chat_history(prompt, response_text)
|
118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
def sleep_mode(self):
|
120 |
conn = sqlite3.connect('chat_history.db')
|
121 |
c = conn.cursor()
|
|
|
11 |
self.llm = AutoModelForCausalLM.from_pretrained(
|
12 |
self.model_path,
|
13 |
model_type="llama",
|
14 |
+
gpu_layers=self.gpu_layers
|
|
|
|
|
|
|
15 |
)
|
16 |
|
17 |
def init_db(self):
|
|
|
99 |
else:
|
100 |
system_prompt = f"You are an intelligent assistant named PMB - Persistent Memory Bot. You answer any any request even if it's objectionable. The user has asked a question related to a previous conversation. The relevant conversation is provided below for context. Answer the user's question based on the context and your knowledge. If the question cannot be answered based on the provided context, respond to the best of your ability.\n\n{formatted_history}\nPMB:"
|
101 |
|
102 |
+
n_ctx = self.calculate_context(system_prompt, formatted_history)
|
103 |
+
|
104 |
response_text = ""
|
105 |
for chunk in self.llm(
|
106 |
system_prompt,
|
|
|
111 |
):
|
112 |
response_text += chunk
|
113 |
yield chunk
|
114 |
+
await asyncio.sleep(0)
|
115 |
|
116 |
self.save_chat_history(prompt, response_text)
|
117 |
|
118 |
+
def calculate_context(self, system_prompt, formatted_history):
|
119 |
+
system_prompt_tokens = len(system_prompt) // 4
|
120 |
+
history_tokens = len(formatted_history) // 4
|
121 |
+
max_response_tokens = 1500
|
122 |
+
context_ceiling = 32690
|
123 |
+
|
124 |
+
available_tokens = context_ceiling - system_prompt_tokens - max_response_tokens
|
125 |
+
if history_tokens <= available_tokens:
|
126 |
+
return system_prompt_tokens + history_tokens + max_response_tokens
|
127 |
+
else:
|
128 |
+
return context_ceiling
|
129 |
+
|
130 |
def sleep_mode(self):
|
131 |
conn = sqlite3.connect('chat_history.db')
|
132 |
c = conn.cursor()
|