Skip to main content

llama-index llms cortex integration

Project description

LlamaIndex Llms Integration: Cortex

Overview

Integrate with Snowflake Cortex API. 3 ways to authenticate:

  1. Snowpark Session object (recommended way) this allows authentication via Snowpark Container Services default token, via Oauth, password, private key, web browser, or any other method.

    Guide to creating sessions: https://docs.snowflake.com/en/developer-guide/snowpark/python/creating-session

  2. Path to a private key file. Encrypted private keys unsupported. For encrypted keys: use a Snowpark Session instead, with the 'private_key_file_pwd' parameter.

  3. JWT token

Installation

pip install llama-index-llms-cortex

Example using a Private Key

import os
from llama_index.llms.cortex import Cortex


llm = Cortex(
    model="llama3.2-1b",
    user=os.environ["YOUR_SF_USER"],
    account=os.environ["YOUR_SF_ACCOUNT"],
    private_key_file=os.environ["PATH_TO_SF_PRIVATE_KEY"],
)

completion_response = llm.complete(
    "write me a haiku about a snowflake", temperature=0.0
)
print(completion_response)

Example Using a Session

import os
from snowflake.snowpark import Session
from llama_index.llms.cortex import Cortex

connection_parameters = {
    "account": "<your snowflake account>",
    "user": "<your snowflake user>",
    "role": "<your snowflake role>",
    "database": "<your snowflake database>",
    "schema": "<your snowflake schema",
    "warehouse": "<your snowflake warehouse>",
    "private_key_file": "<path to file>",
    "authenticator": "JWT_AUTHENTICATOR",  # use this for private key
}
session = Session.builder.configs(connection_parameters).create()

llm = Cortex(
    model="llama3.2-1b",
    user=os.environ["YOUR_SF_USER"],
    account=os.environ["YOUR_SF_ACCOUNT"],
    session=session,
)

completion_response = llm.complete(
    "write me a haiku about a snowflake", temperature=0.0
)
print(completion_response)

Connect in an SPCS environment

# That's it! That's all we need.
llm = Cortex(model="llama3.2-1b")
completion_response = llm.complete(
    "write me a haiku about a snowflake", temperature=0.0
)
print(completion_response)

Create a session within an SPCS environment

import os
from snowflake.snowpark import Session
from llama_index.llms.cortex import Cortex
from llama_index.llms.cortex import utils as cortex_utils

#! Note now the user and role parameters are left blank for SPCS !
connection_parameters = {
    "account": "<your snowflake account>",
    "database": "<your snowflake database>",
    "schema": "<your snowflake schema",
    "warehouse": "<your snowflake warehouse>",
    "token": cortex_utils.get_default_spcs_token(),
    "host": cortex_utils.get_spcs_base_url(),
    "authenticator": "OAUTH",
}
session = Session.builder.configs(connection_parameters).create()

llm = Cortex(model="llama3.2-1b", session=session)

completion_response = llm.complete(
    "write me a haiku about a snowflake", temperature=0.0
)
print(completion_response)

TODO

1 snowflake token counting 2 Pull metadata for snowflake models from Snowflake official documentation (support ticket is out, they said they'll get back to me 4-20-2025)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

llama_index_llms_cortex-0.3.0.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

llama_index_llms_cortex-0.3.0-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file llama_index_llms_cortex-0.3.0.tar.gz.

File metadata

File hashes

Hashes for llama_index_llms_cortex-0.3.0.tar.gz
Algorithm Hash digest
SHA256 3288893af6db285824d3cc90e582056dc6b23f3bd97b17dd74a4de6ff555e199
MD5 373e51828cbe82a79da033e4ee67285f
BLAKE2b-256 99a6bb8686f647db94ed6cd30ca6c14afcb82d79300db1ac9bd47278b5648fdd

See more details on using hashes here.

File details

Details for the file llama_index_llms_cortex-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for llama_index_llms_cortex-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 88c7e5ebd8853526b7dda86e566caa4a40927528d15a94d8f4c1720359958c2a
MD5 96f946b3e38d13476baf3c33cdc2bc83
BLAKE2b-256 b1d94c27d432e6a46d542ed4816a7fd7123c3921c99065fe9c6c8d8a20786e0a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page