Skip to content

Commit 6259ebe

Browse files
twou12031travilyu
authored andcommitted
[feature] ai, change fetch to grafana proxy
**Phenomenon and reproduction steps** none **Root cause and solution** none **Impactions** none **Test method** none **Affected branch(es)** - main **Checklist** - [ ] Dependencies update required - [ ] Common bug (similar problem in other repo)
1 parent 34dee12 commit 6259ebe

File tree

4 files changed

+30
-80
lines changed

4 files changed

+30
-80
lines changed

deepflow-apptracing-panel/src/components/AskGPT.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ export const AskGPT: React.FC<Props> = ({ data }) => {
265265
})
266266
.flat()
267267
setAiEngines(list)
268-
setCheckedAiEngine(list.filter(e => !e.disabled)?.[0].value || '')
268+
setCheckedAiEngine(list.filter(e => !e.disabled)?.[0]?.value || '')
269269
} catch (error: any) {
270270
setErrorMsg(`GET ENGINES FAILED: ${error.message}`)
271271
setDrawerData({
@@ -345,7 +345,7 @@ export const AskGPT: React.FC<Props> = ({ data }) => {
345345
}}
346346
onClick={onStartRequestClick}
347347
icon={drawerData.inRequest ? 'fa fa-spinner' : 'info'}
348-
variant={errorMsg !== '' ? 'destructive' : drawerData.inRequest ? 'secondary' : 'primary'}
348+
variant={drawerData.inRequest ? 'secondary' : 'primary'}
349349
>
350350
{requestBtnText}
351351
</Button>
@@ -355,7 +355,7 @@ export const AskGPT: React.FC<Props> = ({ data }) => {
355355
width: '16px',
356356
height: '16px',
357357
position: 'absolute',
358-
right: '110px',
358+
right: '115px',
359359
top: '7px',
360360
opacity: drawerData.inRequest ? 0 : 1
361361
}}

deepflow-querier-datasource/src/datasource.ts

Lines changed: 23 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -335,38 +335,24 @@ export class DataSource extends DataSourceWithBackend<MyQuery, MyDataSourceOptio
335335
}
336336

337337
async getAIConfigs() {
338-
const { aiUrl } = DATA_SOURCE_SETTINGS
339-
if (!aiUrl) {
340-
throw new Error('Please set AI url in datasource settings.')
341-
}
342-
return await fetch(`${aiUrl}/v1/llm_agent_config`, {
343-
method: 'GET'
344-
})
345-
.then(response => {
346-
if (!response.ok) {
347-
throw Error(response.statusText)
348-
}
349-
return response
350-
})
351-
// 注意这个API支持多个点,但我们用多个API并行查
352-
.then(async res => {
353-
const { DATA } = await res.json()
338+
const fetchOption = {
339+
method: 'get',
340+
url: `${DATA_SOURCE_SETTINGS.basicUrl}/ai/v1/llm_agent_config`
341+
} as BackendSrvRequest
342+
return await getBackendSrv()
343+
.fetch(fetchOption)
344+
.toPromise()
345+
.then((res: any) => {
346+
const { DATA } = res.data
354347
return DATA
355348
})
356-
.catch(e => {
357-
throw new Error(`请求数据失败: ${e}`)
358-
})
359349
}
360350

361351
async askGPTRequest(
362352
engine: { platform: string; engine_name: string },
363353
postData: { system_content: string; user_content: string },
364354
receiveFn: any
365355
) {
366-
const { aiUrl } = DATA_SOURCE_SETTINGS
367-
if (!aiUrl) {
368-
throw new Error('Please set AI url in datasource settings.')
369-
}
370356
let answer = ''
371357

372358
const onStreamEnd = () => {
@@ -384,56 +370,24 @@ export class DataSource extends DataSourceWithBackend<MyQuery, MyDataSourceOptio
384370
streamer
385371
})
386372
}, 32)
387-
const callback = (chunk: string) => {
388-
answer += chunk
389-
streamer.write(chunk)
390-
}
391-
// @ts-ignore
392-
const getGPTAnswerHandler = async (reader, callback) => {
393-
// @ts-ignore
394-
const handleData = async ({ done, value }) => {
395-
if (done) {
396-
return
397-
}
398-
// 将收到的数据处理为字符串
399-
const chunk = new TextDecoder().decode(value)
400-
// 处理单个数据块
401-
if (!callback) {
402-
throw Error('chunked需要指定callback')
403-
}
404-
callback(chunk)
405-
// 继续等待下一个数据块
406-
// reader.read().then(handleData)
407-
const res = await reader.read()
408-
return handleData(res)
409-
}
410-
const res = await reader.read()
411-
return handleData(res)
412-
}
413-
await fetch(`${aiUrl}/v1/ai/stream/${engine.platform}?engine=${engine.engine_name}`, {
414-
method: 'POST',
373+
374+
const fetchOption = {
375+
method: 'post',
376+
url: `${DATA_SOURCE_SETTINGS.basicUrl}/ai/v1/ai/stream/${engine.platform}?engine=${engine.engine_name}`,
377+
responseType: 'text',
415378
headers: {
416379
'Content-Type': 'application/json'
417-
// 'x-user-id': '1',
418-
// 'x-user-type': '1'
419380
},
420-
body: JSON.stringify(postData)
421-
})
422-
.then(response => {
423-
if (!response.ok) {
424-
throw Error(response.statusText)
425-
}
426-
return response
427-
})
428-
// 注意这个API支持多个点,但我们用多个API并行查
429-
.then(async res => {
430-
const reader = res.body?.getReader()
431-
await getGPTAnswerHandler(reader, callback)
381+
data: JSON.stringify(postData)
382+
} as BackendSrvRequest
383+
await getBackendSrv()
384+
.fetch(fetchOption)
385+
.toPromise()
386+
.then((res: any) => {
387+
answer = res.data
388+
streamer.write(res.data)
432389
})
433-
.catch(e => {
434-
throw new Error(`请求数据失败: ${e}`)
435-
})
436-
390+
437391
streamer.end()
438392
}
439393
}

deepflow-querier-datasource/src/plugin.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,11 @@
7474
},
7575
{
7676
"path": "ai",
77-
"url": "http://earth.df",
77+
"url": "{{ .JsonData.aiUrl }}",
7878
"headers": [
7979
{
8080
"name": "Content-Type",
8181
"content": "application/json"
82-
},
83-
{
84-
"name": "Authorization",
85-
"content": "Bearer gAAAAABkmWHtindtStIzizwp2tor8x2bWk3l0IEacEf3KDF-bEHfI8VQOc3Bs-r_rgfoAoHKpmkfKQzlDWWYQ2T_wPEnhW0KLzkZr_qRtqSl-58BVr9wDnhRXVaR2oMPORN7atGexZpCYJ0in9vY0cMp9f6RHVRa-l_quQ2GbDwak6jq6yeyeGRFyiRg5nkWGeu0mn9R97MM"
8682
}
8783
]
8884
}

deepflow-topo-panel/src/components/AskGPT.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ export const AskGPT: React.FC<Props> = ({ data }) => {
260260
})
261261
.flat()
262262
setAiEngines(list)
263-
setCheckedAiEngine(list.filter(e => !e.disabled)?.[0].value || '')
263+
setCheckedAiEngine(list.filter(e => !e.disabled)?.[0]?.value || '')
264264
} catch (error: any) {
265265
setErrorMsg(`GET ENGINES FAILED: ${error.message}`)
266266
setDrawerData({
@@ -340,7 +340,7 @@ export const AskGPT: React.FC<Props> = ({ data }) => {
340340
}}
341341
onClick={onStartRequestClick}
342342
icon={drawerData.inRequest ? 'fa fa-spinner' : 'info'}
343-
variant={errorMsg !== '' ? 'destructive' : drawerData.inRequest ? 'secondary' : 'primary'}
343+
variant={drawerData.inRequest ? 'secondary' : 'primary'}
344344
>
345345
{requestBtnText}
346346
</Button>
@@ -350,7 +350,7 @@ export const AskGPT: React.FC<Props> = ({ data }) => {
350350
width: '16px',
351351
height: '16px',
352352
position: 'absolute',
353-
right: '110px',
353+
right: '115px',
354354
top: '7px',
355355
opacity: drawerData.inRequest ? 0 : 1
356356
}}

0 commit comments

Comments
 (0)