Spaces:
Sleeping
Sleeping
| from config import initialize | |
| from utilities.vectorstore.SummaryManager import SummaryManager | |
| def manage_collection(file_name, collection_name): | |
| if not qdrant_manager.get_collection(collection_name): | |
| if not qdrant_manager.create_collection(collection_name): | |
| print("β Error: Failed to create collection in Qdrant. Exiting application.", flush=True) | |
| return | |
| success, total_tokens, text = qdrant_manager.insert_document(file_name) | |
| if success: | |
| print(f"β Documento inserito correttamente. Token totali: {total_tokens}") | |
| if text: | |
| print(f"β Testo completo disponibile (entro il limite token): {text[:100]}...") | |
| else: | |
| print("β Errore durante l'inserimento del documento.") | |
| #qdrant_manager.delete_collection(collection_name) | |
| def get_initial_summary(collection_name): | |
| """Retrieve initial summary from a Qdrant collection.""" | |
| # Carica la collection se esiste | |
| if not qdrant_manager.get_collection(collection_name): | |
| print(f"β Collection '{collection_name}' non trovata.") | |
| return None | |
| # Inizializza il SummaryManager | |
| summary_manager = SummaryManager(language="en", qdrant_manager=qdrant_manager) | |
| # Genera il riassunto iniziale | |
| return summary_manager.do_initial_summary() | |
| def get_summary(collection_name, type="map_reduce"): | |
| """Retrieve initial summary from a Qdrant collection.""" | |
| # Carica la collection se esiste | |
| if not qdrant_manager.get_collection(collection_name): | |
| print(f"β Collection '{collection_name}' non trovata.") | |
| return None | |
| # Inizializza il SummaryManager | |
| summary_manager = SummaryManager(language="en", qdrant_manager=qdrant_manager) | |
| if type == "map_reduce": | |
| return summary_manager.do_summary_map_reduce() | |
| elif type == "stuff": | |
| print("Using stuff method") | |
| return summary_manager.do_summary_stuff() | |
| else: | |
| return None | |
| def chat_with_bot(llm_manager, contextualize=True): | |
| print("π€ Chatbot! Write 'exit' or 'quit' to close the conversation.\n") | |
| try: | |
| if contextualize: | |
| llm_manager.initialize_conversation() | |
| print(f"π€ ELI: {llm_manager.messages[-1].content}\n") | |
| except Exception as e: | |
| print(f"β οΈ Could not load initial summary: {e}\n") | |
| while True: | |
| try: | |
| user_input = input("π€ You: ") | |
| if user_input.lower() in ["exit", "quit"]: | |
| print("π End of conversation.") | |
| break | |
| response = llm_manager.send_message(user_input, contextualize=contextualize) | |
| print(f"π€ ELI: {response}\n") | |
| except KeyboardInterrupt: | |
| print("\nπ Conversation stopped.") | |
| break | |
| except Exception as e: | |
| print(f"β οΈ Error: {e}\n") | |
| def get_chunk(collection_name, chunk_id): | |
| """Retrieve a specific chunk from a Qdrant collection.""" | |
| # Carica la collection se esiste | |
| if not qdrant_manager.get_collection(collection_name): | |
| print(f"β Collection '{collection_name}' non trovata.") | |
| return None | |
| return qdrant_manager.get_chunk_by_index(chunk_id) | |
| llm_manager, qdrant_manager = initialize() | |
| # file_name="data/txt/Key statisitcs startups.txt" | |
| collection_name="key_statistics" | |
| llm_manager, qdrant_manager = initialize() | |
| # if qdrant_manager.get_collection(collection_name): | |
| # llm_manager.set_qdrant_manager(qdrant_manager) | |
| #chat_with_bot(llm_manager) | |
| #manage_collection(file_name, collection_name) | |
| #summary=get_initial_summary(collection_name) | |
| #summary=get_summary(collection_name,"map_reduce") | |
| #summary=get_summary(collection_name,type="stuff") | |
| # if summary: | |
| # print(f"β Summary:\n{summary}") | |
| # else: | |
| # print("β οΈ Nessun riassunto generato.") | |
| text=get_chunk(collection_name, 1) | |
| print(text) |