Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add finance advisor spanner demo #901

Merged
merged 86 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
86 commits
Select commit Hold shift + click to select a range
a89eec7
initial commit
anirbanbagchi1979 Jul 22, 2024
fc071d3
Readme updates
anirbanbagchi1979 Jul 23, 2024
d645c45
readme updates
anirbanbagchi1979 Jul 23, 2024
396cf2c
modififed network param in app,yaml
anirbanbagchi1979 Jul 23, 2024
12edd21
env file added
anirbanbagchi1979 Jul 24, 2024
7c3d8d8
Env Added
anirbanbagchi1979 Jul 24, 2024
e6fa4bd
modifications for CloudRun Deployment
anirbanbagchi1979 Jul 24, 2024
568c169
modifications on formatting
anirbanbagchi1979 Jul 24, 2024
a6e81fa
Added embedding vector data update scripts
anirbanbagchi1979 Jul 24, 2024
3a4f6cc
more cleanup readme
anirbanbagchi1979 Jul 24, 2024
14d416e
Pylance Cleanup
anirbanbagchi1979 Jul 24, 2024
d9ad2a8
renamed the graph file
anirbanbagchi1979 Jul 24, 2024
29ca3a2
fix: Graph file name
anirbanbagchi1979 Jul 24, 2024
5eb2bd7
fix: Graph Viz
anirbanbagchi1979 Jul 24, 2024
e654e18
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Jul 25, 2024
c538a76
fix: finance-advisor-spanner: Removes App Engine Deployment Options
anirbanbagchi1979 Jul 25, 2024
87b85ac
fix: finance-advisor-spanner remove references to App Engine
anirbanbagchi1979 Jul 25, 2024
50536fc
fix: finance-advisor-spanner Fixed app.yaml reference to Home.py
anirbanbagchi1979 Jul 25, 2024
2351317
fix: finance-advisor-spanner Env file changd to TMP and added instruc…
anirbanbagchi1979 Jul 25, 2024
ad981ab
fix: finance-advisor-spanner Remove .env file
anirbanbagchi1979 Jul 25, 2024
ab8d4af
Merge branch 'main' into finance-advisor-spanner
holtskinner Jul 25, 2024
de55c14
Add terms to allowlist
holtskinner Jul 25, 2024
d286f8b
fix: finance-advisor-spanner Issues from 901 code review resolved
anirbanbagchi1979 Jul 26, 2024
e92088e
fix: finance-advisor-spanner 901 fixes
anirbanbagchi1979 Jul 26, 2024
9aaeb23
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Jul 26, 2024
f76f97e
fix: finance-advisor-spanner lint errros resolved
anirbanbagchi1979 Jul 26, 2024
6385a25
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Jul 26, 2024
228f223
fix: finance-advisor-spanner lint code fixes
anirbanbagchi1979 Jul 26, 2024
a1142d7
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Jul 26, 2024
614743a
Merge branch 'main' into finance-advisor-spanner
holtskinner Jul 29, 2024
2a155db
Merge branch 'main' into finance-advisor-spanner
holtskinner Jul 31, 2024
e5032f9
Merge branch 'main' into finance-advisor-spanner
holtskinner Jul 31, 2024
4e77493
fix: finance-advisor-spanner 901 fixes
anirbanbagchi1979 Jul 31, 2024
aa06c25
fix: finance-advisor-spanner lint errors resovled
anirbanbagchi1979 Jul 31, 2024
061caf4
Merge branch 'main' into finance-advisor-spanner
holtskinner Jul 31, 2024
3eeed3c
Add sqlfluff ignore
holtskinner Jul 31, 2024
5d8ebfb
Update lint ignore for Spanner SQL Files
holtskinner Jul 31, 2024
7e1b519
Update filterregexexclude
holtskinner Jul 31, 2024
8d9cfb1
fix: finance-advisor-spanner fixed lint errors
anirbanbagchi1979 Aug 1, 2024
5da3e42
fix: finance-advisor-spanner remove single quotes linting error
anirbanbagchi1979 Aug 1, 2024
dfe6098
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Aug 1, 2024
400abd5
Merge branch 'finance-advisor-spanner' of https://github.com/GoogleCl…
gcf-owl-bot[bot] Aug 1, 2024
d31eb9e
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Aug 1, 2024
cff7c08
Merge branch 'finance-advisor-spanner' of https://github.com/GoogleCl…
gcf-owl-bot[bot] Aug 1, 2024
b6fb915
fix: finance-advisor-spanner more lint changed
anirbanbagchi1979 Aug 1, 2024
e63af94
fix: finance-advisor-spanner increasing reuse
anirbanbagchi1979 Aug 1, 2024
bf7e377
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Aug 1, 2024
8fd61d8
fix: finance-advisor-spanner
anirbanbagchi1979 Aug 1, 2024
23a218d
fix: finance-advisor-spanner
anirbanbagchi1979 Aug 1, 2024
518b08b
Update gemini/sample-apps/finance-advisor-spanner/pages/3_Graph_Visua…
holtskinner Aug 5, 2024
277611a
Update gemini/sample-apps/finance-advisor-spanner/pages/4_Exposure_Ch…
holtskinner Aug 5, 2024
c0910a0
Apply suggestions from code review
holtskinner Aug 5, 2024
354a689
Merge branch 'main' into finance-advisor-spanner
holtskinner Aug 5, 2024
a24f0fe
Merge branch 'main' into finance-advisor-spanner
holtskinner Aug 6, 2024
fcfdeba
Update gemini/sample-apps/finance-advisor-spanner/README.md
anirbanbagchi1979 Aug 9, 2024
8e5ae28
Update gemini/sample-apps/finance-advisor-spanner/README.md
anirbanbagchi1979 Aug 9, 2024
df3b448
Update gemini/sample-apps/finance-advisor-spanner/pages/1_Asset_Searc…
anirbanbagchi1979 Aug 9, 2024
16b2c90
Update gemini/sample-apps/finance-advisor-spanner/pages/2_Semantic_Se…
anirbanbagchi1979 Aug 9, 2024
a0c06ac
fix: finance-advisor-spanner code fixes
anirbanbagchi1979 Aug 9, 2024
2eadd1a
Update gemini/sample-apps/finance-advisor-spanner/pages/1_Asset_Searc…
anirbanbagchi1979 Aug 9, 2024
8ddd3b2
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Aug 9, 2024
a142aa2
Merge branch 'finance-advisor-spanner' of https://github.com/GoogleCl…
gcf-owl-bot[bot] Aug 9, 2024
40ce8e2
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Aug 9, 2024
4a02ae7
Merge branch 'finance-advisor-spanner' of https://github.com/GoogleCl…
gcf-owl-bot[bot] Aug 9, 2024
b32f72e
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Aug 9, 2024
ab50f62
Merge branch 'finance-advisor-spanner' of https://github.com/GoogleCl…
gcf-owl-bot[bot] Aug 9, 2024
e4c509a
fix: took care of common code
anirbanbagchi1979 Aug 9, 2024
e93fdd9
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Aug 9, 2024
b5b4690
Merge branch 'finance-advisor-spanner' of https://github.com/GoogleCl…
anirbanbagchi1979 Aug 9, 2024
acf257d
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Aug 9, 2024
990c75e
Merge branch 'finance-advisor-spanner' of https://github.com/GoogleCl…
anirbanbagchi1979 Aug 9, 2024
7d65c5a
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Aug 9, 2024
9c40654
fix: duplicate function definition
anirbanbagchi1979 Aug 9, 2024
b75666b
fix: rename Home to home
anirbanbagchi1979 Aug 9, 2024
92cbc2d
Rename Home.py to home.py
anirbanbagchi1979 Aug 9, 2024
5ed68b2
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Aug 9, 2024
8bebddc
Merge branch 'finance-advisor-spanner' of https://github.com/GoogleCl…
gcf-owl-bot[bot] Aug 9, 2024
519b013
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Aug 9, 2024
fc80634
Merge branch 'finance-advisor-spanner' of https://github.com/GoogleCl…
gcf-owl-bot[bot] Aug 9, 2024
d77384d
Merge branch 'main' into finance-advisor-spanner
holtskinner Aug 12, 2024
5bce199
fix: change to google-cloud-spanner
anirbanbagchi1979 Aug 12, 2024
505a525
fix: lint errors
anirbanbagchi1979 Aug 12, 2024
ff05f9c
fix: code cleanup
anirbanbagchi1979 Aug 12, 2024
a44ba03
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Aug 12, 2024
ea496e0
fix: code formatted
anirbanbagchi1979 Aug 12, 2024
3d52fbb
Fix spelling error - "queryies"
holtskinner Aug 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/actions/spelling/allow.txt
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ meme
memes
metadatas
mgrs
miranda
mpn
nbconvert
nbfmt
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,3 @@ testem.log
.DS_Store
Thumbs.db
*.vscode*
gemini/sample-apps/finance-advisor-spanner/graph_viz.html
2 changes: 1 addition & 1 deletion gemini/sample-apps/finance-advisor-spanner/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ RUN pip install --no-cache-dir -r requirements.txt

