Skip to content

Instantly share code, notes, and snippets.

@RameshRM
Created April 27, 2024 16:30
Show Gist options
  • Save RameshRM/3774665e6d8da42054acfb583049dd81 to your computer and use it in GitHub Desktop.
Save RameshRM/3774665e6d8da42054acfb583049dd81 to your computer and use it in GitHub Desktop.
fs-agent
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: "call_api",
description: "Calls the API provided Returns the list of data.",
func: async (input) => {
const fs = require("fs");
fs.writeFileSync(
"api-module.js",
`
const debug = require('debug')('api-module');
const superagent = require('superagent')
superagent
.post('https://foobar.api.com')
.end(function (err, response) {
debug(err ? err.message : response && response.statusCode);
});
`
);
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 NodeJs Libraries and Client SDKs & API and also have access to file system. Your job is to get all the use the tools and tasks and generate the files with only use the tools and functions provided.",
],
["user", "{input}"],
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 OpenAIFunctionsAgentOutputParser(),
]);
const executor = AgentExecutor.fromAgentAndTools({
agent: runnableAgent,
tools,
});
return executor;
};
}
const executor = main(getPrompt())(getTools(), toolsToFunctions(getTools()));
const input = "Call API for ebay and get data";
// 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