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
eceda01c
authored
Apr 08, 2023
by
archer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
perf: openapi auth and lafgpt
parent
ea1681e1
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
61 additions
and
30 deletions
+61
-30
src/pages/api/openapi/chat/lafGpt.ts
+37
-21
src/service/utils/tools.ts
+24
-9
No files found.
src/pages/api/openapi/chat/lafGpt.ts
View file @
eceda01c
...
@@ -12,7 +12,7 @@ import { pushChatBill } from '@/service/events/pushBill';
...
@@ -12,7 +12,7 @@ import { pushChatBill } from '@/service/events/pushBill';
import
{
connectRedis
}
from
'@/service/redis'
;
import
{
connectRedis
}
from
'@/service/redis'
;
import
{
VecModelDataPrefix
}
from
'@/constants/redis'
;
import
{
VecModelDataPrefix
}
from
'@/constants/redis'
;
import
{
vectorToBuffer
}
from
'@/utils/tools'
;
import
{
vectorToBuffer
}
from
'@/utils/tools'
;
import
{
openaiCreateEmbedding
,
g
etOpenApiKey
,
g
pt35StreamResponse
}
from
'@/service/utils/openai'
;
import
{
openaiCreateEmbedding
,
gpt35StreamResponse
}
from
'@/service/utils/openai'
;
/* 发送提示词 */
/* 发送提示词 */
export
default
async
function
handler
(
req
:
NextApiRequest
,
res
:
NextApiResponse
)
{
export
default
async
function
handler
(
req
:
NextApiRequest
,
res
:
NextApiResponse
)
{
...
@@ -31,12 +31,17 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -31,12 +31,17 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
});
});
try
{
try
{
const
{
prompt
,
modelId
}
=
req
.
body
as
{
const
{
prompt
,
modelId
,
isStream
=
true
}
=
req
.
body
as
{
prompt
:
ChatItemType
;
prompt
:
ChatItemType
;
modelId
:
string
;
modelId
:
string
;
isStream
:
boolean
;
};
};
if
(
!
prompt
)
{
if
(
!
prompt
||
!
modelId
)
{
throw
new
Error
(
'缺少参数'
);
throw
new
Error
(
'缺少参数'
);
}
}
...
@@ -45,9 +50,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -45,9 +50,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
let
startTime
=
Date
.
now
();
let
startTime
=
Date
.
now
();
/* 凭证校验 */
/* 凭证校验 */
const
userId
=
await
authOpenApiKey
(
req
);
const
{
apiKey
,
userId
}
=
await
authOpenApiKey
(
req
);
const
{
userApiKey
,
systemKey
}
=
await
getOpenApiKey
(
userId
);
/* 查找数据库里的模型信息 */
/* 查找数据库里的模型信息 */
const
model
=
await
Model
.
findById
(
modelId
);
const
model
=
await
Model
.
findById
(
modelId
);
...
@@ -61,15 +64,18 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -61,15 +64,18 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
if
(
!
modelConstantsData
)
{
if
(
!
modelConstantsData
)
{
throw
new
Error
(
'模型已下架'
);
throw
new
Error
(
'模型已下架'
);
}
}
console
.
log
(
'laf gpt start'
);
// 获取 chatAPI
// 获取 chatAPI
const
chatAPI
=
getOpenAIApi
(
userApiKey
||
system
Key
);
const
chatAPI
=
getOpenAIApi
(
api
Key
);
// 请求一次 chatgpt 拆解需求
// 请求一次 chatgpt 拆解需求
const
promptResponse
=
await
chatAPI
.
createChatCompletion
(
const
promptResponse
=
await
chatAPI
.
createChatCompletion
(
{
{
model
:
ChatModelNameMap
[
ChatModelNameEnum
.
GPT35
],
model
:
ChatModelNameMap
[
ChatModelNameEnum
.
GPT35
],
temperature
:
0
,
temperature
:
0
,
frequency_penalty
:
0.5
,
// 越大,重复内容越少
presence_penalty
:
-
0.5
,
// 越大,越容易出现新内容
messages
:
[
messages
:
[
{
{
role
:
'system'
,
role
:
'system'
,
...
@@ -104,7 +110,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -104,7 +110,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
]
]
},
},
{
{
timeout
:
4
0000
,
timeout
:
12
0000
,
httpsAgent
httpsAgent
}
}
);
);
...
@@ -114,13 +120,13 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -114,13 +120,13 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
throw
new
Error
(
'gpt 异常'
);
throw
new
Error
(
'gpt 异常'
);
}
}
prompt
.
value
+=
`
\n
${
promptResolve
}
`
;
prompt
.
value
+=
`
${
promptResolve
}
`
;
console
.
log
(
'prompt resolve success, time:'
,
`
${(
Date
.
now
()
-
startTime
)
/
1000
}
s`
);
console
.
log
(
'prompt resolve success, time:'
,
`
${(
Date
.
now
()
-
startTime
)
/
1000
}
s`
);
// 获取提示词的向量
// 获取提示词的向量
const
{
vector
:
promptVector
}
=
await
openaiCreateEmbedding
({
const
{
vector
:
promptVector
}
=
await
openaiCreateEmbedding
({
isPay
:
!
userApiKey
,
isPay
:
true
,
apiKey
:
userApiKey
||
system
Key
,
apiKey
:
api
Key
,
userId
,
userId
,
text
:
prompt
.
value
text
:
prompt
.
value
});
});
...
@@ -186,34 +192,44 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -186,34 +192,44 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
const
chatResponse
=
await
chatAPI
.
createChatCompletion
(
const
chatResponse
=
await
chatAPI
.
createChatCompletion
(
{
{
model
:
model
.
service
.
chatModel
,
model
:
model
.
service
.
chatModel
,
temperature
:
temperature
,
temperature
,
// max_tokens: modelConstantsData.maxToken,
messages
:
formatPrompts
,
messages
:
formatPrompts
,
frequency_penalty
:
0.5
,
// 越大,重复内容越少
frequency_penalty
:
0.5
,
// 越大,重复内容越少
presence_penalty
:
-
0.5
,
// 越大,越容易出现新内容
presence_penalty
:
-
0.5
,
// 越大,越容易出现新内容
stream
:
true
stream
:
isStream
},
},
{
{
timeout
:
4
0000
,
timeout
:
12
0000
,
responseType
:
'stream
'
,
responseType
:
isStream
?
'stream'
:
'json
'
,
httpsAgent
httpsAgent
}
}
);
);
console
.
log
(
'
api
response. time:'
,
`
${(
Date
.
now
()
-
startTime
)
/
1000
}
s`
);
console
.
log
(
'
code
response. time:'
,
`
${(
Date
.
now
()
-
startTime
)
/
1000
}
s`
);
step
=
1
;
step
=
1
;
const
{
responseContent
}
=
await
gpt35StreamResponse
({
let
responseContent
=
''
;
if
(
isStream
)
{
const
streamResponse
=
await
gpt35StreamResponse
({
res
,
res
,
stream
,
stream
,
chatResponse
chatResponse
});
});
console
.
log
(
'response done. time:'
,
`
${(
Date
.
now
()
-
startTime
)
/
1000
}
s`
);
responseContent
=
streamResponse
.
responseContent
;
}
else
{
responseContent
=
chatResponse
.
data
.
choices
?.[
0
]?.
message
?.
content
||
''
;
jsonRes
(
res
,
{
data
:
responseContent
});
}
console
.
log
(
'laf gpt done. time:'
,
`
${(
Date
.
now
()
-
startTime
)
/
1000
}
s`
);
const
promptsContent
=
formatPrompts
.
map
((
item
)
=>
item
.
content
).
join
(
''
);
const
promptsContent
=
formatPrompts
.
map
((
item
)
=>
item
.
content
).
join
(
''
);
// 只有使用平台的 key 才计费
pushChatBill
({
pushChatBill
({
isPay
:
!
userApiKey
,
isPay
:
true
,
modelName
:
model
.
service
.
modelName
,
modelName
:
model
.
service
.
modelName
,
userId
,
userId
,
text
:
promptsContent
+
responseContent
text
:
promptsContent
+
responseContent
...
...
src/service/utils/tools.ts
View file @
eceda01c
...
@@ -4,7 +4,8 @@ import jwt from 'jsonwebtoken';
...
@@ -4,7 +4,8 @@ import jwt from 'jsonwebtoken';
import
tunnel
from
'tunnel'
;
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
}
from
'../mongo'
;
import
{
OpenApi
,
User
}
from
'../mongo'
;
import
{
formatPrice
}
from
'@/utils/user'
;
/* 密码加密 */
/* 密码加密 */
export
const
hashPassword
=
(
psw
:
string
)
=>
{
export
const
hashPassword
=
(
psw
:
string
)
=>
{
...
@@ -44,27 +45,41 @@ export const authToken = (token?: string): Promise<string> => {
...
@@ -44,27 +45,41 @@ export const authToken = (token?: string): Promise<string> => {
};
};
/* 校验 open api key */
/* 校验 open api key */
export
const
authOpenApiKey
=
(
req
:
NextApiRequest
)
=>
{
export
const
authOpenApiKey
=
async
(
req
:
NextApiRequest
)
=>
{
return
new
Promise
<
string
>
(
async
(
resolve
,
reject
)
=>
{
const
{
apikey
:
apiKey
}
=
req
.
headers
;
const
{
apikey
:
apiKey
}
=
req
.
headers
;
if
(
!
apiKey
)
{
if
(
!
apiKey
)
{
reject
(
'api key is empty'
);
return
Promise
.
reject
(
'api key is empty'
);
return
;
}
}
try
{
try
{
const
openApi
=
await
OpenApi
.
findOne
({
apiKey
});
const
openApi
=
await
OpenApi
.
findOne
({
apiKey
});
if
(
!
openApi
)
{
if
(
!
openApi
)
{
return
reject
(
'api key is error'
);
return
Promise
.
reject
(
'api key is error'
);
}
const
userId
=
String
(
openApi
.
userId
);
// 余额校验
const
user
=
await
User
.
findById
(
userId
);
if
(
!
user
)
{
return
Promise
.
reject
(
'user is empty'
);
}
}
if
(
formatPrice
(
user
.
balance
)
<=
0
)
{
return
Promise
.
reject
(
'Insufficient account balance'
);
}
// 更新使用的时间
await
OpenApi
.
findByIdAndUpdate
(
openApi
.
_id
,
{
await
OpenApi
.
findByIdAndUpdate
(
openApi
.
_id
,
{
lastUsedTime
:
new
Date
()
lastUsedTime
:
new
Date
()
});
});
resolve
(
String
(
openApi
.
userId
));
return
{
apiKey
:
process
.
env
.
OPENAIKEY
as
string
,
userId
};
}
catch
(
error
)
{
}
catch
(
error
)
{
reject
(
error
);
return
Promise
.
reject
(
error
);
}
}
});
};
};
/* 代理 */
/* 代理 */
...
...
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