Skip to content

Commit

Permalink
[feature] ai, change fetch to grafana proxy
Browse files Browse the repository at this point in the history
**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)
  • Loading branch information
twou12031 committed Apr 10, 2024
1 parent c73e775 commit 5f597b4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 76 deletions.
2 changes: 1 addition & 1 deletion deepflow-apptracing-panel/src/components/AskGPT.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ export const AskGPT: React.FC<Props> = ({ data }) => {
})
.flat()
setAiEngines(list)
setCheckedAiEngine(list.filter(e => !e.disabled)?.[0].value || '')
setCheckedAiEngine(list.filter(e => !e.disabled)?.[0]?.value || '')
} catch (error: any) {
setErrorMsg(`GET ENGINES FAILED: ${error.message}`)
setDrawerData({
Expand Down
92 changes: 23 additions & 69 deletions deepflow-querier-datasource/src/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,38 +335,24 @@ export class DataSource extends DataSourceWithBackend<MyQuery, MyDataSourceOptio
}

async getAIConfigs() {
const { aiUrl } = DATA_SOURCE_SETTINGS
if (!aiUrl) {
throw new Error('Please set AI url in datasource settings.')
}
return await fetch(`${aiUrl}/v1/llm_agent_config`, {
method: 'GET'
})
.then(response => {
if (!response.ok) {
throw Error(response.statusText)
}
return response
})
// 注意这个API支持多个点,但我们用多个API并行查
.then(async res => {
const { DATA } = await res.json()
const fetchOption = {
method: 'get',
url: `${DATA_SOURCE_SETTINGS.basicUrl}/ai/v1/llm_agent_config`
} as BackendSrvRequest
return await getBackendSrv()
.fetch(fetchOption)
.toPromise()
.then((res: any) => {
const { DATA } = res.data
return DATA
})
.catch(e => {
throw new Error(`请求数据失败: ${e}`)
})
}

async askGPTRequest(
engine: { platform: string; engine_name: string },
postData: { system_content: string; user_content: string },
receiveFn: any
) {
const { aiUrl } = DATA_SOURCE_SETTINGS
if (!aiUrl) {
throw new Error('Please set AI url in datasource settings.')
}
let answer = ''

const onStreamEnd = () => {
Expand All @@ -384,56 +370,24 @@ export class DataSource extends DataSourceWithBackend<MyQuery, MyDataSourceOptio
streamer
})
}, 32)
const callback = (chunk: string) => {
answer += chunk
streamer.write(chunk)
}
// @ts-ignore
const getGPTAnswerHandler = async (reader, callback) => {
// @ts-ignore
const handleData = async ({ done, value }) => {
if (done) {
return
}
// 将收到的数据处理为字符串
const chunk = new TextDecoder().decode(value)
// 处理单个数据块
if (!callback) {
throw Error('chunked需要指定callback')
}
callback(chunk)
// 继续等待下一个数据块
// reader.read().then(handleData)
const res = await reader.read()
return handleData(res)
}
const res = await reader.read()
return handleData(res)
}
await fetch(`${aiUrl}/v1/ai/stream/${engine.platform}?engine=${engine.engine_name}`, {
method: 'POST',

const fetchOption = {
method: 'post',
url: `${DATA_SOURCE_SETTINGS.basicUrl}/ai/v1/ai/stream/${engine.platform}?engine=${engine.engine_name}`,
responseType: 'text',
headers: {
'Content-Type': 'application/json'
// 'x-user-id': '1',
// 'x-user-type': '1'
},
body: JSON.stringify(postData)
})
.then(response => {
if (!response.ok) {
throw Error(response.statusText)
}
return response
})
// 注意这个API支持多个点,但我们用多个API并行查
.then(async res => {
const reader = res.body?.getReader()
await getGPTAnswerHandler(reader, callback)
data: JSON.stringify(postData)
} as BackendSrvRequest
await getBackendSrv()
.fetch(fetchOption)
.toPromise()
.then((res: any) => {
answer = res.data
streamer.write(res.data)
})
.catch(e => {
throw new Error(`请求数据失败: ${e}`)
})


streamer.end()
}
}
6 changes: 1 addition & 5 deletions deepflow-querier-datasource/src/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,11 @@
},
{
"path": "ai",
"url": "http://earth.df",
"url": "{{ .JsonData.aiUrl }}",
"headers": [
{
"name": "Content-Type",
"content": "application/json"
},
{
"name": "Authorization",
"content": "Bearer gAAAAABkmWHtindtStIzizwp2tor8x2bWk3l0IEacEf3KDF-bEHfI8VQOc3Bs-r_rgfoAoHKpmkfKQzlDWWYQ2T_wPEnhW0KLzkZr_qRtqSl-58BVr9wDnhRXVaR2oMPORN7atGexZpCYJ0in9vY0cMp9f6RHVRa-l_quQ2GbDwak6jq6yeyeGRFyiRg5nkWGeu0mn9R97MM"
}
]
}
Expand Down
2 changes: 1 addition & 1 deletion deepflow-topo-panel/src/components/AskGPT.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ export const AskGPT: React.FC<Props> = ({ data }) => {
})
.flat()
setAiEngines(list)
setCheckedAiEngine(list.filter(e => !e.disabled)?.[0].value || '')
setCheckedAiEngine(list.filter(e => !e.disabled)?.[0]?.value || '')
} catch (error: any) {
setErrorMsg(`GET ENGINES FAILED: ${error.message}`)
setDrawerData({
Expand Down

0 comments on commit 5f597b4

Please sign in to comment.