Skip to content

Commit

Permalink
Merge branch 'main' into feat/jsii_53
Browse files Browse the repository at this point in the history
  • Loading branch information
krokoko authored Jan 30, 2024
2 parents ce5e508 + 2a3822b commit e925abe
Show file tree
Hide file tree
Showing 20 changed files with 183 additions and 106 deletions.
40 changes: 40 additions & 0 deletions apidocs/classes/bedrock.Agent.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@ Deploy a Bedrock Agent.
- [aliasId](bedrock.Agent.md#aliasid)
- [aliasName](bedrock.Agent.md#aliasname)
- [cdkTagManager](bedrock.Agent.md#cdktagmanager)
- [changeIds](bedrock.Agent.md#changeids)
- [name](bedrock.Agent.md#name)
- [node](bedrock.Agent.md#node)
- [prepareAgent](bedrock.Agent.md#prepareagent)
- [role](bedrock.Agent.md#role)

### Methods

- [\_addPrepareAgentDependency](bedrock.Agent.md#_addprepareagentdependency)
- [addAlias](bedrock.Agent.md#addalias)
- [toString](bedrock.Agent.md#tostring)
- [isConstruct](bedrock.Agent.md#isconstruct)
Expand Down Expand Up @@ -116,6 +119,14 @@ cdk.ITaggableV2.cdkTagManager

___

### changeIds

`Private` **changeIds**: `string`[] = `[]`

A list of values to indicate if PrepareAgent or an Alias needs to be updated.

___

### name

`Readonly` **name**: `string`
Expand All @@ -136,6 +147,16 @@ Construct.node

___

### prepareAgent

`Private` **prepareAgent**: `CustomResource`

The prepareAgent custom resource.

Add other resources as dependencies to ensure Prepare Agent is called after they are updated.

___

### role

`Readonly` **role**: `Role`
Expand All @@ -144,6 +165,25 @@ The IAM role for the agent.

## Methods

### \_addPrepareAgentDependency

**_addPrepareAgentDependency**(`resource`, `changeId?`): `void`

Register a dependency for prepareAgent.

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `resource` | `IResource` | The resource that will be registered as a dependency. |
| `changeId?` | `string` | The changeId of the resource that will be registered as a dependency. This is an internal core function and should not be called directly. |

#### Returns

`void`

___

### addAlias

**addAlias**(`props`): [`AgentAlias`](bedrock.AgentAlias.md)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# and limitations under the License.
#
from langchain.llms.bedrock import Bedrock
from langchain.embeddings import BedrockEmbeddings
from langchain_community.embeddings import BedrockEmbeddings
import os
import boto3
from .helper import get_credentials
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
#
from .helper import load_vector_db_opensearch, send_job_status, JobStatus
from .s3inmemoryloader import S3FileLoaderInMemory
from langchain import PromptTemplate
from langchain.prompts import PromptTemplate
from langchain.callbacks.base import BaseCallbackHandler
from langchain.schema import LLMResult
from llms import get_llm, get_max_tokens
from langchain import LLMChain
from langchain.chains import LLMChain

import boto3
import os
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions
# and limitations under the License.
#
from langchain.vectorstores import OpenSearchVectorSearch
from langchain_community.vectorstores import OpenSearchVectorSearch
from opensearchpy import RequestsHttpConnection
from llms import get_embeddings_llm
import requests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ aws-xray-sdk
fastjsonschema
typing-extensions
aiohttp
boto3>=1.28.69
botocore>=1.31.69
boto3>=1.34.29
botocore>=1.34.29
requests==2.31.0
requests-aws4auth==1.2.3
opensearch-py==2.3.1
opensearch-py==2.4.2
numpy
langchain==0.0.329
langchain==0.1.4
langchain-community==0.0.16
opensearch-py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
# and limitations under the License.
#
from opensearchpy import OpenSearch, RequestsHttpConnection, AWSV4SignerAuth
from langchain.vectorstores import OpenSearchVectorSearch
from langchain.embeddings import BedrockEmbeddings
from langchain_community.vectorstores import OpenSearchVectorSearch
from langchain_community.embeddings import BedrockEmbeddings
from typing import Tuple
import boto3, os
from aws_lambda_powertools import Logger, Tracer, Metrics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
from helpers.credentials_helper import get_credentials
from helpers.opensearch_helper import check_if_index_exists, process_shard
from helpers.update_ingestion_status import updateIngestionJobStatus
from langchain.embeddings import BedrockEmbeddings
from langchain_community.embeddings import BedrockEmbeddings
from helpers.s3inmemoryloader import S3TxtFileLoaderInMemory
from opensearchpy import OpenSearch, RequestsHttpConnection
from langchain.vectorstores import OpenSearchVectorSearch
from langchain_community.vectorstores import OpenSearchVectorSearch
from langchain.text_splitter import RecursiveCharacterTextSplitter
import multiprocessing as mp
from functools import partial
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ aws-lambda-powertools
aws-xray-sdk
fastjsonschema
typing-extensions
boto3>=1.28.61
botocore>=1.31.61
boto3>=1.34.29
botocore>=1.34.29
requests==2.31.0
requests-aws4auth==1.2.3
langchain==0.0.329
opensearch-py==2.3.1
langchain==0.1.4
langchain-community==0.0.16
opensearch-py==2.4.2
sagemaker
numpy
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ fastjsonschema
typing-extensions
boto3
requests
langchain==0.0.329
langchain==0.1.4
pypdf2
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
redis
pypdf2
langchain==0.0.329
langchain==0.1.4
urllib3<2
aws-xray-sdk
aws-lambda-powertools
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from langchain.llms.bedrock import Bedrock
from update_summary_status import updateSummaryJobStatus
from langchain import PromptTemplate
from langchain.prompts import PromptTemplate
# external files
from langchain.docstore.document import Document
from langchain.chains.summarize import load_summarize_chain
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
redis
boto3>=1.28.61
botocore>=1.31.61
boto3>=1.34.29
botocore>=1.34.29
requests==2.31.0
requests-aws4auth==1.2.3
langchain==0.0.329
langchain==0.1.4
nltk==3.8.1
urllib3<2
aws-lambda-powertools
Expand Down
8 changes: 4 additions & 4 deletions layers/langchain-common-deps/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
boto3>=1.34.6
botocore>=1.34.6
boto3>=1.34.29
botocore>=1.34.29
requests==2.31.0
requests-aws4auth==1.2.3
langchain==0.0.351
langchain==0.1.4
opensearch-py==2.4.2
openai==1.6.0
openai==1.10.0
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#
import os
from enum import Enum
from langchain import LLMChain
from langchain.chains import LLMChain
from langchain.callbacks.base import BaseCallbackHandler
from langchain.chains import ConversationalRetrievalChain, ConversationChain
from langchain.memory import ConversationBufferMemory
Expand Down
4 changes: 2 additions & 2 deletions package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 42 additions & 15 deletions src/cdk-lib/bedrock/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,18 @@ export class Agent extends Construct implements cdk.ITaggableV2 {
*/
public readonly cdkTagManager =
new cdk.TagManager(cdk.TagType.MAP, 'Custom::Bedrock-Agent');
/**
* A list of values to indicate if PrepareAgent or an Alias needs to be updated.
* @private
*/
private changeIds: string[] = [];
/**
* The prepareAgent custom resource.
*
* Add other resources as dependencies to ensure Prepare Agent is called after they are updated.
* @private
*/
private prepareAgent: cdk.CustomResource;

constructor(scope: Construct, id: string, props: AgentProps) {
super(scope, id);
Expand Down Expand Up @@ -422,9 +434,17 @@ export class Agent extends Construct implements cdk.ITaggableV2 {
true,
);

const changeIds: string[] = [agent.getAttString('changeId')];
this.prepareAgent = new cdk.CustomResource(this, 'PrepareAgent', {
serviceToken: crProvider.serviceToken,
resourceType: 'Custom::Bedrock-PrepareAgent',
properties: {
agentId: this.agentId,
changeIds: cdk.Lazy.list({ produce: () => this.changeIds }),
},
});

this._addPrepareAgentDependency(agent, agent.getAttString('changeId'));

const kbAssociations: string[] = [];
if (props.knowledgeBases && props.knowledgeBases.length > 0) {
const kbAssocCRPolicy = new iam.Policy(this, 'KBAssocCRPolicy', {
roles: [crProvider.role],
Expand Down Expand Up @@ -483,24 +503,14 @@ export class Agent extends Construct implements cdk.ITaggableV2 {
);
kbAssoc.node.addDependency(kbAssocCRPolicy);
kbAssoc.node.addDependency(crProvider);
kbAssociations.push(kbAssoc.getAttString('changeId'));
this._addPrepareAgentDependency(kbAssoc, kbAssoc.getAttString('changeId'));
}
changeIds.push(...kbAssociations);
}

new cdk.CustomResource(this, 'PrepareAgent', {
serviceToken: crProvider.serviceToken,
resourceType: 'Custom::Bedrock-PrepareAgent',
properties: {
agentId: this.agentId,
changeIds,
},
});

if (props.aliasName) {
const alias = this.addAlias({
agentId: this.agentId,
changeIds,
aliasName: props.aliasName,
});
this.aliasId = alias.aliasId;
Expand All @@ -513,12 +523,29 @@ export class Agent extends Construct implements cdk.ITaggableV2 {
* Add an alias to the agent.
*/
public addAlias(props: Exclude<AgentAliasProps, 'agentId'>): AgentAlias {
return new AgentAlias(this, `AgentAlias-${props.aliasName}`, {
const alias = new AgentAlias(this, `AgentAlias-${props.aliasName}`, {
agentId: this.agentId,
agentVersion: props.agentVersion,
changeIds: props.changeIds,
changeIds: cdk.Lazy.list({ produce: () => this.changeIds }),
aliasName: props.aliasName,
});
alias.node.addDependency(this.prepareAgent);
return alias;
}

/**
* Register a dependency for prepareAgent.
*
* @param resource - The resource that will be registered as a dependency.
* @param changeId - The changeId of the resource that will be registered as a dependency.
*
* @internal This is an internal core function and should not be called directly.
*/
public _addPrepareAgentDependency(resource: cdk.IResource, changeId?: string) {
this.prepareAgent.node.addDependency(resource);
if (changeId) {
this.changeIds.push(changeId);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ export class RagAppsyncStepfnOpensearch extends Construct {
{
maxConcurrency: 100,
},
).iterator(fileTransformationTask);
).itemProcessor(fileTransformationTask);

const jobFailed = new stepfn.Fail(this, 'Job Failed', {
cause: 'Validation job failed',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ export class SummarizationAppsyncStepfn extends Construct {
// step function, run files in parallel
const runFilesInparallel = new sfn.Map(this, 'Run Files in Parallel', {
maxConcurrency: 100,
}).iterator(
}).itemProcessor(
documentReaderTask.next(
summaryfromCacheChoice
.when(
Expand Down
8 changes: 8 additions & 0 deletions test/cdk-lib/bedrock/__snapshots__/agent.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e925abe

Please sign in to comment.