Skip to content
Open
Show file tree
Hide file tree
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
@@ -0,0 +1,12 @@
SELECT snowflake.cortex.complete(
'llama-3-7b',
PARSE_JSON('[
{"role": "system", "content": "You are a support triage assistant. For each incoming support ticket, classify the issue category, urgency (Low, Medium, High), and recommend the best team to handle it."},
{"role": "user", "content": "Customer reports they cannot access their account after multiple password resets."},
{"role": "assistant", "content": "Category: Account Access\nUrgency: High\nAssign To: Tier 1 Support"},
{"role": "user", "content": "User reports slow internet speeds when using our VPN service."},
{"role": "assistant", "content": "Category: Network Performance\nUrgency: Medium\nAssign To: Network Support Team"},
{"role": "user", "content": "New ticket: Application crashes when uploading files larger than 10MB."},
{"role": "assistant", "content": ""}
]')
) AS triage_response;
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
-- This script creates a stored procedure `mini_budget_on_cortex` to monitor
-- Snowflake Cortex functions credit usage. This is a temporary solution until the native
-- Snowflake Budget feature supports monitoring for Cortex services.
--
--
-- The procedure checks usage for the current account and sends a notification
-- if the usage exceeds a specified limit within a given billing period (day, week, or month).
-- It also creates a task `run_mini_budget_on_cortex` to run the check every 30 minutes.


CREATE OR REPLACE PROCEDURE mini_budget_on_cortex(
billing_period VARCHAR,
alert_limit FLOAT,
ni_name VARCHAR
)
RETURNS VARCHAR
LANGUAGE SQL
AS
$$
DECLARE
start_of_period DATE;
INVALID_PERIOD EXCEPTION (-20001, 'Invalid billing_period specified. Must be DAY, WEEK, or MONTH.');
current_usage_credits FLOAT;
notification_body VARCHAR;
BEGIN
CASE UPPER(billing_period)
WHEN 'DAY' THEN
start_of_period := CURRENT_DATE();
WHEN 'WEEK' THEN
start_of_period := DATE_TRUNC('week', CURRENT_DATE());
WHEN 'MONTH' THEN
start_of_period := DATE_TRUNC('month', CURRENT_DATE());
ELSE
RAISE INVALID_PERIOD;
END CASE;

SELECT COALESCE(SUM(TOKEN_CREDITS), 0) INTO current_usage_credits
FROM SNOWFLAKE.ACCOUNT_USAGE.CORTEX_FUNCTIONS_USAGE_HISTORY
WHERE START_TIME >= :start_of_period;

notification_body := 'Account: ' || CURRENT_ACCOUNT() || ' (' || CURRENT_REGION() || ')'
|| ' - Cortex functions credit usage in this ' || billing_period
|| ' = ' || current_usage_credits;
IF (current_usage_credits >= alert_limit) THEN
IF (ni_name IS NOT NULL AND TRIM(ni_name) <> '') THEN
CALL SYSTEM$SEND_SNOWFLAKE_NOTIFICATION(
'{"text/plain":"' || :notification_body || '"}',
'{"' || :ni_name || '": {}}'
);
END IF;
END IF;

RETURN notification_body;
END;
$$;


-- Make sure the notification is sent to the correct Slack channel with a low limit.
call mini_budget_on_cortex('MONTH', 10, 'MY_SLACK_NI');



-- Create a task to run the procedure every 30 minutes.
create or replace task run_mini_budget_on_cortex
schedule = '30 minute'
as
call mini_budget_on_cortex('MONTH', 1000, 'MY_SLACK_NI');



-- Make sure the task is running.
alter task run_mini_budget_on_cortex resume;

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#few shot prompting
# "Customer cannot reset password using the forgot password link."
#"Category: Account Access Issue\nUrgency: High\nResponse: Please verify your email and try resetting your password again."
#"Internet connection keeps dropping every few minutes, affecting video calls."
#"Category: Network Issue\nUrgency: Medium\nResponse: Please restart your router and check for service outages in your area."
#"Application crashes with error code 500 when uploading large files."
#"Category: Application Bug\nUrgency: High\nResponse: Could you please provide the file size and error logs? We will check."


-- Create the fine-tuning job pointing at your training data table
SELECT snowflake.cortex.finetune(
'CREATE',
'MY_DB.MY_SCHEMA.CUSTOMER_SUPPORT_FINETUNED',
'llama3.1-8b',
'SELECT prompt, completion FROM MY_DB.MY_SCHEMA.CUSTOMER_SUPPORT_TRAINING',
NULL -- optional validation data query here
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import snowflake.connector

conn = snowflake.connector.connect(
user='YOUR_USER',
password='YOUR_PASSWORD',
account='YOUR_ACCOUNT',
warehouse='YOUR_WAREHOUSE',
database='YOUR_DB',
schema='YOUR_SCHEMA'
)

cs = conn.cursor()

cs.execute("""
SELECT cortex_finetune.create_finetune_job(
model_name => 'llama3.1-8b',
training_table => 'your_schema.your_training_data',
prompt_col => 'prompt',
completion_col => 'completion',
output_model_name => 'custom_llama_model_v1',
max_epochs => 3,
batch_size => 8,
learning_rate => 0.0001,
peft_method => 'lora'
);
""")

print(cs.fetchone())
cs.close()
conn.close()
Loading