- Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathscript_api.py
More file actions
Latest commit
99 lines (82 loc) · 3.42 KB
/
Copy pathscript_api.py
File metadata and controls
99 lines (82 loc) · 3.42 KB
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
importjson
fromfastapiimportFastAPI
frompydanticimportBaseModel
importuvicorn
importfaiss
fromFlagEmbeddingimportFlagAutoModel
fromtypingimportList
importargparse
fromgraphr1importGraphR1, QueryParam
importasyncio
fromtqdmimporttqdm
parser=argparse.ArgumentParser()
parser.add_argument('--data_source', default='2WikiMultiHopQA')
args=parser.parse_args()
data_source=args.data_source
# 加载 FAISS 索引和 FlagEmbedding 模型
model=FlagAutoModel.from_finetuned(
'BAAI/bge-large-en-v1.5',
query_instruction_for_retrieval="Represent this sentence for searching relevant passages: ",
devices="cpu",
)
# 加载 FAISS 索引和 FlagEmbedding 模型
print(f"[DEBUG] LOADING EMBEDDINGS")
index_entity=faiss.read_index(f"expr/{data_source}/index_entity.bin")
corpus_entity= []
withopen(f"expr/{data_source}/kv_store_entities.json") asf:
entities=json.load(f)
foriteminentities:
corpus_entity.append(entities[item]['entity_name'])
print("[DEBUG] EMBEDDINGS LOADED")
# 加载 FAISS 索引和 FlagEmbedding 模型
print(f"[DEBUG] LOADING EMBEDDINGS")
index_hyperedge=faiss.read_index(f"expr/{data_source}/index_hyperedge.bin")
corpus_hyperedge= []
withopen(f"expr/{data_source}/kv_store_hyperedges.json") asf:
hyperedges=json.load(f)
foriteminhyperedges:
corpus_hyperedge.append(hyperedges[item]['content'])
print("[DEBUG] EMBEDDINGS LOADED")
rag=GraphR1(
working_dir=f"expr/{data_source}",
)
asyncdefprocess_query(query_text, rag_instance, entity_match, hyperedge_match):
result=awaitrag_instance.aquery(query_text, param=QueryParam(only_need_context=True, top_k=10), entity_match=entity_match, hyperedge_match=hyperedge_match)
return {"query": query_text, "result": result}
defalways_get_an_event_loop() ->asyncio.AbstractEventLoop:
try:
loop=asyncio.get_event_loop()
exceptRuntimeError:
loop=asyncio.new_event_loop()
asyncio.set_event_loop(loop)
returnloop
def_format_results(results: List, corpus) ->str:
results_list= []
fori, resultinenumerate(results):
results_list.append(corpus[result])
returnresults_list
defqueries_to_results(queries: List[str]) ->List[str]:
embeddings=model.encode_queries(queries)
_, ids=index_entity.search(embeddings, 5) # 每个查询返回 5 个结果
entity_match= {queries[i]:_format_results(ids[i], corpus_entity) foriinrange(len(ids))}
_, ids=index_hyperedge.search(embeddings, 5) # 每个查询返回 5 个结果
hyperedge_match= {queries[i]:_format_results(ids[i], corpus_hyperedge) foriinrange(len(ids))}
results= []
loop=always_get_an_event_loop()
forquery_textintqdm(queries, desc="Processing queries", unit="query"):
result=loop.run_until_complete(
process_query(query_text, rag, entity_match[query_text], hyperedge_match[query_text])
)
results.append(json.dumps({"results": result["result"]}))
returnresults
########### PREDEFINE ############
# 创建 FastAPI 实例
app=FastAPI(title="Search API", description="An API for document retrieval using FAISS and FlagEmbedding.")
classSearchRequest(BaseModel):
queries: List[str]
@app.post("/search")
defsearch(request: SearchRequest):
results_str=queries_to_results(request.queries)
returnresults_str
if__name__=="__main__":
uvicorn.run(app, host="0.0.0.0", port=8001)