{"metadata":{"title":"Redshift_LLM_Model","kernelspec":{"display_name":"Redshift","language":"postgresql","name":"Redshift"},"language_info":{"file_extension":".sql","name":"Redshift"},"version":1},"nbformat":4,"nbformat_minor":0,"cells":[{"metadata":{"displayMode":"maximized","width":12,"isLimitOn":true},"source":["Using Amazon Redshift ML, create a model referencing the SageMaker JumpStart LLM endpoint"],"cell_type":"markdown"},{"metadata":{"displayMode":"maximized","width":12,"isLimitOn":true},"source":["CREATE MODEL falcon_7b_instruct_llm_model\r\n","FUNCTION falcon_7b_instruct_llm_model(super)\r\n","RETURNS super\r\n","SAGEMAKER '<endpointname>'\r\n","IAM_ROLE default;"],"cell_type":"code","execution_count":0,"outputs":[]},{"metadata":{"displayMode":"maximized","width":12,"isLimitOn":true},"source":["Load sample amazon reviews data set into your Amazon Redshift data warehouse"],"cell_type":"markdown"},{"metadata":{"displayMode":"maximized","width":12,"isLimitOn":true},"source":["CREATE TABLE sample_reviews\r\n","(\r\n","    review varchar(4000)\r\n",");\r\n","\r\n","COPY sample_reviews \r\n","FROM 's3://<<your bucket>>/sample_reviews.csv'\r\n","IAM_ROLE DEFAULT\r\n","csv\r\n","DELIMITER ','\r\n","IGNOREHEADER 1;"],"cell_type":"code","execution_count":0,"outputs":[]},{"metadata":{"displayMode":"maximized","width":12,"isLimitOn":true},"source":["Create a UDF that engineers the prompt for sentiment analysis"],"cell_type":"markdown"},{"metadata":{"displayMode":"maximized","width":12,"isLimitOn":true},"source":["CREATE FUNCTION udf_prompt_eng_sentiment_analysis (varchar)\r\n","  returns super\r\n","stable\r\n","as $$\r\n","  select json_parse(\r\n","  '{\"inputs\":\"Classify the sentiment of this sentence as Positive, Negative, Neutral. Return only the sentiment nothing else.' || $1 || '\",\"parameters\":{\"max_new_tokens\":1000}}')\r\n","$$ language sql;"],"cell_type":"code","execution_count":0,"outputs":[]},{"metadata":{"displayMode":"maximized","width":12,"isLimitOn":true},"source":["Make a remote inference to the LLM model to generate sentiment analysis for input dataset"],"cell_type":"markdown"},{"metadata":{"displayMode":"maximized","width":12,"isLimitOn":true},"source":["CREATE table sentiment_analysis_for_reviews\r\n","as\r\n","(\r\n","    SELECT \r\n","        review, \r\n","        falcon_7b_instruct_llm_model\r\n","            (\r\n","                udf_prompt_eng_sentiment_analysis(review)\r\n","        ) as sentiment\r\n","    from sample_reviews\r\n",");"],"cell_type":"code","execution_count":0,"outputs":[]},{"metadata":{"displayMode":"maximized","width":12,"isLimitOn":true},"source":["Analyze the output"],"cell_type":"markdown"},{"metadata":{"displayMode":"maximized","width":12,"isLimitOn":true},"source":["SELECT review, sentiment[0].\"generated_text\" :: varchar as sentiment\r\n","FROM sentiment_analysis_for_reviews;"],"cell_type":"code","execution_count":0,"outputs":[]}]}