COPY . .

CMD ["streamlit", "run", "Home.py", "--server.enableCORS", "false", "--browser.serverAddress", "0.0.0.0", "--browser.gatherUsageStats", "false", "--server.port", "8080"]
CMD ["streamlit", "run", "home.py", "--server.enableCORS", "false", "--browser.serverAddress", "0.0.0.0", "--browser.gatherUsageStats", "false", "--server.port", "8080"]
35 changes: 0 additions & 35 deletions gemini/sample-apps/finance-advisor-spanner/Home.py

This file was deleted.

4 changes: 2 additions & 2 deletions gemini/sample-apps/finance-advisor-spanner/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Finally, before I recommend a fund, I also want to check the exposure to a parti

With the power of Spanner's multimodel support, I can run complex workloads on a single database for relational, analytical, text and vector use cases with virtually unlimited scale, five nines of availability—including enterprise security and governance for mission critical workloads.

This demo highlights [Spanner](https://cloud.google.com/spanner), integration with [Vertex AI LLMs](https://cloud.google.com/model-garden?hl=en) for both embeddings and text completion models. You will learn how Spanner can help with usecases where you run Full Text Search, Approximate Nearest neighbor search and vector similarity search.
This demo highlights [Spanner](https://cloud.google.com/spanner), integration with [Vertex AI LLMs](https://cloud.google.com/model-garden?hl=en) for both embeddings and text completion models. You will learn how Spanner can help with use cases where you run Full Text Search, Approximate Nearest Neighbor search and vector similarity search.

## Tech Stack

Expand Down Expand Up @@ -71,7 +71,7 @@ The Finvest Spanner demo application was built using:

Change the endpoint as per your project and the spanner instance location

```bash
```sql
ALTER MODEL EmbeddingsModel SET OPTIONS (
endpoint = '//aiplatform.googleapis.com/projects/'YOUR PROJECT ID HERE'/locations/'YOUR SPANNER INSTANCE LOCATION HERE'/publishers/google/models/text-embedding-003'
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ ALTER MODEL EmbeddingsModel SET OPTIONS (
endpoint = '//aiplatform.googleapis.com/projects/<project-name>/locations/<location>/publishers/google/models/text-embedding-003'
)
;

ALTER TABLE EU_MutualFunds ADD COLUMN fund_name_Tokens TOKENLIST AS (TOKENIZE_FULLTEXT(fund_name)) HIDDEN;
ALTER TABLE EU_MutualFunds ADD COLUMN category_Tokens TOKENLIST AS (TOKENIZE_FULLTEXT(category)) HIDDEN;
ALTER TABLE EU_MutualFunds ADD COLUMN investment_strategy_Tokens TOKENLIST AS (TOKENIZE_FULLTEXT(investment_strategy)) HIDDEN;
Expand All @@ -13,15 +12,12 @@ ALTER TABLE EU_MutualFunds ADD COLUMN morningstar_benchmark_Tokens TOKENLIST AS
ALTER TABLE EU_MutualFunds ADD COLUMN top5_regions_Tokens TOKENLIST AS (TOKENIZE_FULLTEXT(top5_regions)) HIDDEN;
ALTER TABLE EU_MutualFunds ADD COLUMN top5_holdings_Tokens TOKENLIST AS (TOKENIZE_FULLTEXT(top5_holdings)) HIDDEN;
ALTER TABLE EU_MutualFunds ADD COLUMN investment_managers_Substring_Tokens TOKENLIST AS (TOKENIZE_SUBSTRING(investment_managers)) HIDDEN;

ALTER TABLE
EU_MutualFunds ADD COLUMN investment_managers_Substring_Tokens_NGRAM TOKENLIST AS ( TOKENIZE_SUBSTRING(investment_managers,
ngram_size_min=>2,
ngram_size_max=>3,
relative_search_types=>["word_prefix",
"word_suffix"])) HIDDEN;


UPDATE EU_MutualFunds SET investment_strategy_Embedding_vector = investment_strategy_Embedding WHERE investment_strategy_Embedding is not NULL and EXTRACT(YEAR from inception_date) = 1958 ;
UPDATE EU_MutualFunds SET investment_strategy_Embedding_vector = investment_strategy_Embedding WHERE investment_strategy_Embedding is not NULL and EXTRACT(YEAR from inception_date) = 2008;
UPDATE EU_MutualFunds SET investment_strategy_Embedding_vector = investment_strategy_Embedding WHERE investment_strategy_Embedding is not NULL and EXTRACT(YEAR from inception_date) = 2004;
Expand Down Expand Up @@ -66,8 +62,6 @@ UPDATE EU_MutualFunds SET investment_strategy_Embedding_vector = investment_stra
UPDATE EU_MutualFunds SET investment_strategy_Embedding_vector = investment_strategy_Embedding WHERE investment_strategy_Embedding is not NULL and EXTRACT(YEAR from inception_date) = 2020;
UPDATE EU_MutualFunds SET investment_strategy_Embedding_vector = investment_strategy_Embedding WHERE investment_strategy_Embedding is not NULL and EXTRACT(YEAR from inception_date) = 2000;
UPDATE EU_MutualFunds SET investment_strategy_Embedding_vector = investment_strategy_Embedding WHERE investment_strategy_Embedding is not NULL and EXTRACT(YEAR from inception_date) = 1992;


CREATE SEARCH INDEX
category_Tokens_IDX
ON
Expand Down Expand Up @@ -109,8 +103,6 @@ CREATE SEARCH INDEX
ON
EU_MutualFunds(investment_managers_Substring_Tokens,
investment_strategy_Tokens);


CREATE SEARCH INDEX
investment_managers_Substring_NgRAM_investment_Strategy_Tokens_Combo_IDX
ON
Expand All @@ -124,15 +116,10 @@ WHERE
investment_strategy_Embedding_vector IS NOT NULL OPTIONS ( tree_depth = 2,
num_leaves = 40,
distance_type = 'EUCLIDEAN' );

CREATE SEARCH INDEX
investment_managers_Substring_Tokens_with_vectors_NGRAM_IDX
ON
EU_MutualFunds(investment_managers_Substring_Tokens_NGRAM) STORING (investment_strategy_Embedding_vector);




CREATE OR REPLACE PROPERTY GRAPH FundGraph NODE TABLES( Companies AS Company DEFAULT LABEL PROPERTIES ALL COLUMNS,
EU_MutualFunds AS Fund DEFAULT LABEL PROPERTIES ALL COLUMNS EXCEPT (_Injected_SearchUid,
_Injected_VectorIndex_InvestmentStrategyEmbeddingIndex_FP8,
Expand All @@ -147,6 +134,3 @@ CREATE OR REPLACE PROPERTY GRAPH FundGraph NODE TABLES( Companies AS Company DEF
Company(CompanySeq) DESTINATION KEY(SectorSeq)
REFERENCES
Sector(SectorSeq) LABEL Belongs_To PROPERTIES ALL COLUMNS );



33 changes: 0 additions & 33 deletions gemini/sample-apps/finance-advisor-spanner/css.py
anirbanbagchi1979 marked this conversation as resolved.
Outdated
Show resolved Hide resolved

This file was deleted.

54 changes: 17 additions & 37 deletions gemini/sample-apps/finance-advisor-spanner/database.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""This file is for database operations done by the application """

# pylint: disable=line-too-long, invalid-name, use-dict-literal, use-list-literal
# pylint: disable=line-too-long
import os

from dotenv import load_dotenv
Expand All @@ -23,42 +23,19 @@
database = instance.database(database_id)


def spanner_read_data(query: str) -> pd.DataFrame:
"""This function helps read data from spanner"""
def spanner_read_data(query: str, *vector_input: list) -> pd.DataFrame:
"""This function helps read data from Spanner"""
with database.snapshot() as snapshot:
results = snapshot.execute_sql(query)
rows = list()
for row in results:
rows.append(row)
if len(vector_input) != 0:
results = snapshot.execute_sql(
query,
params={"vector": vector_input[0]},
)
else:
results = snapshot.execute_sql(query)
rows = list(results)
cols = [x.name for x in results.fields]
result_df = pd.DataFrame(rows, columns=cols)
return result_df


def spanner_read_data_list(query: str) -> list:
"""This function helps read data from spanner and returns data as a list"""
with database.snapshot() as snapshot:
results = snapshot.execute_sql(query)
rows = list()
for row in results:
rows.append(row)
return rows


def spanner_read_data_withparameters(query: str, vector_input: list) -> pd.DataFrame:
"""This function helps read data from spanner with input params"""
with database.snapshot() as snapshot:
results = snapshot.execute_sql(
query,
params={"vector": vector_input},
)
rows = list()
for row in results:
rows.append(row)
cols = [x.name for x in results.fields]
result_df = pd.DataFrame(rows, columns=cols)

return result_df
return pd.DataFrame(rows, columns=cols)


def fts_query(query_params: list) -> dict:
Expand Down Expand Up @@ -120,7 +97,7 @@ def semantic_query_ann(query_params: list) -> dict:
+ query_params[0]
+ '" AS content) ) ;'
)
vector_input = spanner_read_data_list(embedding_query)
vector_input = spanner_read_data(embedding_query).values.tolist()

if query_params[1].strip() != "":
ann_query = (
Expand All @@ -132,7 +109,10 @@ def semantic_query_ann(query_params: list) -> dict:
)
else:
ann_query = "SELECT fund_name, investment_strategy, investment_managers, APPROX_EUCLIDEAN_DISTANCE(investment_strategy_Embedding_vector, @vector, options => JSON '{\"num_leaves_to_search\": 10}') AS distance FROM EU_MutualFunds @{force_index = InvestmentStrategyEmbeddingIndex} WHERE investment_strategy_Embedding_vector IS NOT NULL ORDER BY distance LIMIT 100;"
results_df = spanner_read_data_withparameters(ann_query, vector_input[0][0])
results_df = spanner_read_data(ann_query, vector_input[0][0])
results_df = spanner_read_data(ann_query, vector_input[0][0])
results_df = spanner_read_data(ann_query, vector_input[0][0])
results_df = spanner_read_data(ann_query, vector_input[0][0])

return_vals = dict()
return_vals["query"] = ann_query
Expand Down
Loading
Loading