Skip to content

Commit

Permalink
add namespace to segment vectors, so that e.g packs search doesn't re…
Browse files Browse the repository at this point in the history
…turn items
  • Loading branch information
mikib0 committed Jul 10, 2024
1 parent 5c3c040 commit 280dbc7
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions server/src/vector/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ class VectorClient {
// }

// New API-based insert method
public async insert(id: string, values: number[]) {
public async insert(id: string, values: number[], namespace: string) {
const url = `https://api.cloudflare.com/client/v4/accounts/${this.accountId}/vectorize/indexes/${this.indexName}/insert`;
const ndjsonBody = `${JSON.stringify({ id, values })}\n`;
const ndjsonBody = `${JSON.stringify({ id, values, namespace })}\n`;

const response = await fetch(url, {
method: 'PUT',
Expand Down Expand Up @@ -69,7 +69,7 @@ class VectorClient {
// }

// New API-based search method
public async search(queryEmbedding: number[]) {
public async search(queryEmbedding: number[], namespace: string) {
const url = `https://api.cloudflare.com/client/v4/accounts/${this.accountId}/vectorize/indexes/${this.indexName}/vectors/query`;
const response = await fetch(url, {
method: 'POST',
Expand All @@ -78,7 +78,7 @@ class VectorClient {
Authorization: `Bearer ${this.apiKey}`,
},
body: JSON.stringify({
queries: [{ values: queryEmbedding, topK: 5 }],
queries: [{ values: queryEmbedding, topK: 5, namespace }],
}),
});

Expand All @@ -92,9 +92,9 @@ class VectorClient {
return await response.json();
}

public async syncRecord({ id, content }: { id: string; content: string }) {
public async syncRecord({ id, content, namespace }: { id: string; content: string, namespace: string }) {
const values = await AiClient.getEmbedding(content);
await this.insert(id, values);
await this.insert(id, values, namespace);
}
}

Expand Down

0 comments on commit 280dbc7

Please sign in to comment.