-
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.
Merge pull request #26 from tidbcloud/feat/try-chat2query-api
feat(chat2query-api): try chat2query api
- Loading branch information
Showing
5 changed files
with
172 additions
and
8 deletions.
There are no files selected for viewing
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,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' } |
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,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' } |
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,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' } |
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
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