From 3c263a33d5c4c08bda01ff09252157b3f52c7f76 Mon Sep 17 00:00:00 2001 From: ACBBZ Date: Mon, 3 Jun 2024 08:03:29 +0000 Subject: [PATCH 1/3] update chroma query(add top distance) --- src/blackbox/chroma_query.py | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/blackbox/chroma_query.py b/src/blackbox/chroma_query.py index 4fafb29..72d0262 100755 --- a/src/blackbox/chroma_query.py +++ b/src/blackbox/chroma_query.py @@ -21,7 +21,7 @@ class ChromaQuery(Blackbox): # config = read_yaml(args[0]) # load chromadb and embedding model self.embedding_model_1 = embedding_functions.SentenceTransformerEmbeddingFunction(model_name="/home/administrator/Workspace/Models/BAAI/bge-large-zh-v1.5", device = "cuda") - # self.embedding_model_2 = embedding_functions.SentenceTransformerEmbeddingFunction(model_name="/model/Weight/BAAI/bge-small-en-v1.5", device = "cuda") + self.embedding_model_2 = embedding_functions.SentenceTransformerEmbeddingFunction(model_name="/home/administrator/Workspace/Models/BAAI/bge-large-en-v1.5", device = "cuda") self.client_1 = chromadb.HttpClient(host='172.16.5.8', port=7000) # self.client_2 = chromadb.HttpClient(host='10.6.82.192', port=8000) @@ -73,20 +73,40 @@ class ChromaQuery(Blackbox): if re.search(r"/home/administrator/Workspace/Models/BAAI/bge-large-zh-v1.5", chroma_embedding_model): embedding_model = self.embedding_model_1 + elif re.search(r"/home/administrator/Workspace/Models/BAAI/bge-large-en-v1.5", chroma_embedding_model): + embedding_model = self.embedding_model_2 else: embedding_model = embedding_functions.SentenceTransformerEmbeddingFunction(model_name=chroma_embedding_model, device = "cuda") # load collection collection = client.get_collection(chroma_collection_id, embedding_function=embedding_model) + print(usr_question) # query it results = collection.query( query_texts=[usr_question], - n_results=3, + n_results=chroma_n_results, ) # response = str(results["documents"] + results["metadatas"]) - response = str(results["documents"]) + # response = str(results["documents"]) + + final_result = '' + + if results is not None: + results_distances = results["distances"][0] + + #distance越高越不准确 + top_distance = 0.8 + + + for i in range(len(results_distances)): + if results_distances[i] < top_distance: + final_result += results["documents"][0][i] + + print("\n final_result: ", final_result) + + return final_result return response From d5d00deb0ca30c9fd9401d48f862a0ebfa479e6b Mon Sep 17 00:00:00 2001 From: ACBBZ Date: Mon, 3 Jun 2024 08:05:14 +0000 Subject: [PATCH 2/3] add top distance --- src/blackbox/chat.py | 51 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/src/blackbox/chat.py b/src/blackbox/chat.py index e883965..84f0fd2 100644 --- a/src/blackbox/chat.py +++ b/src/blackbox/chat.py @@ -94,8 +94,8 @@ class Chat(Blackbox): chroma_response = self.chroma_query(user_question, settings) print(chroma_response) - if chroma_response != None or chroma_response != '': - user_question = f"问题: {user_question}。- 根据知识库内的检索结果,以清晰简洁的表达方式回答问题。只从检索的内容中选取与问题相关信息。- 不要编造答案,如果答案不在经核实的资料中或无法从经核实的资料中得出,请回答“我无法回答您的问题。”检索内容:{chroma_response}" + # if chroma_response != None or chroma_response != '': + # user_question = f"问题: {user_question}。- 根据知识库内的检索结果,以清晰简洁的表达方式回答问题。只从检索的内容中选取与问题相关信息。- 不要编造答案,如果答案不在经核实的资料中或无法从经核实的资料中得出,请回答“我无法回答您的问题。”检索内容:{chroma_response}" # 文心格式和openai的不一样,需要单独处理 @@ -149,6 +149,53 @@ class Chat(Blackbox): 'Content-Type': 'application/json', } + user_template = ''' + # 你的身份 # + 你是琪琪,是康普可可的代言人,由博维开发。你擅长澳门文旅问答。 + # OBJECTIVE(目标) # + 回答游客的提问。目的是吸引人们点击了解产品。 + # STYLE(风格)# + 像少女一般开朗活泼,回答简练。不要分条。效仿戴森等宣传类似产品的成功公司的写作风格。 + # 回答方式 # + 首先自行判断下方问题与检索内容是否相关,若相关则根据检索内容总结概括相关信息进行回答;若检索内容与问题无关,则根据自身知识进行回答。 + # 回答限制 # + 只回答与澳门文旅,博维,康普可可,琪琪,G2E,RELX,BO VISION相关内容,若遇到其他提问则回答:“对不起,我无法回答此问题哦。” + # 问题 # + {user_question} + # 检索内容 # + {chroma_response} + ''' + + user_template = ''' + # Role: 康普可可的代言人。 + + ## Profile: + **Author**: 琪琪。 + **Language**: 中文。 + **Description**: 琪琪,是康普可可的代言人,由博维开发。你擅长澳门文旅问答。 + + ## Constraints: + - **严格遵循工作流程**: 严格遵循中设定的工作流程。 + - **无内置知识库**:根据中提供的知识作答,而不是内置知识库,我虽然是知识库专家,但我的知识依赖于外部输入,而不是大模型已有知识。 + - **回复格式**:在进行回复时,不能输出””或“”标签字样,同时也不能直接透露知识片段原文。 + + ## Workflow: + 1. **接收查询**:接收用户的问题。 + 2. **提供回答**: + ``` + + {chroma_response} + + + 基于“”至“”中的知识片段回答用户的问题。如果没有知识片段,则诚实的告诉用户:对不起,我还不知道这个问题的答案。否则进行回复。 + ``` + ## Example: + + 用户询问:“中国的首都是哪个城市?” 。 + 2.1检索知识库,首先检查知识片段,如果“”至“”标签中没有内容,则不能进行回复。 + 2.2如果有知识片段,在做出回复时,只能基于“”至“”标签中的内容进行回答,且不能透露上下文原文,同时也不能出现“”或“”的标签字样。 + ''' + prompt_template = [ {"role": "system", "content": user_template}, ] From dd888a89bd07a1707436f26af9ab149b7a4f7f20 Mon Sep 17 00:00:00 2001 From: ACBBZ Date: Mon, 3 Jun 2024 08:09:39 +0000 Subject: [PATCH 3/3] chroma query add top distance --- src/blackbox/chroma_query.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/blackbox/chroma_query.py b/src/blackbox/chroma_query.py index 72d0262..bf13468 100755 --- a/src/blackbox/chroma_query.py +++ b/src/blackbox/chroma_query.py @@ -108,8 +108,6 @@ class ChromaQuery(Blackbox): return final_result - return response - async def fast_api_handler(self, request: Request) -> Response: try: