|
1 | 1 | # spanking 🍑👋
|
| 2 | + |
| 3 | +To use the 🍑👋 `VectorDB` class, you can follow these steps: |
| 4 | + |
| 5 | +1. Create an instance of the 🍑👋 `VectorDB` class: |
| 6 | +```python |
| 7 | +from spanking import VectorDB |
| 8 | +vector_db = VectorDB(model_name='BAAI/bge-base-en-v1.5') |
| 9 | +``` |
| 10 | +You can optionally specify a different pre-trained sentence transformer model by passing its name to the constructor. |
| 11 | + |
| 12 | +2. Add texts to the database: |
| 13 | +```python |
| 14 | +texts = ["i eat pizza", "i play chess", "i drive bus"] |
| 15 | +vector_db.add_texts(texts) |
| 16 | +``` |
| 17 | +This will encode the texts into embeddings and store them in the database. |
| 18 | + |
| 19 | +3. Search for similar texts: |
| 20 | +```python |
| 21 | +query = "we play football" |
| 22 | +top_results = vector_db.search(query, top_k=3) |
| 23 | +print(top_results) |
| 24 | +``` |
| 25 | +This will retrieve the top-3 most similar texts to the query based on cosine similarity. The `search` method returns a list of tuples, where each tuple contains the text and its similarity score. |
| 26 | + |
| 27 | +4. Delete a text from the database: |
| 28 | +```python |
| 29 | +index = 1 |
| 30 | +vector_db.delete_text(index) |
| 31 | +``` |
| 32 | +This will remove the text and its corresponding embedding at the specified index. |
| 33 | + |
| 34 | +5. Update a text in the database: |
| 35 | +```python |
| 36 | +index = 0 |
| 37 | +new_text = "i enjoy eating pizza" |
| 38 | +vector_db.update_text(index, new_text) |
| 39 | +``` |
| 40 | +This will update the text and its corresponding embedding at the specified index with the new text. |
| 41 | + |
| 42 | +6. Iterate over the stored texts: |
| 43 | +```python |
| 44 | +for text in vector_db: |
| 45 | + print(text) |
| 46 | +``` |
| 47 | +This will iterate over all the texts stored in the database. |
| 48 | + |
| 49 | +7. Access individual texts by index: |
| 50 | +```python |
| 51 | +index = 2 |
| 52 | +text = vector_db[index] |
| 53 | +print(text) |
| 54 | +``` |
| 55 | +This will retrieve the text at the specified index. |
| 56 | + |
| 57 | +8. Get the number of texts in the database: |
| 58 | +```python |
| 59 | +num_texts = len(vector_db) |
| 60 | +print(num_texts) |
| 61 | +``` |
| 62 | +This will return the number of texts currently stored in the database. |
| 63 | + |
| 64 | +Here's an example usage of the 🍑👋 `VectorDB` class: |
| 65 | + |
| 66 | +```python |
| 67 | +from spanking import VectorDB |
| 68 | +vector_db = VectorDB() |
| 69 | + |
| 70 | +# Add texts to the database |
| 71 | +texts = ["i eat pizza", "i play chess", "i drive bus"] |
| 72 | +vector_db.add_texts(texts) |
| 73 | + |
| 74 | +# Search for similar texts |
| 75 | +query = "we play football" |
| 76 | +top_results = vector_db.search(query, top_k=2) |
| 77 | +print("Top results:") |
| 78 | +for text, similarity in top_results: |
| 79 | + print(f"Text: {text}, Similarity: {similarity}") |
| 80 | + |
| 81 | +# Update a text |
| 82 | +vector_db.update_text(1, "i enjoy playing chess") |
| 83 | + |
| 84 | +# Delete a text |
| 85 | +vector_db.delete_text(2) |
| 86 | + |
| 87 | +# Iterate over the stored texts |
| 88 | +print("\nStored texts:") |
| 89 | +for text in vector_db: |
| 90 | + print(text) |
| 91 | +``` |
| 92 | + |
| 93 | +This example demonstrates how to create a 🍑👋 `VectorDB` instance, add texts, search for similar texts, update and delete texts, and iterate over the stored texts. |
0 commit comments