conare 0.17.0 → 0.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -3
- package/dist/index.d.ts +58 -14
- package/dist/index.js +44 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -44,15 +44,20 @@ await docs.add({ id, text, metadata }); // one document
|
|
|
44
44
|
await docs.add([...]); // or many; sent 100 at a time. Returns the ids.
|
|
45
45
|
await docs.write({ id, text, ifVersion: 7 }); // add, only if the document is at version 7 (0: only if
|
|
46
46
|
// it does not exist), else 409 version_conflict.
|
|
47
|
-
// Returns [{ id, version }]
|
|
48
|
-
|
|
47
|
+
// Returns [{ id, version, result, chunks, chunksEmbedded }]:
|
|
48
|
+
// result "unchanged" when Conare already held it as sent
|
|
49
|
+
await docs.add({ id, text, user: "tenant-7:u_42", seq: 1759698843000 }); // a user's document; a lower seq
|
|
50
|
+
// than stored is "stale" and writes nothing
|
|
51
|
+
await docs.query("refunds", { userPrefix: "tenant-7:" }); // one user's documents (user) or a group's (userPrefix)
|
|
52
|
+
await docs.get("refunds"); // { id, text, metadata, updatedAt, version, user?, seq? } or null
|
|
49
53
|
for await (const doc of docs.list()) {} // every document, page by page ({ limit } per page: 1-100, default 50)
|
|
50
54
|
await docs.delete("refunds"); // 404 document_not_found if it does not exist
|
|
51
55
|
await docs.deleteMany(["a", "b"]); // up to 1000 ids in one call; missing ids are skipped. Returns the count
|
|
52
56
|
await docs.deleteWhere({ team: "support" }); // every document matching the filter, whole documents. Returns the count
|
|
53
57
|
await docs.uploadFile(bytes, "report.pdf"); // a PDF (one document per page) or an .xlsx (Markdown tables per sheet),
|
|
54
58
|
// up to 20 MiB. The same name (or { id }) again replaces it.
|
|
55
|
-
// Returns { fileId, type, ids, pages, sheets, pagesWithoutText }
|
|
59
|
+
// Returns { fileId, type, ids, pages, sheets, pagesOcr, pagesWithoutText, ocrPending }:
|
|
60
|
+
// scanned pages are OCR'd after the answer (ocrPending; { wait: true } reads them first)
|
|
56
61
|
await docs.drop(); // delete the namespace and its documents (succeeds if already emptied; 404 namespace_not_found if never written)
|
|
57
62
|
await docs.create({ sparse: true }); // create it with a sparse (SPLADE) index, before its first write
|
|
58
63
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export declare const VERSION = "0.
|
|
1
|
+
export declare const VERSION = "0.18.0";
|
|
2
2
|
/** A metadata value: string, number, boolean or an array of strings. */
|
|
3
3
|
export type MetadataValue = string | number | boolean | string[];
|
|
4
4
|
export type Metadata = Record<string, MetadataValue>;
|
|
@@ -39,6 +39,35 @@ export interface DocumentInput {
|
|
|
39
39
|
* ConareError 409 `version_conflict`. Checked atomically with the write.
|
|
40
40
|
*/
|
|
41
41
|
ifVersion?: number;
|
|
42
|
+
/**
|
|
43
|
+
* Who the document belongs to: 1-128 characters of [A-Za-z0-9._:@-], starting with a letter or
|
|
44
|
+
* digit, at most 8 `:`. Group users with `:` (`tenant-7:u_42`) and query a group with `userPrefix`.
|
|
45
|
+
*/
|
|
46
|
+
user?: string;
|
|
47
|
+
/**
|
|
48
|
+
* Your sequence number for the document (a source timestamp, a byte length), 0 to 2^53. A write
|
|
49
|
+
* with a lower `seq` than stored is `stale` and writes nothing. Without it, the last write wins and
|
|
50
|
+
* the stored `seq` stays.
|
|
51
|
+
*/
|
|
52
|
+
seq?: number;
|
|
53
|
+
}
|
|
54
|
+
/** What a write did with one document. */
|
|
55
|
+
export type WriteOutcome = "created" | "updated" | "unchanged" | "stale";
|
|
56
|
+
export interface WriteResult {
|
|
57
|
+
id: string;
|
|
58
|
+
/** The document's version after the write; null when the server did not answer one. */
|
|
59
|
+
version: number | null;
|
|
60
|
+
/**
|
|
61
|
+
* `unchanged`: Conare already held the document as sent, so nothing was embedded or written and
|
|
62
|
+
* its version stayed, unless the write raised `seq` (stored with a new version: always keep
|
|
63
|
+
* `version`). `stale`: it holds a higher `seq` (`storedSeq`); nothing was written.
|
|
64
|
+
* Absent from a server older than results.
|
|
65
|
+
*/
|
|
66
|
+
result?: WriteOutcome;
|
|
67
|
+
/** The document's passages, and those this write embedded (only changed ones are). */
|
|
68
|
+
chunks?: number;
|
|
69
|
+
chunksEmbedded?: number;
|
|
70
|
+
storedSeq?: number;
|
|
42
71
|
}
|
|
43
72
|
/** What `uploadFile` stored. */
|
|
44
73
|
export interface FileUpload {
|
|
@@ -50,24 +79,33 @@ export interface FileUpload {
|
|
|
50
79
|
pages: number | null;
|
|
51
80
|
/** A workbook's sheets (null for a PDF). */
|
|
52
81
|
sheets: number | null;
|
|
53
|
-
/** PDF pages read by OCR
|
|
82
|
+
/** PDF pages read by OCR before the answer (`wait: true`); stored like the rest. */
|
|
54
83
|
pagesOcr: number;
|
|
55
84
|
/** PDF pages with no text even after OCR (blank, or OCR could not read them): not stored. */
|
|
56
85
|
pagesWithoutText: number[];
|
|
86
|
+
/** PDF pages OCR reads after the answer (without `wait`): each is stored, marked `ocr`, within about a minute. */
|
|
87
|
+
ocrPending: number[];
|
|
57
88
|
}
|
|
58
89
|
export interface Document {
|
|
59
90
|
id: string;
|
|
60
91
|
text: string;
|
|
61
92
|
metadata: Metadata;
|
|
62
93
|
updatedAt: Date;
|
|
63
|
-
/**
|
|
94
|
+
/**
|
|
95
|
+
* Changes when a write stores something new and only grows (an `unchanged` write keeps it unless
|
|
96
|
+
* it raises `seq`): pass it as `ifVersion`.
|
|
97
|
+
*/
|
|
64
98
|
version: number;
|
|
99
|
+
user?: string;
|
|
100
|
+
seq?: number;
|
|
65
101
|
}
|
|
66
102
|
export interface DocumentSummary {
|
|
67
103
|
id: string;
|
|
68
104
|
metadata: Metadata;
|
|
69
105
|
updatedAt: Date;
|
|
70
106
|
version: number;
|
|
107
|
+
user?: string;
|
|
108
|
+
seq?: number;
|
|
71
109
|
}
|
|
72
110
|
export interface QueryResult {
|
|
73
111
|
id: string;
|
|
@@ -78,6 +116,8 @@ export interface QueryResult {
|
|
|
78
116
|
/** The reranking model scored this result (false past its window, with `rerank: false`, or when it failed). */
|
|
79
117
|
reranked: boolean;
|
|
80
118
|
metadata: Metadata;
|
|
119
|
+
/** The document's user, when it has one. */
|
|
120
|
+
user?: string;
|
|
81
121
|
}
|
|
82
122
|
/** What a query fuses: the embedding model, BM25 keywords, or the learned-sparse (SPLADE) model. */
|
|
83
123
|
export type Retriever = "vector" | "keyword" | "sparse";
|
|
@@ -194,32 +234,34 @@ export declare class Namespace {
|
|
|
194
234
|
*/
|
|
195
235
|
add(documents: DocumentInput | DocumentInput[], options?: RequestOptions): Promise<string[]>;
|
|
196
236
|
/**
|
|
197
|
-
* `add` that returns each document
|
|
198
|
-
*
|
|
199
|
-
*
|
|
200
|
-
* answer
|
|
237
|
+
* `add` that returns what the write did with each document: its new version, to pass as
|
|
238
|
+
* `ifVersion` on its next write, and its `result` (`unchanged` when Conare already held it as
|
|
239
|
+
* sent: nothing was embedded or written). A conditional write retried after a lost response can
|
|
240
|
+
* answer 409 `version_conflict` for its own first attempt: `get()` the document to tell.
|
|
241
|
+
* `version` is null when the server did not answer one (a server older than versions): never
|
|
242
|
+
* pass it as `ifVersion`.
|
|
201
243
|
*/
|
|
202
|
-
write(documents: DocumentInput | DocumentInput[], options?: RequestOptions): Promise<
|
|
203
|
-
id: string;
|
|
204
|
-
version: number | null;
|
|
205
|
-
}[]>;
|
|
244
|
+
write(documents: DocumentInput | DocumentInput[], options?: RequestOptions): Promise<WriteResult[]>;
|
|
206
245
|
/**
|
|
207
246
|
* Store a PDF or an Excel workbook (.xlsx), at most 20 MiB: one document per PDF page with a
|
|
208
247
|
* text layer (`<fileId>:page-3`) and Markdown tables per worksheet (`<fileId>:Sheet1-1`), each
|
|
209
248
|
* with `file`, `file_id`, `file_type` and `page` or `sheet` metadata. `fileId` defaults to the
|
|
210
249
|
* name in id characters; uploading the same one again replaces every document it made. Pages
|
|
211
|
-
* without a text layer (scans, images) are read by OCR
|
|
212
|
-
*
|
|
250
|
+
* without a text layer (scans, images) are read by OCR: by default after the answer
|
|
251
|
+
* (`ocrPending`, stored within about a minute), with `wait: true` before it (`pagesOcr`). Pages
|
|
252
|
+
* with no text even then are listed in `pagesWithoutText` and not stored.
|
|
213
253
|
*/
|
|
214
254
|
uploadFile(file: Blob | Uint8Array | ArrayBuffer, name: string, options?: RequestOptions & {
|
|
215
255
|
id?: string;
|
|
256
|
+
wait?: boolean;
|
|
216
257
|
}): Promise<FileUpload>;
|
|
217
258
|
/**
|
|
218
259
|
* Hybrid search, reranked. One result per document: its best passage. `retrievers` picks what
|
|
219
260
|
* the search fuses (default `["vector", "keyword"]`); `"sparse"` needs a namespace created with
|
|
220
261
|
* a sparse index. `rerankDepth` (1-100) has the reranker rank every passage among the first
|
|
221
262
|
* that many in a tournament of 20-passage windows (exact order for the first `topK`, at most
|
|
222
|
-
* 20); unset, it reads one window of 20.
|
|
263
|
+
* 20); unset, it reads one window of 20. `user` keeps the search to one user's documents and
|
|
264
|
+
* `userPrefix` (ending in `:`) to a group's; both combine with `filter`.
|
|
223
265
|
*/
|
|
224
266
|
query(query: string, options?: RequestOptions & {
|
|
225
267
|
topK?: number;
|
|
@@ -227,6 +269,8 @@ export declare class Namespace {
|
|
|
227
269
|
rerank?: boolean;
|
|
228
270
|
rerankDepth?: number;
|
|
229
271
|
retrievers?: Retriever[];
|
|
272
|
+
user?: string;
|
|
273
|
+
userPrefix?: string;
|
|
230
274
|
}): Promise<QueryResult[]>;
|
|
231
275
|
/** One document by id, or null if it does not exist. */
|
|
232
276
|
get(id: string, options?: RequestOptions): Promise<Document | null>;
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// conare: TypeScript client for the Conare API. Zero dependencies; uses global fetch.
|
|
2
|
-
export const VERSION = "0.
|
|
2
|
+
export const VERSION = "0.18.0";
|
|
3
3
|
const DEFAULT_BASE_URL = "https://api.conare.ai";
|
|
4
4
|
const BATCH_SIZE = 100;
|
|
5
5
|
// ---- Errors ----------------------------------------------------------------
|
|
@@ -189,10 +189,12 @@ export class Namespace {
|
|
|
189
189
|
return (await this.write(documents, options)).map((d) => d.id);
|
|
190
190
|
}
|
|
191
191
|
/**
|
|
192
|
-
* `add` that returns each document
|
|
193
|
-
*
|
|
194
|
-
*
|
|
195
|
-
* answer
|
|
192
|
+
* `add` that returns what the write did with each document: its new version, to pass as
|
|
193
|
+
* `ifVersion` on its next write, and its `result` (`unchanged` when Conare already held it as
|
|
194
|
+
* sent: nothing was embedded or written). A conditional write retried after a lost response can
|
|
195
|
+
* answer 409 `version_conflict` for its own first attempt: `get()` the document to tell.
|
|
196
|
+
* `version` is null when the server did not answer one (a server older than versions): never
|
|
197
|
+
* pass it as `ifVersion`.
|
|
196
198
|
*/
|
|
197
199
|
async write(documents, options) {
|
|
198
200
|
const all = Array.isArray(documents) ? documents : [documents];
|
|
@@ -204,10 +206,23 @@ export class Namespace {
|
|
|
204
206
|
id: d.id ?? newDocumentId(),
|
|
205
207
|
text: d.text,
|
|
206
208
|
metadata: d.metadata,
|
|
209
|
+
user: d.user,
|
|
210
|
+
seq: d.seq,
|
|
207
211
|
if_version: d.ifVersion,
|
|
208
212
|
})),
|
|
209
213
|
}, options, true);
|
|
210
|
-
body.ids.forEach((id, j) =>
|
|
214
|
+
body.ids.forEach((id, j) => {
|
|
215
|
+
const r = body.results?.[j];
|
|
216
|
+
const result = { id, version: body.versions?.[j] ?? null };
|
|
217
|
+
if (r) {
|
|
218
|
+
result.result = r.result;
|
|
219
|
+
result.chunks = r.chunks;
|
|
220
|
+
result.chunksEmbedded = r.chunks_embedded;
|
|
221
|
+
if (r.stored_seq !== undefined)
|
|
222
|
+
result.storedSeq = r.stored_seq;
|
|
223
|
+
}
|
|
224
|
+
out.push(result);
|
|
225
|
+
});
|
|
211
226
|
}
|
|
212
227
|
return out;
|
|
213
228
|
}
|
|
@@ -216,11 +231,16 @@ export class Namespace {
|
|
|
216
231
|
* text layer (`<fileId>:page-3`) and Markdown tables per worksheet (`<fileId>:Sheet1-1`), each
|
|
217
232
|
* with `file`, `file_id`, `file_type` and `page` or `sheet` metadata. `fileId` defaults to the
|
|
218
233
|
* name in id characters; uploading the same one again replaces every document it made. Pages
|
|
219
|
-
* without a text layer (scans, images) are read by OCR
|
|
220
|
-
*
|
|
234
|
+
* without a text layer (scans, images) are read by OCR: by default after the answer
|
|
235
|
+
* (`ocrPending`, stored within about a minute), with `wait: true` before it (`pagesOcr`). Pages
|
|
236
|
+
* with no text even then are listed in `pagesWithoutText` and not stored.
|
|
221
237
|
*/
|
|
222
238
|
async uploadFile(file, name, options = {}) {
|
|
223
|
-
const query = new URLSearchParams({
|
|
239
|
+
const query = new URLSearchParams({
|
|
240
|
+
name,
|
|
241
|
+
...(options.id ? { id: options.id } : {}),
|
|
242
|
+
...(options.wait ? { wait: "true" } : {}),
|
|
243
|
+
});
|
|
224
244
|
const body = await this.client.request("POST", this.path(`/files?${query}`), file, { timeout: 120_000, ...options }, true);
|
|
225
245
|
return {
|
|
226
246
|
fileId: body.file_id,
|
|
@@ -230,6 +250,7 @@ export class Namespace {
|
|
|
230
250
|
sheets: body.sheets ?? null,
|
|
231
251
|
pagesOcr: body.pages_ocr ?? 0,
|
|
232
252
|
pagesWithoutText: body.pages_without_text ?? [],
|
|
253
|
+
ocrPending: body.ocr_pending ?? [],
|
|
233
254
|
};
|
|
234
255
|
}
|
|
235
256
|
/**
|
|
@@ -237,17 +258,27 @@ export class Namespace {
|
|
|
237
258
|
* the search fuses (default `["vector", "keyword"]`); `"sparse"` needs a namespace created with
|
|
238
259
|
* a sparse index. `rerankDepth` (1-100) has the reranker rank every passage among the first
|
|
239
260
|
* that many in a tournament of 20-passage windows (exact order for the first `topK`, at most
|
|
240
|
-
* 20); unset, it reads one window of 20.
|
|
261
|
+
* 20); unset, it reads one window of 20. `user` keeps the search to one user's documents and
|
|
262
|
+
* `userPrefix` (ending in `:`) to a group's; both combine with `filter`.
|
|
241
263
|
*/
|
|
242
264
|
async query(query, options = {}) {
|
|
243
|
-
const body = await this.client.request("POST", this.path("/query"), {
|
|
265
|
+
const body = await this.client.request("POST", this.path("/query"), {
|
|
266
|
+
query,
|
|
267
|
+
top_k: options.topK,
|
|
268
|
+
filter: options.filter,
|
|
269
|
+
rerank: options.rerank,
|
|
270
|
+
rerank_depth: options.rerankDepth,
|
|
271
|
+
retrievers: options.retrievers,
|
|
272
|
+
user: options.user,
|
|
273
|
+
user_prefix: options.userPrefix,
|
|
274
|
+
}, options, true);
|
|
244
275
|
return body.results;
|
|
245
276
|
}
|
|
246
277
|
/** One document by id, or null if it does not exist. */
|
|
247
278
|
async get(id, options) {
|
|
248
279
|
try {
|
|
249
280
|
const d = await this.client.request("GET", this.path(`/documents/${segment(id)}`), undefined, options);
|
|
250
|
-
return { id: d.id, text: d.text, metadata: d.metadata, updatedAt: new Date(d.updated_at), version: d.version };
|
|
281
|
+
return { id: d.id, text: d.text, metadata: d.metadata, updatedAt: new Date(d.updated_at), version: d.version, user: d.user, seq: d.seq };
|
|
251
282
|
}
|
|
252
283
|
catch (err) {
|
|
253
284
|
if (err instanceof ConareError && err.status === 404)
|
|
@@ -267,7 +298,7 @@ export class Namespace {
|
|
|
267
298
|
const qs = params.toString();
|
|
268
299
|
const page = await this.client.request("GET", this.path(`/documents${qs ? `?${qs}` : ""}`), undefined, options);
|
|
269
300
|
for (const d of page.documents) {
|
|
270
|
-
yield { id: d.id, metadata: d.metadata, updatedAt: new Date(d.updated_at), version: d.version };
|
|
301
|
+
yield { id: d.id, metadata: d.metadata, updatedAt: new Date(d.updated_at), version: d.version, user: d.user, seq: d.seq };
|
|
271
302
|
}
|
|
272
303
|
cursor = page.next_cursor;
|
|
273
304
|
} while (cursor);
|