-
Notifications
You must be signed in to change notification settings - Fork 0
/
kb_main.py
136 lines (116 loc) · 4.58 KB
/
kb_main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# from vectorstores.IndivDingo import *
# from vectorstores.dingo import *
# from models.model_load import KbModel
# # 数据库相关参数
# index_name = "zetyun_new"
# KB_DEFAULT_VECTOR_USERNAME = 'root'
# KB_DEFAULT_VECTOR_PASSWORD = '123456'
# KB_DEFAULT_VECTOR_ADDRESS = "172.20.31.10:14000"
# # 编码模型相关服务地址
# embeddings = KbModel("172.20.1.32:9911","172.20.1.32:9911")
# # 连接数据库初始化
# vector_store = IndivDingo(
# embedding_function=embeddings.embed_documents,
# text_key="text",
# index_name=index_name,
# host=[KB_DEFAULT_VECTOR_ADDRESS],
# user=KB_DEFAULT_VECTOR_USERNAME,
# password=KB_DEFAULT_VECTOR_PASSWORD,
# )
# if __name__ == "__main__":
# # 简单测试
# # query = 'dingodb的特点?'
# # qa_docs,related_docs_with_score = vector_store.similarity_search_with_chunk_conent_sx(
# # query,
# # k=5,
# # index_name=index_name
# # )
# # print(qa_docs)
# # print(related_docs_with_score)
# # for chunk in related_docs_with_score:
# # print(chunk.metadata['text'])
# query_list = []
# answer_list = []
# hit = 0
# with open('/data/rhtao/multi-modal/bge_fine_tune/FlagEmbedding/datasets/query_val.txt', 'r') as file , open("/data/rhtao/multi-modal/bge_fine_tune/FlagEmbedding/datasets/answer_val.txt", "r") as answer_file:
# queries = file.readlines()
# answers = answer_file.readlines()
# for query, answer in zip(queries, answers):
# query_list.append(query)
# answer_list.append(answer)
# for i in range(len(query_list)):
# query = query_list[i]
# answer = answer_list[i]
# qa_docs,related_docs_with_score = vector_store.similarity_search_with_chunk_conent_sx(
# query,
# k=6,
# index_name=index_name
# )
# for chunk in related_docs_with_score[::-1]:
# if chunk.metadata['text'] in answer:
# hit += 1
# # break
# print(hit/len(query_list))
import csv
from vectorstores.IndivDingo import *
from vectorstores.dingo import *
from models.model_load import KbModel
# 数据库相关参数
index_name = "zetyun_new"
KB_DEFAULT_VECTOR_USERNAME = 'root'
KB_DEFAULT_VECTOR_PASSWORD = '123456'
KB_DEFAULT_VECTOR_ADDRESS = "172.20.31.10:14000"
# 编码模型相关服务地址
embeddings = KbModel("172.20.1.32:9911","172.20.1.32:9911")
# 连接数据库初始化
vector_store = IndivDingo(
embedding_function=embeddings.embed_documents,
text_key="text",
index_name=index_name,
host=[KB_DEFAULT_VECTOR_ADDRESS],
user=KB_DEFAULT_VECTOR_USERNAME,
password=KB_DEFAULT_VECTOR_PASSWORD,
)
if __name__ == "__main__":
query_list = []
answer_list = []
# correct_results = []
# error_results = []
correct_num = 0
with open('/data/rhtao/multi-modal/bge_fine_tune/FlagEmbedding/datasets/query_val.txt', 'r') as file, open("/data/rhtao/multi-modal/bge_fine_tune/FlagEmbedding/datasets/answer_val.txt", "r") as answer_file:
queries = file.readlines()
answers = answer_file.readlines()
for query, answer in zip(queries, answers):
query_list.append(query)
answer_list.append(answer)
for i in range(len(query_list)):#len(query_list)
query = query_list[i]
answer = answer_list[i]
qa_docs, related_docs_with_score = vector_store.similarity_search_with_chunk_conent_sx(
query,
k=6,
index_name=index_name
)
top5chunks = [chunk.metadata['text'] for chunk in related_docs_with_score[::-1]]
# print(top5chunks)
for chunk in top5chunks:
if chunk.strip() in answer:
correct_num += 1
# correct_results.append([answer, top5chunks])
break
# else:
# # error_results.append([answer, top5chunks])
# break
print(correct_num/len(query_list))
# # 将结果写入到correct.csv文件
# with open('correct.csv', 'w', newline='') as correct_file:
# writer = csv.writer(correct_file)
# writer.writerow(['Answer', 'Chunk'])
# writer.writerows(correct_results)
# # 将结果写入到error.csv文件
# with open('error.csv', 'w', newline='') as error_file:
# writer = csv.writer(error_file)
# writer.writerow(['Answer', 'Chunk'])
# writer.writerows(error_results)
# 全匹配模式下的Top5正确率为:0.38822525597269625
# 忽略空格模式下的Top5正确率为: