How To Automate S3 Storage With Functions
How to automate tasks with functions (S3 bucket example)
This code demonstrates how to interact with ChatGPT functions to perform tasks related to Amazon S3 buckets. The notebook covers S3 bucket key functionalities such as running simple listing commands, searching for a specific file in all buckets, uploading a file to a bucket, and downloading a file from a bucket. The OpenAI Chat API understands the user instructions, generates the natural language responses, and extracts appropriate function calls based on the user's input.
Requirements:
To run the notebook generate AWS access key with S3 bucket writing permission and store them in a local environment file alongside the Openai key. The ".env" file format:
AWS_ACCESS_KEY_ID=<your-key>
AWS_SECRET_ACCESS_KEY=<your-key>
OPENAI_API_KEY=<your-key>
True
Initials
Utilities
To connect user questions or commands to the appropriate function, we need to provide ChatGPT with the necessary function details and expected parameters.
Create helper functions to interact with the S3 service, such as listing buckets, listing objects, downloading and uploading files, and searching for specific files.
The below dictionary connects the name with the function to use it for execution based on ChatGPT responses.
ChatGPT
Conversation flow
Create a main function for the chatbot, which takes user input, sends it to the OpenAI Chat API, receives a response, executes any function calls generated by the API, and returns a final response to the user.
S3 bucket bot testing
In the following examples, make sure to replace the placeholders such as <file_name>, <bucket_name>, and <directory_path> with your specific values before execution.
Listing and searching
Let's start by listing all the available buckets.
You can ask the assistant to search for a specific file name either in all the buckets or in a specific one.
The model is expected to clarify the ask from the user in case of ambiguity in the parameters values as described in the system message.
Sure, to help me find what you're looking for, could you please provide the name of the file you want to search for and the name of the S3 bucket? Also, should the search match the file name exactly, or should it also consider partial matches?
Validate edge cases
We also instructed the model to reject irrelevant tasks. Let's test it out and see how it works in action.
Apologies for the misunderstanding, but I am only able to assist with S3 bucket functions. Can you please ask a question related to S3 bucket functions?
The provided functions are not limited to just retrieving information. They can also assist the user in uploading or downloading files.