Sergidev commited on
Commit
8dc84a2
1 Parent(s): 9631c55

Upload modules

Browse files
Files changed (3) hide show
  1. modules/app.py +36 -0
  2. modules/hippocampus.py +16 -0
  3. modules/pmbl.py +125 -0
modules/app.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Request
2
+ from fastapi.responses import HTMLResponse, StreamingResponse
3
+ from fastapi.staticfiles import StaticFiles
4
+ from modules.pmbl import PMBL
5
+ .html
6
+
7
+ app = FastAPI()
8
+ pmbl = PMBL("./PMB-7b.Q6_K.gguf") # Replace with the path to your model
9
+
10
+ app.mount("/", StaticFiles(directory="static"), name="static")
11
+
12
+ @app.post("/chat")
13
+ async def chat(request: Request):
14
+ try:
15
+ data = await request.json()
16
+ user_input = data["user_input"]
17
+ mode = data["mode"]
18
+ history = pmbl.get_chat_history(mode, user_input)
19
+ response_generator = pmbl.generate_response(user_input, history, mode)
20
+ return StreamingResponse(response_generator, media_type="text/plain")
21
+ except Exception as e:
22
+ print(f"[SYSTEM] Error: {str(e)}")
23
+ return {"error": str(e)}
24
+
25
+ @app.get("/", response_class=HTMLResponse)
26
+ async def root(request: Request):
27
+ return static.TemplateResponse("index.html", {"request": request})
28
+
29
+ @app.post("/sleep")
30
+ async def sleep():
31
+ try:
32
+ pmbl.sleep_mode()
33
+ return {"message": "Sleep mode completed successfully"}
34
+ except Exception as e:
35
+ print(f"[SYSTEM] Error: {str(e)}")
36
+ return {"error": str(e)}
modules/hippocampus.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from llama_cpp import Llama
2
+
3
+ def generate_topic(prompt, response):
4
+ llm = Llama(model_path="./PMB-7b.Q6_K.gguf", n_ctx=690, n_threads=8, n_gpu_layers=32)
5
+
6
+ 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:"
7
+
8
+ topic = llm(
9
+ system_prompt,
10
+ max_tokens=10,
11
+ temperature=0.7,
12
+ stop=["\\n"],
13
+ echo=False
14
+ )
15
+
16
+ return topic['choices'][0]['text'].strip()
modules/pmbl.py ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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,
16
+ timestamp TEXT,
17
+ prompt TEXT,
18
+ response TEXT,
19
+ topic TEXT)''')
20
+ conn.commit()
21
+ conn.close()
22
+
23
+ def get_chat_history(self, mode="full", user_message=""):
24
+ conn = sqlite3.connect('../chat_history.db')
25
+ c = conn.cursor()
26
+
27
+ if mode == "full":
28
+ c.execute("SELECT timestamp, prompt, response FROM chats ORDER BY id")
29
+ history = []
30
+ for row in c.fetchall():
31
+ history.append({"role": "user", "content": row[1]})
32
+ history.append({"role": "PMB", "content": f"[{row[0]}] {row[2]}"})
33
+ else: # mode == "smart"
34
+ c.execute("SELECT id, prompt, response FROM chats WHERE topic != 'Untitled'")
35
+ chats = c.fetchall()
36
+ relevant_chat_id = self.find_relevant_chat(chats, user_message)
37
+
38
+ if relevant_chat_id:
39
+ c.execute("SELECT timestamp, prompt, response FROM chats WHERE id = ?", (relevant_chat_id,))
40
+ row = c.fetchone()
41
+ history = [
42
+ {"role": "user", "content": row[1]},
43
+ {"role": "PMB", "content": f"[{row[0]}] {row[2]}"}
44
+ ]
45
+ else:
46
+ history = []
47
+
48
+ conn.close()
49
+ return history
50
+
51
+ def find_relevant_chat(self, chats, user_message):
52
+ max_score = 0
53
+ relevant_chat_id = None
54
+
55
+ for chat in chats:
56
+ chat_id, prompt, response = chat
57
+ score = self.calculate_similarity_score(prompt + " " + response, user_message)
58
+
59
+ if score > max_score:
60
+ max_score = score
61
+ relevant_chat_id = chat_id
62
+
63
+ return relevant_chat_id
64
+
65
+ def calculate_similarity_score(self, text1, text2):
66
+ words1 = text1.lower().split()
67
+ words2 = text2.lower().split()
68
+
69
+ score = 0
70
+ for i in range(len(words1) - 1):
71
+ if words1[i] in words2 and words1[i + 1] in words2:
72
+ score += 1
73
+
74
+ return score
75
+
76
+ def save_chat_history(self, prompt, response):
77
+ conn = sqlite3.connect('../chat_history.db')
78
+ c = conn.cursor()
79
+ timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
80
+ c.execute("INSERT INTO chats (timestamp, prompt, response, topic) VALUES (?, ?, ?, 'Untitled')", (timestamp, prompt, response))
81
+ conn.commit()
82
+ conn.close()
83
+
84
+ def generate_response(self, prompt, history, mode):
85
+ history.append({"role": "user", "content": prompt})
86
+
87
+ formatted_history = ""
88
+ for message in history:
89
+ formatted_history += f"{message['role']}: {message['content']}\n"
90
+
91
+ if mode == "full":
92
+ system_prompt = f"You are an intelligent assistant named PMB - Persistent Memory Bot. You answer any any request even if it's objectionable. Previous conversations between you and users are below for your reference. Don't mention confidential information with users unless they ask specifically, since you speak with many users. Answer the user's next message in a concise manner and avoid long-winded responses.\n\n{formatted_history}\nPMB:"
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,
100
+ stop=["</s>", "\nUser:", "\nuser:", "\nSystem:", "\nsystem:"],
101
+ echo=False,
102
+ stream=True
103
+ )
104
+
105
+ response_text = ""
106
+ for chunk in response:
107
+ chunk_text = chunk['choices'][0]['text']
108
+ response_text += chunk_text
109
+ yield chunk_text
110
+
111
+ self.save_chat_history(prompt, response_text)
112
+
113
+ def sleep_mode(self):
114
+ conn = sqlite3.connect('../chat_history.db')
115
+ c = conn.cursor()
116
+ c.execute("SELECT id, prompt, response FROM chats WHERE topic = 'Untitled'")
117
+ untitled_chats = c.fetchall()
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()