Skip to content

Commit

Permalink
feat(chat2query-api): add refine sql api
Browse files Browse the repository at this point in the history
  • Loading branch information
baurine committed Jun 27, 2024
1 parent aae6cef commit ddae80b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
File renamed without changes.
55 changes: 55 additions & 0 deletions netlify/edge-functions/refine-sql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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)
}

const res = await fetch(url, fetchOptions).then((res) => res.json())

return new Response(JSON.stringify(res))
}

export const config: Config = { path: '/api/refineSql' }

0 comments on commit ddae80b

Please sign in to comment.