Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
赵月辉
/
fastgpt-migrated
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Unverified
Commit
9b1c0e1a
authored
Apr 10, 2023
by
archer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
perf: openapi. error catch
parent
a7988c16
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
66 additions
and
12 deletions
+66
-12
src/api/openapi.ts
+1
-1
src/pages/api/openapi/chat/vectorGpt.ts
+1
-1
src/pages/api/openapi/delKey.ts
+0
-0
src/pages/model/detail/components/ModelEditForm.tsx
+6
-0
src/pages/openapi/index.tsx
+16
-3
src/service/errorCode.ts
+27
-0
src/service/response.ts
+11
-3
src/service/utils/tools.ts
+4
-4
No files found.
src/api/openapi.ts
View file @
9b1c0e1a
...
@@ -13,4 +13,4 @@ export const getOpenApiKeys = () => GET<UserOpenApiKey[]>('/openapi/getKeys');
...
@@ -13,4 +13,4 @@ export const getOpenApiKeys = () => GET<UserOpenApiKey[]>('/openapi/getKeys');
/**
/**
* delete api by id
* delete api by id
*/
*/
export
const
delOpenApiById
=
(
id
:
string
)
=>
DELETE
(
`/openapi/delKe
t
?id=
${
id
}
`
);
export
const
delOpenApiById
=
(
id
:
string
)
=>
DELETE
(
`/openapi/delKe
y
?id=
${
id
}
`
);
src/pages/api/openapi/chat/vectorGpt.ts
View file @
9b1c0e1a
...
@@ -123,7 +123,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -123,7 +123,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
formatRedisPrompt
.
unshift
(
prompts
.
shift
()?.
value
||
''
);
formatRedisPrompt
.
unshift
(
prompts
.
shift
()?.
value
||
''
);
}
}
//
textArr
筛选,最多 2800 tokens
//
系统提示词
筛选,最多 2800 tokens
const
systemPrompt
=
systemPromptFilter
(
formatRedisPrompt
,
2800
);
const
systemPrompt
=
systemPromptFilter
(
formatRedisPrompt
,
2800
);
prompts
.
unshift
({
prompts
.
unshift
({
...
...
src/pages/api/openapi/delKe
t
.ts
→
src/pages/api/openapi/delKe
y
.ts
View file @
9b1c0e1a
File moved
src/pages/model/detail/components/ModelEditForm.tsx
View file @
9b1c0e1a
...
@@ -53,6 +53,12 @@ const ModelEditForm = ({
...
@@ -53,6 +53,12 @@ const ModelEditForm = ({
})}
})}
></
Input
>
></
Input
>
</
Flex
>
</
Flex
>
<
Flex
alignItems=
{
'center'
}
mt=
{
4
}
>
<
Box
flex=
{
'0 0 80px'
}
w=
{
0
}
>
modelId:
</
Box
>
<
Box
>
{
getValues
(
'_id'
)
}
</
Box
>
</
Flex
>
</
FormControl
>
</
FormControl
>
<
Flex
alignItems=
{
'center'
}
mt=
{
4
}
>
<
Flex
alignItems=
{
'center'
}
mt=
{
4
}
>
<
Box
flex=
{
'0 0 80px'
}
w=
{
0
}
>
<
Box
flex=
{
'0 0 80px'
}
w=
{
0
}
>
...
...
src/pages/openapi/index.tsx
View file @
9b1c0e1a
import
React
,
{
useState
}
from
'react'
;
import
React
,
{
useState
}
from
'react'
;
import
Link
from
'next/link'
;
import
{
import
{
Card
,
Card
,
Box
,
Box
,
...
@@ -54,10 +55,21 @@ const OpenApi = () => {
...
@@ -54,10 +55,21 @@ const OpenApi = () => {
<>
<>
<
Card
px=
{
6
}
py=
{
4
}
position=
{
'relative'
}
>
<
Card
px=
{
6
}
py=
{
4
}
position=
{
'relative'
}
>
<
Box
fontSize=
{
'xl'
}
fontWeight=
{
'bold'
}
>
<
Box
fontSize=
{
'xl'
}
fontWeight=
{
'bold'
}
>
Open Api
Key
Open Api
</
Box
>
</
Box
>
<
Box
fontSize=
{
'sm'
}
mt=
{
2
}
>
<
Box
fontSize=
{
'sm'
}
mt=
{
2
}
>
请注意保管你的 key,不要泄露!
Open Api 允许你将 Fast Gpt 的部分功能通过 api 的形式接入到自己的应用中。请注意保管你的 Api
Key,不要泄露!
</
Box
>
<
Box
my=
{
1
}
as=
"a"
href=
"https://kjqvjse66l.feishu.cn/docx/DmLedTWtUoNGX8xui9ocdUEjnNh"
color=
{
'blue.800'
}
textDecoration=
{
'underline'
}
target=
{
'_blank'
}
>
点击查看文档
</
Box
>
</
Box
>
<
TableContainer
mt=
{
2
}
position=
{
'relative'
}
>
<
TableContainer
mt=
{
2
}
position=
{
'relative'
}
>
<
Table
>
<
Table
>
...
@@ -93,7 +105,9 @@ const OpenApi = () => {
...
@@ -93,7 +105,9 @@ const OpenApi = () => {
))
}
))
}
</
Tbody
>
</
Tbody
>
</
Table
>
</
Table
>
</
TableContainer
>
<
Button
<
Button
maxW=
{
'200px'
}
mt=
{
5
}
mt=
{
5
}
isLoading=
{
isCreating
}
isLoading=
{
isCreating
}
isDisabled=
{
apiKeys
.
length
>=
5
}
isDisabled=
{
apiKeys
.
length
>=
5
}
...
@@ -102,7 +116,6 @@ const OpenApi = () => {
...
@@ -102,7 +116,6 @@ const OpenApi = () => {
>
>
添加新的 Api Key
添加新的 Api Key
</
Button
>
</
Button
>
</
TableContainer
>
<
Loading
loading=
{
isGetting
||
isDeleting
}
fixed=
{
false
}
/>
<
Loading
loading=
{
isGetting
||
isDeleting
}
fixed=
{
false
}
/>
</
Card
>
</
Card
>
<
Modal
isOpen=
{
!!
apiKey
}
onClose=
{
()
=>
setApiKey
(
''
)
}
>
<
Modal
isOpen=
{
!!
apiKey
}
onClose=
{
()
=>
setApiKey
(
''
)
}
>
...
...
src/service/errorCode.ts
View file @
9b1c0e1a
...
@@ -14,3 +14,30 @@ export const proxyError: Record<string, boolean> = {
...
@@ -14,3 +14,30 @@ export const proxyError: Record<string, boolean> = {
ECONNABORTED
:
true
,
ECONNABORTED
:
true
,
ECONNRESET
:
true
ECONNRESET
:
true
};
};
export
enum
ERROR_ENUM
{
unAuthorization
=
'unAuthorization'
,
insufficientQuota
=
'insufficientQuota'
}
export
const
ERROR_RESPONSE
:
Record
<
any
,
{
code
:
number
;
statusText
:
string
;
message
:
string
;
data
?:
any
;
}
>
=
{
[
ERROR_ENUM
.
unAuthorization
]:
{
code
:
403
,
statusText
:
ERROR_ENUM
.
unAuthorization
,
message
:
'凭证错误'
,
data
:
null
},
[
ERROR_ENUM
.
insufficientQuota
]:
{
code
:
403
,
statusText
:
ERROR_ENUM
.
insufficientQuota
,
message
:
'账号余额不足'
,
data
:
null
}
};
src/service/response.ts
View file @
9b1c0e1a
import
{
NextApiResponse
}
from
'next'
;
import
{
NextApiResponse
}
from
'next'
;
import
{
openaiError
,
openaiError2
,
proxyError
}
from
'./errorCode'
;
import
{
openaiError
,
openaiError2
,
proxyError
,
ERROR_RESPONSE
}
from
'./errorCode'
;
export
interface
ResponseType
<
T
=
any
>
{
export
interface
ResponseType
<
T
=
any
>
{
code
:
number
;
code
:
number
;
...
@@ -18,7 +18,14 @@ export const jsonRes = <T = any>(
...
@@ -18,7 +18,14 @@ export const jsonRes = <T = any>(
)
=>
{
)
=>
{
const
{
code
=
200
,
message
=
''
,
data
=
null
,
error
}
=
props
||
{};
const
{
code
=
200
,
message
=
''
,
data
=
null
,
error
}
=
props
||
{};
let
msg
=
message
;
const
errResponseKey
=
typeof
error
===
'string'
?
error
:
error
?.
message
;
// Specified error
if
(
ERROR_RESPONSE
[
errResponseKey
])
{
return
res
.
json
(
ERROR_RESPONSE
[
errResponseKey
]);
}
// another error
let
msg
=
message
||
error
?.
message
;
if
((
code
<
200
||
code
>=
400
)
&&
!
message
)
{
if
((
code
<
200
||
code
>=
400
)
&&
!
message
)
{
msg
=
error
?.
message
||
'请求错误'
;
msg
=
error
?.
message
||
'请求错误'
;
if
(
typeof
error
===
'string'
)
{
if
(
typeof
error
===
'string'
)
{
...
@@ -40,7 +47,8 @@ export const jsonRes = <T = any>(
...
@@ -40,7 +47,8 @@ export const jsonRes = <T = any>(
res
.
json
({
res
.
json
({
code
,
code
,
statusText
:
''
,
message
:
msg
,
message
:
msg
,
data
data
:
data
||
error
||
null
});
});
};
};
src/service/utils/tools.ts
View file @
9b1c0e1a
import
type
{
NextApiRequest
}
from
'next'
;
import
type
{
NextApiRequest
}
from
'next'
;
import
crypto
from
'crypto'
;
import
crypto
from
'crypto'
;
import
jwt
from
'jsonwebtoken'
;
import
jwt
from
'jsonwebtoken'
;
import
tunnel
from
'tunnel'
;
import
{
ChatItemType
}
from
'@/types/chat'
;
import
{
ChatItemType
}
from
'@/types/chat'
;
import
{
encode
}
from
'gpt-token-utils'
;
import
{
encode
}
from
'gpt-token-utils'
;
import
{
OpenApi
,
User
}
from
'../mongo'
;
import
{
OpenApi
,
User
}
from
'../mongo'
;
import
{
formatPrice
}
from
'@/utils/user'
;
import
{
formatPrice
}
from
'@/utils/user'
;
import
{
ERROR_ENUM
}
from
'../errorCode'
;
/* 密码加密 */
/* 密码加密 */
export
const
hashPassword
=
(
psw
:
string
)
=>
{
export
const
hashPassword
=
(
psw
:
string
)
=>
{
...
@@ -49,20 +49,20 @@ export const authOpenApiKey = async (req: NextApiRequest) => {
...
@@ -49,20 +49,20 @@ export const authOpenApiKey = async (req: NextApiRequest) => {
const
{
apikey
:
apiKey
}
=
req
.
headers
;
const
{
apikey
:
apiKey
}
=
req
.
headers
;
if
(
!
apiKey
)
{
if
(
!
apiKey
)
{
return
Promise
.
reject
(
'api key is empty'
);
return
Promise
.
reject
(
ERROR_ENUM
.
unAuthorization
);
}
}
try
{
try
{
const
openApi
=
await
OpenApi
.
findOne
({
apiKey
});
const
openApi
=
await
OpenApi
.
findOne
({
apiKey
});
if
(
!
openApi
)
{
if
(
!
openApi
)
{
return
Promise
.
reject
(
'api key is error'
);
return
Promise
.
reject
(
ERROR_ENUM
.
unAuthorization
);
}
}
const
userId
=
String
(
openApi
.
userId
);
const
userId
=
String
(
openApi
.
userId
);
// 余额校验
// 余额校验
const
user
=
await
User
.
findById
(
userId
);
const
user
=
await
User
.
findById
(
userId
);
if
(
!
user
)
{
if
(
!
user
)
{
return
Promise
.
reject
(
'user is empty'
);
return
Promise
.
reject
(
ERROR_ENUM
.
unAuthorization
);
}
}
if
(
formatPrice
(
user
.
balance
)
<=
0
)
{
if
(
formatPrice
(
user
.
balance
)
<=
0
)
{
return
Promise
.
reject
(
'Insufficient account balance'
);
return
Promise
.
reject
(
'Insufficient account balance'
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment