Skip to content
This repository has been archived by the owner on Oct 17, 2023. It is now read-only.

Commit

Permalink
修复 0719 一言接口更新
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuweiyou committed Jul 19, 2023
1 parent 68786e2 commit e19e3db
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
File renamed without changes.
25 changes: 25 additions & 0 deletions chat_query.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"code": 0,
"msg": "success",
"data": {
"content": "<img src=\"http://eb118-file.cdn.bcebos.com/upload/572D958CC331E8A80FC99EC809D5A500?x-bce-process=style/wm_ai\" /><br>好的,根据你的需求,我为你创作了一幅画作。<br>我的作画技能还在不断进化中,暂时还不支持对画作的修改和解释。<br>如果需要继续让我为你作画,请完整描述你的需求,如:“帮我画一枝晶莹剔透的牡丹花”。",
"text": "<img src=\"http://eb118-file.cdn.bcebos.com/upload/572D958CC331E8A80FC99EC809D5A500?x-bce-process=style/wm_ai\" /><br>好的,根据你的需求,我为你创作了一幅画作。<br>我的作画技能还在不断进化中,暂时还不支持对画作的修改和解释。<br>如果需要继续让我为你作画,请完整描述你的需求,如:“帮我画一枝晶莹剔透的牡丹花”。",
"stop": 0,
"tips": "",
"wordGengStatus": false,
"pluginInfo": null,
"isIncr": true,
"isBan": false,
"flag": 0,
"logid": null,
"usageVo": null,
"content_type": "image",
"sent_id": 0,
"is_end": 1,
"is_truncated": 0,
"error_code": 0,
"chat_id": null,
"tokens_all": "<img src=\"http://eb118-file.cdn.bcebos.com/upload/572D958CC331E8A80FC99EC809D5A500?x-bce-process=style/wm_ai\" /><br>好的,根据你的需求,我为你创作了一幅画作。<br>我的作画技能还在不断进化中,暂时还不支持对画作的修改和解释。<br>如果需要继续让我为你作画,请完整描述你的需求,如:“帮我画一枝晶莹剔透的牡丹花”。"
},
"logId": "118000005318129674"
}
29 changes: 22 additions & 7 deletions headless.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,39 @@ export async function headless({cookie, timeout = 1000 * 60, headless = 'new', p
let text = null
await page.waitForResponse(
async response => {
if (response.url().startsWith('https://yiyan.baidu.com/eb/chat/conversation')) {
try {
// 参考 conversation_example.txt 处理响应
try {
const response_url = response.url()
if (response_url.startsWith('https://yiyan.baidu.com/eb/chat/conversation')) {
// event-stream
// 参考 chat_conversation.txt
const response_text = await response.text()
const last_line = response_text.trim().split('\n').pop()
const json_string = last_line.replace('data:', '')
const json_object = JSON.parse(json_string)
text = json_object.data.tokens_all
return json_object.data.is_end === 1
} catch (e) {
console.error(e)
if (json_object.data.is_end === 1) {
text = json_object.data.tokens_all
return true
}
}

if (response_url.startsWith('https://yiyan.baidu.com/eb/chat/query')) {
// json api
// 参考 chat_query.json
const response_json = await response.json()
if (response_json.data.is_end === 1) {
text = response_json.data.tokens_all
return true
}
}
} catch (e) {
console.error(e)
}
},
{
timeout,
}
)

const image = text.match(/<img src="(.*?)"/)
return {text, image: image ? image[1].replace('=style/wm_ai', '') : null}
} finally {
Expand Down
1 change: 1 addition & 0 deletions headless_test.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// 设置环境变量 export YIYAN_COOKIE=xxxx 之后运行 npm test 测试
import { headless } from './headless.mjs'

console.log(
Expand Down

0 comments on commit e19e3db

Please sign in to comment.