-
Notifications
You must be signed in to change notification settings - Fork 147
Add Qdrant vector database REST wrapper #487
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jynbil1
wants to merge
2
commits into
arakoodev:ts
Choose a base branch
from
jynbil1:codex/qdrant-vector-db-273
base: ts
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+502
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| export { Supabase } from "./lib/supabase/supabase.js"; | ||
| export { Qdrant, QdrantDistanceMetric } from "./lib/qdrant/qdrant.js"; |
352 changes: 352 additions & 0 deletions
352
JS/edgechains/arakoodev/src/vector-db/src/lib/qdrant/qdrant.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,352 @@ | ||
| import axios, { AxiosInstance, AxiosRequestConfig } from "axios"; | ||
| import retry from "retry"; | ||
| import { config } from "dotenv"; | ||
| config(); | ||
|
|
||
| type QdrantPointId = number | string; | ||
| type QdrantVector = number[] | Record<string, number[]>; | ||
| type QdrantSearchVector = QdrantVector | { name: string; vector: number[] }; | ||
| type QdrantPayload = Record<string, any>; | ||
|
|
||
| interface QdrantPoint { | ||
| id: QdrantPointId; | ||
| vector: QdrantVector; | ||
| payload?: QdrantPayload; | ||
| } | ||
|
|
||
| interface QdrantRequestOptions { | ||
| wait?: boolean; | ||
| ordering?: "weak" | "medium" | "strong"; | ||
| } | ||
|
|
||
| interface CreateCollectionArgs { | ||
| client: AxiosInstance; | ||
| collectionName: string; | ||
| vectorSize?: number; | ||
| distance?: QdrantDistanceMetric; | ||
| vectorsConfig?: Record<string, any>; | ||
| [key: string]: any; | ||
| } | ||
|
|
||
| interface UpsertPointsArgs extends QdrantRequestOptions { | ||
| client: AxiosInstance; | ||
| collectionName: string; | ||
| points: QdrantPoint[]; | ||
| } | ||
|
|
||
| interface InsertVectorDataArgs extends QdrantRequestOptions { | ||
| client: AxiosInstance; | ||
| collectionName: string; | ||
| points?: QdrantPoint[]; | ||
| id?: QdrantPointId; | ||
| vector?: QdrantVector; | ||
| payload?: QdrantPayload; | ||
| } | ||
|
|
||
| interface SearchPointsArgs { | ||
| client: AxiosInstance; | ||
| collectionName: string; | ||
| vector: QdrantSearchVector; | ||
| limit?: number; | ||
| offset?: QdrantPointId; | ||
| filter?: Record<string, any>; | ||
| params?: Record<string, any>; | ||
| withPayload?: boolean | string[] | Record<string, any>; | ||
| withVector?: boolean | string[] | Record<string, any>; | ||
| scoreThreshold?: number; | ||
| } | ||
|
|
||
| interface ScrollPointsArgs { | ||
| client: AxiosInstance; | ||
| collectionName: string; | ||
| limit?: number; | ||
| offset?: QdrantPointId; | ||
| filter?: Record<string, any>; | ||
| withPayload?: boolean | string[] | Record<string, any>; | ||
| withVector?: boolean | string[] | Record<string, any>; | ||
| } | ||
|
|
||
| interface GetPointByIdArgs { | ||
| client: AxiosInstance; | ||
| collectionName: string; | ||
| id: QdrantPointId; | ||
| withPayload?: boolean | string[]; | ||
| withVector?: boolean | string[]; | ||
| } | ||
|
|
||
| interface DeletePointsArgs extends QdrantRequestOptions { | ||
| client: AxiosInstance; | ||
| collectionName: string; | ||
| points?: QdrantPointId[]; | ||
| filter?: Record<string, any>; | ||
| } | ||
|
|
||
| export class Qdrant { | ||
| QDRANT_URL: string; | ||
| QDRANT_API_KEY?: string; | ||
|
|
||
| constructor(QDRANT_URL?: string, QDRANT_API_KEY?: string) { | ||
| this.QDRANT_URL = | ||
| QDRANT_URL || process.env.QDRANT_URL || "http://localhost:6333"; | ||
| this.QDRANT_API_KEY = QDRANT_API_KEY || process.env.QDRANT_API_KEY; | ||
| } | ||
|
|
||
| createClient() { | ||
| const headers: Record<string, string> = { | ||
| "Content-Type": "application/json", | ||
| }; | ||
|
|
||
| if (this.QDRANT_API_KEY) { | ||
| headers["api-key"] = this.QDRANT_API_KEY; | ||
| } | ||
|
|
||
| return axios.create({ | ||
| baseURL: this.QDRANT_URL.replace(/\/+$/, ""), | ||
| headers, | ||
| }); | ||
| } | ||
|
|
||
| async createCollection({ | ||
| client, | ||
| collectionName, | ||
| vectorSize, | ||
| distance = QdrantDistanceMetric.COSINE, | ||
| vectorsConfig, | ||
| ...args | ||
| }: CreateCollectionArgs): Promise<any> { | ||
| if (!vectorsConfig && !vectorSize) { | ||
| throw new Error( | ||
| "Either vectorSize or vectorsConfig is required to create a collection", | ||
| ); | ||
| } | ||
|
|
||
| const body = { | ||
| vectors: vectorsConfig || { | ||
| size: vectorSize, | ||
| distance, | ||
| }, | ||
| ...args, | ||
| }; | ||
|
|
||
| return this.requestWithRetry(async () => { | ||
| const response = await client.put( | ||
| this.collectionPath(collectionName), | ||
| body, | ||
| ); | ||
| return response.data; | ||
| }); | ||
| } | ||
|
|
||
| async upsertPoints({ | ||
| client, | ||
| collectionName, | ||
| points, | ||
| wait, | ||
| ordering, | ||
| }: UpsertPointsArgs): Promise<any> { | ||
| return this.requestWithRetry(async () => { | ||
| const response = await client.put( | ||
| `${this.collectionPath(collectionName)}/points`, | ||
| { points }, | ||
| { params: this.requestParams({ wait, ordering }) }, | ||
| ); | ||
| return response.data; | ||
| }); | ||
| } | ||
|
|
||
| async insertVectorData({ | ||
| points, | ||
| id, | ||
| vector, | ||
| payload, | ||
| ...args | ||
| }: InsertVectorDataArgs): Promise<any> { | ||
| const qdrantPoints = | ||
| points || | ||
| (id !== undefined && vector ? [{ id, vector, payload }] : undefined); | ||
|
|
||
| if (!qdrantPoints) { | ||
| throw new Error( | ||
| "Either points or both id and vector are required to insert Qdrant data", | ||
| ); | ||
| } | ||
|
|
||
| return this.upsertPoints({ | ||
| ...args, | ||
| points: qdrantPoints, | ||
| }); | ||
| } | ||
|
|
||
| async searchPoints({ | ||
| client, | ||
| collectionName, | ||
| vector, | ||
| limit = 10, | ||
| offset, | ||
| filter, | ||
| params, | ||
| withPayload = true, | ||
| withVector = false, | ||
| scoreThreshold, | ||
| }: SearchPointsArgs): Promise<any> { | ||
| const body = this.cleanBody({ | ||
| vector, | ||
| limit, | ||
| offset, | ||
| filter, | ||
| params, | ||
| with_payload: withPayload, | ||
| with_vector: withVector, | ||
| score_threshold: scoreThreshold, | ||
| }); | ||
|
|
||
| return this.requestWithRetry(async () => { | ||
| const response = await client.post( | ||
| `${this.collectionPath(collectionName)}/points/search`, | ||
| body, | ||
| ); | ||
| return response.data; | ||
| }); | ||
| } | ||
|
|
||
| async getData({ | ||
| client, | ||
| collectionName, | ||
| limit = 10, | ||
| offset, | ||
| filter, | ||
| withPayload = true, | ||
| withVector = false, | ||
| }: ScrollPointsArgs): Promise<any> { | ||
| const body = this.cleanBody({ | ||
| limit, | ||
| offset, | ||
| filter, | ||
| with_payload: withPayload, | ||
| with_vector: withVector, | ||
| }); | ||
|
|
||
| return this.requestWithRetry(async () => { | ||
| const response = await client.post( | ||
| `${this.collectionPath(collectionName)}/points/scroll`, | ||
| body, | ||
| ); | ||
| return response.data; | ||
| }); | ||
| } | ||
|
|
||
| async getDataById({ | ||
| client, | ||
| collectionName, | ||
| id, | ||
| withPayload = true, | ||
| withVector = false, | ||
| }: GetPointByIdArgs): Promise<any> { | ||
| return this.requestWithRetry(async () => { | ||
| const response = await client.get( | ||
| `${this.collectionPath(collectionName)}/points/${encodeURIComponent(id)}`, | ||
| { | ||
| params: { | ||
| with_payload: withPayload, | ||
| with_vector: withVector, | ||
| }, | ||
| }, | ||
| ); | ||
| return response.data; | ||
| }); | ||
| } | ||
|
|
||
| async deleteById({ | ||
| client, | ||
| collectionName, | ||
| id, | ||
| wait, | ||
| ordering, | ||
| }: { | ||
| client: AxiosInstance; | ||
| collectionName: string; | ||
| id: QdrantPointId; | ||
| } & QdrantRequestOptions): Promise<any> { | ||
| return this.deletePoints({ | ||
| client, | ||
| collectionName, | ||
| points: [id], | ||
| wait, | ||
| ordering, | ||
| }); | ||
| } | ||
|
|
||
| async deletePoints({ | ||
| client, | ||
| collectionName, | ||
| points, | ||
| filter, | ||
| wait, | ||
| ordering, | ||
| }: DeletePointsArgs): Promise<any> { | ||
| if (!points && !filter) { | ||
| throw new Error( | ||
| "Either points or filter is required to delete Qdrant points", | ||
| ); | ||
| } | ||
|
|
||
| return this.requestWithRetry(async () => { | ||
| const response = await client.post( | ||
| `${this.collectionPath(collectionName)}/points/delete`, | ||
| points ? { points } : { filter }, | ||
| { params: this.requestParams({ wait, ordering }) }, | ||
| ); | ||
| return response.data; | ||
| }); | ||
| } | ||
|
|
||
| private collectionPath(collectionName: string) { | ||
| return `/collections/${encodeURIComponent(collectionName)}`; | ||
| } | ||
|
|
||
| private requestParams(options: QdrantRequestOptions) { | ||
| return this.cleanBody(options); | ||
| } | ||
|
|
||
| private cleanBody<T extends Record<string, any>>(body: T) { | ||
| return Object.fromEntries( | ||
| Object.entries(body).filter(([, value]) => value !== undefined), | ||
| ); | ||
| } | ||
|
|
||
| private async requestWithRetry<T>(request: () => Promise<T>): Promise<T> { | ||
| return new Promise((resolve, reject) => { | ||
| const operation = retry.operation({ | ||
| retries: 5, | ||
| factor: 3, | ||
| minTimeout: 1 * 1000, | ||
| maxTimeout: 60 * 1000, | ||
| randomize: true, | ||
| }); | ||
|
|
||
| operation.attempt(async () => { | ||
| try { | ||
| resolve(await request()); | ||
| } catch (error: any) { | ||
| const retryError = this.toError(error); | ||
| if (operation.retry(retryError)) return; | ||
| reject(operation.mainError() || retryError); | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| private toError(error: any) { | ||
| if (error instanceof Error) return error; | ||
|
|
||
| const message = typeof error === "string" ? error : JSON.stringify(error); | ||
| return new Error(message); | ||
| } | ||
| } | ||
|
|
||
| export enum QdrantDistanceMetric { | ||
| COSINE = "Cosine", | ||
| DOT = "Dot", | ||
| EUCLID = "Euclid", | ||
| MANHATTAN = "Manhattan", | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.