### Install Python Dependencies for Local Development Source: https://github.com/snowflake-labs/snowflake-ai-toolkit/blob/main/README.md Command to install all necessary Python libraries for local development of the Snowflake AI Toolkit. These are listed in the requirements.txt file. ```bash pip install -r requirements.txt ``` -------------------------------- ### Run Snowflake AI Toolkit Locally Source: https://github.com/snowflake-labs/snowflake-ai-toolkit/blob/main/README.md This command launches the Snowflake AI Toolkit application locally using the Streamlit framework. Ensure Streamlit is installed and the `streamlit_app.py` file exists in the project root. ```bash streamlit run streamlit_app.py ``` -------------------------------- ### Install Pandas and PyArrow Dependencies Source: https://github.com/snowflake-labs/snowflake-ai-toolkit/blob/main/README.md This command installs the pandas and pyarrow libraries using pip. This is typically used for troubleshooting dependency issues, particularly if their installation fails during the project setup. ```bash pip install pandas pyarrow ``` -------------------------------- ### Create Hybrid Search Service with Snowflake AI Toolkit Source: https://context7.com/snowflake-labs/snowflake-ai-toolkit/llms.txt Sets up and manages Snowflake Cortex Search services for hybrid search capabilities, combining vector and keyword search. This example demonstrates creating a search service for product descriptions and then performing a search query with filters. ```python from src.cortex_functions import create_cortex_search_service from snowflake.core import Root # Create search service create_cortex_search_service( session=session, database="PRODUCT_DB", schema="PUBLIC", table="PRODUCT_CATALOG", column="PRODUCT_DESCRIPTION", attributes=["PRODUCT_NAME", "CATEGORY", "PRICE"], service_name="product_search_service", embedding_model="snowflake-arctic-embed-l", warehouse="COMPUTE_WH" ) # Search using the service root = Root(session) search_service = ( root .databases["PRODUCT_DB"] .schemas["PUBLIC"] .cortex_search_services["product_search_service"] ) results = search_service.search( query="wireless noise cancelling headphones", columns=["product_name", "product_description", "price"], filter={"@gte": {"price": 50}}, limit=10 ) for result in results.results: print(f"Product: {result['product_name']}, Price: ${result['price']}") ``` -------------------------------- ### Create and Manage Cortex Agents in Python Source: https://context7.com/snowflake-labs/snowflake-ai-toolkit/llms.txt This example demonstrates how to create and manage intelligent agents using `CortexAgent` and `CortexAgentManager`. It includes defining agent settings with specific tools (text-to-SQL and search), configuring tool resources, and saving the agent. The snippet also shows how to interact with the agent via chat. ```python from src.cortex_agent import CortexAgent, CortexAgentManager # Initialize agent manager agent_manager = CortexAgentManager(session) # Create agent with SQL and search tools agent_settings = { "model": "llama3.1-70b", "tools": [ { "tool_spec": { "type": "cortex_analyst_text_to_sql", "name": "sales_analyst" } }, { "tool_spec": { "type": "cortex_search", "name": "document_search" } } ], "tool_resources": { "sales_analyst": { "semantic_model_file": "@MODELS.PUBLIC.STAGE/sales_model.yaml" }, "document_search": { "name": "DOCS.PUBLIC.doc_search_service", "max_results": 5, "database": "DOCS", "schema": "PUBLIC" } }, "response_instruction": "Provide concise answers using data and documents. Question: {{.Question}} Context: {{.Context}}", "starter_questions": [ "What were last quarter's sales?", "Find documentation about data policies" ] } # Create and save agent agent = CortexAgent(name="sales_assistant", settings=agent_settings) agent_manager.save_agent(agent) # Chat with agent response, sql = agent.chat( session=session, message="Show me top 5 products by revenue this month" ) print(f"Agent Response: {response}") if sql: print(f"Generated SQL: {sql}") ``` -------------------------------- ### Deploy Snowflake AI Toolkit Natively on Snowflake Source: https://github.com/snowflake-labs/snowflake-ai-toolkit/blob/main/README.md This command deploys the Snowflake AI Toolkit application natively within Snowflake. It requires specifying account details, user credentials, role, warehouse, and database. The `--replace` flag indicates that an existing application with the same name should be replaced. ```bash snow streamlit deploy --account "" --user "" --password "" --role "" --warehouse "" --database "" --replace ``` -------------------------------- ### Clone Snowflake AI Toolkit Repository Source: https://github.com/snowflake-labs/snowflake-ai-toolkit/blob/main/README.md Command to clone the Snowflake AI Toolkit repository from GitHub. This is the first step in setting up the project locally. ```bash git clone https://github.com/sgsshankar/Snowflake-AI-Toolkit.git cd snowflake-ai-toolkit ``` -------------------------------- ### Configure Application Mode in settings_config.json Source: https://github.com/snowflake-labs/snowflake-ai-toolkit/blob/main/README.md Illustrates how to configure the application mode by updating the 'mode' parameter in the src/settings_config.json file. Use 'debug' for local development and 'native' for running in Snowflake. ```json { "mode": "debug" } ``` ```json { "mode": "native" } ``` -------------------------------- ### Fine-Tune Custom Language Model in Python Source: https://context7.com/snowflake-labs/snowflake-ai-toolkit/llms.txt This snippet shows how to fine-tune a language model on a custom dataset. It involves creating a training data table, inserting sample data, and initiating the fine-tuning job using `execute_fine_tune_query`. Finally, it demonstrates how to use the newly fine-tuned model for text completion. ```python from src.query_result_builder import execute_fine_tune_query # Prepare training data table with PROMPT and COMPLETION columns training_data_sql = """ CREATE TABLE CUSTOM_MODEL.PUBLIC.TRAINING_DATA ( PROMPT VARCHAR, COMPLETION VARCHAR ); INSERT INTO CUSTOM_MODEL.PUBLIC.TRAINING_DATA VALUES ('Translate to SQL: Show all customers', 'SELECT * FROM customers'), ('Translate to SQL: Count orders by status', 'SELECT status, COUNT(*) FROM orders GROUP BY status'); """ session.sql(training_data_sql).collect() # Start fine-tuning job tracking_id = execute_fine_tune_query( session=session, db="CUSTOM_MODEL", schema="PUBLIC", train_table="TRAINING_DATA", validation_table="VALIDATION_DATA", base_model="mistral-7b", new_model_name="sql_translator_model" ) print(f"Fine-tuning job started. Tracking ID: {tracking_id}") # Use fine-tuned model result = get_complete_result( session=session, model=f"CUSTOM_MODEL.PUBLIC.sql_translator_model", prompt="Translate to SQL: Show revenue by product category", temperature=0.3, max_tokens=150, guardrails=False ) ``` -------------------------------- ### Configure Snowflake User with RSA Public Key Source: https://github.com/snowflake-labs/snowflake-ai-toolkit/blob/main/README.md This SQL snippet demonstrates how to update a Snowflake user's configuration by setting their RSA public key. The public key content should be pasted directly into the command, excluding the PEM headers. The `desc user` command is then used to retrieve the public key fingerprint. ```sql alter user set rsa_public_key=''; desc user ; ``` -------------------------------- ### Create RAG System with Snowflake AI Toolkit Source: https://context7.com/snowflake-labs/snowflake-ai-toolkit/llms.txt Builds retrieval-augmented generation (RAG) systems using document embeddings for context-aware question answering. This involves creating vector embeddings from documents stored in a Snowflake stage and then querying the system. ```python from src.cortex_functions import create_vector_embedding_from_stage from pathlib import Path import io # Upload documents to stage stage_config = { 'database': 'KNOWLEDGE_BASE', 'schema': 'PUBLIC', 'stage': 'DOCUMENTS' } # Create vector embeddings from documents create_vector_embedding_from_stage( session=session, db=stage_config['database'], schema=stage_config['schema'], stage=stage_config['stage'], embedding_type="EMBED_TEXT_768", embedding_model="snowflake-arctic-embed-m", output_table="DOCUMENT_EMBEDDINGS" ) # Query the RAG system query = "What are the company's vacation policies?" rag_query = f""" WITH relevant_docs AS ( SELECT chunk, VECTOR_COSINE_SIMILARITY( vector_embeddings, SNOWFLAKE.CORTEX.EMBED_TEXT_768('snowflake-arctic-embed-m', '{query}') ) as similarity FROM {stage_config['database']}.{stage_config['schema']}.DOCUMENT_EMBEDDINGS ORDER BY similarity DESC LIMIT 5 ) SELECT SNOWFLAKE.CORTEX.COMPLETE( 'llama3.1-70b', ARRAY_CONSTRUCT( OBJECT_CONSTRUCT('role', 'system', 'content', 'Answer based on provided context'), OBJECT_CONSTRUCT('role', 'user', 'content', 'Context: ' || LISTAGG(chunk, ' ') || ' Question: {query}') ) ) as answer FROM relevant_docs """ session.sql(rag_query).collect()[0]['ANSWER'] ``` -------------------------------- ### Multimodal Complete for Image and Text Analysis in Python Source: https://context7.com/snowflake-labs/snowflake-ai-toolkit/llms.txt This snippet illustrates how to use the multimodal `get_complete_multimodal_result` function to process images combined with text prompts. It assumes an image is uploaded to a Snowflake stage and then analyzes it by providing a descriptive prompt, enabling visual question answering and detailed image analysis. ```python from src.cortex_functions import get_complete_multimodal_result # Upload image to stage # Assume image is uploaded to @IMAGE_STAGE/products/laptop.jpg # Analyze image with prompt result = get_complete_multimodal_result( session=session, model="claude-3-5-sonnet", prompt="Describe this product in detail, including colors, features, and condition", stage="PRODUCT_DB.PUBLIC.IMAGE_STAGE", files=["products/laptop.jpg"] ) print(f"Image analysis: {result}") ``` -------------------------------- ### Perform Multi-Image Comparison with Claude 3.5 Sonnet Source: https://context7.com/snowflake-labs/snowflake-ai-toolkit/llms.txt This Python code snippet demonstrates how to perform a multi-image comparison using the Snowflake AI Toolkit. It requires an active Snowflake session, specifies the model, prompt, stage, and files for comparison. The function returns a complete multimodal result. ```python comparison_result = get_complete_multimodal_result( session=session, model="claude-3-5-sonnet", prompt="Compare these two product images and highlight differences", stage="PRODUCT_DB.PUBLIC.IMAGE_STAGE", files=["products/laptop_v1.jpg", "products/laptop_v2.jpg"] ) ``` -------------------------------- ### Text Generation with Cortex AI Models - Python Source: https://context7.com/snowflake-labs/snowflake-ai-toolkit/llms.txt Generates text completions using Snowflake Cortex language models. Supports system prompts, temperature control, and guardrails. Requires an active Snowflake session and the `cortex_functions` module. ```python from src.cortex_functions import get_complete_result import json # Initialize Snowflake session session = Session.builder.configs({ "account": "your_account", "user": "your_user", "password": "your_password", "role": "ACCOUNTADMIN", "warehouse": "COMPUTE_WH", "database": "AI_TOOLKIT_DB", "schema": "PUBLIC" }).create() # Generate text completion with guardrails result = get_complete_result( session=session, model="llama3.1-70b", prompt="Explain quantum computing in simple terms", temperature=0.7, max_tokens=500, guardrails=True, system_prompt="You are a helpful AI assistant specializing in technology education." ) # Parse the response if isinstance(result, dict) and 'choices' in result: generated_text = result['choices'][0]['messages'] print(f"Generated: {generated_text}") print(f"Tokens used: {result.get('usage', {})}") ``` -------------------------------- ### Extract Information from Text using Snowflake AI Toolkit Source: https://context7.com/snowflake-labs/snowflake-ai-toolkit/llms.txt Demonstrates how to extract specific information from text using Snowflake's AI functions. It supports both single text extraction and batch extraction from a table column. Requires an active session and specifies the text source and the query prompt. ```python from src.cortex_functions import get_extraction, get_extraction_from_column # Extract information from single text document = """ John Smith joined Acme Corporation on January 15, 2020 as Senior Engineer. His annual salary is $120,000 and he reports to Jane Doe in the Engineering department. """ name = get_extraction( session=session, text=document, query_text="What is the employee's name?" ) print(f"Extracted name: {name}") # Batch extraction from table get_extraction_from_column( session=session, db="HR_DATA", schema="PUBLIC", table="EMPLOYMENT_CONTRACTS", input_column="CONTRACT_TEXT", query_text="What is the employee's start date?", output_table="EXTRACTED_START_DATES", output_column="START_DATE" ) ``` -------------------------------- ### Track AI Usage and Notifications in Python Source: https://context7.com/snowflake-labs/snowflake-ai-toolkit/llms.txt This code demonstrates how to track AI function calls, token usage, and estimated costs. It includes functions for manually logging usage (`log_cortex_usage`), adding notification entries for ongoing operations (`add_notification_entry`), updating notification statuses (`update_notification_entry`), and querying usage statistics from a dedicated log table. ```python from src.notification import log_cortex_usage, add_notification_entry, update_notification_entry from datetime import datetime # Log usage manually log_cortex_usage( session=session, function_name="COMPLETE", model_name="llama3.1-70b", total_tokens=1250, estimated_credits=0.125, start_time=datetime.now(), end_time=datetime.now(), details="Customer review analysis batch job" ) # Track async operations notification_id = add_notification_entry( session=session, functionality="RAG Pipeline", status="In-Progress", details="Processing 10,000 documents for embedding" ) # Update notification on completion update_notification_entry(session, notification_id, "Success") # Query usage statistics usage_report = session.sql(""" SELECT function_name, model_name, SUM(total_tokens) as total_tokens, SUM(estimated_credits) as total_credits, COUNT(*) as call_count FROM AI_TOOLKIT_DB.PUBLIC.CORTEX_USAGE_LOG WHERE created_at >= DATEADD(day, -7, CURRENT_TIMESTAMP()) GROUP BY function_name, model_name ORDER BY total_credits DESC """).collect() ``` -------------------------------- ### Batch Text Processing from Table Columns - Python Source: https://context7.com/snowflake-labs/snowflake-ai-toolkit/llms.txt Processes entire table columns using language model completions and writes results to output tables. Utilizes the `get_complete_result_from_column` function. Requires a Snowflake session and specific table/column configurations. ```python from src.cortex_functions import get_complete_result_from_column # Define source and destination input_config = { 'database': 'CUSTOMER_DATA', 'schema': 'RAW', 'table': 'CUSTOMER_REVIEWS', 'input_column': 'REVIEW_TEXT', 'output_table': 'PROCESSED_REVIEWS', 'output_column': 'SENTIMENT_ANALYSIS' } # Process entire column with AI get_complete_result_from_column( session=session, model="mistral-large2", db=input_config['database'], schema=input_config['schema'], table=input_config['table'], input_column=input_config['input_column'], temperature=0.3, max_tokens=200, guardrails=True, output_table=input_config['output_table'], output_column=input_config['output_column'], system_prompt="Analyze sentiment and extract key themes", user_prompt="Analyze the following review:" ) # Query results results = session.sql(f""" SELECT {input_config['input_column']}, {input_config['output_column']} FROM {input_config['database']}.{input_config['schema']}.{input_config['output_table']} LIMIT 10 """).collect() ``` -------------------------------- ### Generate RSA Key Pair using OpenSSL Source: https://github.com/snowflake-labs/snowflake-ai-toolkit/blob/main/README.md This snippet generates an RSA private key and its corresponding public key using OpenSSL commands. The private key is saved in PKCS#8 PEM format, and the public key is saved in PEM format. These keys are essential for JWT-based authentication. ```bash openssl genrsa 2048 | openssl pkcs8 -topk8 -inform PEM -out rsa_private_key.p8 -nocrypt openssl rsa -in rsa_private_key.p8 -pubout -out rsa_public_key.pub ``` -------------------------------- ### Batch Sentiment Analysis using Snowflake AI Toolkit Source: https://context7.com/snowflake-labs/snowflake-ai-toolkit/llms.txt Performs batch sentiment analysis on a specified text column in a Snowflake table. It requires an active Snowflake session and specifies input/output tables and columns. The function analyzes sentiment and outputs a score. ```python get_sentiment_from_column( session=session, db="FEEDBACK_DB", schema="PUBLIC", table="CUSTOMER_FEEDBACK", input_column="FEEDBACK_TEXT", output_table="FEEDBACK_SENTIMENTS", output_column="SENTIMENT_SCORE" ) sentiment_summary = session.sql(""" SELECT CASE WHEN CAST(SENTIMENT_SCORE AS FLOAT) > 0.5 THEN 'Positive' WHEN CAST(SENTIMENT_SCORE AS FLOAT) < -0.5 THEN 'Negative' ELSE 'Neutral' END as sentiment_category, COUNT(*) as count FROM FEEDBACK_DB.PUBLIC.FEEDBACK_SENTIMENTS GROUP BY sentiment_category """).collect() ``` -------------------------------- ### Sentiment Analysis with Cortex AI - Python Source: https://context7.com/snowflake-labs/snowflake-ai-toolkit/llms.txt Analyzes the sentiment of text content and returns sentiment scores using Snowflake Cortex. Supports analysis of single text inputs and batch processing of table columns. Requires a Snowflake session and the `cortex_functions` module. ```python from src.cortex_functions import get_sentiment, get_sentiment_from_column # Analyze single text sentiment sentiment_score = get_sentiment( session=session, text="This product exceeded my expectations! Absolutely fantastic quality." ) print(f"Sentiment score: {sentiment_score}") ``` -------------------------------- ### Language Translation with Cortex AI - Python Source: https://context7.com/snowflake-labs/snowflake-ai-toolkit/llms.txt Translates text between languages using Snowflake Cortex translation capabilities. Supports single text translation and batch translation of table columns. Requires a Snowflake session and the `cortex_functions` module. ```python from src.cortex_functions import get_translation, get_translation_from_column # Single text translation translated_text = get_translation( session=session, text="Hello, how are you today?", source_lang="en", target_lang="es" ) print(f"Translated: {translated_text}") # Batch translate table column get_translation_from_column( session=session, db="GLOBAL_CONTENT", schema="PUBLIC", table="PRODUCT_DESCRIPTIONS", input_column="DESCRIPTION_EN", source_lang="en", target_lang="fr", output_table="PRODUCT_DESCRIPTIONS_FR", output_column="DESCRIPTION_FR" ) ``` -------------------------------- ### Summarize Text using Snowflake AI Toolkit Source: https://context7.com/snowflake-labs/snowflake-ai-toolkit/llms.txt Enables text summarization using Snowflake's AI capabilities. This function can summarize a single document or perform batch summarization on a column within a Snowflake table. It requires an active session and specifies the input text or table details. ```python from src.cortex_functions import get_summary, get_summary_from_column # Summarize single document long_article = """ [Long article text about artificial intelligence developments...]""" summary = get_summary( session=session, text=long_article ) print(f"Summary: {summary}") # Batch summarization get_summary_from_column( session=session, db="CONTENT_DB", schema="ARTICLES", table="NEWS_ARTICLES", input_column="ARTICLE_BODY", output_table="ARTICLE_SUMMARIES", output_column="SUMMARY" ) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.