-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathuseBigQueryExamples.ts
147 lines (135 loc) · 4.38 KB
/
useBigQueryExamples.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { useContext, useEffect, useRef } from 'react'
import { useDispatch, useSelector } from 'react-redux'
import {
setExploreGenerationExamples,
setExploreRefinementExamples,
setExploreSamples,
ExploreSamples,
setisBigQueryMetadataLoaded,
setCurrenExplore,
RefinementExamples,
ExploreExamples,
AssistantState
} from '../slices/assistantSlice'
import { ExtensionContext } from '@looker/extension-sdk-react'
import process from 'process'
import { useErrorBoundary } from 'react-error-boundary'
import { RootState } from '../store'
export const useBigQueryExamples = () => {
const connectionName = process.env.BIGQUERY_EXAMPLE_PROMPTS_CONNECTION_NAME || ''
const datasetName = process.env.BIGQUERY_EXAMPLE_PROMPTS_DATASET_NAME || 'explore_assistant'
const dispatch = useDispatch()
const { showBoundary } = useErrorBoundary()
const { isBigQueryMetadataLoaded } = useSelector((state: RootState) => state.assistant as AssistantState)
const { core40SDK } = useContext(ExtensionContext)
const runSQLQuery = async (sql: string) => {
try {
const createSqlQuery = await core40SDK.ok(
core40SDK.create_sql_query({
connection_name: connectionName,
sql: sql,
}),
)
const { slug } = await createSqlQuery
if (slug) {
const runSQLQuery = await core40SDK.ok(
core40SDK.run_sql_query(slug, 'json'),
)
const examples = await runSQLQuery
return examples
}
return []
} catch(error) {
showBoundary(error)
throw new Error('error')
}
}
const getExamplePrompts = async () => {
const sql = `
SELECT
explore_id,
examples
FROM
\`${datasetName}.explore_assistant_examples\`
`
return runSQLQuery(sql).then((response) => {
if(response.length === 0 || !Array.isArray(response)) {
return
}
const generationExamples: ExploreExamples = {}
if(response.length === 0 || !Array.isArray(response)) {
return
}
response.forEach((row: any) => {
generationExamples[row['explore_id']] = JSON.parse(row['examples'])
})
dispatch(setExploreGenerationExamples(generationExamples))
}).catch((error) => showBoundary(error))
}
const getRefinementPrompts = async () => {
const sql = `
SELECT
explore_id,
examples
FROM
\`${datasetName}.explore_assistant_refinement_examples\`
`
return runSQLQuery(sql).then((response) => {
if(response.length === 0 || !Array.isArray(response)) {
return
}
const refinementExamples: RefinementExamples = {}
if(response.length === 0 || !Array.isArray(response)) {
return
}
response.forEach((row: any) => {
refinementExamples[row['explore_id']] = JSON.parse(row['examples'])
})
dispatch(setExploreRefinementExamples(refinementExamples))
}).catch((error) => showBoundary(error))
}
const getSamples = async () => {
const sql = `
SELECT
explore_id,
samples
FROM
\`${datasetName}.explore_assistant_samples\`
`
return runSQLQuery(sql).then((response) => {
const exploreSamples: ExploreSamples = {}
if(response.length === 0 || !Array.isArray(response)) {
return
}
response.forEach((row: any) => {
exploreSamples[row['explore_id']] = JSON.parse(row['samples'])
})
const exploreKey: string = response[0]['explore_id']
const [modelName, exploreId] = exploreKey.split(':')
dispatch(setExploreSamples(exploreSamples))
dispatch(setCurrenExplore({
exploreKey,
modelName,
exploreId
}))
}).catch((error) => showBoundary(error))
}
// Create a ref to track if the hook has already been called
const hasFetched = useRef(false)
// get the example prompts provide completion status
useEffect(() => {
if (hasFetched.current) return
hasFetched.current = true
// if we already fetch everything, return
if(isBigQueryMetadataLoaded) return
dispatch(setisBigQueryMetadataLoaded(false))
Promise.all([getExamplePrompts(), getRefinementPrompts(), getSamples()])
.then(() => {
dispatch(setisBigQueryMetadataLoaded(true))
})
.catch((error) => {
showBoundary(error)
dispatch(setisBigQueryMetadataLoaded(false))
})
}, [showBoundary])
}