mongodb_client = pymongo.MongoClient(_mongoURI) store = MongoDBAtlasVectorSearch(mongodb_client, db_name=indexName) # Initialisation de l'index via les index sur MongoDB Atlas # Et inversement, commenter/décommenter si on veut juste query l'index existant # build index index = VectorStoreIndex.from_vector_store(store) # configure retriever retriever = VectorIndexRetriever( index=index, ) # configure response synthesizer response_synthesizer = get_response_synthesizer( response_mode= ResponseMode.TREE_SUMMARIZE, ) # Template obligatoire du system prompt définissant le comprtement du LLM) qa_template = Prompt(requestDTO.get_system_template()) if not qa_template or qa_template == "" or qa_template == None: responseDTO = ServiceQueryResponse.ServiceQueryResponseDTO(True, "Un historique de type 'System' est obligatoire.", None) return GenerateQueryResponse(requestDTO, responseDTO), 400 # assemble query engine query_engine = RetrieverQueryEngine( retriever=retriever, response_synthesizer=response_synthesizer, ) #query_engine = query_engine.query(similarity_top_k=requestDTO.LinkNumber,text_qa_template=qa_template) # Partie permettant de créer la réponse query_engine = index.as_query_engine(similarity_top_k=requestDTO.LinkNumber,text_qa_template=qa_template) query_text = requestDTO.Prompt gpt_result = query_engine.query(query_text) resultDTO = ServiceQueryResponse.ServiceQueryResponseResultDTO(gpt_result.response, answer.message.content, []) for item in gpt_result.source_nodes: node = ServiceQueryResponse.ServiceQueryResponseNodeDTO(item.node.extra_info.get("file_name"), item.node.extra_info.get("page_label"), item.node.text, item.score) resultDTO.Nodes.append(node) # Construction de la réponse responseDTO = ServiceQueryResponse.ServiceQueryResponseDTO(False, None, resultDTO) # Terminée, on envoi la réponse définitive return GenerateQueryResponse(requestDTO, responseDTO), 200