Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor update to S3 functions cookbook #1679

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,9 @@
"source": [
"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.\n",
"\n",
"**Requirements**:\n",
"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:\n",
"```\n",
"AWS_ACCESS_KEY_ID=<your-key>\n",
"AWS_SECRET_ACCESS_KEY=<your-key>\n",
"OPENAI_API_KEY=<your-key>\n",
"```"
"*Requirements*: \n",
"\n",
"To run the notebook, install the AWS CLI and configure it with your AWS access key and secret using the `aws configure` command (see [AWS CLI Configuration](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html)). Set the OpenAI key as an environment variable using the `export` method (see [OpenAI API Key Documentation](https://platform.openai.com/docs/quickstart#create-and-export-an-api-key))."
]
},
{
Expand All @@ -33,40 +29,22 @@
"metadata": {},
"outputs": [],
"source": [
"! pip install openai\n",
"! pip install boto3\n",
"! pip install tenacity\n",
"! pip install python-dotenv"
"!pip install openai boto3 "
]
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 44,
"id": "9617e95e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"from openai import OpenAI\n",
"import os\n",
"import json\n",
"import boto3\n",
"import os\n",
"import datetime\n",
"from urllib.request import urlretrieve\n",
"\n",
"# load environment variables\n",
"from dotenv import load_dotenv\n",
"load_dotenv() "
"from openai import OpenAI\n",
"from urllib.request import urlretrieve"
]
},
{
Expand All @@ -80,26 +58,22 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 45,
"id": "6d5b1991",
"metadata": {},
"outputs": [],
"source": [
"OpenAI.api_key = os.environ.get(\"OPENAI_API_KEY\")\n",
"GPT_MODEL = \"gpt-3.5-turbo\""
"# For a list of available models, see https://platform.openai.com/docs/models\n",
"GPT_MODEL = \"gpt-4o\""
]
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 46,
"id": "a571b8d7",
"metadata": {},
"outputs": [],
"source": [
"# Optional - if you had issues loading the environment file, you can set the AWS values using the below code\n",
"# os.environ['AWS_ACCESS_KEY_ID'] = ''\n",
"# os.environ['AWS_SECRET_ACCESS_KEY'] = ''\n",
"\n",
"# Create S3 client\n",
"s3_client = boto3.client('s3')\n",
"\n",
Expand Down Expand Up @@ -127,7 +101,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 47,
"id": "da4a804b",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -224,7 +198,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 48,
"id": "cf30f14e",
"metadata": {},
"outputs": [],
Expand All @@ -237,7 +211,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 49,
"id": "37736b74",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -304,7 +278,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 50,
"id": "03c3d555",
"metadata": {},
"outputs": [],
Expand All @@ -329,7 +303,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 51,
"id": "796acfdd",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -369,14 +343,14 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 52,
"id": "3e2e9192",
"metadata": {},
"outputs": [],
"source": [
"def run_conversation(user_input, topic=\"S3 bucket functions.\", is_log=False):\n",
"\n",
" system_message=f\"Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous. If the user ask question not related to {topic} response your scope is {topic} only.\"\n",
" system_message=f\"Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous. If the user ask question not related to {topic} response your scope to {topic} only.\"\n",
" \n",
" messages = [{\"role\": \"system\", \"content\": system_message},\n",
" {\"role\": \"user\", \"content\": user_input}]\n",
Expand Down Expand Up @@ -467,7 +441,7 @@
"metadata": {},
"outputs": [],
"source": [
"search_file = '<file_name>'\n",
"search_file = '<filename>'\n",
"print(run_conversation(f'search for a file {search_file} in all buckets'))"
]
},
Expand All @@ -494,15 +468,15 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": null,
"id": "c58d7372",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"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?\n"
"Please provide the name of the file you want to search for and the name of the S3 bucket where you'd like to search. Additionally, let me know if you want to specify a folder path within the bucket or if you need an exact match for the file name.\n"
]
}
],
Expand All @@ -523,15 +497,15 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 31,
"id": "baaedf21",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"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?\n"
"I'm only able to assist with S3 bucket functions. Please let me know if you need help with anything related to that!\n"
]
}
],
Expand Down Expand Up @@ -565,7 +539,7 @@
"metadata": {},
"outputs": [],
"source": [
"search_file = '<file_name>'\n",
"search_file = '<filename>'\n",
"bucket_name = '<bucket_name>'\n",
"local_directory = '<directory_path>'\n",
"print(run_conversation(f'download {search_file} from {bucket_name} bucket to {local_directory} directory'))"
Expand Down Expand Up @@ -595,7 +569,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": ".venv",
"language": "python",
"name": "python3"
},
Expand All @@ -609,7 +583,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.2"
}
},
"nbformat": 4,
Expand Down