BarryWang commited on
Commit
8ca8882
1 Parent(s): c085fc0

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +5 -1
  2. memory.py +16 -12
app.py CHANGED
@@ -74,6 +74,8 @@ with gr.Blocks(title=title) as demo:
74
  neighborhoods = mb.search(history[-1][0])
75
  messages += neighborhoods
76
  messages.append({"role": "user", "content": history[-1][0]})
 
 
77
  print('user:', {"role": "user", "content": history[-1][0]})
78
  print('neighborhoods:', neighborhoods)
79
 
@@ -101,6 +103,7 @@ with gr.Blocks(title=title) as demo:
101
  def reset_model():
102
  global messages
103
  messages = [{"role": "system", "content": prompt_default}]
 
104
  print("已重置模型")
105
  return [[None, "已重置模型"]]
106
 
@@ -109,7 +112,7 @@ with gr.Blocks(title=title) as demo:
109
  user_content = text[0:text.find("|")]
110
  assistant_content = text[text.find("|") + 1:]
111
  mb.upsert([Dialogue(user_content, assistant_content)])
112
- return [[None, f"已将样例存入记忆区块 ({user_content} -> {assistant_content})"]]
113
 
114
 
115
  msg.submit(user, [prompt, msg, chatbot], [msg, chatbot], queue=False).then(
@@ -121,3 +124,4 @@ with gr.Blocks(title=title) as demo:
121
 
122
  if __name__ == "__main__":
123
  demo.launch(height=520, show_api=False, auth=("luoshaoye", "19970812"))
 
 
74
  neighborhoods = mb.search(history[-1][0])
75
  messages += neighborhoods
76
  messages.append({"role": "user", "content": history[-1][0]})
77
+
78
+ print('messages:', messages)
79
  print('user:', {"role": "user", "content": history[-1][0]})
80
  print('neighborhoods:', neighborhoods)
81
 
 
103
  def reset_model():
104
  global messages
105
  messages = [{"role": "system", "content": prompt_default}]
106
+ mb.reset()
107
  print("已重置模型")
108
  return [[None, "已重置模型"]]
109
 
 
112
  user_content = text[0:text.find("|")]
113
  assistant_content = text[text.find("|") + 1:]
114
  mb.upsert([Dialogue(user_content, assistant_content)])
115
+ return [[None, f"已将对话样例存入记忆区块\n问: {user_content}\n答: {assistant_content}"]]
116
 
117
 
118
  msg.submit(user, [prompt, msg, chatbot], [msg, chatbot], queue=False).then(
 
124
 
125
  if __name__ == "__main__":
126
  demo.launch(height=520, show_api=False, auth=("luoshaoye", "19970812"))
127
+ # demo.launch(height=520, show_api=False)
memory.py CHANGED
@@ -3,8 +3,7 @@
3
  # @Author : BarryWang
4
  # @FileName: memory.py
5
  # @Github : https://github.com/BarryWangQwQ
6
-
7
- # import time
8
 
9
  from txtai.embeddings import Embeddings
10
 
@@ -38,8 +37,8 @@ class MemoryBlocks:
38
  def upsert(self, dialogue_list):
39
  self.embeddings.upsert(
40
  (
41
- uid, {'text': dialogue.user_content, 'raw': dialogue.raw()}, None
42
- ) for uid, dialogue in enumerate(dialogue_list)
43
  )
44
 
45
  def search(self, question: str) -> list:
@@ -51,12 +50,17 @@ class MemoryBlocks:
51
  neighborhoods += eval(r['raw'])
52
  return neighborhoods
53
 
54
- # data = [
55
- # Dialogue('你的生日是哪一天?', '我的生日是1997年8月12日'),
56
- # Dialogue('你叫什么名字?', '我叫洛少爷'),
57
- # Dialogue('你擅长什么?', '我擅长音乐,偶尔也会配音'),
58
- # ]
 
 
 
 
 
 
59
 
60
- # if __name__ == "__main__":
61
- # upsert(data)
62
- # print(search("你是谁?"))
 
3
  # @Author : BarryWang
4
  # @FileName: memory.py
5
  # @Github : https://github.com/BarryWangQwQ
6
+ import uuid
 
7
 
8
  from txtai.embeddings import Embeddings
9
 
 
37
  def upsert(self, dialogue_list):
38
  self.embeddings.upsert(
39
  (
40
+ str(uuid.uuid4()), {'text': dialogue.user_content, 'raw': dialogue.raw()}, None
41
+ ) for dialogue in dialogue_list
42
  )
43
 
44
  def search(self, question: str) -> list:
 
50
  neighborhoods += eval(r['raw'])
51
  return neighborhoods
52
 
53
+ def reset(self):
54
+ self.embeddings.close()
55
+ self.embeddings = Embeddings(
56
+ {
57
+ "path": "sentence-transformers/distiluse-base-multilingual-cased-v2",
58
+ 'content': True
59
+ }
60
+ )
61
+
62
+ def save(self, output_path):
63
+ self.embeddings.save(output_path)
64
 
65
+ def load(self, load_path):
66
+ self.embeddings.load(load_path)