@or-sdk/lookup 1.1.0-beta.1830.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.
Files changed (87) hide show
  1. package/README.md +983 -0
  2. package/dist/cjs/Lookup.js +413 -0
  3. package/dist/cjs/Lookup.js.map +1 -0
  4. package/dist/cjs/__tests__/QnA.collections.spec.js +177 -0
  5. package/dist/cjs/__tests__/QnA.collections.spec.js.map +1 -0
  6. package/dist/cjs/__tests__/fixtures/collection.json +34 -0
  7. package/dist/cjs/constants.js +5 -0
  8. package/dist/cjs/constants.js.map +1 -0
  9. package/dist/cjs/error-parser/OrNetworkError.js +45 -0
  10. package/dist/cjs/error-parser/OrNetworkError.js.map +1 -0
  11. package/dist/cjs/error-parser/index.js +34 -0
  12. package/dist/cjs/error-parser/index.js.map +1 -0
  13. package/dist/cjs/error-parser/parse-axios-error.js +38 -0
  14. package/dist/cjs/error-parser/parse-axios-error.js.map +1 -0
  15. package/dist/cjs/error-parser/processors.js +22 -0
  16. package/dist/cjs/error-parser/processors.js.map +1 -0
  17. package/dist/cjs/error-parser/types.js +3 -0
  18. package/dist/cjs/error-parser/types.js.map +1 -0
  19. package/dist/cjs/guards.js +9 -0
  20. package/dist/cjs/guards.js.map +1 -0
  21. package/dist/cjs/index.js +23 -0
  22. package/dist/cjs/index.js.map +1 -0
  23. package/dist/cjs/types.js +11 -0
  24. package/dist/cjs/types.js.map +1 -0
  25. package/dist/esm/Lookup.js +221 -0
  26. package/dist/esm/Lookup.js.map +1 -0
  27. package/dist/esm/__tests__/QnA.collections.spec.js +89 -0
  28. package/dist/esm/__tests__/QnA.collections.spec.js.map +1 -0
  29. package/dist/esm/__tests__/fixtures/collection.json +34 -0
  30. package/dist/esm/constants.js +2 -0
  31. package/dist/esm/constants.js.map +1 -0
  32. package/dist/esm/error-parser/OrNetworkError.js +14 -0
  33. package/dist/esm/error-parser/OrNetworkError.js.map +1 -0
  34. package/dist/esm/error-parser/index.js +12 -0
  35. package/dist/esm/error-parser/index.js.map +1 -0
  36. package/dist/esm/error-parser/parse-axios-error.js +34 -0
  37. package/dist/esm/error-parser/parse-axios-error.js.map +1 -0
  38. package/dist/esm/error-parser/processors.js +16 -0
  39. package/dist/esm/error-parser/processors.js.map +1 -0
  40. package/dist/esm/error-parser/types.js +2 -0
  41. package/dist/esm/error-parser/types.js.map +1 -0
  42. package/dist/esm/guards.js +5 -0
  43. package/dist/esm/guards.js.map +1 -0
  44. package/dist/esm/index.js +5 -0
  45. package/dist/esm/index.js.map +1 -0
  46. package/dist/esm/types.js +8 -0
  47. package/dist/esm/types.js.map +1 -0
  48. package/dist/types/Lookup.d.ts +27 -0
  49. package/dist/types/Lookup.d.ts.map +1 -0
  50. package/dist/types/__tests__/QnA.collections.spec.d.ts +2 -0
  51. package/dist/types/__tests__/QnA.collections.spec.d.ts.map +1 -0
  52. package/dist/types/constants.d.ts +2 -0
  53. package/dist/types/constants.d.ts.map +1 -0
  54. package/dist/types/error-parser/OrNetworkError.d.ts +14 -0
  55. package/dist/types/error-parser/OrNetworkError.d.ts.map +1 -0
  56. package/dist/types/error-parser/index.d.ts +5 -0
  57. package/dist/types/error-parser/index.d.ts.map +1 -0
  58. package/dist/types/error-parser/parse-axios-error.d.ts +3 -0
  59. package/dist/types/error-parser/parse-axios-error.d.ts.map +1 -0
  60. package/dist/types/error-parser/processors.d.ts +7 -0
  61. package/dist/types/error-parser/processors.d.ts.map +1 -0
  62. package/dist/types/error-parser/types.d.ts +6 -0
  63. package/dist/types/error-parser/types.d.ts.map +1 -0
  64. package/dist/types/guards.d.ts +3 -0
  65. package/dist/types/guards.d.ts.map +1 -0
  66. package/dist/types/index.d.ts +5 -0
  67. package/dist/types/index.d.ts.map +1 -0
  68. package/dist/types/types.d.ts +146 -0
  69. package/dist/types/types.d.ts.map +1 -0
  70. package/package.json +37 -0
  71. package/src/Lookup.ts +353 -0
  72. package/src/__tests__/QnA.collections.spec.ts +108 -0
  73. package/src/__tests__/fixtures/collection.json +34 -0
  74. package/src/constants.ts +1 -0
  75. package/src/error-parser/OrNetworkError.ts +23 -0
  76. package/src/error-parser/index.ts +16 -0
  77. package/src/error-parser/parse-axios-error.ts +40 -0
  78. package/src/error-parser/processors.ts +21 -0
  79. package/src/error-parser/types.ts +6 -0
  80. package/src/guards.ts +5 -0
  81. package/src/index.ts +4 -0
  82. package/src/types.ts +629 -0
  83. package/tsconfig.dev.json +8 -0
  84. package/tsconfig.esm.json +12 -0
  85. package/tsconfig.json +7 -0
  86. package/tsconfig.types.json +10 -0
  87. package/vitest.config.js +24 -0
