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 0ba6b43
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 66 deletions.
84 changes: 23 additions & 61 deletions deepflow-querier-datasource/src/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,23 +339,17 @@ export class DataSource extends DataSourceWithBackend<MyQuery, MyDataSourceOptio
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(
Expand Down Expand Up @@ -384,56 +378,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

0 comments on commit 0ba6b43

Please sign in to comment.