Browse Source

Update 'README.md'

天问 2 years ago
parent
commit
b84a1b02aa
1 changed files with 31 additions and 0 deletions
  1. 31 0
      README.md

+ 31 - 0
README.md

@@ -240,3 +240,34 @@ This technique was mainly implemented in the [the Self-Refine: Iterative Refinem
 For more detailed usage and code examples check `./examples`.
 
 
+## 源码分析
+
+上下文记忆功能:
+
+上下文记忆其实就是每次提问都把之前的问题作为一个list,重复发给openai接口,ThinkGPT 提供记忆函数 llm.remember() ,具体实现如下:
+
+```
+    def remember(self, concept: Union[str, Document] = None, limit: int = 5, sort_by_order: bool = False, max_tokens: Optional[int] = None) -> List[str]:
+        if len(self.memory) == 0:
+            return []
+        if concept is None:
+            return [doc.text for doc in self.memory[-limit:]]
+        elif isinstance(concept, str):
+            query_input = Document(embedding=np.asarray(self.embeddings_model.embed_query(concept)))
+        elif isinstance(concept, Document):
+            assert concept.embedding is not None
+            query_input = concept
+        else:
+            raise ValueError('wrong type, must be either str or Document')
+
+        docs = self.memory.find(query_input, limit=limit)[0]
+        # memory needs to be sorted in chronological order
+        if sort_by_order:
+            docs = sorted(docs, key=lambda doc: doc.tags['mem_cnt'])
+        text_results = [doc.text for doc in docs]
+        if max_tokens:
+            text_results = fit_context(text_results, max_tokens)
+        return text_results
+
+```
+上面函数就是对 embeddings_model 处理,把 concept 加入,排序,标准化(不能超过最大tokens)等。