package/README.md ADDED
@@ -0,0 +1,983 @@
1
+ # Lookup SDK
2
+
3
+ The `@or-sdk/lookup` package provides a client for working with the Question and Answer and Text Search system in OneReach.ai a.k.a Lookup. With this client, you can perform operations such as loading documents, creating collections, searching in collections, asking questions to generate answers based on the loaded documents, and managing properties of collections.
4
+
5
+ ## Concepts
6
+
7
+ ### Collection
8
+
9
+ A collection is a group of searchable items, also known as passages. It allows you to organize and manage related documents and their passages together under a single entity. When you perform a search or ask a question, the Lookup system will search through the passages within the specified collection.
10
+
11
+ ### Document
12
+
13
+ Documents are used to load a bulk of data into a collection. They can be any type of text-based file such as PDFs, text files, or web pages. When you provide a URL for a document, the Lookup system downloads the content, splits it into smaller chunks (passages), and adds them to the collection.
14
+
15
+ ### Passage
16
+
17
+ A passage is an atomic piece of data that represents a portion of a document. It is the smallest searchable unit within a collection. When you search or ask a question, the Lookup system uses vector similarity to find the most relevant passages from all the documents in the collection.
18
+
19
+ The search process involves comparing the vector representation of the input query (search term or question) with the vector representations of the passages in the collection. The results are ranked based on the similarity between the query and passages, with the most similar passages being returned as the search results.
20
+
21
+ ### Property
22
+
23
+ A property is an additional piece of information that can be added to a collection to store custom metadata about the collection. Properties can have a name, datatype, and an optional description. You can manage properties by adding, updating, or deleting them from a collection.
24
+
25
+ ## Installation
26
+
27
+ To install the package, run the following command:
28
+
29
+ ```
30
+ $ npm install @or-sdk/lookup
31
+ ```
32
+
33
+ ## Usage
34
+
35
+ To use the `Lookup` client, you need to create an instance of the class with the appropriate configuration options. You can either provide the direct API URL or the service discovery URL. Here is an example:
36
+
37
+ ```typescript
38
+ import { Lookup } from '@or-sdk/lookup';
39
+
40
+ // with direct api url
41
+ const lookup = new Lookup({
42
+ token: 'my-account-token-string',
43
+ serviceUrl: 'http://example.lookup/endpoint'
44
+ });
45
+
46
+ // with service discovery(slower)
47
+ const lookup = new Lookup({
48
+ token: 'my-account-token-string',
49
+ discoveryUrl: 'http://example.lookup/endpoint'
50
+ });
51
+ ```
52
+
53
+ Once you have an instance of the `Lookup` client, you can use its methods to interact with the OneReach.ai Lookup system. The available methods are:
54
+
55
+ ### `loadDocument`
56
+
57
+ Loads a document into a collection by providing a URL to the document. The document can be in PDF, CSV, or HTML format. The Lookup system downloads the content, splits it into smaller chunks called passages, and adds them to the collection. For CSV files, the primary data source is the `content` column, and additional columns can be matched to custom properties in the collection schema. The loading process is asynchronous, and the document status can be used to check if the loading is in progress or if an error occurred.
58
+
59
+ #### Params
60
+ - `collectionId`: `string` - The ID of the collection the document will be loaded into
61
+ - `params`: `LoadDocument` - Document loading parameters
62
+ - `description`: `string` (optional) - Document description
63
+ - `url`: `string` - The URL of the document to load
64
+ - `name`: `string` - Document name
65
+ - `defaultProperties`: `Record<string, unknown>` (optional) - Default custom properties for the document passages. Note that these properties should be listed in the collection properties.
66
+
67
+ #### Status
68
+ The `status` field is used to identify the document loading progress and possible errors. The possible status values are:
69
+ - `NEW` - The document has been created, but the loading process has not started yet.
70
+ - `LOADING` - The document loading is in progress.
71
+ - `READY` - The document loading has been completed successfully.
72
+ - `ERROR` - An error occurred during the document loading process due to unknown reasons.
73
+ - `string` - If a specific error occurred during the loading process, the error message will be provided in the status field.
74
+
75
+ #### Example
76
+
77
+ In this example, the `loadDocument` method downloads a document from the provided URL, splits the content into passages, and adds them to the specified collection. Additionally, the `defaultProperties` attribute is used to set custom properties for the passages.
78
+
79
+ ```typescript
80
+ const collectionId = 'a1b2c3d4-uuid';
81
+ const document = await lookup.loadDocument(collectionId, {
82
+ name: 'Sample Document',
83
+ description: 'A sample document',
84
+ url: 'http://example.com/sample-document.pdf',
85
+ defaultProperties: {
86
+ author: 'John Doe',
87
+ publishDate: '2020-12-31'
88
+ }
89
+ });
90
+
91
+ // Example response
92
+ {
93
+ id: 'e5f6g7h8-uuid',
94
+ accountId: 'i9j0k1l2-uuid',
95
+ collection: 'a1b2c3d4-uuid',
96
+ name: 'Sample Document',
97
+ description: 'A sample document',
98
+ status: 'LOADING'
99
+ }
100
+ ```
101
+
102
+ After the document loading process, the status will update according to the result:
103
+
104
+ ```typescript
105
+ // Example response when the document loading is successful
106
+ {
107
+ id: 'e5f6g7h8-uuid',
108
+ accountId: 'i9j0k1l2-uuid',
109
+ collection: 'a1b2c3d4-uuid',
110
+ name: 'Sample Document',
111
+ description: 'A sample document',
112
+ status: 'READY'
113
+ }
114
+
115
+ // Example response when there is an error during the document loading
116
+ {
117
+ id: 'e5f6g7h8-uuid',
118
+ accountId: 'i9j0k1l2-uuid',
119
+ collection: 'a1b2c3d4-uuid',
120
+ name: 'Sample Document',
121
+ description: 'A sample document',
122
+ status: 'Could not load the document due to an error...'
123
+ }
124
+ ```
125
+
126
+ To handle cases where you need to wait for the document to fully load, you can use polling. Here's an example:
127
+
128
+ ```typescript
129
+ import { sleep } from 'utils';
130
+
131
+ async function waitForDocumentLoading(collectionId: string, documentId: string) {
132
+ let document;
133
+ let isLoading = true;
134
+
135
+ while (isLoading) {
136
+ document = await lookup.getDocument(collectionId, documentId);
137
+
138
+ if (document.status === 'READY' || typeof document.status === 'string') {
139
+ isLoading = false;
140
+ } else {
141
+ await sleep(5000); // Wait for 5 seconds before checking the status again
142
+ }
143
+ }
144
+
145
+ return document;
146
+ }
147
+
148
+ async function loadDocumentAndWait(collectionId: string, documentParams: LoadDocument) {
149
+ const document = await lookup.loadDocument(collectionId, documentParams);
150
+ const loadedDocument = await waitForDocumentLoading(collectionId, document.id);
151
+ return loadedDocument;
152
+ }
153
+
154
+ // Example usage
155
+ const collectionId = 'a1b2c3d4-uuid';
156
+ const documentParams = {
157
+ name: 'Sample Document',
158
+ description: 'A sample document',
159
+ url: 'http://example.com/sample-document.pdf',
160
+ defaultProperties: {
161
+ author: 'John Doe',
162
+ publishDate: '2020-12-31'
163
+ }
164
+ };
165
+
166
+ const loadedDocument = await loadDocumentAndWait(collectionId, documentParams);
167
+ console.log('Loaded document:', loadedDocument);
168
+ ```
169
+
170
+ In this example, the `loadDocumentAndWait` function first calls the `loadDocument` method to start loading a document and then uses the `waitForDocumentLoading` function to poll for the document's status until it's either `READY` or an error message is provided. The `sleep` function is used to introduce a delay before checking the status again.
171
+
172
+ ### `createCollection`
173
+
174
+ Creates a new collection with optional properties. A collection is a group of related documents that are searchable together. Additional custom properties can be defined to store metadata associated with the documents within the collection.
175
+
176
+ #### Params
177
+
178
+ - `createParams`: `CreateCollection` - An object containing the properties of the collection:
179
+ - `name`: `string` - The name of the collection. It can start only with a letter and it must be at least 3 characters long, and cannot exceed 100 characters in length.
180
+ - `description`: `string` (optional) - A brief description of the collection. If present, it must be at least 3 characters and no more than 500 characters in length.
181
+ - `properties`: `Property[]` (optional) - An array of custom properties to add to the collection. Each property should include a name and datatype. Please see list of supported datatypes.
182
+ - `imageUrl`: `string` (optional) - The URL of the thumbnail image for the collection. It must be at least 3 characters and it cannot exceed 2048 characters in length.
183
+ - `answerInstruction`: `string` (optional) - An instruction used to generate a correct answer. If present, it cannot be more than 1000 characters long.
184
+ - `questionInstruction`: `string` (optional) - An instruction used to generate a proper search query. If present, it cannot be more than 1000 characters long.
185
+ - `greetingInstruction`: `string` (optional) - An instruction used to generate a correct first message. If present, it cannot be more than 1000 characters long.
186
+ - `temperature`: `number` (optional) - Sampling temperature to use. If present, it must be a number between 0 and 2.
187
+ - `maxTokens`: `number` (optional) - The maximum output length from the Large Language Model (LLM). If present, it must be a number between 128 and 2048.
188
+ - `frequencyPenalty`: `number` (optional) - Value to penalize new tokens based on their frequency in existing text. If present, it must be a number between -2 and 2.
189
+ - `presencePenalty`: `number` (optional) - Value to penalize new tokens based on their presence in existing text. If present, it must be a number between -2 and 2.
190
+ - `maxDistance`: `number` (optional) - Value to filter out passages that are further than this value from the question or context. If present, it must be a number between 0 and 1.
191
+ - `modelName`: `string` (optional) - Name of the large language model to be used. It can't be more than 64 characters long.
192
+
193
+ #### Example
194
+ Creating a collection with a name, description, and additional instructions
195
+
196
+ ```typescript
197
+ const collection = await lookup.createCollection({
198
+ name: 'Sample Collection',
199
+ description: 'A sample collection for Lookup',
200
+ properties: [
201
+ { name: 'author', dataType: 'string' },
202
+ { name: 'year', dataType: 'int' },
203
+ ],
204
+ imageUrl: 'https://example.com/sample-collection.jpg',
205
+ answerInstruction: 'Provide relevant answer from the text',
206
+ questionInstruction: 'Generate a question based on understanding of the text',
207
+ greetingInstruction: 'Initiate conversation providing context about the document',
208
+ temperature: 0.7,
209
+ maxTokens: 200,
210
+ frequencyPenalty: 0.8,
211
+ presencePenalty: 1,
212
+ maxDistance: 0.5,
213
+ modelName: 'text-davinci-002'
214
+ });
215
+
216
+ console.log(collection);
217
+ ```
218
+ This will print collection information with its ID, name, and other parameters. It will also include any custom properties.
219
+ Creating a collection with additional properties
220
+
221
+ ```typescript
222
+ const collectionWithProperties = await lookup.createCollection({
223
+ name: 'Collection With Properties',
224
+ description: 'A collection with properties',
225
+ properties: [
226
+ {
227
+ name: 'author',
228
+ dataType: 'string',
229
+ description: 'Author of the document',
230
+ },
231
+ {
232
+ name: 'publishDate',
233
+ dataType: 'date',
234
+ description: 'Date when the document was published',
235
+ },
236
+ ],
237
+ });
238
+
239
+ // Example response
240
+ {
241
+ id: 'x1y2z3w4-uuid',
242
+ accountId: 'i9j0k1l2-uuid',
243
+ name: 'Collection With Properties',
244
+ description: 'A collection with properties',
245
+ properties: [
246
+ {
247
+ name: 'author',
248
+ dataType: 'string',
249
+ description: 'Author of the document',
250
+ },
251
+ {
252
+ name: 'publishDate',
253
+ dataType: 'date',
254
+ description: 'Date when the document was published',
255
+ },
256
+ ],
257
+ }
258
+ ```
259
+
260
+ **Note:** The custom properties defined for the collection can be used for filtering, sorting, and selecting specific properties when performing search or ask operations.
261
+
262
+ #### `addProperty`
263
+ Add a custom property to an existing collection. Custom properties can be used to store additional metadata about the collection or its passages. The available data types for properties can be found at https://weaviate.io/developers/weaviate/config-refs/datatypes. Note that there are reserved property names ['accountId', 'collection', 'document', 'content', 'loaderMetadata', 'sourceUrl']
264
+
265
+ #### Params
266
+ - `collectionId` (string): The ID of the collection to which the property will be added.
267
+ - `property` (Property): The property to be added, which includes:
268
+ - `name` (string): The name of the property, e.g. `nameOfTheAuthor`. Name should not include any space.
269
+ - `dataType` (string): The data type of the property. See https://weaviate.io/developers/weaviate/config-refs/schema#datatypes for the available types
270
+ - `description` (string, optional): An optional description of the property.
271
+
272
+ #### Example
273
+ ```typescript
274
+ const collectionId = 'a1b2c3d4-uuid';
275
+ const property = {
276
+ name: 'author',
277
+ dataType: 'string',
278
+ description: 'Author of the document',
279
+ };
280
+
281
+ await lookup.addProperty(collectionId, property);
282
+
283
+ // No direct response, the property is added to the collection
284
+ ```
285
+
286
+ You can verify the added property by retrieving the collection:
287
+
288
+ ```typescript
289
+ const collection = await lookup.getCollection(collectionId);
290
+
291
+ // Example response
292
+ {
293
+ id: 'a1b2c3d4-uuid',
294
+ accountId: 'i9j0k1l2-uuid',
295
+ name: 'Sample Collection',
296
+ description: 'A sample collection',
297
+ countPassages: 42,
298
+ properties: [
299
+ {
300
+ name: 'author',
301
+ dataType: 'string',
302
+ description: 'Author of the document',
303
+ },
304
+ ],
305
+ }
306
+ ```
307
+
308
+ ### `deleteCollection`
309
+
310
+ Delete a collection by its ID. This operation will permanently remove the collection along with all the documents, passages, and custom properties associated with the collection.
311
+
312
+ #### Params
313
+ - collectionId: `string` - The ID of the collection to delete
314
+
315
+ #### Example
316
+
317
+ ```typescript
318
+ const collectionId = 'a1b2c3d4-uuid';
319
+ await lookup.deleteCollection(collectionId);
320
+ ```
321
+
322
+ **Note:** Use this operation with caution, as deleting a collection is an irreversible action. Once a collection is deleted, all data associated with it will be lost and cannot be recovered. Always make sure to backup any important data before deleting a collection.
323
+
324
+ ### `search`
325
+
326
+ Search for documents in a collection based on a query. This method accepts several parameters that can be used to filter, sort, and limit the results.
327
+
328
+ #### Params
329
+ - term: `string` - The search term, query, or keywords
330
+ - limit: `number` (optional) - Limit the number of search results
331
+ - where: `Record<string, unknown>` (optional) - Weaviate filter object for advanced filtering. For more details, visit https://weaviate.io/developers/weaviate/api/graphql/filters
332
+ - select: `string[]` (optional) - An array of custom properties to include in the results
333
+ - maxDistance: `number` (optional) - Filters out passages that are further than this value from the question or context. Measured by cosine metric. Default is 0. Range accepted is 0 to 1
334
+
335
+ #### Example
336
+
337
+ ```typescript
338
+ const collectionId = 'a1b2c3d4-uuid';
339
+
340
+ const searchResults = await lookup.search(collectionId, {
341
+ term: 'search term',
342
+ limit: 10,
343
+ where: {
344
+ operator: 'And',
345
+ operands: [
346
+ {
347
+ operator: 'GreaterThan',
348
+ path: ['publishDate'],
349
+ valueDate: '2020-01-01T00:00:00Z',
350
+ },
351
+ {
352
+ operator: 'LessThan',
353
+ path: ['publishDate'],
354
+ valueDate: '2022-01-01T00:00:00Z',
355
+ },
356
+ ],
357
+ },
358
+ select: ['author', 'publishDate'],
359
+ });
360
+
361
+ // Example response
362
+ [
363
+ {
364
+ id: 'e5f6g7h8-uuid',
365
+ distance: '0.1234',
366
+ content: 'Found passage content',
367
+ sourceUrl: 'http://example.com/source-url',
368
+ loaderMetadata: {
369
+ custom: 'metadata'
370
+ },
371
+ document: {
372
+ id: 'i9j0k1l2-uuid',
373
+ name: 'Document Name'
374
+ }
375
+ }
376
+ ]
377
+ ```
378
+
379
+ In this example, the `search` method is called with advanced filtering and property selection options. The `where` parameter is used to filter the results based on the `publishDate` property, and the `select` parameter is used to include the `author` and `publishDate` properties of the selected passages in the response. As a result, the search returns a list of passages that meet the specified conditions and include the custom property values. Along with these properties, the result also includes new properties like `sourceUrl`, `loaderMetadata` and `document`.
380
+
381
+ ### `ask`
382
+
383
+ Asks a question and generates an answer based on the documents in a collection. This method accepts a set of parameters to configure the question-answering process and to fine-tune the generated outputs.
384
+
385
+ The `ask` method supports two modes: 'ask' and 'conversation'. Here are the details of each mode:
386
+
387
+ 1. 'ask' mode:
388
+
389
+ This mode allows you to ask a direct, standalone question. In this mode, the processing of the question and the generation of the answer are not influenced by any prior context or conversation history.
390
+
391
+ This mode is suitable for scenarios where a single question is to be answered independently, such as a search query or a factual question.
392
+
393
+ 2. 'conversation' mode:
394
+
395
+ This mode allows you to ask questions in the context of a conversation. This means that prior messages from the conversation will be taken into account while processing the question and generating the answer.
396
+
397
+ This mode is suitable for scenarios where a dialogue is happening, and the generation of an answer is dependent on the previous exchanges in the conversation. In this mode, you can supply previous conversation messages in the `messages` parameter, which accepts an array of message objects, each with a sender role ('assistant' or 'user') and the content of the message.
398
+
399
+ Both modes use a language model to generate an answer given a question and a collection of documents. How the prior messages are used to influence the retrieval of information and the generation of responses differs based on the mode used.
400
+
401
+ #### Params
402
+ - question: `string` - The question to ask
403
+ - messages: `CompletionRequestMessage[]` (optional) - An array of previous chat messages with roles 'user' and 'assistant'. Default is an empty array
404
+ - limit: `number` (optional) - Limit the number of search results. Default is 5. Accepts values between 1 and 100
405
+ - where: `Record<string, unknown>` (optional) - Weaviate filter object for advanced filtering. For more details, visit https://weaviate.io/developers/weaviate/api/graphql/filters
406
+ - select: `string[]` (optional) - Array of the custom properties to select
407
+ - mode: `AskMode` (optional) - Question mode, can be either 'ask' or 'conversation'. Default is 'conversation'
408
+ - answerInstruction: `string | null` (optional) - A summarization instruction used to generate correct answer. Maximum Length: 1000 characters
409
+ - questionInstruction: `string | null` (optional) - A summarization instruction used to generate correct search query. Maximum Length: 1000 characters
410
+ - temperature: `number` (optional) - Adjusts the randomness in the model's output. Default is 1. Accepts values between 0 and 2
411
+ - maxTokens: `number` (optional) - Maximum output length from the language model. Default is 1024. Accepts values between 128 and 2048
412
+ - frequencyPenalty: `number` (optional) - Penalizes new tokens based on their existing frequency in the text so far. Default is 0. Range accepted is -2 to 2
413
+ - presencePenalty: `number` (optional) - Penalizes new tokens based on whether they appear in the text so far. Default is 0. Range accepted is -2 to 2
414
+ - maxDistance: `number` (optional) - Filters out passages that are further than this value from the question or context. Measured by cosine metric. Default is 0. Range accepted is 0 to 1
415
+
416
+ #### Example
417
+
418
+ ```typescript
419
+ const collectionId = 'a1b2c3d4-uuid';
420
+
421
+ const askResults = await lookup.ask(collectionId, {
422
+ question: 'What is the meaning of life?',
423
+ messages: [
424
+ { role: 'user', content: 'Hello' },
425
+ { role: 'assistant', content: 'Hi there!' },
426
+ ],
427
+ limit: 10,
428
+ where: {
429
+ operator: 'And',
430
+ operands: [
431
+ {
432
+ operator: 'GreaterThan',
433
+ path: ['publishDate'],
434
+ valueDate: '2020-01-01T00:00:00Z',
435
+ },
436
+ {
437
+ operator: 'LessThan',
438
+ path: ['publishDate'],
439
+ valueDate: '2022-01-01T00:00:00Z',
440
+ },
441
+ ],
442
+ },
443
+ select: ['title', 'author', 'publishDate'],
444
+ mode: 'conversation',
445
+ answerInstruction: 'Generate a brief and concise answer.',
446
+ questionInstruction: 'Find documents related to the meaning of life.',
447
+ temperature: 0.8,
448
+ maxTokens: 1500,
449
+ frequencyPenalty: 0.5;
450
+ presencePenalty: 0.5;
451
+ maxDistance: 0.9,
452
+ });
453
+
454
+ // Example response
455
+ {
456
+ result: { role: 'assistant', content: 'The meaning of life is 42.' },
457
+ searchResult: [
458
+ {
459
+ id: 'e5f6g7h8-uuid',
460
+ distance: '0.1234',
461
+ content: 'Found passage content',
462
+ sourceUrl: 'https://example.com/meaning-of-life',
463
+ loaderMetadata: {...},
464
+ document: {
465
+ id: 'i9j0k1l2-uuid',
466
+ name: 'On the Meaning of Life',
467
+ },
468
+ title: 'On the Meaning of Life',
469
+ author: 'John Doe',
470
+ publishDate: '2020-12-31',
471
+ },
472
+ // ... More results if available
473
+ ],
474
+ }
475
+ ```
476
+
477
+
478
+ ### `updateDocument`
479
+
480
+ Update a document's description in a collection. This method allows you to modify the description of an existing document, providing an updated text to better explain or detail the document's content.
481
+
482
+ #### Params
483
+ - collectionId: `string` - The ID of the collection the document belongs to
484
+ - documentId: `string` - The ID of the document to update
485
+ - params: `UpdateDocument` - The update document parameters, which include:
486
+ - description: `string` - The updated description of the document
487
+
488
+ #### Example
489
+
490
+ Updating a document's description in a collection:
491
+
492
+ ```typescript
493
+ const collectionId = 'a1b2c3d4-uuid';
494
+ const documentId = 'e5f6g7h8-uuid';
495
+ const updatedDocument = await lookup.updateDocument(collectionId, documentId, {
496
+ description: 'Updated document description',
497
+ });
498
+
499
+ // Example response
500
+ {
501
+ id: 'e5f6g7h8-uuid',
502
+ accountId: 'i9j0k1l2-uuid',
503
+ collection: 'a1b2c3d4-uuid',
504
+ name: 'Sample Document',
505
+ description: 'Updated document description'
506
+ }
507
+ ```
508
+
509
+ **Note:** Only the document description can be updated using this method. Other metadata such as the document name or URL cannot be modified once the document is created and added to the collection.
510
+
511
+ ### `updateCollection`
512
+
513
+ Update an existing collection's properties. The collection must be previously created using the `createCollection` method. The updated collection will preserve its existing properties, ID, and name, but its associated documents might be affected based on the new instructions.
514
+
515
+ #### Params
516
+ - `collectionId`: `string` - The ID of the collection you want to update
517
+ - `updateParams`: `UpdateCollection` - An object containing the properties you want to update:
518
+ - `description`: `string` (optional) - The new description for the collection. `Minimum length: 3, Maximum length: 500`
519
+ - `imageUrl`: `string` (optional) - A collection thumbnail image URL. `Minimum length: 3, Maximum length: 2048`
520
+ - `answerInstruction`: `string` (optional) - A summarization instruction used to generate a correct answer. `Maximum length: 1000`
521
+ - `questionInstruction`: `string` (optional) - A summarization instruction used to generate a correct search query. `Maximum length: 1000`
522
+ - `greetingInstruction`: `string` (optional) - A summarization instruction used to generate a correct first message. `Maximum length: 1000`
523
+ - `temperature`: `number` (optional) - What sampling temperature to use. Default value: 1 Range: [0, 2]
524
+ - `maxTokens`: `number` (optional) - Max output length from the Large Language Model (LLM). Range: [128, 2048]
525
+ - `frequencyPenalty`: `number` (optional) - Positive values penalize new tokens based on their existing frequency in the text. Range: [-2, 2]
526
+ - `presencePenalty`: `number` (optional) - Penalize new tokens based on whether they appear in the text. Range: [-2, 2]
527
+ - `maxDistance`: `number` (optional) - Filter out passages that are further then maxDistance from the question or context. Range: [0, 1]
528
+ - `modelName`: `string` (optional) - Name of the large language model to be used. `Maximum length: 64`
529
+
530
+ #### Example
531
+ Updating a collection's description and adding a new question instruction.
532
+
533
+ ```typescript
534
+ const collectionId = 'a1b2c3d4-uuid';
535
+ const updatedCollection = await lookup.updateCollection(collectionId, {
536
+ description: 'Updated collection description',
537
+ questionInstruction: 'Consider the main theme of the document for the query',
538
+ maxTokens: 200,
539
+ temperature: 1.5,
540
+ modelName: 'gpt-3'
541
+ });
542
+
543
+ // Example response
544
+ {
545
+ id: 'a1b2c3d4-uuid',
546
+ accountId: 'i9j0k1l2-uuid',
547
+ name: 'Sample Collection',
548
+ description: 'Updated collection description',
549
+ questionInstruction: 'Consider the main theme of the document for the query',
550
+ maxTokens: 200,
551
+ temperature: 1.5,
552
+ modelName: 'gpt-3'
553
+ }
554
+ ```
555
+
556
+
557
+ ### `getDocument`
558
+
559
+ Retrieve a single document from a collection by its ID. A document is a text-based file (e.g., PDF, TXT, or HTML) that contains content, which is split into passages for searching within a collection.
560
+
561
+ #### Params
562
+ - collectionId: `string` - The ID of the collection the document belongs to.
563
+ - documentId: `string` - The ID of the document to retrieve.
564
+
565
+ #### Example
566
+
567
+ ```typescript
568
+ const collectionId = 'a1b2c3d4-uuid';
569
+ const documentId = 'e5f6g7h8-uuid';
570
+ const document = await lookup.getDocument(collectionId, documentId);
571
+
572
+ // Example response
573
+ {
574
+ id: 'e5f6g7h8-uuid',
575
+ accountId: 'i9j0k1l2-uuid',
576
+ collection: 'a1b2c3d4-uuid',
577
+ name: 'Sample Document',
578
+ description: 'A sample document',
579
+ sourceUrl: 'http://example.com/sample-document.pdf',
580
+ countPassages: 42,
581
+ }
582
+ ```
583
+
584
+ ### `getCollection`
585
+
586
+ Retrieve a collection by its ID. This method returns a collection object including its ID, account ID, name, description, and any additional properties defined during the collection's creation.
587
+
588
+ #### Params
589
+ - collectionId: `string` - The ID of the collection to retrieve
590
+
591
+ #### Example
592
+
593
+ In this example, the `getCollection` method is called with a `collectionId` to retrieve the corresponding collection object. The response includes the collection's metadata, such as its ID, name, and description, as well as any custom properties defined for the collection (in this case, `author` and `publishDate`).
594
+
595
+ ```typescript
596
+ const collectionId = 'a1b2c3d4-uuid';
597
+ const collection = await lookup.getCollection(collectionId);
598
+
599
+ // Example response
600
+ {
601
+ id: 'a1b2c3d4-uuid',
602
+ accountId: 'i9j0k1l2-uuid',
603
+ name: 'Sample Collection',
604
+ description: 'A sample collection',
605
+ countPassages: 42,
606
+ properties: [
607
+ {
608
+ name: 'author',
609
+ dataType: 'string',
610
+ description: 'Author of the document',
611
+ },
612
+ {
613
+ name: 'publishDate',
614
+ dataType: 'date',
615
+ description: 'Date when the document was published',
616
+ },
617
+ ],
618
+ }
619
+ ```
620
+
621
+ ### `listDocuments`
622
+
623
+ List documents in a collection with optional pagination, query, and ordering.
624
+
625
+ #### Params
626
+
627
+ - `collectionId`: `string` - The ID of the collection to retrieve documents from.
628
+ - `find`: `Find` (optional) - Optional find parameters.
629
+
630
+ * `query`: `string` (optional) - The search query to filter documents based on their name and/or description.
631
+ * `mode`: `SearchMode` (optional) - The search mode, either 'bm25' for full-text search or 'vector' for vector similarity search. Default is 'bm25'
632
+ * `size`: `number` (optional) - The number of documents to return per request. Defaults to 10.
633
+ * `from`: `number` (optional) - The starting index for fetching the documents, used for pagination.
634
+ * `orderProperty`: `string` (optional) - The property by which to order the documents, e.g., 'name' or 'createdAt'.
635
+ * `orderDirection`: `OrderDirection` (optional) - The direction to order the documents, either 'asc' for ascending or 'desc' for descending.
636
+
637
+ #### Returns
638
+
639
+ - An array of documents with pagination info.
640
+
641
+ #### Example
642
+
643
+ ```typescript
644
+ const collectionId = 'a1b2c3d4-uuid';
645
+ const documents = await lookup.listDocuments(collectionId, {
646
+ query: 'search query',
647
+ mode: 'bm25',
648
+ size: 10,
649
+ from: 0,
650
+ orderProperty: 'name',
651
+ orderDirection: 'asc',
652
+ });
653
+
654
+ // Example response
655
+ {
656
+ "items": [
657
+ {
658
+ "id": "e5f6g7h8-uuid",
659
+ "accountId": "i9j0k1l2-uuid",
660
+ "collection": "a1b2c3d4-uuid",
661
+ "name": "Sample Document",
662
+ "description": "A sample document",
663
+ "createdAt": "2022-01-02T03:04:05Z"
664
+ }
665
+ ],
666
+ "total": 1
667
+ }
668
+ ```
669
+
670
+ ### `listCollections`
671
+
672
+ List collections with optional pagination, ordering, and query.
673
+
674
+ #### Params
675
+
676
+ - `find`: `Find` - Optional find parameters.
677
+
678
+ * `query`: `string` (optional) - The search query to filter collections based on their name and/or description.
679
+ * `mode`: `SearchMode` (optional) - The search mode, either 'bm25' for full-text search or 'vector' for vector similarity search. Default is 'bm25'
680
+ * `size`: `number` (optional) - The number of collections to return per request. Defaults to 10.
681
+ * `from`: `number` (optional) - The starting index for fetching the collections, used for pagination.
682
+ * `orderProperty`: `string` (optional) - The property by which to order the collections, e.g., 'name' or 'createdAt'.
683
+ * `orderDirection`: `OrderDirection` (optional) - The direction to order the collections, either 'asc' for ascending or 'desc' for descending.
684
+
685
+ #### Returns
686
+
687
+ - An array of collections with pagination info.
688
+
689
+ #### Example
690
+
691
+ ```typescript
692
+ const collections = await lookup.listCollections({
693
+ query: 'search query',
694
+ mode: 'bm25',
695
+ size: 10,
696
+ from: 0,
697
+ orderProperty: 'name',
698
+ orderDirection: 'asc',
699
+ });
700
+
701
+ // Example response
702
+ {
703
+ "items": [
704
+ {
705
+ "id": "a1b2c3d4-uuid",
706
+ "accountId": "i9j0k1l2-uuid",
707
+ "name": "Sample Collection",
708
+ "description": "A sample collection",
709
+ "createdAt": "2022-01-02T03:04:05Z"
710
+ }
711
+ ],
712
+ "total": 1
713
+ }
714
+ ```
715
+
716
+ ### `deleteDocument`
717
+
718
+ Delete a document from a collection by its ID. This will remove the document and all its associated passages from the collection, and the removed data will no longer be searchable.
719
+
720
+ #### Params
721
+ - collectionId: `string` - The ID of the collection containing the document
722
+ - documentId: `string` - The ID of the document to delete
723
+
724
+ #### Example
725
+
726
+ ```typescript
727
+ const collectionId = 'a1b2c3d4-uuid';
728
+ const documentId = 'e5f6g7h8-uuid';
729
+ await lookup.deleteDocument(collectionId, documentId);
730
+
731
+ // No direct response, the document is deleted from the collection
732
+ ```
733
+
734
+ **Note:** Deleting a document is a permanent operation, and the removed data cannot be recovered. Be cautious when using this method and ensure you have backups of your data if necessary.
735
+
736
+ ### `createPassage`
737
+
738
+ Create a passage and add it to a specific document within a collection. A passage is an atomic piece of data that represents a portion of a document. It is the smallest searchable unit within a collection.
739
+
740
+ #### Params
741
+ - collectionId: `string` - The ID of the collection the document belongs to
742
+ - params: `CreatePassage` - An object containing parameters required to create a passage
743
+ - `content` (string): The content of the passage to be created
744
+ - `documentId` (string): The ID of the document the passage will be associated with
745
+ - `property: value` (optional): Custom properties of the passage. Note that these properties should be listed in the collection properties
746
+
747
+ #### Example
748
+
749
+ ```typescript
750
+ const collectionId = 'a1b2c3d4-uuid';
751
+ const params: CreatePassage = {
752
+ content: 'This is the content of the passage.',
753
+ documentId: 'e5f6g7h8-uuid',
754
+ };
755
+ const passage = await lookup.createPassage(collectionId, params);
756
+
757
+ // Example response
758
+ {
759
+ id: 'x1y2z3w4-uuid',
760
+ content: 'This is the content of the passage.',
761
+ // ...more properties
762
+ }
763
+ ```
764
+
765
+ **Note:** When creating a passage, you can also include additional custom properties if they are defined in the collection schema. These custom properties can later be used for filtering, sorting, and selecting specific properties when performing search or ask operations.
766
+
767
+ ### `createManyPassages`
768
+
769
+ Create a batch of passages and add it to a specific document within a collection. A passage is an atomic piece of data that represents a portion of a document. It is the smallest searchable unit within a collection.
770
+
771
+ #### Params
772
+ - collectionId: `string` - The ID of the collection the document belongs to
773
+ - passages: `CreatePassage[]` - An array of objects containing parameters required to create each passage
774
+ - `content` (string): The content of the passage to be created
775
+ - `documentId` (string): The ID of the document the passage will be associated with
776
+ - `property: value` (optional): Custom properties of the passage. Note that these properties should be listed in the collection properties
777
+
778
+ #### Example
779
+
780
+ ```typescript
781
+ const collectionId = 'a1b2c3d4-uuid';
782
+ const passages: CreatePassage[] = [
783
+ {
784
+ content: 'This is the content of the first passage.',
785
+ documentId: 'e5f6g7h8-uuid',
786
+ },
787
+ {
788
+ content: 'This is the content of the second passage.',
789
+ documentId: 'e5f6g7h8-uuid',
790
+ },
791
+ ];
792
+ const passage = await lookup.createPassage(collectionId, params);
793
+
794
+ // No direct response, the batch of passages are added to the collection
795
+ ```
796
+
797
+ **Note:** The number of the passages are not limited but the total amount of the data to create in a single batch is limited to 1MB.
798
+
799
+ ### `listPassages`
800
+
801
+ List all passages in a collection with optional pagination, search query filtering, and search mode. This method allows you to retrieve passages from a collection and optionally filter them based on a search query, page size, and offset.
802
+
803
+ #### Params
804
+ - `collectionId`: `string` - The ID of the collection to retrieve passages from.
805
+ - `params`: `FindPassages` (optional) - An object containing the following optional properties:
806
+ * `query`: `string` (optional): Search query for filtering passages.
807
+ * `from`: `number` (optional): Pagination offset (the index of the first item on the page).
808
+ * `size`: `number` (optional): Maximum number of passages to retrieve (page size).
809
+ * `mode`: `SearchMode` (optional) - The search mode, either 'bm25' for full-text search or 'vector' for vector similarity search. Default is 'bm25'
810
+ * `where`: `Record<string, unknown>` (optional): A weaviate where filter. See [Weaviate Filters](https://www.semi.technology/developers/weaviate/current/graphql-references/filters.html) for more information.
811
+
812
+ #### Example
813
+ Listing passages with pagination, query filter, and a specific search mode:
814
+
815
+ ```typescript
816
+ const collectionId = 'a1b2c3d4-uuid';
817
+ const params: FindPassages = {
818
+ query: 'sample query',
819
+ from: 0,
820
+ size: 10,
821
+ mode: 'vector',
822
+ };
823
+ const passages = await lookup.listPassages(collectionId, params);
824
+
825
+ // Example response
826
+ {
827
+ items: [
828
+ {
829
+ id: 'x1y2z3w4-uuid',
830
+ content: 'This is the content of the passage.',
831
+ // ...other properties
832
+ },
833
+ // ...more passages
834
+ ],
835
+ total: 123
836
+ }
837
+ ```
838
+ This will return a list of passages from the specified collection that match the filter query and are ordered according to their vector similarity. It also supports a 'where' query for advanced filtering.
839
+
840
+ ### `listPassagesInDocument`
841
+
842
+ List all passages in a document with optional pagination, query, and ordering.
843
+
844
+ #### Params
845
+
846
+ - `collectionId`: `string` - The ID of the collection containing the document.
847
+ - `documentId`: `string` - The ID of the document to retrieve passages from.
848
+ - `find`: `Find` (optional) - Optional find parameters.
849
+
850
+ * `query`: `string` (optional) - The search query to filter passages based on their content.
851
+ * `mode`: `SearchMode` (optional) - The search mode, either 'bm25' for full-text search or 'vector' for vector similarity search. Default is 'bm25'
852
+ * `size`: `number` (optional) - The number of passages to return per request. Defaults to 10.
853
+ * `from`: `number` (optional) - The starting index for fetching the passages, used for pagination.
854
+ * `orderProperty`: `string` (optional) - The property by which to order the passages, e.g., 'createdAt'.
855
+ * `orderDirection`: `OrderDirection` (optional) - The direction to order the passages, either 'asc' for ascending or 'desc' for descending.
856
+
857
+ #### Returns
858
+
859
+ - An array of passages with pagination info.
860
+
861
+ #### Example
862
+
863
+ ```typescript
864
+ const collectionId = 'a1b2c3d4-uuid';
865
+ const documentId = 'e5f6g7h8-uuid';
866
+ const passages = await lookup.listPassagesInDocument(collectionId, documentId, {
867
+ query: 'search query',
868
+ mode: 'bm25',
869
+ size: 10,
870
+ from: 0,
871
+ orderProperty: 'createdAt',
872
+ orderDirection: 'asc',
873
+ });
874
+
875
+ // Example response
876
+ {
877
+ "items": [
878
+ {
879
+ "id": "m3n4o5p6-uuid",
880
+ "accountId": "i9j0k1l2-uuid",
881
+ "collection": "a1b2c3d4-uuid",
882
+ "documentId": "e5f6g7h8-uuid",
883
+ "content": "This is a sample passage in the document.",
884
+ "createdAt": "2022-01-02T03:04:05Z"
885
+ }
886
+ ],
887
+ "total": 1
888
+ }
889
+ ```
890
+
891
+ ### `getPassage`
892
+
893
+ Retrieve a single passage from a collection by its ID. The passage object will include any custom properties specified in the collection schema.
894
+
895
+ #### Params
896
+ - collectionId: `string` - The ID of the collection the passage belongs to
897
+ - passageId: `string` - The ID of the passage to retrieve
898
+
899
+ #### Example
900
+
901
+ ```typescript
902
+ const collectionId = 'a1b2c3d4-uuid';
903
+ const passageId = 'x1y2z3w4-uuid';
904
+ const passage = await lookup.getPassage(collectionId, passageId);
905
+
906
+ // Example response
907
+ {
908
+ id: 'x1y2z3w4-uuid',
909
+ content: 'This is the content of the passage.',
910
+ // ...more properties based on the collection schema
911
+ }
912
+ ```
913
+
914
+ **Returns:**
915
+
916
+ A `Passage` object representing the retrieved passage:
917
+
918
+ - `id` (string): The ID of the passage
919
+ - `content` (string): The content of the passage
920
+ - The rest of the properties specified in the collection schema
921
+
922
+ ### `updatePassage`
923
+
924
+ Update a passage in a collection. This method allows you to modify properties of a specific passage, including both content and custom properties.
925
+
926
+ #### Params
927
+ - collectionId: `string` - The ID of the collection the passage belongs to
928
+ - passageId: `string` - The ID of the passage to update
929
+ - params: `UpdatePassage` - The update passage parameters, which can include:
930
+ - `content` (string): Updated content of the passage
931
+ - You can also include additional properties to update if they exist in the collection schema and have a `text` datatype
932
+
933
+ #### Example
934
+
935
+ Updating a passage with new content and a custom property
936
+
937
+ ```typescript
938
+ const collectionId = 'a1b2c3d4-uuid';
939
+ const passageId = 'x1y2z3w4-uuid';
940
+ const params: UpdatePassage = {
941
+ content: 'Updated content of the passage',
942
+ customProperty: 'Updated custom property value',
943
+ };
944
+ const updatedPassage = await lookup.updatePassage(collectionId, passageId, params);
945
+
946
+ // Example response
947
+ {
948
+ id: 'x1y2z3w4-uuid',
949
+ content: 'Updated content of the passage',
950
+ customProperty: 'Updated custom property value',
951
+ // ...more properties if available
952
+ }
953
+ ```
954
+
955
+ **Returns:**
956
+
957
+ A `Passage` object representing the updated passage:
958
+
959
+ - `id` (string): The ID of the updated passage
960
+ - The updated properties, including the new content and any other properties specified in the update parameters
961
+
962
+ **Note** that updating `text` datatype properties will update the passage vector.
963
+
964
+ ### `deletePassage`
965
+
966
+ Delete a passage from a collection by its ID. This method removes a specific passage from the given collection, based on the passage's ID.
967
+
968
+ #### Params
969
+ - collectionId: `string` - The ID of the collection the passage belongs to
970
+ - passageId: `string` - The ID of the passage to delete
971
+
972
+ #### Example
973
+ In this example, we provide the `collectionId` and `passageId` as input parameters to the `deletePassage` method. The specified passage will be deleted from the collection, with no direct response returned.
974
+
975
+ ```typescript
976
+ const collectionId = 'a1b2c3d4-uuid';
977
+ const passageId = 'x1y2z3w4-uuid';
978
+ await lookup.deletePassage(collectionId, passageId);
979
+
980
+ // There's no direct response, the passage is deleted from the collection
981
+ ```
982
+
983
+ **Note:** Deleting a passage is a permanent operation, and the removed data cannot be recovered. Be cautious when using this method and ensure you have backups of your data if necessary.