repo_name
string
dataset
string
owner
string
lang
string
func_name
string
code
string
docstring
string
url
string
sha
string
project-lakechain
github_2023
awslabs
typescript
LlamaTextProcessorBuilder.withModelParameters
public withModelParameters(parameters: ModelParameters) { this.middlewareProps.modelParameters = parameters; return (this); }
/** * Sets the parameters to pass to the text model. * @param parameters the parameters to pass to the text model. * @default {} * @returns the current builder instance. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/meta/index.ts#L94-L97
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
LlamaTextProcessorBuilder.withSystemPrompt
public withSystemPrompt(prompt: string) { this.middlewareProps.systemPrompt = prompt; return (this); }
/** * Sets the system prompt to use for generating text. * @param prompt the system prompt to use for generating text. * @returns the current builder instance. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/meta/index.ts#L104-L107
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
LlamaTextProcessorBuilder.withPrompt
public withPrompt(prompt: string | r.IReference<any>) { let reference = null; if (typeof prompt === 'string') { reference = r.reference(r.value(prompt)); } else { reference = prompt; } this.middlewareProps.prompt = reference; return (this); }
/** * Sets the prompt to use for generating text. * @param prompt the prompt to use for generating text. * @returns the current builder instance. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/meta/index.ts#L114-L125
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
LlamaTextProcessorBuilder.withRegion
public withRegion(region: string) { this.middlewareProps.region = region; return (this); }
/** * Sets the AWS region in which the model * will be invoked. * @param region the AWS region in which the model * will be invoked. * @returns the current builder instance. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/meta/index.ts#L134-L137
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
LlamaTextProcessorBuilder.build
public build(): LlamaTextProcessor { return (new LlamaTextProcessor( this.scope, this.identifier, { ...this.middlewareProps as LlamaTextProcessorProps, ...this.props } )); }
/** * @returns a new instance of the `LlamaTextProcessor` * service constructed with the given parameters. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/meta/index.ts#L143-L151
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
LlamaTextProcessor.constructor
constructor(scope: Construct, id: string, private props: LlamaTextProcessorProps) { super(scope, id, description, { ...props, queueVisibilityTimeout: cdk.Duration.seconds( 2 * PROCESSING_TIMEOUT.toSeconds() ) }); // Validate the properties. this.props = this.parse(LlamaTextProcessorPropsSchema, props); /////////////////////////////////////////// //////// Processing Storage /////// /////////////////////////////////////////// this.storage = new CacheStorage(this, 'Storage', { encryptionKey: props.kmsKey }); /////////////////////////////////////////// ////////// Prompt Handler ///////// /////////////////////////////////////////// // If the given prompt is a static value, and it is bigger than a certain // threshold, we upload the prompt to the internal storage and reference it // in the lambda environment. if (this.props.prompt.subject.type === 'value' && this.props.prompt.subject.value.length > 3072) { // Upload the prompt as a document in the internal storage. new s3deploy.BucketDeployment(this, 'Prompt', { sources: [s3deploy.Source.data('prompt.txt', this.props.prompt.subject.value)], destinationBucket: this.storage.getBucket() }); this.props.prompt = r.reference(r.url(`s3://${this.storage.getBucket().bucketName}/prompt.txt`)); } /////////////////////////////////////////// ////// Middleware Event Handler //// /////////////////////////////////////////// this.eventProcessor = new node.NodejsFunction(this, 'Compute', { description: 'Generates text using Llama models on Amazon Bedrock.', entry: path.resolve(__dirname, 'lambdas', 'handler', 'index.js'), vpc: this.props.vpc, memorySize: this.props.maxMemorySize ?? DEFAULT_MEMORY_SIZE, timeout: PROCESSING_TIMEOUT, runtime: EXECUTION_RUNTIME, architecture: lambda.Architecture.ARM_64, tracing: lambda.Tracing.ACTIVE, environmentEncryption: this.props.kmsKey, logGroup: this.logGroup, insightsVersion: this.props.cloudWatchInsights ? LAMBDA_INSIGHTS_VERSION : undefined, environment: { POWERTOOLS_SERVICE_NAME: description.name, POWERTOOLS_METRICS_NAMESPACE: NAMESPACE, SNS_TARGET_TOPIC: this.eventBus.topicArn, PROCESSED_FILES_BUCKET: this.storage.id(), MODEL_ID: this.props.model.name, SYSTEM_PROMPT: this.props.systemPrompt ?? '', PROMPT: JSON.stringify(this.props.prompt), MODEL_PARAMETERS: JSON.stringify(this.props.modelParameters), BEDROCK_REGION: this.props.region ?? cdk.Aws.REGION, OVERFLOW_STRATEGY: this.props.overflowStrategy }, bundling: { minify: true, externalModules: [ '@aws-sdk/client-s3', '@aws-sdk/client-sns' ] } }); // Allows this construct to act as a `IGrantable` // for other middlewares to grant the processing // lambda permissions to access their resources. this.grantPrincipal = this.eventProcessor.grantPrincipal; // Plug the SQS queue into the lambda function. this.eventProcessor.addEventSource(new sources.SqsEventSource(this.eventQueue, { batchSize: this.props.batchSize ?? 1, maxConcurrency: 2, reportBatchItemFailures: true })); // Allow access to the Bedrock API. this.eventProcessor.addToRolePolicy(new iam.PolicyStatement({ effect: iam.Effect.ALLOW, actions: [ 'bedrock:InvokeModel' ], resources: [ `arn:${cdk.Aws.PARTITION}:bedrock:${this.props.region ?? cdk.Aws.REGION}::foundation-model/${this.props.model.name}`, ] })); // Grant the compute type permissions to // write to the post-processing storage. this.storage.grantWrite(this.grantPrincipal); // Grant the compute type permissions to // publish to the SNS topic. this.eventBus.grantPublish(this.grantPrincipal); super.bind(); }
/** * Construct constructor. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/meta/index.ts#L178-L285
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
LlamaTextProcessor.grantReadProcessedDocuments
grantReadProcessedDocuments(grantee: iam.IGrantable): iam.Grant { return (this.storage.grantRead(grantee)); }
/** * Allows a grantee to read from the processed documents * generated by this middleware. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/meta/index.ts#L291-L293
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
LlamaTextProcessor.supportedInputTypes
supportedInputTypes(): string[] { return ([ 'text/plain', 'text/markdown', 'text/csv', 'text/html', 'application/x-subrip', 'text/vtt', 'application/json', 'application/xml', 'application/json+scheduler' ]); }
/** * @returns an array of mime-types supported as input * type by this middleware. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/meta/index.ts#L299-L311
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
LlamaTextProcessor.supportedOutputTypes
supportedOutputTypes(): string[] { return ([ 'text/plain' ]); }
/** * @returns an array of mime-types supported as output * type by the data producer. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/meta/index.ts#L317-L321
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
LlamaTextProcessor.supportedComputeTypes
supportedComputeTypes(): ComputeType[] { return ([ ComputeType.CPU ]); }
/** * @returns the supported compute types by a given * middleware. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/meta/index.ts#L327-L331
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
LlamaTextProcessor.conditional
conditional() { return (super .conditional() .and(when('type').equals('document-created')) ); }
/** * @returns the middleware conditional statement defining * in which conditions this middleware should be executed. * In this case, we want the middleware to only be invoked * when the document mime-type is supported, and the event * type is `document-created`. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/meta/index.ts#L340-L345
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
LlamaModel.of
public static of(props: LlamaModelProps) { return (new LlamaModel(props)); }
/** * Create a new instance of the `LlamaModel` * by name. * @param props the properties of the model. * @returns a new instance of the `LlamaModel`. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/meta/definitions/model.ts#L84-L86
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
LlamaModel.constructor
constructor(props: LlamaModelProps) { this.name = props.name; }
/** * Create a new instance of the `LlamaModel`. * @param props the properties of the model. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/meta/definitions/model.ts#L92-L94
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
Lambda.getContent
private async getContent(event: CloudEvent) { const content = [{ text: (await event.resolve(USER_PROMPT)).toString('utf-8') }]; // Add the document to the prompt. const document = event.data().document(); const text = (await document.data().asBuffer()).toString('utf-8'); content.push({ text: `<document>\n${text}\n</document>` }); return (content); }
/** * Creates the content array to pass to the model. * @param events the events to create a prompt for. * @returns a promise to an array of messages to pass to the model. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/meta/lambdas/handler/index.ts#L72-L85
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
Lambda.transform
private async transform(event: CloudEvent): Promise<Buffer> { const messages: Message[] = [{ role: 'user', content: await this.getContent(event) }]; // Request to the model. const request: ConverseCommandInput = { modelId: MODEL_ID, messages, inferenceConfig: MODEL_PARAMETERS }; // Add the system prompt if available. if (SYSTEM_PROMPT) { request.system = [{ text: SYSTEM_PROMPT }]; } // Invoke the model. const response = await bedrock.send(new ConverseCommand(request)); // Return the generated text. return (Buffer.from( response.output?.message?.content?.[0].text! )); }
/** * Transforms the document using the given parameters. * @param event the CloudEvent to process. * @returns a promise to a buffer containing the transformed document. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/meta/lambdas/handler/index.ts#L92-L119
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
Lambda.recordHandler
async recordHandler(record: SQSRecord): Promise<CloudEvent> { return (await this.processEvent( CloudEvent.from(record.body) )); }
/** * @param record the SQS record to process that contains * the EventBridge event providing information about the * S3 event. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/meta/lambdas/handler/index.ts#L153-L157
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
Lambda.handler
public async handler(event: SQSEvent, _: Context) { return (await processPartialResponse( event, this.recordHandler.bind(this), processor )); }
// eslint-disable-next-line @typescript-eslint/no-unused-vars
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/meta/lambdas/handler/index.ts#L167-L171
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
MistralTextProcessorBuilder.withModel
public withModel(model: MistralTextModel) { this.middlewareProps.model = model; return (this); }
/** * Sets the Mistral model to use for generating text. * @param model the Mistral text model to use. * @returns the current builder instance. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/mistral/index.ts#L83-L86
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
MistralTextProcessorBuilder.withModelParameters
public withModelParameters(parameters: ModelParameters) { this.middlewareProps.modelParameters = parameters; return (this); }
/** * Sets the parameters to pass to the text model. * @param parameters the parameters to pass to the text model. * @default {} * @returns the current builder instance. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/mistral/index.ts#L94-L97
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
MistralTextProcessorBuilder.withSystemPrompt
public withSystemPrompt(prompt: string) { this.middlewareProps.systemPrompt = prompt; return (this); }
/** * Sets the system prompt to use for generating text. * @param prompt the system prompt to use for generating text. * @returns the current builder instance. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/mistral/index.ts#L104-L107
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
MistralTextProcessorBuilder.withPrompt
public withPrompt(prompt: string | r.IReference<any>) { let reference = null; if (typeof prompt === 'string') { reference = r.reference(r.value(prompt)); } else { reference = prompt; } this.middlewareProps.prompt = reference; return (this); }
/** * Sets the prompt to use for generating text. * @param prompt the prompt to use for generating text. * @returns the current builder instance. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/mistral/index.ts#L114-L125
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
MistralTextProcessorBuilder.withRegion
public withRegion(region: string) { this.middlewareProps.region = region; return (this); }
/** * Sets the AWS region in which the model * will be invoked. * @param region the AWS region in which the model * will be invoked. * @returns the current builder instance. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/mistral/index.ts#L134-L137
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
MistralTextProcessorBuilder.build
public build(): MistralTextProcessor { return (new MistralTextProcessor( this.scope, this.identifier, { ...this.middlewareProps as MistralTextProcessorProps, ...this.props } )); }
/** * @returns a new instance of the `MistralTextProcessor` * service constructed with the given parameters. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/mistral/index.ts#L143-L151
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
MistralTextProcessor.constructor
constructor(scope: Construct, id: string, private props: MistralTextProcessorProps) { super(scope, id, description, { ...props, queueVisibilityTimeout: cdk.Duration.seconds( 2 * PROCESSING_TIMEOUT.toSeconds() ) }); // Validate the properties. this.props = this.parse(MistralTextProcessorPropsSchema, props); /////////////////////////////////////////// //////// Processing Storage /////// /////////////////////////////////////////// this.storage = new CacheStorage(this, 'Storage', { encryptionKey: props.kmsKey }); /////////////////////////////////////////// ////////// Prompt Handler ///////// /////////////////////////////////////////// // If the given prompt is a static value, and it is bigger than a certain // threshold, we upload the prompt to the internal storage and reference it // in the lambda environment. if (this.props.prompt.subject.type === 'value' && this.props.prompt.subject.value.length > 3072) { // Upload the prompt as a document in the internal storage. new s3deploy.BucketDeployment(this, 'Prompt', { sources: [s3deploy.Source.data('prompt.txt', this.props.prompt.subject.value)], destinationBucket: this.storage.getBucket() }); this.props.prompt = r.reference(r.url(`s3://${this.storage.getBucket().bucketName}/prompt.txt`)); } /////////////////////////////////////////// ////// Middleware Event Handler //// /////////////////////////////////////////// this.eventProcessor = new node.NodejsFunction(this, 'Compute', { description: 'Generates text using Mistral models on Amazon Bedrock.', entry: path.resolve(__dirname, 'lambdas', 'handler', 'index.js'), vpc: this.props.vpc, memorySize: this.props.maxMemorySize ?? DEFAULT_MEMORY_SIZE, timeout: PROCESSING_TIMEOUT, runtime: EXECUTION_RUNTIME, architecture: lambda.Architecture.ARM_64, tracing: lambda.Tracing.ACTIVE, environmentEncryption: this.props.kmsKey, logGroup: this.logGroup, insightsVersion: this.props.cloudWatchInsights ? LAMBDA_INSIGHTS_VERSION : undefined, environment: { POWERTOOLS_SERVICE_NAME: description.name, POWERTOOLS_METRICS_NAMESPACE: NAMESPACE, SNS_TARGET_TOPIC: this.eventBus.topicArn, PROCESSED_FILES_BUCKET: this.storage.id(), MODEL_ID: this.props.model.name, SYSTEM_PROMPT: this.props.systemPrompt ?? '', PROMPT: JSON.stringify(this.props.prompt), MODEL_PARAMETERS: JSON.stringify(this.props.modelParameters), BEDROCK_REGION: this.props.region ?? cdk.Aws.REGION, OVERFLOW_STRATEGY: this.props.overflowStrategy }, bundling: { minify: true, externalModules: [ '@aws-sdk/client-s3', '@aws-sdk/client-sns' ] } }); // Allows this construct to act as a `IGrantable` // for other middlewares to grant the processing // lambda permissions to access their resources. this.grantPrincipal = this.eventProcessor.grantPrincipal; // Plug the SQS queue into the lambda function. this.eventProcessor.addEventSource(new sources.SqsEventSource(this.eventQueue, { batchSize: this.props.batchSize ?? 1, maxConcurrency: 2, reportBatchItemFailures: true })); // Allow access to the Bedrock API. this.eventProcessor.addToRolePolicy(new iam.PolicyStatement({ effect: iam.Effect.ALLOW, actions: [ 'bedrock:InvokeModel' ], resources: [ `arn:${cdk.Aws.PARTITION}:bedrock:${this.props.region ?? cdk.Aws.REGION}::foundation-model/${this.props.model.name}`, ] })); // Grant the compute type permissions to // write to the post-processing storage. this.storage.grantReadWrite(this.grantPrincipal); // Grant the compute type permissions to // publish to the SNS topic. this.eventBus.grantPublish(this.grantPrincipal); super.bind(); }
/** * Construct constructor. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/mistral/index.ts#L178-L285
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
MistralTextProcessor.grantReadProcessedDocuments
grantReadProcessedDocuments(grantee: iam.IGrantable): iam.Grant { return (this.storage.grantRead(grantee)); }
/** * Allows a grantee to read from the processed documents * generated by this middleware. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/mistral/index.ts#L291-L293
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
MistralTextProcessor.supportedInputTypes
supportedInputTypes(): string[] { return (this.props.model.inputs); }
/** * @returns an array of mime-types supported as input * type by this middleware. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/mistral/index.ts#L299-L301
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
MistralTextProcessor.supportedOutputTypes
supportedOutputTypes(): string[] { return (this.props.model.outputs); }
/** * @returns an array of mime-types supported as output * type by the data producer. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/mistral/index.ts#L307-L309
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
MistralTextProcessor.supportedComputeTypes
supportedComputeTypes(): ComputeType[] { return ([ ComputeType.CPU ]); }
/** * @returns the supported compute types by a given * middleware. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/mistral/index.ts#L315-L319
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
MistralTextProcessor.conditional
conditional() { return (super .conditional() .and(when('type').equals('document-created')) ); }
/** * @returns the middleware conditional statement defining * in which conditions this middleware should be executed. * In this case, we want the middleware to only be invoked * when the document mime-type is supported, and the event * type is `document-created`. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/mistral/index.ts#L328-L333
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
MistralTextModel.of
public static of(props: MistralTextModelProps) { return (new MistralTextModel(props)); }
/** * Create a new instance of the `MistralTextModel` * by name. * @param props the properties of the model. * @returns a new instance of the `MistralTextModel`. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/mistral/definitions/model.ts#L139-L141
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
MistralTextModel.constructor
constructor(props: MistralTextModelProps) { this.name = props.name; this.inputs = props.inputs; this.outputs = props.outputs; }
/** * Constructs a new instance of the `MistralTextModel`. * @param props the properties of the model. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/mistral/definitions/model.ts#L147-L151
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
Lambda.getContent
private async getContent(event: CloudEvent) { const content = [{ text: (await event.resolve(USER_PROMPT)).toString('utf-8') }]; // Add the document to the prompt. const document = event.data().document(); const text = (await document.data().asBuffer()).toString('utf-8'); content.push({ text: `<document>\n${text}\n</document>` }); return (content); }
/** * Creates the content array to pass to the model. * @param events the events to create a prompt for. * @returns a promise to an array of messages to pass to the model. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/mistral/lambdas/handler/index.ts#L72-L85
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
Lambda.transform
private async transform(event: CloudEvent): Promise<Buffer> { const messages: Message[] = [{ role: 'user', content: await this.getContent(event) }]; // Request to the model. const request: ConverseCommandInput = { modelId: MODEL_ID, messages, inferenceConfig: MODEL_PARAMETERS }; // Add the system prompt if available. if (SYSTEM_PROMPT) { request.system = [{ text: SYSTEM_PROMPT }]; } // Invoke the model. const response = await bedrock.send(new ConverseCommand(request)); // Return the generated text. return (Buffer.from( response.output?.message?.content?.[0].text! )); }
/** * Transforms the document using the given parameters. * @param event the CloudEvent to process. * @returns a promise to a buffer containing the transformed document. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/mistral/lambdas/handler/index.ts#L92-L119
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
Lambda.recordHandler
async recordHandler(record: SQSRecord): Promise<CloudEvent> { return (await this.processEvent( CloudEvent.from(record.body) )); }
/** * @param record the SQS record to process that contains * the EventBridge event providing information about the * S3 event. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/mistral/lambdas/handler/index.ts#L153-L157
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
Lambda.handler
public async handler(event: SQSEvent, _: Context) { return (await processPartialResponse( event, this.recordHandler.bind(this), processor )); }
// eslint-disable-next-line @typescript-eslint/no-unused-vars
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/mistral/lambdas/handler/index.ts#L167-L171
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
TitanTextProcessorBuilder.withModel
public withModel(model: TitanTextModel) { this.middlewareProps.model = model; return (this); }
/** * Sets the Titan model to use for generating text. * @param model the Titan text model to use. * @returns the current builder instance. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/titan/index.ts#L83-L86
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
TitanTextProcessorBuilder.withModelParameters
public withModelParameters(parameters: ModelParameters) { this.middlewareProps.modelParameters = parameters; return (this); }
/** * Sets the parameters to pass to the text model. * @param parameters the parameters to pass to the text model. * @default {} * @returns the current builder instance. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/titan/index.ts#L94-L97
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
TitanTextProcessorBuilder.withPrompt
public withPrompt(prompt: string | r.IReference<any>) { let reference = null; if (typeof prompt === 'string') { reference = r.reference(r.value(prompt)); } else { reference = prompt; } this.middlewareProps.prompt = reference; return (this); }
/** * Sets the prompt to use for generating text. * @param prompt the prompt to use for generating text. * @returns the current builder instance. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/titan/index.ts#L104-L115
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
TitanTextProcessorBuilder.withRegion
public withRegion(region: string) { this.middlewareProps.region = region; return (this); }
/** * Sets the AWS region in which the model * will be invoked. * @param region the AWS region in which the model * will be invoked. * @returns the current builder instance. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/titan/index.ts#L124-L127
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
TitanTextProcessorBuilder.build
public build(): TitanTextProcessor { return (new TitanTextProcessor( this.scope, this.identifier, { ...this.middlewareProps as TitanTextProcessorProps, ...this.props } )); }
/** * @returns a new instance of the `TitanTextProcessor` * service constructed with the given parameters. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/titan/index.ts#L133-L141
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
TitanTextProcessor.constructor
constructor(scope: Construct, id: string, private props: TitanTextProcessorProps) { super(scope, id, description, { ...props, queueVisibilityTimeout: cdk.Duration.seconds( 2 * PROCESSING_TIMEOUT.toSeconds() ) }); // Validate the properties. this.props = this.parse(TitanTextProcessorPropsSchema, props); /////////////////////////////////////////// //////// Processing Storage /////// /////////////////////////////////////////// this.storage = new CacheStorage(this, 'Storage', { encryptionKey: props.kmsKey }); /////////////////////////////////////////// ////////// Prompt Handler ///////// /////////////////////////////////////////// // If the given prompt is a static value, and it is bigger than a certain // threshold, we upload the prompt to the internal storage and reference it // in the lambda environment. if (this.props.prompt.subject.type === 'value' && this.props.prompt.subject.value.length > 3072) { // Upload the prompt as a document in the internal storage. new s3deploy.BucketDeployment(this, 'Prompt', { sources: [s3deploy.Source.data('prompt.txt', this.props.prompt.subject.value)], destinationBucket: this.storage.getBucket() }); this.props.prompt = r.reference(r.url(`s3://${this.storage.getBucket().bucketName}/prompt.txt`)); } /////////////////////////////////////////// ////// Middleware Event Handler //// /////////////////////////////////////////// this.eventProcessor = new node.NodejsFunction(this, 'Compute', { description: 'Generates text using Amazon Titan models on Amazon Bedrock.', entry: path.resolve(__dirname, 'lambdas', 'handler', 'index.js'), vpc: this.props.vpc, memorySize: this.props.maxMemorySize ?? DEFAULT_MEMORY_SIZE, timeout: PROCESSING_TIMEOUT, runtime: EXECUTION_RUNTIME, architecture: lambda.Architecture.ARM_64, tracing: lambda.Tracing.ACTIVE, environmentEncryption: this.props.kmsKey, logGroup: this.logGroup, insightsVersion: this.props.cloudWatchInsights ? LAMBDA_INSIGHTS_VERSION : undefined, environment: { POWERTOOLS_SERVICE_NAME: description.name, POWERTOOLS_METRICS_NAMESPACE: NAMESPACE, SNS_TARGET_TOPIC: this.eventBus.topicArn, PROCESSED_FILES_BUCKET: this.storage.id(), MODEL_ID: this.props.model.name, USER_PROMPT: JSON.stringify(this.props.prompt), MODEL_PARAMETERS: JSON.stringify(this.props.modelParameters), BEDROCK_REGION: this.props.region ?? cdk.Aws.REGION, OVERFLOW_STRATEGY: this.props.overflowStrategy }, bundling: { minify: true, externalModules: [ '@aws-sdk/client-s3', '@aws-sdk/client-sns' ] } }); // Allows this construct to act as a `IGrantable` // for other middlewares to grant the processing // lambda permissions to access their resources. this.grantPrincipal = this.eventProcessor.grantPrincipal; // Plug the SQS queue into the lambda function. this.eventProcessor.addEventSource(new sources.SqsEventSource(this.eventQueue, { batchSize: this.props.batchSize ?? 1, maxConcurrency: 2, reportBatchItemFailures: true })); // Allow access to the Bedrock API. this.eventProcessor.addToRolePolicy(new iam.PolicyStatement({ effect: iam.Effect.ALLOW, actions: [ 'bedrock:InvokeModel' ], resources: [ `arn:${cdk.Aws.PARTITION}:bedrock:${this.props.region ?? cdk.Aws.REGION}::foundation-model/${this.props.model.name}`, ] })); // Grant the compute type permissions to // write to the post-processing storage. this.storage.grantWrite(this.grantPrincipal); // Grant the compute type permissions to // publish to the SNS topic. this.eventBus.grantPublish(this.grantPrincipal); super.bind(); }
/** * Construct constructor. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/titan/index.ts#L168-L274
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
TitanTextProcessor.grantReadProcessedDocuments
grantReadProcessedDocuments(grantee: iam.IGrantable): iam.Grant { return (this.storage.grantRead(grantee)); }
/** * Allows a grantee to read from the processed documents * generated by this middleware. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/titan/index.ts#L280-L282
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
TitanTextProcessor.supportedInputTypes
supportedInputTypes(): string[] { return ([ 'text/plain', 'text/markdown', 'text/csv', 'text/html', 'application/x-subrip', 'text/vtt', 'application/json', 'application/xml', 'application/json+scheduler' ]); }
/** * @returns an array of mime-types supported as input * type by this middleware. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/titan/index.ts#L288-L300
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
TitanTextProcessor.supportedOutputTypes
supportedOutputTypes(): string[] { return ([ 'text/plain' ]); }
/** * @returns an array of mime-types supported as output * type by the data producer. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/titan/index.ts#L306-L310
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
TitanTextProcessor.supportedComputeTypes
supportedComputeTypes(): ComputeType[] { return ([ ComputeType.CPU ]); }
/** * @returns the supported compute types by a given * middleware. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/titan/index.ts#L316-L320
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
TitanTextProcessor.conditional
conditional() { return (super .conditional() .and(when('type').equals('document-created')) ); }
/** * @returns the middleware conditional statement defining * in which conditions this middleware should be executed. * In this case, we want the middleware to only be invoked * when the document mime-type is supported, and the event * type is `document-created`. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/titan/index.ts#L329-L334
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
TitanTextModel.of
public static of(props: TitanTextModelProps) { return (new TitanTextModel(props)); }
/** * Create a new instance of the `TitanTextModel` * by name. * @param props the properties of the model. * @returns a new instance of the `TitanTextModel`. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/titan/definitions/model.ts#L68-L70
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
TitanTextModel.constructor
constructor(props: TitanTextModelProps) { this.name = props.name; }
/** * Initialize the Titan text model. * @param props the properties of the model. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/titan/definitions/model.ts#L76-L78
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
Lambda.getContent
private async getContent(event: CloudEvent) { const content = [{ text: (await event.resolve(USER_PROMPT)).toString('utf-8') }]; // Add the document to the prompt. const document = event.data().document(); const text = (await document.data().asBuffer()).toString('utf-8'); content.push({ text: `<document>\n${text}\n</document>` }); return (content); }
/** * Creates the content array to pass to the model. * @param events the events to create a prompt for. * @returns a promise to an array of messages to pass to the model. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/titan/lambdas/handler/index.ts#L70-L83
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
Lambda.transform
private async transform(event: CloudEvent): Promise<Buffer> { const messages: Message[] = [{ role: 'user', content: await this.getContent(event) }]; // Invoke the model. const response = await bedrock.send(new ConverseCommand({ modelId: MODEL_ID, messages, inferenceConfig: MODEL_PARAMETERS })); // Return the generated text. return (Buffer.from( response.output?.message?.content?.[0].text! )); }
/** * Transforms the document using the given parameters. * @param event the CloudEvent to process. * @returns a promise to a buffer containing the transformed document. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/titan/lambdas/handler/index.ts#L90-L107
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
Lambda.recordHandler
async recordHandler(record: SQSRecord): Promise<CloudEvent> { return (await this.processEvent( CloudEvent.from(record.body) )); }
/** * @param record the SQS record to process that contains * the EventBridge event providing information about the * S3 event. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/titan/lambdas/handler/index.ts#L141-L145
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
Lambda.handler
public async handler(event: SQSEvent, _: Context) { return (await processPartialResponse( event, this.recordHandler.bind(this), processor )); }
// eslint-disable-next-line @typescript-eslint/no-unused-vars
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bedrock-text-processors/src/impl/titan/lambdas/handler/index.ts#L155-L159
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
BertExtractiveSummarizerBuilder.withMaxInstances
public withMaxInstances(maxInstances: number) { this.middlewareProps.maxInstances = maxInstances; return (this); }
/** * The maximum amount of instances the * cluster can have. Keep this number to * a reasonable value to avoid over-provisioning * the cluster. * @param maxInstances the maximum amount of instances. * @default 5 */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bert-extractive-summarizer/src/index.ts#L94-L97
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
BertExtractiveSummarizerBuilder.withRatio
public withRatio(ratio: number) { this.middlewareProps.ratio = ratio; return (this); }
/** * Sets the summarization ratio to use expressed * in percentage between 0.1 and 1.0. * @param ratio the summarization ratio to use. * @returns the builder instance. * @default 0.2 */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bert-extractive-summarizer/src/index.ts#L106-L109
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
BertExtractiveSummarizerBuilder.build
public build(): BertExtractiveSummarizer { const props = BertExtractiveSummarizerPropsSchema.parse({ ...this.middlewareProps, ...this.props }); return (new BertExtractiveSummarizer( this.scope, this.identifier, { ...this.middlewareProps as BertExtractiveSummarizerProps, ...props } )); }
/** * @returns a new instance of the `BertExtractiveSummarizer` * service constructed with the given parameters. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bert-extractive-summarizer/src/index.ts#L115-L128
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
BertExtractiveSummarizer.constructor
constructor(scope: Construct, id: string, private props: BertExtractiveSummarizerProps) { super(scope, id, description, { ...props, queueVisibilityTimeout: cdk.Duration.seconds( 6 * PROCESSING_TIMEOUT.toSeconds() ) }); // Validate the properties. this.props = this.parse(BertExtractiveSummarizerPropsSchema, props); /////////////////////////////////////////// ///////// Processing Storage ////// /////////////////////////////////////////// this.storage = new CacheStorage(this, 'Storage', { encryptionKey: props.kmsKey }); /////////////////////////////////////////// ////// Middleware Event Handler //// /////////////////////////////////////////// if (this.props.computeType === ComputeType.GPU) { this.createGpuImpl(); } else { this.createCpuImpl(); } // Grant the compute type permissions to // write to the post-processing bucket. this.storage.grantWrite(this.grantPrincipal); // Grant the compute type permissions to // publish to the SNS topic. this.eventBus.grantPublish(this.grantPrincipal); super.bind(); }
/** * Construct constructor. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bert-extractive-summarizer/src/index.ts#L156-L194
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
BertExtractiveSummarizer.grantReadProcessedDocuments
grantReadProcessedDocuments(grantee: iam.IGrantable): iam.Grant { return (this.storage.grantRead(grantee)); }
/** * Allows a grantee to read from the processed documents * generated by this middleware. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bert-extractive-summarizer/src/index.ts#L343-L345
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
BertExtractiveSummarizer.supportedInputTypes
supportedInputTypes(): string[] { return ([ 'text/plain' ]); }
/** * @returns an array of mime-types supported as input * type by this middleware. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bert-extractive-summarizer/src/index.ts#L351-L355
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
BertExtractiveSummarizer.supportedOutputTypes
supportedOutputTypes(): string[] { return ([ 'text/plain' ]); }
/** * @returns an array of mime-types supported as output * type by the data producer. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bert-extractive-summarizer/src/index.ts#L361-L365
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
BertExtractiveSummarizer.supportedComputeTypes
supportedComputeTypes(): ComputeType[] { return ([ ComputeType.CPU, ComputeType.GPU ]); }
/** * @returns the supported compute types by a given * middleware. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bert-extractive-summarizer/src/index.ts#L371-L376
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
BertExtractiveSummarizer.conditional
conditional() { return (super .conditional() .and(when('type').equals('document-created')) ); }
/** * @returns the middleware conditional statement defining * in which conditions this middleware should be executed. * In this case, we want the middleware to only be invoked * when the document mime-type is supported, and the event * type is `document-created`. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/bert-extractive-summarizer/src/index.ts#L385-L390
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
CharacterTextSplitterBuilder.withChunkSize
public withChunkSize(chunkSize: number) { this.providerProps.chunkSize = chunkSize; return (this); }
/** * Sets the chunk size. * @param chunkSize the chunk size to assign. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/character-text-splitter/src/index.ts#L89-L92
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
CharacterTextSplitterBuilder.withChunkOverlap
public withChunkOverlap(chunkOverlap: number) { this.providerProps.chunkOverlap = chunkOverlap; return (this); }
/** * Sets the chunk overlap. * @param chunkOverlap the chunk overlap to assign. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/character-text-splitter/src/index.ts#L98-L101
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
CharacterTextSplitterBuilder.withSeparator
public withSeparator(separator: string) { this.providerProps.separator = separator; return (this); }
/** * Sets the separator to use between chunks. * @param separator the separator to use. * @returns the builder itself. * @default `\n\n` */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/character-text-splitter/src/index.ts#L109-L112
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
CharacterTextSplitterBuilder.build
public build(): CharacterTextSplitter { return (new CharacterTextSplitter( this.scope, this.identifier, { ...this.providerProps as CharacterTextSplitterProps, ...this.props } )); }
/** * @returns a new instance of the `CharacterTextSplitter` * service constructed with the given parameters. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/character-text-splitter/src/index.ts#L118-L126
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
CharacterTextSplitter.constructor
constructor(scope: Construct, id: string, props: CharacterTextSplitterProps) { super(scope, id, description, { ...props, queueVisibilityTimeout: cdk.Duration.seconds( 6 * PROCESSING_TIMEOUT.toSeconds() ) }); // Validate the properties. props = this.parse(CharacterTextSplitterPropsSchema, props); /////////////////////////////////////////// ///////// Processing Storage ////// /////////////////////////////////////////// this.storage = new CacheStorage(this, 'Storage', { encryptionKey: props.kmsKey }); /////////////////////////////////////////// ////// Middleware Event Handler //// /////////////////////////////////////////// this.processor = new node.NodejsFunction(this, 'Compute', { description: 'Middleware splitting text documents in chunks.', entry: path.resolve(__dirname, 'lambdas', 'text-splitter', 'index.js'), vpc: props.vpc, memorySize: props.maxMemorySize ?? DEFAULT_MEMORY_SIZE, timeout: PROCESSING_TIMEOUT, runtime: EXECUTION_RUNTIME, architecture: lambda.Architecture.ARM_64, tracing: lambda.Tracing.ACTIVE, environmentEncryption: props.kmsKey, logGroup: this.logGroup, insightsVersion: props.cloudWatchInsights ? LAMBDA_INSIGHTS_VERSION : undefined, environment: { POWERTOOLS_SERVICE_NAME: description.name, POWERTOOLS_METRICS_NAMESPACE: NAMESPACE, SNS_TARGET_TOPIC: this.eventBus.topicArn, PROCESSED_FILES_BUCKET: this.storage.id(), SEPARATOR: props.separator ?? DEFAULT_SEPARATOR, CHUNK_SIZE: `${props.chunkSize ?? DEFAULT_CHUNK_SIZE}`, CHUNK_OVERLAP: `${props.chunkOverlap ?? DEFAULT_CHUNK_OVERLAP}` }, bundling: { minify: true, externalModules: [ '@aws-sdk/client-s3', '@aws-sdk/client-sns' ] } }); // Allows this construct to act as a `IGrantable` // for other middlewares to grant the processing // lambda permissions to access their resources. this.grantPrincipal = this.processor.grantPrincipal; // Plug the SQS queue into the lambda function. this.processor.addEventSource(new sources.SqsEventSource(this.eventQueue, { batchSize: props.batchSize ?? 5, reportBatchItemFailures: true })); // Function permissions. this.storage.grantWrite(this.processor); this.eventBus.grantPublish(this.processor); super.bind(); }
/** * Construct constructor. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/character-text-splitter/src/index.ts#L153-L224
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
CharacterTextSplitter.grantReadProcessedDocuments
grantReadProcessedDocuments(grantee: iam.IGrantable): iam.Grant { return (this.storage.grantRead(grantee)); }
/** * Allows a grantee to read from the processed documents * generated by this middleware. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/character-text-splitter/src/index.ts#L230-L232
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
CharacterTextSplitter.supportedInputTypes
supportedInputTypes(): string[] { return ([ 'text/plain' ]); }
/** * @returns an array of mime-types supported as input * type by this middleware. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/character-text-splitter/src/index.ts#L238-L242
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
CharacterTextSplitter.supportedOutputTypes
supportedOutputTypes(): string[] { return ([ 'text/plain' ]); }
/** * @returns an array of mime-types supported as output * type by the data producer. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/character-text-splitter/src/index.ts#L248-L252
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
CharacterTextSplitter.supportedComputeTypes
supportedComputeTypes(): ComputeType[] { return ([ ComputeType.CPU ]); }
/** * @returns the supported compute types by a given * middleware. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/character-text-splitter/src/index.ts#L258-L262
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
CharacterTextSplitter.conditional
conditional() { return (super .conditional() .and(when('type').equals('document-created')) ); }
/** * @returns the middleware conditional statement defining * in which conditions this middleware should be executed. * In this case, we want the middleware to only be invoked * when the document mime-type is supported, and the event * type is `document-created`. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/character-text-splitter/src/index.ts#L271-L276
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
Lambda.getMetadata
getMetadata(chunk: string, order: number, total: number): DocumentMetadata { return ({ properties: { kind: 'text', attrs: { chunk: { id: crypto .createHash('sha256') .update(chunk) .digest('hex'), order, total } } } }); }
/** * @param chunk the chunk of text to return metadata for. * @param order the order of the chunk. * @param total the total number of chunks in the document. * @returns the metadata for the chunk. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/character-text-splitter/src/lambdas/text-splitter/index.ts#L67-L83
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
Lambda.processEvent
async processEvent(event: CloudEvent): Promise<CloudEvent> { const document = event.data().document(); // We load the text file in memory. const text = (await document.data().asBuffer()).toString('utf-8'); // Create the text splitter. const textSplitter = new CharacterTextSplitter({ separator: SEPARATOR, chunkSize: CHUNK_SIZE, chunkOverlap: CHUNK_OVERLAP }); // Split the text into chunks. const chunks = await textSplitter.splitText(text); const total = chunks.length; // Publish each chunk as a separate document. for (const [idx, chunk] of chunks.entries()) { await this.onChunk(chunk, idx, total, event); } return (event); }
/** * @param event the event to process. * @note the next decorator will automatically forward the * returned cloud event to the next middlewares */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/character-text-splitter/src/lambdas/text-splitter/index.ts#L124-L147
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
Lambda.handler
async handler(event: SQSEvent, _: Context): Promise<SQSBatchResponse> { return (await processPartialResponse( event, (record: SQSRecord) => this.processEvent( CloudEvent.from(JSON.parse(record.body)) ), processor )); }
// eslint-disable-next-line @typescript-eslint/no-unused-vars
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/character-text-splitter/src/lambdas/text-splitter/index.ts#L156-L164
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
EmailTextProcessorBuilder.withOutputFormat
public withOutputFormat(outputFormat: OutputFormat) { this.providerProps.outputFormat = outputFormat; return (this); }
/** * Sets the desired output format. * @param outputFormat the desired output format. * @default text * @returns the builder instance. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/email-text-processor/src/index.ts#L78-L81
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
EmailTextProcessorBuilder.withIncludeAttachments
public withIncludeAttachments(includeAttachments: boolean) { this.providerProps.includeAttachments = includeAttachments; return (this); }
/** * Sets whether to include attachments. * @param includeAttachments whether to include attachments. * @default false * @returns the builder instance. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/email-text-processor/src/index.ts#L89-L92
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
EmailTextProcessorBuilder.withIncludeImageLinks
public withIncludeImageLinks(includeImageLinks: boolean) { this.providerProps.includeImageLinks = includeImageLinks; return (this); }
/** * Sets whether to include CID attachments to data URL images. * @param includeImageLinks whether to include image links. * @default false * @returns the builder instance. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/email-text-processor/src/index.ts#L100-L103
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
EmailTextProcessorBuilder.build
public build(): EmailTextProcessor { return (new EmailTextProcessor( this.scope, this.identifier, { ...this.providerProps as EmailTextProcessorProps, ...this.props } )); }
/** * @returns a new instance of the `EmailTextProcessor` * service constructed with the given parameters. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/email-text-processor/src/index.ts#L109-L117
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
EmailTextProcessor.constructor
constructor(scope: Construct, id: string, private props: EmailTextProcessorProps) { super(scope, id, description, { ...props, queueVisibilityTimeout: cdk.Duration.seconds( 6 * PROCESSING_TIMEOUT.toSeconds() ) }); // Validate the properties. this.props = this.parse(EmailTextProcessorPropsSchema, props); /////////////////////////////////////////// ///////// Processing Storage ////// /////////////////////////////////////////// this.storage = new CacheStorage(this, 'Storage', { encryptionKey: this.props.kmsKey }); /////////////////////////////////////////// ////// Middleware Event Handler //// /////////////////////////////////////////// this.processor = new node.NodejsFunction(this, 'Compute', { description: 'Middleware converting e-mail documents into different output formats.', entry: path.resolve(__dirname, 'lambdas', 'parser', 'index.js'), vpc: this.props.vpc, memorySize: this.props.maxMemorySize ?? DEFAULT_MEMORY_SIZE, timeout: PROCESSING_TIMEOUT, runtime: EXECUTION_RUNTIME, architecture: lambda.Architecture.ARM_64, tracing: lambda.Tracing.ACTIVE, environmentEncryption: this.props.kmsKey, logGroup: this.logGroup, insightsVersion: this.props.cloudWatchInsights ? LAMBDA_INSIGHTS_VERSION : undefined, environment: { POWERTOOLS_SERVICE_NAME: description.name, POWERTOOLS_METRICS_NAMESPACE: NAMESPACE, SNS_TARGET_TOPIC: this.eventBus.topicArn, PROCESSED_FILES_BUCKET: this.storage.id(), OUTPUT_FORMAT: this.props.outputFormat, INCLUDE_ATTACHMENTS: this.props.includeAttachments ? 'true' : 'false', INCLUDE_IMAGE_LINKS: this.props.includeImageLinks ? 'true' : 'false' }, bundling: { minify: true, externalModules: [ '@aws-sdk/client-s3', '@aws-sdk/client-sns', '@aws-lambda-powertools/batch', '@aws-lambda-powertools/logger', '@aws-lambda-powertools/metrics', '@aws-lambda-powertools/tracer' ] }, layers: [ PowerToolsLayer .typescript() .layer(this, 'PowerToolsLayer') ] }); // Allows this construct to act as a `IGrantable` // for other middlewares to grant the processing // lambda permissions to access their resources. this.grantPrincipal = this.processor.grantPrincipal; // Plug the SQS queue into the lambda function. this.processor.addEventSource(new sources.SqsEventSource(this.eventQueue, { batchSize: this.props.batchSize ?? 1, reportBatchItemFailures: true })); // Function permissions. this.storage.grantWrite(this.processor); this.eventBus.grantPublish(this.processor); super.bind(); }
/** * Construct constructor. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/email-text-processor/src/index.ts#L144-L224
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
EmailTextProcessor.grantReadProcessedDocuments
grantReadProcessedDocuments(grantee: iam.IGrantable): iam.Grant { return (this.storage.grantRead(grantee)); }
/** * Allows a grantee to read from the processed documents * generated by this middleware. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/email-text-processor/src/index.ts#L230-L232
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
EmailTextProcessor.supportedInputTypes
supportedInputTypes(): string[] { return ([ 'message/rfc822', 'application/vnd.ms-outlook' ]); }
/** * @returns an array of mime-types supported as input * type by this middleware. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/email-text-processor/src/index.ts#L238-L243
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
EmailTextProcessor.supportedOutputTypes
supportedOutputTypes(): string[] { if (this.props.includeAttachments) { return (['*/*']); } if (this.props.outputFormat === 'text') { return (['text/plain']); } else if (this.props.outputFormat === 'html') { return (['text/html']); } else { return (['application/json']); } }
/** * @returns an array of mime-types supported as output * type by the data producer. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/email-text-processor/src/index.ts#L249-L260
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
EmailTextProcessor.supportedComputeTypes
supportedComputeTypes(): ComputeType[] { return ([ ComputeType.CPU ]); }
/** * @returns the supported compute types by a given * middleware. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/email-text-processor/src/index.ts#L266-L270
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
EmailTextProcessor.conditional
conditional() { return (super .conditional() .and(when('type').equals('document-created')) ); }
/** * @returns the middleware conditional statement defining * in which conditions this middleware should be executed. * In this case, we want the middleware to only be invoked * when the document mime-type is supported, and the event * type is `document-created`. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/email-text-processor/src/index.ts#L279-L284
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
asJson
const asJson = (message: MessageText, headers: Headers): string => { return (JSON.stringify({ headers: Object.fromEntries(headers), text: message.text, html: message.html })); };
/** * @param message the message associated with the email. * @returns an object describing the attributes * of the email. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/email-text-processor/src/lambdas/parser/convert.ts#L35-L41
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
Lambda.createCallbacks
private createCallbacks(event: CloudEvent, parser: MailParser) { let headers: Headers; return ({ onData: (data: AttachmentStream | MessageText) => { if (data.type === 'attachment') { if (!INCLUDE_ATTACHMENTS) { data.release(); } else { this.onAttachment(event, data).then(() => data.release()); } } else { parser.pause(); this.onText(event, data, headers).then(() => parser.resume()); } }, onHeaders: (h: Headers) => headers = h }); }
/** * A function creating a closure capturing the * email document parsing results. * @param e the cloud event associated with the * email document. * @returns a closure exposing the event handlers * to process the email document. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/email-text-processor/src/lambdas/parser/index.ts#L181-L199
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
Lambda.processEvent
async processEvent(event: CloudEvent): Promise<any> { const document = event.data().document(); const parser = new MailParser({ skipImageLinks: !INCLUDE_IMAGE_LINKS }); // We create a readable stream from the document. const readable = await document.data().asReadStream(); // Create a closure capturing the parsing results. const callback = this.createCallbacks(event, parser); // Setup event handlers. parser.on('headers', callback.onHeaders); parser.on('data', callback.onData); return (new Promise((resolve, reject) => { readable .pipe(parser) .on('error', reject) .on('end', resolve); })); }
/** * Parses the input email document, and processes its * content, attachments and headers as a stream. * @param event the event to process. * @note the next decorator will automatically forward the * returned cloud event to the next middlewares */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/email-text-processor/src/lambdas/parser/index.ts#L208-L230
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
Lambda.handler
async handler(event: SQSEvent, _: Context): Promise<SQSBatchResponse> { return (await processPartialResponse( event, (record: SQSRecord) => this.processEvent( CloudEvent.from(JSON.parse(record.body)) ), processor )); }
// eslint-disable-next-line @typescript-eslint/no-unused-vars
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/email-text-processor/src/lambdas/parser/index.ts#L239-L247
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
JMESPathProcessorBuilder.withExpression
public withExpression(expression: string) { // Validate the expression. jmespath.search({}, expression); this.providerProps.expression = expression; return (this); }
/** * Sets the JMESPath expression to apply. * @param expression the JMESPath expression to apply. * @returns the builder itself. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/jmespath-processor/src/index.ts#L76-L81
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
JMESPathProcessorBuilder.build
public build(): JMESPathProcessor { return (new JMESPathProcessor( this.scope, this.identifier, { ...this.providerProps as JMESPathProcessorProps, ...this.props } )); }
/** * @returns a new instance of the `JMESPathProcessor` * service constructed with the given parameters. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/jmespath-processor/src/index.ts#L87-L95
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
JMESPathProcessor.constructor
constructor(scope: Construct, id: string, props: JMESPathProcessorProps) { super(scope, id, description, { ...props, queueVisibilityTimeout: cdk.Duration.seconds( 6 * PROCESSING_TIMEOUT.toSeconds() ) }); // Validate the properties. props = this.parse(JMESPathProcessorPropsSchema, props); /////////////////////////////////////////// ///////// Processing Storage ////// /////////////////////////////////////////// this.storage = new CacheStorage(this, 'Storage', { encryptionKey: props.kmsKey }); /////////////////////////////////////////// ////// Middleware Event Handler //// /////////////////////////////////////////// this.processor = new node.NodejsFunction(this, 'Compute', { description: 'Middleware applying JMESPath expressions to JSON documents.', entry: path.resolve(__dirname, 'lambdas', 'processor', 'index.js'), vpc: props.vpc, memorySize: props.maxMemorySize ?? DEFAULT_MEMORY_SIZE, timeout: PROCESSING_TIMEOUT, runtime: EXECUTION_RUNTIME, architecture: lambda.Architecture.ARM_64, tracing: lambda.Tracing.ACTIVE, environmentEncryption: props.kmsKey, logGroup: this.logGroup, insightsVersion: props.cloudWatchInsights ? LAMBDA_INSIGHTS_VERSION : undefined, environment: { POWERTOOLS_SERVICE_NAME: description.name, POWERTOOLS_METRICS_NAMESPACE: NAMESPACE, SNS_TARGET_TOPIC: this.eventBus.topicArn, PROCESSED_FILES_BUCKET: this.storage.id(), EXPRESSION: props.expression }, bundling: { minify: true, externalModules: [ '@aws-sdk/client-s3', '@aws-sdk/client-sns' ] } }); // Allows this construct to act as a `IGrantable` // for other middlewares to grant the processing // lambda permissions to access their resources. this.grantPrincipal = this.processor.grantPrincipal; // Plug the SQS queue into the lambda function. this.processor.addEventSource(new sources.SqsEventSource(this.eventQueue, { batchSize: props.batchSize ?? 5, reportBatchItemFailures: true })); // Function permissions. this.storage.grantWrite(this.processor); this.eventBus.grantPublish(this.processor); super.bind(); }
/** * Construct constructor. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/jmespath-processor/src/index.ts#L122-L191
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
JMESPathProcessor.grantReadProcessedDocuments
grantReadProcessedDocuments(grantee: iam.IGrantable): iam.Grant { return (this.storage.grantRead(grantee)); }
/** * Allows a grantee to read from the processed documents * generated by this middleware. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/jmespath-processor/src/index.ts#L197-L199
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
JMESPathProcessor.supportedInputTypes
supportedInputTypes(): string[] { return ([ 'application/json' ]); }
/** * @returns an array of mime-types supported as input * type by this middleware. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/jmespath-processor/src/index.ts#L205-L209
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
JMESPathProcessor.supportedOutputTypes
supportedOutputTypes(): string[] { return ([ 'application/json', 'text/plain' ]); }
/** * @returns an array of mime-types supported as output * type by the data producer. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/jmespath-processor/src/index.ts#L215-L220
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
JMESPathProcessor.supportedComputeTypes
supportedComputeTypes(): ComputeType[] { return ([ ComputeType.CPU ]); }
/** * @returns the supported compute types by a given * middleware. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/jmespath-processor/src/index.ts#L226-L230
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
JMESPathProcessor.conditional
conditional() { return (super .conditional() .and(when('type').equals('document-created')) ); }
/** * @returns the middleware conditional statement defining * in which conditions this middleware should be executed. * In this case, we want the middleware to only be invoked * when the document mime-type is supported, and the event * type is `document-created`. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/jmespath-processor/src/index.ts#L239-L244
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
Lambda.getType
private getType(obj: any): string { return (typeof obj === 'object' ? 'application/json' : 'text/plain' ); }
/** * @param obj a javascript object. * @returns the type of the object. */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/jmespath-processor/src/lambdas/processor/index.ts#L70-L75
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
Lambda.handler
async handler(event: SQSEvent, _: Context): Promise<SQSBatchResponse> { return (await processPartialResponse( event, (record: SQSRecord) => this.processEvent( CloudEvent.from(JSON.parse(record.body)) ), processor )); }
// eslint-disable-next-line @typescript-eslint/no-unused-vars
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/jmespath-processor/src/lambdas/processor/index.ts#L126-L134
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
KeybertTextProcessorBuilder.withTopN
public withTopN(topN: number) { this.middlewareProps.topN = topN; return (this); }
/** * Sets the maximum number of keywords to extract. * @param topN the maximum number of keywords to extract. * @default 5 */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/keybert-text-processor/src/index.ts#L72-L75
4285173e80584eedfc1a8424d3d1b6c1a7038088
project-lakechain
github_2023
awslabs
typescript
KeybertTextProcessorBuilder.withUseMaxSum
public withUseMaxSum(useMaxSum: boolean) { this.middlewareProps.useMaxSum = useMaxSum; return (this); }
/** * Sets whether to use the max sum algorithm. * @param useMaxSum whether to use the max sum algorithm. * @default false */
https://github.com/awslabs/project-lakechain/blob/4285173e80584eedfc1a8424d3d1b6c1a7038088/packages/middlewares/text-processors/keybert-text-processor/src/index.ts#L82-L85
4285173e80584eedfc1a8424d3d1b6c1a7038088