Skip to content

Commit 99bb061

Browse files
committed
docs: add example app
1 parent 4c987b6 commit 99bb061

File tree

1 file changed

+9
-215
lines changed

1 file changed

+9
-215
lines changed

README.md

Lines changed: 9 additions & 215 deletions
Original file line numberDiff line numberDiff line change
@@ -1,225 +1,24 @@
1-
# <p align="center">TinkerBird🐦</p>
1+
# <p align="center">TinkerBird</p>
22

3-
TinkerBird is a Chrome-native vector database designed for efficient storage and
4-
retrieval of high-dimensional vectors (embeddings). Its query engine, written in
3+
TinkerBird is a browser native vector database designed for efficient storage and
4+
retrieval of high-dimensional vectors (embeddings). It's query engine, written in
55
TypeScript, leverages HNSW (Hierarchical Navigable Small World) indexes for fast
66
vector retrieval. The storage layer utilizes IndexedDB, which could be extended
77
with an lru-cache.
88

9-
One significant challenge facing large language models (LLMs) is their tendency
10-
to generate syntactically correct but factually inaccurate responses, a
11-
phenomenon known as hallucination. To address this issue, vector databases
12-
provide LLMs with relevant context to structure it's response. This not only
13-
improves the accuracy of LLM responses but also minimizes computational costs
14-
incurred by irrelevant tokens. Additionally, vector databases ensure that LLMs
15-
are anchored to the required context.
16-
17-
TinkerBird disrupts traditional RAG workflows, which rely heavily on server-side
18-
interactions. By co-locating data and embeddings, it eliminates the roundtrip
19-
delays associated with client-server model. With Tinkerbird, sensitive data
20-
remains local, thus benefiting from vector search, without the associated
9+
By co-locating data and embeddings, Tikerbird eliminates the roundtrip and reduces
10+
reliance on server-side interactions for vector search workloads. With Tinkerbird,
11+
sensitive data remains local, thus benefiting from vector search, without the associated cost,
2112
compliance and security risks.
2213

2314
TinkerBird uses IndexedDB as it's storage layer, which in turn builds upon Blobs
2415
and LevelDB storage systems. By using Indexeddb, it benefits from IndexedDB's
2516
adoption, stability and familiarity as a native choice for offline first
2617
workflows.
2718

28-
## Documentation
29-
30-
Documentation covers the key methods and usage scenarios for the `HNSW` and
31-
`VectorStore` classes.
32-
33-
### `HNSW` Class
34-
35-
Encapsulates an in-memory implementation of HNSW index
36-
37-
```typescript
38-
// Initialize HNSW index
39-
const hnsw = new HNSW();
40-
41-
// Build HNSW index incrementally
42-
await hnsw.buildIndex(data);
43-
44-
// Save the serialized index to a file
45-
const serializedIndex = hnsw.serialize();
46-
47-
// Query Index with queryVector and kValue
48-
const results = hnsw.query(queryVector, kValue);
49-
```
50-
51-
#### Properties:
52-
53-
- `metric: SimilarityMetric`: The similarity metric used in the HNSW graph.
54-
- `similarityFunction: vectorReducer`: The similarity function for reducing
55-
vectors.
56-
- `d: number | null`: The vector dimension. It can be `null` if not specified.
57-
- `M: number`: The maximum neighbor count.
58-
- `efConstruction: number`: The effervescence coefficient used during
59-
construction.
60-
- `entryPointId: number`: The ID of the entry node in the graph.
61-
- `nodes: Map<number, Node>`: A mapping of node IDs to nodes.
62-
- `probs: number[]`: Probabilities for each level in the graph.
63-
- `levelMax: number`: The maximum level of the graph.
64-
65-
#### Usage:
66-
67-
```typescript
68-
// Example usage of the HNSW class
69-
const hnsw = new HNSW();
70-
```
71-
72-
### `SimilarityMetric` Enum
73-
74-
An enumeration of similarity metrics used in the context of the HNSW class.
75-
76-
#### Members:
77-
78-
- `cosine`: Represents the cosine similarity metric.
79-
- `euclidean`: Represents the Euclidean similarity metric.
80-
81-
#### Usage:
82-
83-
```typescript
84-
// Example usage of the SimilarityMetric enum
85-
const selectedMetric: SimilarityMetric = SimilarityMetric.cosine;
86-
```
87-
88-
### VectorStore API
89-
90-
This documentation provides an overview of the `VectorStore` class, its methods,
91-
and usage examples. Feel free to adjust the parameters and methods as per your
92-
needs.
93-
94-
The `VectorStore` class extends the HNSW class and provides functionality for
95-
managing a vector index, including building, loading, saving, deleting the
96-
index, and querying vectors. It supports caching to improve query performance.
97-
98-
### Class: VectorStore
99-
100-
#### Static Method: create
101-
102-
```typescript
103-
static async create(options: VectorStoreOptions): Promise<VectorStore>
104-
```
105-
106-
- Creates a new instance of the `VectorStore` class.
107-
- `options`: Object containing options for vector store initialization.
108-
- `collectionName`: A unique name for the vector collection.
109-
- `M`: Maximum neighbor count (default is 16).
110-
- `efConstruction`: Effervescence coefficient during construction (default
111-
is 200).
112-
113-
#### Static Method: recreate
114-
115-
```typescript
116-
static async recreate(collectionName: string): Promise<VectorStore>
117-
```
19+
## Examples
11820

