-
Notifications
You must be signed in to change notification settings - Fork 303
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
Implement Vectorize GA binding changes #2443
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,144 +16,166 @@ enum Operation { | |
VECTOR_DELETE = 5, | ||
} | ||
|
||
class VectorizeIndexImpl implements VectorizeIndex { | ||
type VectorizeVersion = "v1" | "v2"; | ||
|
||
/* | ||
* The Vectorize beta VectorizeIndex shares the same methods, so to keep things simple, they share one implementation. | ||
* The types here are specific to Vectorize GA, but the types here don't actually matter as they are stripped away | ||
* and not visible to end users. | ||
*/ | ||
class VectorizeIndexImpl implements Vectorize { | ||
public constructor( | ||
private readonly fetcher: Fetcher, | ||
private readonly indexId: string | ||
private readonly indexId: string, | ||
private readonly indexVersion: VectorizeVersion | ||
) {} | ||
|
||
public async describe(): Promise<VectorizeIndexDetails> { | ||
const res = await this._send( | ||
Operation.INDEX_GET, | ||
`indexes/${this.indexId}`, | ||
{ | ||
method: "GET", | ||
} | ||
); | ||
public async describe(): Promise<VectorizeIndexInfo> { | ||
const endpoint = | ||
this.indexVersion === "v2" ? `info` : `binding/indexes/${this.indexId}`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For v2, account info and index id are included in the base url of the fetcher so these endpoints just become the final path part. |
||
const res = await this._send(Operation.INDEX_GET, endpoint, { | ||
method: "GET", | ||
}); | ||
|
||
return await toJson<VectorizeIndexDetails>(res); | ||
return await toJson<VectorizeIndexInfo>(res); | ||
} | ||
|
||
public async query( | ||
vector: VectorFloatArray | number[], | ||
options: VectorizeQueryOptions | ||
options: VectorizeQueryOptions<VectorizeMetadataRetrievalLevel> | ||
): Promise<VectorizeMatches> { | ||
const compat = { | ||
queryMetadataOptional: flags.vectorizeQueryMetadataOptional, | ||
}; | ||
const res = await this._send( | ||
Operation.VECTOR_QUERY, | ||
`indexes/${this.indexId}/query`, | ||
{ | ||
if (this.indexVersion === "v2") { | ||
const res = await this._send(Operation.VECTOR_QUERY, `query`, { | ||
method: "POST", | ||
body: JSON.stringify({ | ||
...options, | ||
vector: Array.isArray(vector) ? vector : Array.from(vector), | ||
compat, | ||
}), | ||
headers: { | ||
"content-type": "application/json", | ||
accept: "application/json", | ||
"cf-vector-search-query-compat": JSON.stringify(compat), | ||
}, | ||
} | ||
); | ||
|
||
return await toJson<VectorizeMatches>(res); | ||
}); | ||
|
||
return await toJson<VectorizeMatches>(res); | ||
} else { | ||
const compat = { | ||
queryMetadataOptional: flags.vectorizeQueryMetadataOptional, | ||
}; | ||
const res = await this._send( | ||
Operation.VECTOR_QUERY, | ||
`binding/indexes/${this.indexId}/query`, | ||
{ | ||
method: "POST", | ||
body: JSON.stringify({ | ||
...options, | ||
vector: Array.isArray(vector) ? vector : Array.from(vector), | ||
compat, | ||
}), | ||
headers: { | ||
"content-type": "application/json", | ||
accept: "application/json", | ||
"cf-vector-search-query-compat": JSON.stringify(compat), | ||
}, | ||
} | ||
); | ||
|
||
return await toJson<VectorizeMatches>(res); | ||
} | ||
} | ||
|
||
public async insert( | ||
vectors: VectorizeVector[] | ||
): Promise<VectorizeVectorMutation> { | ||
const res = await this._send( | ||
Operation.VECTOR_INSERT, | ||
`indexes/${this.indexId}/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<VectorizeVectorMutation>(res); | ||
): Promise<VectorizeAsyncMutation> { | ||
const endpoint = | ||
this.indexVersion === "v2" | ||
? `insert` | ||
: `binding/indexes/${this.indexId}/insert`; | ||
const res = await this._send(Operation.VECTOR_INSERT, endpoint, { | ||
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<VectorizeVectorMutation> { | ||
const res = await this._send( | ||
Operation.VECTOR_UPSERT, | ||
`indexes/${this.indexId}/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<VectorizeVectorMutation>(res); | ||
): Promise<VectorizeAsyncMutation> { | ||
const endpoint = | ||
this.indexVersion === "v2" | ||
? `upsert` | ||
: `binding/indexes/${this.indexId}/upsert`; | ||
const res = await this._send(Operation.VECTOR_UPSERT, endpoint, { | ||
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, | ||
`indexes/${this.indexId}/getByIds`, | ||
{ | ||
method: "POST", | ||
body: JSON.stringify({ ids }), | ||
headers: { | ||
"content-type": "application/json", | ||
accept: "application/json", | ||
}, | ||
} | ||
); | ||
const endpoint = | ||
this.indexVersion === "v2" | ||
? `getByIds` | ||
: `binding/indexes/${this.indexId}/getByIds`; | ||
const res = await this._send(Operation.VECTOR_GET, endpoint, { | ||
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<VectorizeVectorMutation> { | ||
const res = await this._send( | ||
Operation.VECTOR_DELETE, | ||
`indexes/${this.indexId}/deleteByIds`, | ||
{ | ||
method: "POST", | ||
body: JSON.stringify({ ids }), | ||
headers: { | ||
"content-type": "application/json", | ||
accept: "application/json", | ||
}, | ||
} | ||
); | ||
|
||
return await toJson<VectorizeVectorMutation>(res); | ||
public async deleteByIds(ids: string[]): Promise<VectorizeAsyncMutation> { | ||
const endpoint = | ||
this.indexVersion === "v2" | ||
? `deleteByIds` | ||
: `binding/indexes/${this.indexId}/deleteByIds`; | ||
const res = await this._send(Operation.VECTOR_DELETE, endpoint, { | ||
method: "POST", | ||
body: JSON.stringify({ ids }), | ||
headers: { | ||
"content-type": "application/json", | ||
accept: "application/json", | ||
}, | ||
}); | ||
|
||
return await toJson<VectorizeAsyncMutation>(res); | ||
} | ||
|
||
private async _send( | ||
|
@@ -162,7 +184,7 @@ class VectorizeIndexImpl implements VectorizeIndex { | |
init: RequestInit | ||
): Promise<Response> { | ||
const res = await this.fetcher.fetch( | ||
`http://vector-search/binding/${endpoint}`, // `http://vector-search` is just a dummy host, the attached fetcher will receive the request | ||
`http://vector-search/${endpoint}`, // `http://vector-search` is just a dummy host, the attached fetcher will receive the request | ||
init | ||
); | ||
if (res.status !== 200) { | ||
|
@@ -217,8 +239,13 @@ async function toJson<T = unknown>(response: Response): Promise<T> { | |
export function makeBinding(env: { | ||
fetcher: Fetcher; | ||
indexId: string; | ||
}): VectorizeIndex { | ||
return new VectorizeIndexImpl(env.fetcher, env.indexId); | ||
indexVersion?: VectorizeVersion; | ||
}): Vectorize { | ||
return new VectorizeIndexImpl( | ||
env.fetcher, | ||
env.indexId, | ||
env.indexVersion ?? "v1" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This ensures backwards compatibility, since previously built pipelines won't have these inner globals. |
||
); | ||
} | ||
|
||
export default makeBinding; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is what we discussed @mikea and if this holds true, I think we're happy with this approach!