|
1 | | -# Database Connectors |
| 1 | +# Database Configuration |
2 | 2 |
|
3 | | -A database connector is a compatibility layer that converts data structures that a |
4 | | -database natively works with to the ones that VectorCode works with. The connector |
5 | | -classes provide abstractions for VectorCode operations (`vectorise`, `query`, etc.), |
6 | | -which enables the use of different database backends. |
| 3 | +This document provides the `db_params` configuration for the supported database connectors in VectorCode. |
7 | 4 |
|
8 | | -<!-- mtoc-start --> |
9 | | - |
10 | | -* [Adding a New Database Connector](#adding-a-new-database-connector) |
11 | | -* [Key Implementation Details](#key-implementation-details) |
12 | | - * [The `Config` Object](#the-config-object) |
13 | | - * [Implementing Abstract Methods](#implementing-abstract-methods) |
14 | | - * [Error Handling](#error-handling) |
15 | | -* [Testing](#testing) |
16 | | - |
17 | | -<!-- mtoc-end --> |
18 | | - |
19 | | -# Adding a New Database Connector |
20 | | - |
21 | | -To add support for a new database backend, you will need to: |
22 | | - |
23 | | -1. **Implement a connector class**: Create a new file in this directory and implement a child class of `vectorcode.database.base.DatabaseConnectorBase`. You must implement all of its abstract methods. |
24 | | -2. **Write tests**: Add tests for your new connector in the `tests/database/` directory. The tests should mock the database's API and verify that your connector correctly converts data between the database's native format and VectorCode's data structures. |
25 | | -3. **Register your connector**: Add a new entry in the `get_database_connector` function in `src/vectorcode/database/__init__.py` to initialize your new connector. |
26 | | - |
27 | | -For a concrete example, refer to the implementation of `DatabaseConnectorBase` and the `ChromaDB0Connector`. |
28 | | - |
29 | | -# Key Implementation Details |
30 | | - |
31 | | -## The `Config` Object |
32 | | - |
33 | | -All settings for a connector are passed through a single `vectorcode.cli_utils.Config` object, which is available as `self._configs`. This includes: |
| 5 | +For instructions on how to add a new database connector, please refer to [DEVELOPERS.md](./DEVELOPERS.md). |
34 | 6 |
|
35 | | -- **Database Settings**: The `db_type` string and `db_params` dictionary are used to configure the connection to the database backend. As a contributor, you should document the specific `db_params` your connector requires in the class's docstring. |
36 | | -- **Operation Parameters**: Parameters for operations like `query` or `vectorise` are also present in this object. |
37 | 7 |
|
38 | | -The `self._configs` attribute is mutable and can be updated for subsequent operations, but the database connection settings (`db_type`, `db_params`) should not be changed after initialization. |
39 | | - |
40 | | -## Implementing Abstract Methods |
41 | | - |
42 | | -When implementing the abstract methods from `DatabaseConnectorBase`, you should: |
43 | | - |
44 | | -- Read the necessary parameters from the `self._configs` object. |
45 | | -- Perform the corresponding operation against the database. |
46 | | -- Return data in the format specified by the method's type hints (e.g., `QueryResult`, `CollectionInfo`). |
47 | | - |
48 | | -**Please refer to the docstrings in `DatabaseConnectorBase` for the specific API contract of each method.** |
49 | | -They contain detailed information about what each method is expected to do and what parameters it uses from the `Config` object. |
50 | | - |
51 | | -There are also some helper methods (non-abstract methods) in `DatabaseConnectorBase` that may be |
52 | | -helpful. |
53 | | -For example, `self.get_embedding(texts)` provides a convenient way to get the embeddings of some strings that takes all config parameters into account (embedding function, embedding dimension, etc.). |
54 | | -Using these methods helps keeping your implementation consistent with the overall design. |
55 | | - |
56 | | -## Error Handling |
57 | | - |
58 | | -If the underlying database library raises a specific exception (e.g., for a collection not being found), you should consider catching it and re-raise it as one of VectorCode's custom database exceptions from `vectorcode.database.errors`. This ensures consistent error handling in the CLI and other clients. |
59 | | - |
60 | | -For example: |
61 | | -```python |
62 | | -from vectorcode.database.errors import CollectionNotFoundError |
63 | | - |
64 | | -try: |
65 | | - some_action_here() |
66 | | -except SomeCustomException as e: |
67 | | - raise CollectionNotFoundError("The collection was not found.") from e |
68 | | -``` |
| 8 | +<!-- mtoc-start --> |
69 | 9 |
|
70 | | -# Testing |
| 10 | +* [ChromaDB (v0.6.3)](#chromadb-v063) |
71 | 11 |
|
72 | | -The unit tests for database backends should go under [`tests/database/`](../../../tests/database/). |
73 | | -The tests should mock the request body and return values of the database. Integration |
74 | | -tests that interact with an actual database are out of scope for now. |
| 12 | +<!-- mtoc-end --> |
75 | 13 |
|
76 | | -> The tests for the subcommands currently use mocked database connectors. They're not |
77 | | -> supposed to interact with live databases. |
| 14 | +## ChromaDB (v0.6.3) |
| 15 | + |
| 16 | +The `ChromaDB0Connector` is used for ChromaDB versions 0.6.3. |
| 17 | + |
| 18 | +- **`db_type`**: `"ChromaDB0"`. The `Connector` suffix is optional and will be added automatically. |
| 19 | + |
| 20 | +- **`db_params`**: |
| 21 | + An example of the `db_params` for `ChromaDB0Connector` in your `config.json5`: |
| 22 | + ```json5 |
| 23 | + { |
| 24 | + "db_params": { |
| 25 | + "db_url": "http://127.0.0.1:8000", |
| 26 | + "db_path": "~/.local/share/vectorcode/chromadb/", |
| 27 | + "db_log_path": "~/.local/share/vectorcode/", |
| 28 | + "db_settings": {}, |
| 29 | + "hnsw": { |
| 30 | + "hnsw:M": 64, |
| 31 | + }, |
| 32 | + }, |
| 33 | + } |
| 34 | + ``` |
| 35 | + |
| 36 | + - `db_url`: The URL of the ChromaDB server. Defaults to `"http://127.0.0.1:8000"`. |
| 37 | + - `db_path`: Path to the directory where ChromaDB stores its data. Defaults to `"~/.local/share/vectorcode/chromadb/"`. |
| 38 | + - `db_log_path`: Path to the directory for ChromaDB log files. Defaults to `"~/.local/share/vectorcode/"`. |
| 39 | + - `db_settings`: Additional ChromaDB settings. You usually don't need to touch this, but in case you do, you can refer to [ChromaDB source](https://github.com/chroma-core/chroma/blob/a3b86a0302a385350a8f092a5f89a2dcdebcf6be/chromadb/config.py#L101) for details. Defaults to `{}`. |
| 40 | + - `hnsw`: HNSW index parameters. Defaults to `{"hnsw:M": 64}`. |
0 commit comments