How to Set Up Snowflake’s Enterprise LLM Artic in Snowflake using Replicate

Mebin Joy
3 min readApr 27, 2024

--

Snowflake Artic

This article serves as a comprehensive guide on configuring and utilizing Artic in Snowflake without relying on Cortex, which is presently in private preview. By leveraging Replicate as the API provider for Artic and employing some Python scripting along with Snowflake’s adaptable architecture, you can seamlessly integrate Artic into your Snowflake environment.

Step 1: Sign Up for Replicate

Begin by signing up for Replicate using the provided link: https://replicate.com/snowflake/snowflake-arctic-instruct. You can conveniently use your GitHub account for registration. After signing up, navigate to your account and copy the Replicate token provided. With this token, your Replicate setup is complete. Additionally, you can experiment with Artic via the following link: [https://replicate.com/snowflake/snowflake-arctic-instruct].

Step 2: Configure Network Rules

Execute the following command to configure the network rule:

CREATE OR REPLACE NETWORK RULE REPLICATE_RULE
TYPE = ‘HOST_PORT’
MODE= ‘EGRESS’
VALUE_LIST = (‘api.replicate.com’);

This command establishes the network rule necessary for Snowflake to communicate with Replicate. Further details about network rules can be found [here](https://docs.snowflake.com/en/user-guide/network-rules).

Step 3: Configure Replicate Token

Utilize the following command to configure the Replicate token. Ensure to replace `<token>` with your actual token:

CREATE OR REPLACE SECRET REPLICATE_KEY
TYPE = GENERIC_STRING
SECRET_STRING = ‘<token>’;

For more information regarding secrets, refer to the Snowflake documentation on [secrets](https://docs.snowflake.com/en/sql-reference/sql/create-secret).

Step 4: Create External Access Integrations

Execute the following command to create external access integrations:

CREATE OR REPLACE EXTERNAL ACCESS INTEGRATION REPLICATE_ACCESS_INTEGRATION
ALLOWED_NETWORK_RULES = (REPLICATE_RULE)
ALLOWED_AUTHENTICATION_SECRETS = (REPLICATE_KEY)
ENABLED = true;

This step enables the connection to Replicate from Snowflake.

Step 5: Create a Python UDF

Next, create a Python UDF for accessing Artic from Replicate. Here’s the code:

CREATE OR REPLACE FUNCTION ARTIC(query varchar)
RETURNS STRING
LANGUAGE PYTHON
RUNTIME_VERSION = 3.10
HANDLER = ‘artic’
EXTERNAL_ACCESS_INTEGRATIONS = (REPLICATE_ACCESS_INTEGRATION)
SECRETS = (‘REPLICATE_API_TOKEN’ = REPLICATE_KEY)
PACKAGES = (‘requests’)
AS
$$
import requests
import _snowflake

REPLICATE_API_TOKEN = _snowflake.get_generic_secret_string(‘REPLICATE_API_TOKEN’)

def artic(query:str) :
# Define the request payload
request_payload = {
“input”: {
“prompt”: query,
“temperature”: 0.2,
“stop_sequences”: ‘im_end’
}
}

# Make the API request
response = requests.post(
https://api.replicate.com/v1/models/snowflake/snowflake-arctic-instruct/predictions",
headers={
“Authorization”: f”Bearer {REPLICATE_API_TOKEN}”,
“Content-Type”: “application/json”
},
json=request_payload
)

# Extract the prediction
prediction = response.json()
pred_id = prediction[‘id’]
preds = requests.get(url=f”
https://api.replicate.com/v1/predictions/{pred_id}",headers={
“Authorization”: f”Bearer {REPLICATE_API_TOKEN}”,
“Content-Type”: “application/json”
})
while preds.json()[‘status’] != ‘succeeded’:
preds = requests.get(url=f”
https://api.replicate.com/v1/predictions/{pred_id}”,headers={
“Authorization”: f”Bearer {REPLICATE_API_TOKEN}”,
“Content-Type”: “application/json”
})
final_res = preds.json()
return(‘’.join(final_res[‘output’]))

$$;

Step 6: Verification

You have now successfully enabled Artic in your Snowflake instance as a function. Verify its functionality by executing the following command:

SELECT ARTIC(‘Introduce yourself’);

By following these steps, you can integrate Artic into your Snowflake environment seamlessly. Stay connected for more insights and explorations into Snowflake Artic.

Here’s a link to learn more about Snowflake Artic — https://www.snowflake.com/blog/arctic-open-efficient-foundation-language-models-snowflake/

--

--

Mebin Joy

AI Alchemist: Mastering Data Science and Engineering Wizardry ✨ | AI Engineer | Data Scientist