conare 0.16.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 +60 -13
- package/dist/index.js +45 -12
- 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,22 +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
|
|
82
|
+
/** PDF pages read by OCR before the answer (`wait: true`); stored like the rest. */
|
|
83
|
+
pagesOcr: number;
|
|
84
|
+
/** PDF pages with no text even after OCR (blank, or OCR could not read them): not stored. */
|
|
54
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[];
|
|
55
88
|
}
|
|
56
89
|
export interface Document {
|
|
57
90
|
id: string;
|
|
58
91
|
text: string;
|
|
59
92
|
metadata: Metadata;
|
|
60
93
|
updatedAt: Date;
|
|
61
|
-
/**
|
|
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
|
+
*/
|
|
62
98
|
version: number;
|
|
99
|
+
user?: string;
|
|
100
|
+
seq?: number;
|
|
63
101
|
}
|
|
64
102
|
export interface DocumentSummary {
|
|
65
103
|
id: string;
|
|
66
104
|
metadata: Metadata;
|
|
67
105
|
updatedAt: Date;
|
|
68
106
|
version: number;
|
|
107
|
+
user?: string;
|
|
108
|
+
seq?: number;
|
|
69
109
|
}
|
|
70
110
|
export interface QueryResult {
|
|
71
111
|
id: string;
|
|
@@ -76,6 +116,8 @@ export interface QueryResult {
|
|
|
76
116
|
/** The reranking model scored this result (false past its window, with `rerank: false`, or when it failed). */
|
|
77
117
|
reranked: boolean;
|
|
78
118
|
metadata: Metadata;
|
|
119
|
+
/** The document's user, when it has one. */
|
|
120
|
+
user?: string;
|
|
79
121
|
}
|
|
80
122
|
/** What a query fuses: the embedding model, BM25 keywords, or the learned-sparse (SPLADE) model. */
|
|
81
123
|
export type Retriever = "vector" | "keyword" | "sparse";
|
|
@@ -192,31 +234,34 @@ export declare class Namespace {
|
|
|
192
234
|
*/
|
|
193
235
|
add(documents: DocumentInput | DocumentInput[], options?: RequestOptions): Promise<string[]>;
|
|
194
236
|
/**
|
|
195
|
-
* `add` that returns each document
|
|
196
|
-
*
|
|
197
|
-
*
|
|
198
|
-
* 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`.
|
|
199
243
|
*/
|
|
200
|
-
write(documents: DocumentInput | DocumentInput[], options?: RequestOptions): Promise<
|
|
201
|
-
id: string;
|
|
202
|
-
version: number | null;
|
|
203
|
-
}[]>;
|
|
244
|
+
write(documents: DocumentInput | DocumentInput[], options?: RequestOptions): Promise<WriteResult[]>;
|
|
204
245
|
/**
|
|
205
246
|
* Store a PDF or an Excel workbook (.xlsx), at most 20 MiB: one document per PDF page with a
|
|
206
247
|
* text layer (`<fileId>:page-3`) and Markdown tables per worksheet (`<fileId>:Sheet1-1`), each
|
|
207
248
|
* with `file`, `file_id`, `file_type` and `page` or `sheet` metadata. `fileId` defaults to the
|
|
208
249
|
* name in id characters; uploading the same one again replaces every document it made. Pages
|
|
209
|
-
* without a text layer (scans, images) are
|
|
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.
|
|
210
253
|
*/
|
|
211
254
|
uploadFile(file: Blob | Uint8Array | ArrayBuffer, name: string, options?: RequestOptions & {
|
|
212
255
|
id?: string;
|
|
256
|
+
wait?: boolean;
|
|
213
257
|
}): Promise<FileUpload>;
|
|
214
258
|
/**
|
|
215
259
|
* Hybrid search, reranked. One result per document: its best passage. `retrievers` picks what
|
|
216
260
|
* the search fuses (default `["vector", "keyword"]`); `"sparse"` needs a namespace created with
|
|
217
261
|
* a sparse index. `rerankDepth` (1-100) has the reranker rank every passage among the first
|
|
218
262
|
* that many in a tournament of 20-passage windows (exact order for the first `topK`, at most
|
|
219
|
-
* 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`.
|
|
220
265
|
*/
|
|
221
266
|
query(query: string, options?: RequestOptions & {
|
|
222
267
|
topK?: number;
|
|
@@ -224,6 +269,8 @@ export declare class Namespace {
|
|
|
224
269
|
rerank?: boolean;
|
|
225
270
|
rerankDepth?: number;
|
|
226
271
|
retrievers?: Retriever[];
|
|
272
|
+
user?: string;
|
|
273
|
+
userPrefix?: string;
|
|
227
274
|
}): Promise<QueryResult[]>;
|
|
228
275
|
/** One document by id, or null if it does not exist. */
|
|
229
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,10 +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
|
|
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.
|
|
220
237
|
*/
|
|
221
238
|
async uploadFile(file, name, options = {}) {
|
|
222
|
-
const query = new URLSearchParams({
|
|
239
|
+
const query = new URLSearchParams({
|
|
240
|
+
name,
|
|
241
|
+
...(options.id ? { id: options.id } : {}),
|
|
242
|
+
...(options.wait ? { wait: "true" } : {}),
|
|
243
|
+
});
|
|
223
244
|
const body = await this.client.request("POST", this.path(`/files?${query}`), file, { timeout: 120_000, ...options }, true);
|
|
224
245
|
return {
|
|
225
246
|
fileId: body.file_id,
|
|
@@ -227,7 +248,9 @@ export class Namespace {
|
|
|
227
248
|
ids: body.ids,
|
|
228
249
|
pages: body.pages ?? null,
|
|
229
250
|
sheets: body.sheets ?? null,
|
|
251
|
+
pagesOcr: body.pages_ocr ?? 0,
|
|
230
252
|
pagesWithoutText: body.pages_without_text ?? [],
|
|
253
|
+
ocrPending: body.ocr_pending ?? [],
|
|
231
254
|
};
|
|
232
255
|
}
|
|
233
256
|
/**
|
|
@@ -235,17 +258,27 @@ export class Namespace {
|
|
|
235
258
|
* the search fuses (default `["vector", "keyword"]`); `"sparse"` needs a namespace created with
|
|
236
259
|
* a sparse index. `rerankDepth` (1-100) has the reranker rank every passage among the first
|
|
237
260
|
* that many in a tournament of 20-passage windows (exact order for the first `topK`, at most
|
|
238
|
-
* 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`.
|
|
239
263
|
*/
|
|
240
264
|
async query(query, options = {}) {
|
|
241
|
-
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);
|
|
242
275
|
return body.results;
|
|
243
276
|
}
|
|
244
277
|
/** One document by id, or null if it does not exist. */
|
|
245
278
|
async get(id, options) {
|
|
246
279
|
try {
|
|
247
280
|
const d = await this.client.request("GET", this.path(`/documents/${segment(id)}`), undefined, options);
|
|
248
|
-
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 };
|
|
249
282
|
}
|
|
250
283
|
catch (err) {
|
|
251
284
|
if (err instanceof ConareError && err.status === 404)
|
|
@@ -265,7 +298,7 @@ export class Namespace {
|
|
|
265
298
|
const qs = params.toString();
|
|
266
299
|
const page = await this.client.request("GET", this.path(`/documents${qs ? `?${qs}` : ""}`), undefined, options);
|
|
267
300
|
for (const d of page.documents) {
|
|
268
|
-
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 };
|
|
269
302
|
}
|
|
270
303
|
cursor = page.next_cursor;
|
|
271
304
|
} while (cursor);
|