import requests import json from tqdm import tqdm with open('questions.json') as f: questions = json.load(f) headers = { 'Authorization': 'Basic Og==', 'Content-Type': 'application/json' } url = "http://127.0.0.1:5001/get_relevant_documents" url_es_get_doc_by_id = "http://127.0.0.1:5001/get_document_by_id" top_five_results=[] for question_item in tqdm(questions): question= question_item["question"] payload = json.dumps({ "query": question, "index": "ib" }) response = requests.request("POST", url, headers=headers, data=payload) results= response.json() ids = [result["meta"]["es_id"] for result in results[:5]] top_five_results=[] for id in ids: payload = json.dumps({ "id": id }) response = requests.request("POST", url_es_get_doc_by_id, headers=headers, data=payload) es_docs=response.json() for es_doc in es_docs: score= [result["score"] for result in results if result["meta"]["es_id"] == es_doc["id"]] top_five_results.append({"title": es_doc["meta"]["name_de"], "score":score, "description":es_doc["content"] }) question_item["top_results"]= top_five_results with open('answered_questions.json', 'w', encoding="utf-8") as outfile: json.dump(questions, outfile, ensure_ascii=False)