Skip to content

Commit

Permalink
tests fixed, added instructions in the readme
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-ralfaroviquez committed Jun 25, 2024
1 parent 8f083b4 commit 3fd5d18
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 14 deletions.
14 changes: 12 additions & 2 deletions snowflake-cortex/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
This Native App examplifies how to implement the Cortex Complete and to make it interact with user data.

For this usecase, the dataset used is rather small, with only 10 entries, this is because the language model used in the Cortex function restricts the bytes of input data size and costs. YOu can change the language model used to a bigger or different style according to your necessities.
For more information about it please visit **[this page](https://docs.snowflake.com/en/user-guide/snowflake-cortex/llm-functions#cost-considerations)**.

To run this example first execute this command:
```bash
```sh
snow sql -f 'prepare/prepare_data.sql'
```

Then run `snow app run` on your terminal.

Currently it has a very short dataset due to llm model bytes limit.
To delete the database and the app run

```sh
snow sql -q 'DROP DATABASE SPOTIFY_CORTEX_DB;'
snow app teardown
```
9 changes: 7 additions & 2 deletions snowflake-cortex/src/ui/cortexCaller.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

class CortexCaller:

def call_complete(_self,input_with_table):
response = Complete('llama2-70b-chat', input_with_table)
return response

def call_cortex(_self, df, input: str):
sample_df = df.to_string()
input_with_table = f"""The following data is a table that contains songs information it is wrapped in this tags <dataset>{sample_df}</dataset> {input}"""
response = Complete('llama2-70b-chat', input_with_table)
return response
response = _self.call_complete(input_with_table)
return response

4 changes: 2 additions & 2 deletions snowflake-cortex/src/ui/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class Dashboard:

def __init__(self, session: Session = Session.builder.getOrCreate(), cortex_caller: CortexCaller = CortexCaller()) -> None:
def __init__(self, session: Session, cortex_caller: CortexCaller = CortexCaller()) -> None:
self.session = session
self.cortex_caller = cortex_caller

Expand All @@ -23,4 +23,4 @@ def run_streamlit(self):
cortex_chat.chat_message("assistant").write(cortex_fn)

if __name__ == '__main__':
Dashboard().run_streamlit()
Dashboard(Session.builder.getOrCreate()).run_streamlit()
20 changes: 20 additions & 0 deletions snowflake-cortex/test/test_cortex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import unittest
from unittest.mock import patch, MagicMock
import pandas as pd
import pytest as pytest
from snowflake import cortex
from snowflake.cortex import Complete

class TestDataExporter(unittest.TestCase):

def test_cortex_response(self):
from cortexCaller import CortexCaller

mock_df = pd.DataFrame([{"TestColumn": "TestValue"}])
mock_input = 'Test input from user'
cortexCaller = CortexCaller()
cortexCaller.call_complete = MagicMock(name='call_complete')
cortexCaller.call_complete.return_value = 'test answer from cortex'
response = cortexCaller.call_cortex(mock_df, mock_input)
cortexCaller.call_complete.assert_called_once()
assert response == cortexCaller.call_complete.return_value
30 changes: 22 additions & 8 deletions snowflake-cortex/test/test_ui.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from dashboard import Dashboard
from cortexCaller import CortexCaller
from streamlit.testing.v1 import AppTest
from snowflake.snowpark import Session
Expand Down Expand Up @@ -30,18 +29,33 @@ def script(session, cortex_caller):
at = AppTest.from_function(script, kwargs={ "session": session, "cortex_caller": cortex_caller }).run()

at.chat_input(key='cortex_input').set_value('text for testing purposes').run()
assert at.chat_message[0].name == 'user'
call_cortex.assert_called_once()
assert at.chat_message[1].name == 'assistant'

def test_input_showed_in_chat():
dashboard = Dashboard(session)
at = AppTest.from_function(dashboard.run_streamlit).run()
@patch('cortexCaller.CortexCaller.call_cortex')
@patch('snowflake.snowpark.session.Session.table')
def test_input_showed_in_chat(table: MagicMock, call_cortex: MagicMock, session, cortex_caller):

def script(session, cortex_caller):
from dashboard import Dashboard
dashboard = Dashboard(session, cortex_caller)
return dashboard.run_streamlit()

table.return_value=session.create_dataframe([{"TestColumn": "TestValue"}])
at = AppTest.from_function(script, kwargs={ "session": session, "cortex_caller": cortex_caller }).run()
at.chat_input(key='cortex_input').set_value('text for testing purposes').run()
assert at.chat_message[0].name == 'user'

def test_input_empty():
dashboard = Dashboard(session)
at = AppTest.from_function(dashboard.run_streamlit).run()
@patch('cortexCaller.CortexCaller.call_cortex')
@patch('snowflake.snowpark.session.Session.table')
def test_input_empty(table: MagicMock, call_cortex: MagicMock, session, cortex_caller):
def script(session, cortex_caller):
from dashboard import Dashboard
dashboard = Dashboard(session, cortex_caller)
return dashboard.run_streamlit()

table.return_value=session.create_dataframe([{"TestColumn": "TestValue"}])
at = AppTest.from_function(script, kwargs={ "session": session, "cortex_caller": cortex_caller }).run()
at.chat_input(key='cortex_input').set_value(' ').run()
assert not at.chat_message

Expand Down

0 comments on commit 3fd5d18

Please sign in to comment.