119-
- Attempts to recreate index from collection or else create a new instance of
120-
the `VectorStore` class if collection doesn't exist. This has been reported
121-
to result in unintended results, prefer create() instead.
122-
- `options`: Object containing options for vector store initialization.
123-
- `collectionName`: A unique name for the vector collection.
124-
125-
#### Method: loadIndex
126-
127-
```typescript
128-
async loadIndex(): Promise<void>
129-
```
130-
131-
- Loads the vector index and meta data from the IndexedDB database. Used
132-
internally to recreate index
133-
134-
#### Method: saveIndex
135-
136-
```typescript
137-
async saveIndex(): Promise<void>
138-
```
139-
140-
- Persists the current state of the vector index and meta data onto the
141-
IndexedDB database.
142-
143-
#### Method: deleteIndex
144-
145-
```typescript
146-
async deleteIndex(): Promise<void>
147-
```
148-
149-
- Deletes the persisted index and metadata from the IndexedDB and populates it
150-
with an empty index and metadata
151-
152-
#### Method: query
153-
154-
```typescript
155-
query(target: number[], k: number = 3): vectorResult
156-
```
157-
158-
- Performs a vector search on the index.
159-
- `target`: The vector for which the search is performed.
160-
- `k`: The number of neighbors to retrieve (default is 3).
161-
- Returns: The result of the vector query.
162-
163-
#### Constants:
164-
165-
- `VectorStoreUnintialized`: Error indicating that the vector store is
166-
uninitialized.
167-
- `VectorStoreIndexMissing`: Error indicating that the vector store index is
168-
missing.
169-
- `VectorStoreIndexPurgeFailed`: Error indicating that the vector store index
170-
deletion failed.
171-
172-
### Example Usage:
173-
174-
```typescript
175-
// Create VectorStore instance
176-
const vectorStore = await VectorStore.create({
177-
collectionName: "my-collection",
178-
M: 16,
179-
efConstruction: 200
180-
});
181-
182-
// Load, Save, and Delete Index
183-
await vectorStore.loadIndex();
184-
await vectorStore.saveIndex();
185-
await vectorStore.deleteIndex();
186-
187-
// Perform Vector Query
188-
const queryVector = [
189-
/* ... */
190-
];
191-
const kValue = 5;
192-
const results = vectorStore.query(queryVector, kValue);
193-
```
194-
195-
## VectorStoreOptions
196-
197-
The `VectorStoreOptions` type defines the configuration options for initializing
198-
a VectorStore instance.
199-
200-
### Type: VectorStoreOptions
201-
202-
#### Properties:
203-
204-
- `collectionName: string`: A unique name for the vector collection.
205-
- `M?: number`: Maximum neighbor count (default is 16).
206-
- `efConstruction?: number`: Effervescence coefficient during construction
207-
(default is 200).
208-
- `cacheOptions?: CacheOptions | null`: Options for caching (optional).
209-
210-
### Example:
211-
212-
```typescript
213-
const vectorStoreOptions: VectorStoreOptions = {
214-
collectionName: "my-collection",
215-
M: 16,
216-
efConstruction: 200,
217-
cacheOptions: {
218-
max: 1000,
219-
maxAge: 60000 // 1 minute
220-
}
221-
};
222-
```
21+
Here's a sample app built using TinkerBird. Check out [Tinkerboard](https://tinkerboard.vercel.app/) and [Source](https://github.com/wizenheimer/tinkerboard).
22322

22423
## Contributing
22524

@@ -233,13 +32,8 @@ make are **greatly appreciated**.
23332
Distributed under the MIT License. See [LICENSE](LICENSE) for more information.
23433
TinkerBird is provided "as is" and comes with absolutely no guarantees. We take
23534
no responsibility for irrelevant searches, confused users, or existential crises
236-
induced by unpredictable results. If it breaks, well, that's your problem now
237-
💀.
238-
239-
## Credits
35+
induced by unpredictable results. If it breaks, well, that's your problem now! jk.
24036

241-
TinkerBird was brought to you by an indie developer who should probably be
242-
working on something more productive. But hey I am.
24337

24438
## References
24539

0 commit comments

Comments
 (0)