Sergidev commited on
Commit
a716951
1 Parent(s): 433a55b
Files changed (1) hide show
  1. modules/pmbl.py +30 -6
modules/pmbl.py CHANGED
@@ -1,15 +1,16 @@
1
  import sqlite3
2
  from datetime import datetime
3
  from llama_cpp import Llama
4
- from .hippocampus import generate_topic
5
 
6
  class PMBL:
7
  def __init__(self, model_path):
8
- self.llm = Llama(model_path=model_path, n_ctx=13000, n_threads=8, n_gpu_layers=32)
9
  self.init_db()
 
10
 
11
  def init_db(self):
12
- conn = sqlite3.connect('../chat_history.db')
13
  c = conn.cursor()
14
  c.execute('''CREATE TABLE IF NOT EXISTS chats
15
  (id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -93,7 +94,15 @@ class PMBL:
93
  else: # mode == "smart"
94
  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:"
95
 
96
- response = self.llm(
 
 
 
 
 
 
 
 
97
  system_prompt,
98
  max_tokens=1500,
99
  temperature=0.7,
@@ -118,8 +127,23 @@ class PMBL:
118
 
119
  for chat in untitled_chats:
120
  chat_id, prompt, response = chat
121
- topic = generate_topic(prompt, response)
122
  c.execute("UPDATE chats SET topic = ? WHERE id = ?", (topic, chat_id))
123
  conn.commit()
124
 
125
- conn.close()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import sqlite3
2
  from datetime import datetime
3
  from llama_cpp import Llama
4
+ from concurrent.futures import ThreadPoolExecutor
5
 
6
  class PMBL:
7
  def __init__(self, model_path):
8
+ self.model_path = model_path
9
  self.init_db()
10
+ self.executor = ThreadPoolExecutor(max_workers=6) # Adjust the max_workers as needed
11
 
12
  def init_db(self):
13
+ conn = sqlite3.connect('./chat_history.db')
14
  c = conn.cursor()
15
  c.execute('''CREATE TABLE IF NOT EXISTS chats
16
  (id INTEGER PRIMARY KEY AUTOINCREMENT,
 
94
  else: # mode == "smart"
95
  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:"
96
 
97
+ response = self.executor.submit(self.generate_response_task, system_prompt, prompt)
98
+
99
+ for chunk in response.result():
100
+ yield chunk
101
+
102
+ def generate_response_task(self, system_prompt, prompt):
103
+ llm = Llama(model_path=self.model_path, n_ctx=13000, n_threads=8, n_gpu_layers=32)
104
+
105
+ response = llm(
106
  system_prompt,
107
  max_tokens=1500,
108
  temperature=0.7,
 
127
 
128
  for chat in untitled_chats:
129
  chat_id, prompt, response = chat
130
+ topic = self.generate_topic(prompt, response)
131
  c.execute("UPDATE chats SET topic = ? WHERE id = ?", (topic, chat_id))
132
  conn.commit()
133
 
134
+ conn.close()
135
+
136
+ def generate_topic(self, prompt, response):
137
+ llm = Llama(model_path=self.model_path, n_ctx=690, n_threads=8, n_gpu_layers=32)
138
+
139
+ system_prompt = f"Based on the following interaction between a user and an AI assistant, generate a concise topic for the conversation in 2-6 words:\n\nUser: {prompt}\nAssistant: {response}\n\nTopic:"
140
+
141
+ topic = llm(
142
+ system_prompt,
143
+ max_tokens=10,
144
+ temperature=0.7,
145
+ stop=["\\n"],
146
+ echo=False
147
+ )
148
+
149
+ return topic['choices'][0]['text'].strip()