-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(chat2query-api): add refine sql api
- Loading branch information
Showing
2 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' } |