Skip to content

Commit

Permalink
feat(apify): support Document[] return type for mapping function (lan…
Browse files Browse the repository at this point in the history
…gchain-ai#3262)

* feat(apify): support Document[] for mapping function

* misc: make it backwards compatible
  • Loading branch information
omikader authored Nov 17, 2023
1 parent da38143 commit 02f6662
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions langchain/src/document_loaders/web/apify_dataset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@ import { getEnvironmentVariable } from "../../util/env.js";
/**
* A type that represents a function that takes a single object (an Apify
* dataset item) and converts it to an instance of the Document class.
*
* Change function signature to only be asynchronous for simplicity in v0.1.0
* https://github.com/langchain-ai/langchainjs/pull/3262
*/
export type ApifyDatasetMappingFunction<Metadata extends Record<string, any>> =
(
item: Record<string | number, unknown>
) => Document<Metadata> | Promise<Document<Metadata>>;
) =>
| Document<Metadata>
| Array<Document<Metadata>>
| Promise<Document<Metadata> | Array<Document<Metadata>>>;

export interface ApifyDatasetLoaderConfig<Metadata extends Record<string, any>>
extends AsyncCallerParams {
Expand Down Expand Up @@ -66,15 +72,17 @@ export class ApifyDatasetLoader<Metadata extends Record<string, any>>
* @returns An array of Document instances.
*/
async load(): Promise<Document<Metadata>[]> {
const datasetItems = (
await this.apifyClient.dataset(this.datasetId).listItems({ clean: true })
).items;
const dataset = await this.apifyClient
.dataset(this.datasetId)
.listItems({ clean: true });

return await Promise.all(
datasetItems.map((item) =>
const documentList = await Promise.all(
dataset.items.map((item) =>
this.caller.call(async () => this.datasetMappingFunction(item))
)
);

return documentList.flat();
}

/**
Expand Down

0 comments on commit 02f6662

Please sign in to comment.