Skip to content

Commit

Permalink
Merge pull request #26 from tidbcloud/feat/try-chat2query-api
Browse files Browse the repository at this point in the history
feat(chat2query-api): try chat2query api
  • Loading branch information
baurine authored Jun 27, 2024
2 parents 39cdc3b + a6a8eb8 commit 8443543
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 8 deletions.
58 changes: 58 additions & 0 deletions netlify/edge-functions/chat2data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import type { Config, Context } from '@netlify/edge-functions'

const url =
Netlify.env.get('TIDBCLOUD_CHAT2QUERY_APP_URL_ENDPOINT') + '/v3/chat2data'
const publicKey = Netlify.env.get('TIDBCLOUD_CHAT2QUERY_APP_PUBLIC_KEY')
const privateKey = Netlify.env.get('TIDBCLOUD_CHAT2QUERY_APP_PRIVATE_KEY')
const clusterId = Netlify.env.get('TIDBCLOUD_CHAT2QUERY_CLUSTER_ID')

type Chat2DataReq = {
database: string
question: string
}

export default async (req: Request, _context: Context) => {
const body: Chat2DataReq = await req.json()

if (!body.database || !body.question) {
return new Response(
JSON.stringify({
code: 400,
message: 'bad request, database or question should not empty.',
data: {}
}),
{ status: 400 }
)
}

const data = {
cluster_id: clusterId,
...body
}

// const data = {
// cluster_id: clusterId,
// database: 'game',
// question: 'show most 10 popular games'
// }

const fetchOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: 'Basic ' + btoa(`${publicKey}:${privateKey}`)
},
body: JSON.stringify(data)
}

let statusCode = 200
const res = await fetch(url, fetchOptions).then((res) => {
console.log(res.status)
statusCode = res.status
return res.json()
})

return new Response(JSON.stringify(res), { status: statusCode })
}

export const config: Config = { path: '/api/chat2data' }
42 changes: 42 additions & 0 deletions netlify/edge-functions/job-status.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type { Config, Context } from '@netlify/edge-functions'

const endpoint = Netlify.env.get('TIDBCLOUD_CHAT2QUERY_APP_URL_ENDPOINT')
const publicKey = Netlify.env.get('TIDBCLOUD_CHAT2QUERY_APP_PUBLIC_KEY')
const privateKey = Netlify.env.get('TIDBCLOUD_CHAT2QUERY_APP_PRIVATE_KEY')

export default async (req: Request, _context: Context) => {
const url = new URL(req.url)
const jobId = url.searchParams.get('job_id')
if (!jobId) {
return new Response(
JSON.stringify({
code: 400,
message: 'bad request, job_id is empty',
data: {}
}),
{ status: 400 }
)
}

const jobStatusUrl = endpoint + `/v2/jobs/${jobId}`

const fetchOptions = {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: 'Basic ' + btoa(`${publicKey}:${privateKey}`)
}
}

let statusCode = 200
const res = await fetch(url, fetchOptions).then((res) => {
console.log(res.status)
statusCode = res.status
return res.json()
})

return new Response(JSON.stringify(res), { status: statusCode })
}

// /api/jobs?job_id=xxx
export const config: Config = { path: '/api/jobs' }
60 changes: 60 additions & 0 deletions netlify/edge-functions/refine-sql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import type { Config, Context } from '@netlify/edge-functions'

const url =
Netlify.env.get('TIDBCLOUD_CHAT2QUERY_APP_URL_ENDPOINT') + '/v3/refineSql'
const publicKey = Netlify.env.get('TIDBCLOUD_CHAT2QUERY_APP_PUBLIC_KEY')
const privateKey = Netlify.env.get('TIDBCLOUD_CHAT2QUERY_APP_PRIVATE_KEY')
const clusterId = Netlify.env.get('TIDBCLOUD_CHAT2QUERY_CLUSTER_ID')

type RefineSqlReq = {
database: string
sql: string
feedback: string
}

export default async (req: Request, _context: Context) => {
const body: RefineSqlReq = await req.json()

if (!body.database || !body.sql) {
return new Response(
JSON.stringify({
code: 400,
message: 'bad request, database or sql should not empty.',
data: {}
}),
{ status: 400 }
)
}

const data = {
cluster_id: clusterId,
...body
}

// const data = {
// cluster_id: clusterId,
// database: 'game',
// sql: 'SELECT * FROM `games` ORDER BY `recommendations` DESC LIMIT 20;',
// feedback: 'limit 10'
// }

const fetchOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: 'Basic ' + btoa(`${publicKey}:${privateKey}`)
},
body: JSON.stringify(data)
}

let statusCode = 200
const res = await fetch(url, fetchOptions).then((res) => {
console.log(res.status)
statusCode = res.status
return res.json()
})

return new Response(JSON.stringify(res), { status: statusCode })
}

export const config: Config = { path: '/api/refineSql' }
10 changes: 6 additions & 4 deletions netlify/edge-functions/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ type SchemaRes = {
}[]
}[]

// url:
// mysql://[user]:[pwd]@[host]/
// database is skipped and its default value is `test`
const DATABASE_URL = Netlify.env.get('TIDBCLOUD_DATABASE_URL')

export default async () => {
// url:
// mysql://[user]:[pwd]@[host]/
// database is skipped and its default value is `test`
const conn = connect({
url: Netlify.env.get('TIDBCLOUD_DATABASE_URL')
url: DATABASE_URL
})

let schema: SchemaRes = []
Expand Down
10 changes: 6 additions & 4 deletions netlify/edge-functions/statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ type StatementReq = {
sql: string
}

// url:
// mysql://[user]:[pwd]@[host]/
// database is skipped and its default value is `test`
const DATABASE_URL = Netlify.env.get('TIDBCLOUD_DATABASE_URL')

export default async (req: Request, _context: Context) => {
const body: StatementReq = await req.json()

// url:
// mysql://[user]:[pwd]@[host]/
// database is skipped and its default value is `test`
const conn = connect({
url: Netlify.env.get('TIDBCLOUD_DATABASE_URL') + body.database
url: DATABASE_URL + body.database
})

if (body.sql.trim().toLowerCase().startsWith('select')) {
Expand Down

0 comments on commit 8443543

Please sign in to comment.