"This page provides an instruction & guide for developers building a GPT Action for a specific application. Before you proceed, make sure to first familiarize yourself with the following information: \n",
"- [Introduction to GPT Actions](https://platform.openai.com/docs/actions)\n",
"- [Introduction to GPT Actions Library](https://platform.openai.com/docs/actions/actions-library)\n",
"- [Example of Building a GPT Action from Scratch](https://platform.openai.com/docs/actions/getting-started)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This solution enables a GPT action to retrieve data from Redshift and perform data analysis.It uses AWS Functions, performing every action from AWS ecosystem and network. The middleware (AWS function) will perform the SQL query, wait for its completion and return the data as a file. The code is provided for information purpose only and should be modified to your needs.\n",
"\n",
"This solution uses the ability to [retrieve files in Actions](https://platform.openai.com/docs/actions/sending-files) and use them as if you had uploaded them directly to a conversation.\n",
"\n",
"This solution highlight a connection to Redshift serverless, the integration with a provisioned Redshift might differ slighltly to retrieve networks and set-up connection, the overall code and (minimal) integration should be similar."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Value & Example Business Use Cases"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Value**: Leverage ChatGPT's natural language capabilities to connect to Redshift's DWH.\n",
"\n",
"**Example Use Cases**:\n",
"- Data scientists can connect to tables and run data analyses using ChatGPT's Data Analysis\n",
"- Citizen data users can ask basic questions of their transactional data\n",
"- Users gain more visibility into their data & potential anomalies"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Application Information"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Application Prerequisites"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Before you get started, make sure that:\n",
"- You have access to a Redshift environment\n",
"- You have the rights to deploy AWS function in the same VPC (Virtual Private Network)\n",
"- Your AWS CLI is authenticated"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Middleware Information"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Install required libraries\n",
"- Install AWS CLI, required for AWS SAM ([docs](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html#getting-started-install-instructions))\n",
"- Install AWS SAM CLI ([docs](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html))\n",
"To create a function, follow the steps in the [AWS Middleware Action cookbook](https://cookbook.openai.com/examples/chatgpt/gpt_actions_library/gpt_middleware_aws_function).\n",
"\n",
"To deploy specifically an application that connects to Redshift, use the following code instead of the \"hello-world\" GitHub repository referenced in the Middleware AWS Function cookbook. You can either clone the repository or take the code pasted below and modify it to your needs.\n",
"\n",
"> This code is meant to be directional - while it should work out of the box, it is designed to be customized to your needs (see examples towards the end of this document).\n",
"\n",
"To get the code, you can clone openai-cookbook repository and navigate to the redshift-middleware directory\n",
"We will need to connnect our function to Redshift, therefore we need to find the network used by Redshift. You can find this on your Redshift interface the AWS console, under Amazon Redshift Serverless > Workgroup configuration > `your_workgroup` > Data access, or through the CLI:"
"Once you've created a Custom GPT, copy the text below in the Instructions panel."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"**Context**: You are an expert at writing Redshift SQL queries. You will initially retrieve the table schema that you will use thoroughly. Every attributes, table names or data type will be known by you.\n",
"\n",
"**Instructions**:\n",
"1. No matter the user's question, start by running `runQuery` operation using this query: \"SELECT table_name, column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = 'public' ORDER BY table_name, ordinal_position;\" It will help you understand how to query the data. A CSV will be returned with all the attributes and their table. Make sure to read it fully and understand all available tables & their attributes before querying. You don't have to show this to the user.\n",
"2. Convert the user's question into a SQL statement that leverages the step above and run the `runQuery` operation on that SQL statement to confirm the query works. Let the user know which table you will use/query.\n",
"3. Execute the query and show him the data. Show only the first few rows.\n",
"\n",
"**Additional Notes**: If the user says \"Let's get started\", explain they can ask a question they want answered about data that we have access to. If the user has no ideas, suggest that we have transactions data they can query - ask if they want you to query that.\n",
"**Important**: Never make up a table name or table attribute. If you don't know, go back to the data you've retrieved to check what is available. If you think no table or attribute is available, then tell the user you can't perform this query for them."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### OpenAPI Schema "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Once you've created a Custom GPT, copy the text below in the Actions panel.\n",
"\n",
"This expects a response that matches the file retrieval structure in our doc [here](https://platform.openai.com/docs/actions/sending-files) and passes in a `query` as a parameter to execute.\n",
"\n",
"Make sure to follow the steps in the [AWS Middleware cookbook](https://cookbook.openai.com/examples/chatgpt/gpt_actions_library/gpt_middleware_aws_function) to set up authentication.\n",
"\n",
"> Make sure to switch the function app name based on your function deployment."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "yaml"
}
},
"outputs": [],
"source": [
"openapi: 3.1.0\n",
"info:\n",
" title: SQL Execution API\n",
" description: API to execute SQL statements and return results as a file.\n",
" version: 1.0.0\n",
"servers:\n",
" - url: {your_function_url}/Prod\n",
" description: Production server\n",
"paths:\n",
" /sql_statement:\n",
" post:\n",
" operationId: executeSqlStatement\n",
" summary: Executes a SQL statement and returns the result as a file.\n",
" requestBody:\n",
" required: true\n",
" content:\n",
" application/json:\n",
" schema:\n",
" type: object\n",
" properties:\n",
" sql_statement:\n",
" type: string\n",
" description: The SQL statement to execute.\n",
" example: SELECT * FROM customers LIMIT 10\n",
" required:\n",
" - sql_statement\n",
" responses:\n",
" '200':\n",
" description: The SQL query result as a JSON file.\n",
" content:\n",
" application/json:\n",
" schema:\n",
" type: object\n",
" properties:\n",
" openaiFileResponse:\n",
" type: array\n",
" items:\n",
" type: object\n",
" properties:\n",
" name:\n",
" type: string\n",
" description: The name of the file.\n",
" example: query_result.json\n",
" mime_type:\n",
" type: string\n",
" description: The MIME type of the file.\n",
" example: application/json\n",
" content:\n",
" type: string\n",
" description: The base64 encoded content of the file.\n",
" format: byte\n",
" example: eyJrZXkiOiJ2YWx1ZSJ9\n",
" '500':\n",
" description: Error response\n",
" content:\n",
" application/json:\n",
" schema:\n",
" type: object\n",
" properties:\n",
" error:\n",
" type: string\n",
" description: Error message.\n",
" example: Database query failed error details\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Conclusion\n",
"\n",
"You now have deployed a GPT that uses a middleware in AWS, in an authenticated manner, that's able to connect to Redsfhit. Users with access (that are in Cognito) can now query your databases to perform data analysis task:\n",