WHAT CAN CORTEX FUNCTIONS DO?


Interested in building your own AI-driven data applications and harnessing the power of generative AI through Snowflake Cortex functions? Let’s explore the demo I’ve created in the video above.

Cortex functions have been gaining popularity recently, as they simplify the integration of large language models like Claude 3.5 Sonnet, Llama 3.1-405B, DeepSeek-R1, and Mistral-7B and more into data applications, workflows, and queries. You can explore more about this in our blog here.

With the growing popularity of Cortex functions, I decided to create a simple demonstration showcasing 3 simple use cases. Utilizing Streamlit and Snowpark, I employed the ‘COMPLETE‘ function from Snowflake Cortex, integrating large language models such as Claude 3.5 Sonnet and Llama 3.2-8B.

The use cases demonstrated in the video you can see above are;

  • A simple chatbox
  • Comparing images in JPEG, JPG and PNG format
  • Analyzing images

To implement this data application using Streamlit, Snowpark, and Python, I utilized the following packages. Notably, I imported the Complete function from the snowflake.cortex package. Snowflake Cortex functions can be invoked either through SQL queries or via the snowflake.cortex module in Python. Below, I’ll demonstrate both approaches with code examples.

import os
import toml

from config import *
from snowflake.snowpark.session import Session 
from snowflake.cortex import Complete

import streamlit as st
import pandas as pd  
import numpy as np
import io 
from PIL import Image

Here’s a code snippet I developed that incorporates the chatbox interface in the demo. In this implementation, the Complete function from the snowflake.cortex Python package is utilized to generate responses. Specifically, I employed the llama3.1-8b large language model (LLM), which is optimized for lightweight and rapid inference tasks.

def execute_cortex_complete_api(prompt , session):    
    response_txt = Complete(
                    'llama3.1-8b',
                    prompt,
                    session=session
                    )

    if response_txt is not None: 
        return response_txt
    else:   
        return f"Sorry, I don't know the answer of your question." 

The other code snippet I utilized employs SQL to compare two multidimensional images. In this implementation, I leverage the claude-3-5-sonnet model, which is particularly adept at handling complex visual data. The code compares two images provided through dropdown selections and returns an analysis highlighting the differences between them.


def compare_images_via_cortex_SQL(session, img1_bytes, img2_bytes):
    response = session.sql(
          f"""
                SELECT SNOWFLAKE.CORTEX.COMPLETE('claude-3-5-sonnet',
                PROMPT('Compare this image {{0}} to this image {{1}} and describe the ideal audience for each in two concise bullets no longer than 10 words',
                TO_FILE('@IMAGE_REP', '{img1_bytes}'),
                TO_FILE('@IMAGE_REP', '{img2_bytes}')
                   ));
           """ 
    ).collect()

    if response is not None: 
        return response[0][0]
    else:   
        return "please choose another image, this image is not suitable for processesing. " 
 

As demonstrated, building AI-powered data applications has become remarkably straightforward with Snowflake Cortex. By leveraging fully managed large language models (LLMs) from Snowflake and leading providers like Meta, OpenAI, and others, you can seamlessly integrate generative AI capabilities into your workflows. With the complexity of implementation significantly reduced, the possibilities are limited only by your imagination.



Leave a Reply

Your email address will not be published. Required fields are marked *