Skip to content

Commit 8bdd146

Browse files
sfc-gh-ralfaroviquezsfc-gh-osalazarlizanosfc-gh-cgorrie
authored
SNOW-1449796 Snowflake Cortex Example (snowflakedb#5)
* cortex initial commit * cortex initial commit missing file * draft for cortex, missing db integrations and several improvements * minor improvements, using real dataset * using cortex from ml library, adding chat, adding test sample * testing attempts * fix test * Update dashboard.py * tests fixed, added instructions in the readme * trying to fix ci runtest flow * adding cortex lib to ci env * adding snowflake-cortex project to readme index * Update README.md Co-authored-by: Cam Gorrie <cam.gorrie@snowflake.com> * Update snowflake-cortex/README.md Co-authored-by: Cam Gorrie <cam.gorrie@snowflake.com> * Update snowflake-cortex/README.md Co-authored-by: Cam Gorrie <cam.gorrie@snowflake.com> * addressing comments * changing name to dataset columns and db, fixing some wrong commands, deleting version from manifest * repeated dot deletion * changing name from dashboard to ui * added logic for ungiven permissions * fixing tests and adding more * pushing to fix path error * attempt to fix the path, deleting init.py --------- Co-authored-by: Oscar Salazar <oscar.salazarlizano@snowflake.com> Co-authored-by: Cam Gorrie <cam.gorrie@snowflake.com>
1 parent 9693c6f commit 8bdd146

File tree

19 files changed

+336
-3
lines changed

19 files changed

+336
-3
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ jobs:
104104
python -m pip install pytest
105105
- name: Run tests
106106
run: |
107-
args=${{ steps.tests_to_run.outputs.pytestArgs }}
108-
pythonpath=${{ steps.tests_to_run.outputs.pytestPaths }}
107+
args="${{ steps.tests_to_run.outputs.pytestArgs }}"
108+
pythonpath="${{ steps.tests_to_run.outputs.pytestPaths }}"
109109
if [ -z "${args}" ] || [ -z "${pythonpath}" ]; then
110110
echo “Nothing to test”
111111
else

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Some applications require other account-level setup before they can be properly
2727
| [Object-level References](./object-level-references/) | A simple dashboard to show how to interact with the object-level references and bindings. |
2828
| [Reference Usage](./reference-usage/) | How to share a provider table with a native application whose data is replicated to any consumer in the data cloud. |
2929
| [SPCS Three-tier](./spcs-three-tier/) | A simple three-tiered web app that can be deployed in Snowpark Container Services. It queries the TPC-H 100 data set and returns the top sales clerks. |
30+
| [Snowflake Cortex](./snowflake-cortex/) | A simple example on how to implement the Cortex Complete and to make it interact with user data. |
3031
| [Tasks and Streams](./tasks-streams/) | How to execute a task and visualize changes using streams within a native application. |
3132

3233
## Contributing

object-level-references/app/manifest.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ version:
99
artifacts:
1010
setup_script: setup_script.sql
1111
readme: README.md
12-
default_streamlit: core.dashboard
12+
default_streamlit: core.ui
1313
extension_code: true
1414

1515
references:

shared_python_ci_env.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ dependencies:
1212
- snowflake-cli-labs>=2.0.0
1313
- pytest
1414
- streamlit>=1.26.0
15+
- snowflake-ml-python
1516

snowflake-cortex/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
snowflake.local.yml
2+
output/
3+
**/__pycache__/
4+
**/.pytest_cache/

snowflake-cortex/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Snowflake Cortex
2+
3+
This simple Native App shows how to use Cortex Complete and make it interact with user data.
4+
5+
For this use case, the dataset used is rather small: only 10 entries. This is because the language model used in the Cortex function restricts input data size. You can change the language model used to a bigger / different one, according to your needs.
6+
For more information about it please visit **[this page](https://docs.snowflake.com/en/user-guide/snowflake-cortex/llm-functions#cost-considerations)**.
7+
8+
## Data preparation
9+
10+
To run this example first execute this command, that is going to create a database and table with information about songs charts:
11+
```sh
12+
snow sql -f 'prepare/provider_data.sql'
13+
```
14+
## App execution
15+
16+
Then run `snow app run` on your terminal.
17+
18+
## App and Data Deletion
19+
To delete the database and the app run
20+
21+
```sh
22+
snow sql -q 'DROP DATABASE SONGS_CORTEX_DB;'
23+
snow app teardown
24+
```
25+
26+
## Further reading
27+
28+
For more information about the different ways to use snowflake AI capabilities visit this page:
29+
**[Snowflake AI and ML documentation](https://docs.snowflake.com/en/guides-overview-ai-features)**

snowflake-cortex/app/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Snowflake Native App - Snowflake Cortex
2+
3+
This is a sample Snowflake Native App that shows the use of Snowflake Cortex inside Native Apps.

snowflake-cortex/app/manifest.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# For more information on creating manifest, go to https://docs.snowflake.com/en/developer-guide/native-apps/creating-manifest
2+
manifest_version: 1
3+
4+
artifacts:
5+
setup_script: setup_script.sql
6+
readme: README.md
7+
default_streamlit: core.ui
8+
extension_code: true
9+
10+
privileges:
11+
- IMPORTED PRIVILEGES ON SNOWFLAKE DB:
12+
description: "Imported privileges to use cortex DB"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
-- This is the setup script that runs while installing a Snowflake Native App in a consumer account.
2+
-- For more information on how to create setup file, visit https://docs.snowflake.com/en/developer-guide/native-apps/creating-setup-script
3+
4+
-- A general guideline to building this script looks like:
5+
-- 1. Create application roles
6+
CREATE APPLICATION ROLE IF NOT EXISTS app_public;
7+
8+
-- 2. Create a versioned schema to hold those UDFs/Stored Procedures
9+
CREATE OR ALTER VERSIONED SCHEMA core;
10+
GRANT USAGE ON SCHEMA core TO APPLICATION ROLE app_public;
11+
12+
-- 3. Create a streamlit object using the code you wrote in you wrote in src/module-ui, as shown below.
13+
-- The `from` value is derived from the stage path described in snowflake.yml
14+
CREATE STREAMLIT core.ui
15+
FROM '/streamlit/'
16+
MAIN_FILE = 'dashboard.py';
17+
18+
-- 4. Grant appropriate privileges over these objects to your application roles.
19+
GRANT USAGE ON STREAMLIT core.ui TO APPLICATION ROLE app_public;
20+
21+
-- A detailed explanation can be found at https://docs.snowflake.com/en/developer-guide/native-apps/adding-streamlit
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# This file is used to install packages for local testing
2+
name: snowflake-cortex-testing
3+
channels:
4+
- snowflake
5+
dependencies:
6+
- python=3.8
7+
- pip
8+
- pip:
9+
- snowflake-native-apps-permission-stub
10+
- snowflake-snowpark-python>=1.15.0
11+
- snowflake-cli-labs>=2.0.0
12+
- pytest
13+
- streamlit>=1.26.0
14+
- snowflake-ml-python
15+

0 commit comments

Comments
 (0)