-
Notifications
You must be signed in to change notification settings - Fork 448
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kerwin
committed
Apr 16, 2023
1 parent
f25e9c7
commit f82b4ee
Showing
19 changed files
with
474 additions
and
20 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 |
---|---|---|
|
@@ -266,6 +266,13 @@ services: | |
SMTP_TSL: true | ||
SMTP_USERNAME: [email protected] | ||
SMTP_PASSWORD: xxx | ||
# Enable sensitive word review, because the response result is streaming, so there is currently no review. | ||
AUDIT_ENABLED: false | ||
# https://ai.baidu.com/ai-doc/ANTIPORN/Vk3h6xaga | ||
AUDIT_PROVIDER: baidu | ||
AUDIT_API_KEY: xxx | ||
AUDIT_API_SECRET: xxx | ||
AUDIT_TEXT_LABEL: xxx | ||
links: | ||
- database | ||
|
||
|
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 |
---|---|---|
|
@@ -270,6 +270,13 @@ services: | |
SMTP_TSL: true | ||
SMTP_USERNAME: [email protected] | ||
SMTP_PASSWORD: xxx | ||
# 是否开启敏感词审核, 因为响应结果是流式 所以暂时没审核 | ||
AUDIT_ENABLED: false | ||
# https://ai.baidu.com/ai-doc/ANTIPORN/Vk3h6xaga | ||
AUDIT_PROVIDER: baidu | ||
AUDIT_API_KEY: xxx | ||
AUDIT_API_SECRET: xxx | ||
AUDIT_TEXT_LABEL: xxx | ||
links: | ||
- database | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "chatgpt-web", | ||
"version": "2.11.7", | ||
"version": "2.12.0", | ||
"private": false, | ||
"description": "ChatGPT Web", | ||
"author": "ChenZhaoYu <[email protected]>", | ||
|
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import fetch from 'node-fetch' | ||
|
||
export interface TextAuditServiceOptions { | ||
apiKey: string | ||
apiSecret: string | ||
label: string | ||
} | ||
|
||
export interface TextAuditService { | ||
audit(text: string): Promise<boolean> | ||
} | ||
|
||
/** | ||
* https://ai.baidu.com/ai-doc/ANTIPORN/Vk3h6xaga | ||
*/ | ||
export class BaiduTextAuditService implements TextAuditService { | ||
private accessToken: string | ||
private expiredTime: number | ||
|
||
constructor(private options: TextAuditServiceOptions) { } | ||
|
||
async audit(text: string): Promise<boolean> { | ||
if (!await this.refreshAccessToken()) | ||
return | ||
const url = `https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined?access_token=${this.accessToken}` | ||
let headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded' | ||
'Accept': 'application/json' | ||
} | ||
const response = await fetch(url, { headers, method: 'POST', body: `text=${encodeURIComponent(text)}` }) | ||
const data = await response.json() as { conclusionType: number; data: any } | ||
|
||
if (data.error_msg) | ||
throw new Error(data.error_msg) | ||
|
||
// 审核结果类型,可取值1、2、3、4,分别代表1:合规,2:不合规,3:疑似,4:审核失败 | ||
if (data.conclusionType === 1) | ||
return true | ||
|
||
// https://ai.baidu.com/ai-doc/ANTIPORN/Nk3h6xbb2#%E7%BB%86%E5%88%86%E6%A0%87%E7%AD%BE%E5%AF%B9%E7%85%A7%E8%A1%A8 | ||
|
||
// 3 仅政治 | ||
const safe = data.data.filter(d => d.subType === 3).length <= 0 | ||
if (!safe || !this.options.label) | ||
return safe | ||
const str = JSON.stringify(data) | ||
for (const l of this.options.label.split(',')) { | ||
if (str.indexOf(l)) | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
async refreshAccessToken() { | ||
if (!this.options.apiKey || !this.options.apiSecret) | ||
throw new Error('未配置 | Not configured.') | ||
|
||
try { | ||
if (this.accessToken && Math.floor(new Date().getTime() / 1000) <= this.expiredTime) | ||
return true | ||
|
||
const url = `https://aip.baidubce.com/oauth/2.0/token?client_id=${this.options.apiKey}&client_secret=${this.options.apiSecret}&grant_type=client_credentials` | ||
let headers: { | ||
'Content-Type': 'application/json' | ||
'Accept': 'application/json' | ||
} | ||
const response = await fetch(url, { headers }) | ||
const data = (await response.json()) as { access_token: string; expires_in: number } | ||
|
||
this.accessToken = data.access_token | ||
this.expiredTime = Math.floor(new Date().getTime() / 1000) + (+data.expires_in) | ||
return true | ||
} | ||
catch (error) { | ||
global.console.error(`百度审核${error}`) | ||
} | ||
return false | ||
} | ||
} | ||
|
||
export type TextAuditServiceProvider = 'baidu' // | 'ali' | ||
|
||
export type TextAuditServices = { | ||
[key in TextAuditServiceProvider]: new ( | ||
options: TextAuditServiceOptions, | ||
) => TextAuditService; | ||
} | ||
|
||
export const textAuditServices: TextAuditServices = { | ||
baidu: BaiduTextAuditService, | ||
} |
Oops, something went wrong.