|
| 1 | + |
| 2 | +""" Test for embedding vector creation and storage in a selected vector DB with selected embedding model. """ |
| 3 | + |
| 4 | + |
| 5 | +import os |
| 6 | +from llmware.library import Library |
| 7 | +from llmware.retrieval import Query |
| 8 | +from llmware.setup import Setup |
| 9 | +from llmware.status import Status |
| 10 | +from llmware.configs import LLMWareConfig |
| 11 | + |
| 12 | + |
| 13 | +def setup_library(library_name): |
| 14 | + |
| 15 | + """ Note: this setup_library method is provided to enable a self-contained example to create a test library """ |
| 16 | + |
| 17 | + # Step 1 - Create library which is the main 'organizing construct' in llmware |
| 18 | + print ("\nupdate: Creating library: {}".format(library_name)) |
| 19 | + |
| 20 | + library = Library().create_new_library(library_name) |
| 21 | + |
| 22 | + # check the embedding status 'before' installing the embedding |
| 23 | + embedding_record = library.get_embedding_status() |
| 24 | + print("embedding record - before embedding ", embedding_record) |
| 25 | + |
| 26 | + # Step 2 - Pull down the sample files from S3 through the .load_sample_files() command |
| 27 | + # --note: if you need to refresh the sample files, set 'over_write=True' |
| 28 | + print ("update: Downloading Sample Files") |
| 29 | + |
| 30 | + sample_files_path = Setup().load_sample_files(over_write=False) |
| 31 | + |
| 32 | + # Step 3 - point ".add_files" method to the folder of documents that was just created |
| 33 | + # this method parses the documents, text chunks, and captures in database |
| 34 | + |
| 35 | + print("update: Parsing and Text Indexing Files") |
| 36 | + |
| 37 | + library.add_files(input_folder_path=os.path.join(sample_files_path, "Agreements"), |
| 38 | + chunk_size=400, max_chunk_size=600, smart_chunking=1) |
| 39 | + |
| 40 | + return library |
| 41 | + |
| 42 | + |
| 43 | +def test_install_vector_embeddings(): |
| 44 | + |
| 45 | + LLMWareConfig().set_active_db("sqlite") |
| 46 | + |
| 47 | + library = setup_library("test_emb_install_09123") |
| 48 | + |
| 49 | + # select vector db that you would like to test |
| 50 | + vector_db = "chromadb" |
| 51 | + |
| 52 | + LLMWareConfig().set_vector_db(vector_db) |
| 53 | + |
| 54 | + # select embedding model |
| 55 | + embedding_model = "mini-lm-sbert" |
| 56 | + |
| 57 | + library_name = library.library_name |
| 58 | + |
| 59 | + print(f"\nupdate: Starting the Embedding: " |
| 60 | + f"library - {library_name} - " |
| 61 | + f"vector_db - {vector_db} - " |
| 62 | + f"model - {embedding_model}") |
| 63 | + |
| 64 | + # *** this is the one key line of code to create the embedding *** |
| 65 | + library.install_new_embedding(embedding_model_name=embedding_model, vector_db=vector_db,batch_size=100) |
| 66 | + |
| 67 | + # note: for using llmware as part of a larger application, you can check the real-time status by polling Status() |
| 68 | + # --both the EmbeddingHandler and Parsers write to Status() at intervals while processing |
| 69 | + update = Status().get_embedding_status(library_name, embedding_model) |
| 70 | + print("update: Embeddings Complete - Status() check at end of embedding - ", update) |
| 71 | + |
| 72 | + # Start using the new vector embeddings with Query |
| 73 | + sample_query = "incentive compensation" |
| 74 | + print("\n\nupdate: Run a sample semantic/vector query: {}".format(sample_query)) |
| 75 | + |
| 76 | + # queries are constructed by creating a Query object, and passing a library as input |
| 77 | + query_results = Query(library).semantic_query(sample_query, result_count=20) |
| 78 | + |
| 79 | + assert query_results is not None |
| 80 | + |
| 81 | + for i, entries in enumerate(query_results): |
| 82 | + |
| 83 | + # each query result is a dictionary with many useful keys |
| 84 | + |
| 85 | + text = entries["text"] |
| 86 | + document_source = entries["file_source"] |
| 87 | + page_num = entries["page_num"] |
| 88 | + vector_distance = entries["distance"] |
| 89 | + |
| 90 | + # to see all of the dictionary keys returned, uncomment the line below |
| 91 | + # print("update: query_results - all - ", i, entries) |
| 92 | + |
| 93 | + # for display purposes only, we will only show the first 125 characters of the text |
| 94 | + if len(text) > 125: text = text[0:125] + " ... " |
| 95 | + |
| 96 | + print("\nupdate: query results - {} - document - {} - page num - {} - distance - {} " |
| 97 | + .format( i, document_source, page_num, vector_distance)) |
| 98 | + |
| 99 | + print("update: text sample - ", text) |
| 100 | + |
| 101 | + # lets take a look at the library embedding status again at the end to confirm embeddings were created |
| 102 | + embedding_record = library.get_embedding_status() |
| 103 | + |
| 104 | + assert embedding_record is not None |
| 105 | + |
| 106 | + print("\nupdate: embedding record - ", embedding_record) |
| 107 | + |
| 108 | + return 0 |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | + |
0 commit comments