Skip to content

Instantly share code, notes, and snippets.

@RameshRM
Created April 23, 2024 05:35
Show Gist options
  • Save RameshRM/896238b9e5fec54017f5b931f53dc441 to your computer and use it in GitHub Desktop.
Save RameshRM/896238b9e5fec54017f5b931f53dc441 to your computer and use it in GitHub Desktop.
Agent-S3-SDK
const dotEnv = require("dotenv");
dotEnv.config();
const {
S3Client,
PutObjectCommand,
CreateBucketCommand,
DeleteObjectCommand,
DeleteBucketCommand,
paginateListObjectsV2,
GetObjectCommand,
ListBucketsCommand,
ListObjectsCommand,
ListObjectsV2Command,
} = require("@aws-sdk/client-s3");
function getTools() {
const { DynamicTool } = require("@langchain/core/tools");
const customTool = new DynamicTool({
name: "get_s3_buckets",
description: "Returns the list of S3 Buckets.",
func: async (input) => {
const listCommand = new ListBucketsCommand({});
const s3Instance = getAWSS3();
const result = await s3Instance.send(listCommand);
console.log(result);
return JSON.stringify(result);
},
});
// s3://amplify-react-draw-devm-55540-deployment/#current-cloud-backend.zip
// https://amplify-react-draw-devm-55540-deployment.s3.ap-south-1.amazonaws.com/%23current-cloud-backend.zip
const bucketReader = new DynamicTool({
name: "read_bucket",
description: "Reads the contents of the S3 Bucket.",
func: async (input) => {
const bucketName = `${input}.s3express-${"ap-south-1"}.region.amazonaws.com`;
const listObjectsCommand = new ListObjectsV2Command({
Bucket: `${input}`,
MaxKeys: 2,
});
const s3Instance = getAWSS3();
try {
const result = await s3Instance.send(listObjectsCommand);
return {
name: input,
statusCode: 200,
contents: result.Contents.map((content) => {
return content.Key;
}),
};
} catch (e) {
return {
name: input,
statusCode: 404,
contents: [],
};
}
},
});
const tools = [customTool, bucketReader];
return tools;
}
function getPrompt() {
const {
ChatPromptTemplate,
MessagesPlaceholder,
} = require("@langchain/core/prompts");
const prompt = ChatPromptTemplate.fromMessages([
[
"system",
"You are a helpful assistant and have access to provided tools and functions. You must use only the tools and the functions provided to complete the operation and dont infer anything you dont know.",
],
[
"system",
"You have access to AWS API and provided set of Client SDK and credentials, Your job is to get all the S3 Buckets with name starting as the provided input. You should only use the tools and functions provided.",
],
["user", "{input}"],
[
"system",
"You have access to AWS API and provided list of S3 buckets, your job is to read bucket with the set of SDK & credentials, Your job is to get all the S3 Buckets with name starting as the provided input. You should only use the tools and functions provided.",
],
["user", "Read every single S3 bucket and get the contents"],
new MessagesPlaceholder("agent_scratchpad"),
]);
return prompt;
}
function getModel() {
const { ChatOpenAI } = require("@langchain/openai");
/**
* Define your chat model to use.
*/
return new ChatOpenAI({
temperature: 0,
verbose: false,
model: "gpt-3.5-turbo",
});
}
function getAWSS3() {
return new S3Client({
region: process.env.AWS_REGION_AP_SOUTH,
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEYID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
},
});
}
function toolsToFunctions(tools) {
const {
convertToOpenAIFunction,
} = require("@langchain/core/utils/function_calling");
const model = getModel();
const modelWithFunctions = model.bind({
functions: tools.map((tool) => convertToOpenAIFunction(tool)),
});
return modelWithFunctions;
}
function main(prompt) {
return function (tools, modelWithFunctions) {
const { RunnableSequence } = require("@langchain/core/runnables");
const { AgentExecutor, AgentStep } = require("langchain/agents");
const {
formatToOpenAIFunctionMessages,
} = require("langchain/agents/format_scratchpad");
const {
OpenAIFunctionsAgentOutputParser,
JsonOutputFunctionsParser,
} = require("langchain/agents/openai/output_parser");
const runnableAgent = RunnableSequence.from([
{
input: (i) => i.input,
agent_scratchpad: (i) => formatToOpenAIFunctionMessages(i && i.steps),
},
prompt,
modelWithFunctions,
new JsonOutputFunctionsParser(),
]);
const executor = AgentExecutor.fromAgentAndTools({
agent: runnableAgent,
tools,
});
return executor;
};
}
const executor = main(getPrompt())(getTools(), toolsToFunctions(getTools()));
const input =
"List all of the S3 buckets and get all the object for the bucket name starting farm";
// return;
(async function () {
const {
OpenAIFunctionsAgentOutputParser,
} = require("langchain/agents/openai/output_parser");
const result = await executor.invoke(
{
input,
},
[
{
handleAgentAction(action, runId) {
console.log("\nhandleAgentAction", action, runId);
},
handleAgentEnd(action, runId) {
console.log("\nhandleAgentEnd", action, runId);
},
handleToolEnd(output, runId) {
console.log("\nhandleToolEnd", output, runId);
},
},
]
);
console.log(result);
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment