-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Vectorize GA binding changes
- Loading branch information
Showing
7 changed files
with
226 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,187 @@ | ||
// Copyright (c) 2023 Cloudflare, Inc. | ||
// Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
// https://opensource.org/licenses/Apache-2.0 | ||
interface Fetcher { | ||
fetch: typeof fetch; | ||
} | ||
|
||
enum Operation { | ||
INDEX_GET = 0, | ||
VECTOR_QUERY = 1, | ||
VECTOR_INSERT = 2, | ||
VECTOR_UPSERT = 3, | ||
VECTOR_GET = 4, | ||
VECTOR_DELETE = 5, | ||
} | ||
|
||
class VectorizeImpl implements Vectorize { | ||
public constructor(private readonly fetcher: Fetcher) {} | ||
|
||
public async describe(): Promise<VectorizeIndexInfo> { | ||
const res = await this._send(Operation.INDEX_GET, `/info`, { | ||
method: "GET", | ||
}); | ||
|
||
return await toJson<VectorizeIndexInfo>(res); | ||
} | ||
|
||
public async query( | ||
vector: VectorFloatArray | number[], | ||
options: VectorizeQueryOptions<VectorizeMetadataRetrievalLevel> | ||
): Promise<VectorizeMatches> { | ||
const res = await this._send(Operation.VECTOR_QUERY, `/query`, { | ||
method: "POST", | ||
body: JSON.stringify({ | ||
...options, | ||
vector: Array.isArray(vector) ? vector : Array.from(vector), | ||
}), | ||
headers: { | ||
"content-type": "application/json", | ||
accept: "application/json", | ||
}, | ||
}); | ||
|
||
return await toJson<VectorizeMatches>(res); | ||
} | ||
|
||
public async insert( | ||
vectors: VectorizeVector[] | ||
): Promise<VectorizeAsyncMutation> { | ||
const res = await this._send(Operation.VECTOR_INSERT, `/insert`, { | ||
method: "POST", | ||
body: JSON.stringify({ | ||
vectors: vectors.map((vec) => ({ | ||
...vec, | ||
values: Array.isArray(vec.values) | ||
? vec.values | ||
: Array.from(vec.values), | ||
})), | ||
}), | ||
headers: { | ||
"content-type": "application/json", | ||
"cf-vector-search-dim-width": String( | ||
vectors.length ? vectors[0]?.values?.length : 0 | ||
), | ||
"cf-vector-search-dim-height": String(vectors.length), | ||
accept: "application/json", | ||
}, | ||
}); | ||
|
||
return await toJson<VectorizeAsyncMutation>(res); | ||
} | ||
|
||
public async upsert( | ||
vectors: VectorizeVector[] | ||
): Promise<VectorizeAsyncMutation> { | ||
const res = await this._send(Operation.VECTOR_UPSERT, `/upsert`, { | ||
method: "POST", | ||
body: JSON.stringify({ | ||
vectors: vectors.map((vec) => ({ | ||
...vec, | ||
values: Array.isArray(vec.values) | ||
? vec.values | ||
: Array.from(vec.values), | ||
})), | ||
}), | ||
headers: { | ||
"content-type": "application/json", | ||
"cf-vector-search-dim-width": String( | ||
vectors.length ? vectors[0]?.values?.length : 0 | ||
), | ||
"cf-vector-search-dim-height": String(vectors.length), | ||
accept: "application/json", | ||
}, | ||
}); | ||
|
||
return await toJson<VectorizeAsyncMutation>(res); | ||
} | ||
|
||
public async getByIds(ids: string[]): Promise<VectorizeVector[]> { | ||
const res = await this._send(Operation.VECTOR_GET, `/getByIds`, { | ||
method: "POST", | ||
body: JSON.stringify({ ids }), | ||
headers: { | ||
"content-type": "application/json", | ||
accept: "application/json", | ||
}, | ||
}); | ||
|
||
return await toJson<VectorizeVector[]>(res); | ||
} | ||
|
||
public async deleteByIds(ids: string[]): Promise<VectorizeAsyncMutation> { | ||
const res = await this._send(Operation.VECTOR_DELETE, `/deleteByIds`, { | ||
method: "POST", | ||
body: JSON.stringify({ ids }), | ||
headers: { | ||
"content-type": "application/json", | ||
accept: "application/json", | ||
}, | ||
}); | ||
|
||
return await toJson<VectorizeAsyncMutation>(res); | ||
} | ||
|
||
private async _send( | ||
operation: Operation, | ||
endpoint: string, | ||
init: RequestInit | ||
): Promise<Response> { | ||
const res = await this.fetcher.fetch( | ||
`http://vector-search/${endpoint}`, // `http://vector-search` is just a dummy host, the attached fetcher will receive the request | ||
init | ||
); | ||
if (res.status !== 200) { | ||
let err: Error | null = null; | ||
|
||
try { | ||
const errResponse = (await res.json()) as VectorizeError; | ||
err = new Error( | ||
`${Operation[operation]}_ERROR${ | ||
typeof errResponse.code === "number" | ||
? ` (code = ${errResponse.code})` | ||
: "" | ||
}: ${errResponse.error}`, | ||
{ | ||
cause: new Error(errResponse.error), | ||
} | ||
); | ||
} catch {} | ||
|
||
if (err) { | ||
throw err; | ||
} else { | ||
throw new Error( | ||
`${Operation[operation]}_ERROR: Status + ${res.status}`, | ||
{ | ||
cause: new Error(`Status ${res.status}`), | ||
} | ||
); | ||
} | ||
} | ||
|
||
return res; | ||
} | ||
} | ||
|
||
const maxBodyLogChars = 1_000; | ||
async function toJson<T = unknown>(response: Response): Promise<T> { | ||
const body = await response.text(); | ||
try { | ||
return JSON.parse(body) as T; | ||
} catch { | ||
throw new Error( | ||
`Failed to parse body as JSON, got: ${ | ||
body.length > maxBodyLogChars | ||
? `${body.slice(0, maxBodyLogChars)}…` | ||
: body | ||
}` | ||
); | ||
} | ||
} | ||
|
||
export function makeBinding(env: { fetcher: Fetcher }): Vectorize { | ||
return new VectorizeImpl(env.fetcher); | ||
} | ||
|
||
export default makeBinding; |
This file contains 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
This file contains 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