llmware

Форк
0
/
using_mongo_atlas.py 
66 строк · 2.4 Кб
1

2
# This example demonstrates working with MongoDB Atlas
3

4
"""
5
You can use Mongo Atlas in 2 ways:
6

7
1. Only as a vector DB (and use local or another mongo for data storage)
8
   In this case, you can set the following environment variable:
9
      MONGO_ATLAS_CONNECTION_URI=mongo+srv://<username>:<password>@<cluster-domain>...
10

11
2. For both vector and data storage
12
   In this case, you can set the following environment configuration:
13
      LLMWareConfig().set_config("collection_db_uri", "mongo+srv://<username>:<password>@<cluster-domain>..."
14

15
This example demonstrates the 2nd approach
16
"""
17

18
import os
19
from llmware.configs import LLMWareConfig
20
from llmware.library import Library
21
from llmware.retrieval import Query
22
from llmware.setup import Setup
23

24

25
# Use MongoDB Atlas for both data storage and vector embedding storage/search
26

27

28
def using_mongo_atlas(mongo_atlas_connection_string):
29

30
    LLMWareConfig().set_config("collection_db_uri", mongo_atlas_connection_string)
31

32
    # Create a library and populate it with some sample documents
33

34
    library_name = "test_mongo_atlas"
35
    print(f"\n > Creating library {library_name}...")
36

37
    library = Library().create_new_library(library_name)
38
    print(f"\n > Loading the llmware Sample Files...")
39

40
    sample_files_path = Setup().load_sample_files()
41
    print(f"\n > Adding some files to the library...")
42

43
    library.add_files(input_folder_path=os.path.join(sample_files_path, "Agreements"))
44

45
    # Create vector embeddings using Mongo Atlas
46
    print(f"\n > Generating embedding vectors (using the 'mini-lm-sbert' model) and storing them (in 'Mongo Atlas')...")
47
    library.install_new_embedding(embedding_model_name="mini-lm-sbert", vector_db="mongo_atlas")
48

49
    # Do a semantic search in the library
50
    print(f"\n > Running a query for 'Salary'...")
51
    query_results = Query(library).semantic_query(query="salary", result_count=10, results_only=True)
52

53
    print(f"\n\nResults for 'Salary' in {library_name}:\n")
54
    for query_result in query_results:
55
        print(
56
            "File: " + query_result["file_source"] + " (Page " + str(query_result["page_num"]) + "):\n" + query_result[
57
                "text"] + "\n")
58

59
    return query_results
60

61

62
if __name__ == "__main__":
63

64
    # Set this env var appropriately or just paste in your Mongo Atlas connection string:
65
    mongo_config = os.environ["MONGO_ATLAS_CONNECTION_URI"]
66

67
    output = using_mongo_atlas(mongo_config)
68

69

70

71

72

73

74

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.