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
144bed5a
authored
Apr 05, 2023
by
archer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
perf: 优化tokens计算
parent
96fc917b
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
44 additions
and
43 deletions
+44
-43
src/constants/common.ts
+1
-1
src/service/events/generateQA.ts
+17
-16
src/service/events/pushBill.ts
+18
-21
src/service/utils/openai.ts
+8
-5
No files found.
src/constants/common.ts
View file @
144bed5a
...
@@ -43,7 +43,7 @@ wx号: YNyiqi
...
@@ -43,7 +43,7 @@ wx号: YNyiqi
| --- | --- |
| --- | --- |
| chatgpt - 对话 | 0.03 |
| chatgpt - 对话 | 0.03 |
| 知识库 - 对话 | 0.03 |
| 知识库 - 对话 | 0.03 |
| 知识库 - 索引 | 0.0
1
|
| 知识库 - 索引 | 0.0
04
|
| 文件拆分 | 0.03 |
| 文件拆分 | 0.03 |
`
;
`
;
...
...
src/service/events/generateQA.ts
View file @
144bed5a
...
@@ -40,7 +40,8 @@ export async function generateQA(next = false): Promise<any> {
...
@@ -40,7 +40,8 @@ export async function generateQA(next = false): Promise<any> {
const
textList
:
string
[]
=
dataItem
.
textList
.
slice
(
-
5
);
const
textList
:
string
[]
=
dataItem
.
textList
.
slice
(
-
5
);
// 获取 openapi Key
// 获取 openapi Key
let
userApiKey
,
systemKey
;
let
userApiKey
=
''
,
systemKey
=
''
;
try
{
try
{
const
key
=
await
getOpenApiKey
(
dataItem
.
userId
);
const
key
=
await
getOpenApiKey
(
dataItem
.
userId
);
userApiKey
=
key
.
userApiKey
;
userApiKey
=
key
.
userApiKey
;
...
@@ -93,10 +94,21 @@ export async function generateQA(next = false): Promise<any> {
...
@@ -93,10 +94,21 @@ export async function generateQA(next = false): Promise<any> {
httpsAgent
httpsAgent
}
}
)
)
.
then
((
res
)
=>
({
.
then
((
res
)
=>
{
rawContent
:
res
?.
data
.
choices
[
0
].
message
?.
content
||
''
,
// chatgpt原本的回复
const
rawContent
=
res
?.
data
.
choices
[
0
].
message
?.
content
||
''
;
result
:
splitText
(
res
?.
data
.
choices
[
0
].
message
?.
content
||
''
)
// 格式化后的QA对
// 计费
}))
pushSplitDataBill
({
isPay
:
!
userApiKey
,
userId
:
dataItem
.
userId
,
type
:
'QA'
,
text
:
systemPrompt
.
content
+
text
+
rawContent
,
tokenLen
:
res
.
data
.
usage
?.
total_tokens
||
0
});
return
{
rawContent
,
// chatgpt 原本的回复
result
:
splitText
(
res
?.
data
.
choices
[
0
].
message
?.
content
||
''
)
// 格式化后的QA对
};
})
)
)
);
);
...
@@ -141,17 +153,6 @@ export async function generateQA(next = false): Promise<any> {
...
@@ -141,17 +153,6 @@ export async function generateQA(next = false): Promise<any> {
resultList
.
length
resultList
.
length
);
);
// 计费
pushSplitDataBill
({
isPay
:
!
userApiKey
&&
resultList
.
length
>
0
,
userId
:
dataItem
.
userId
,
type
:
'QA'
,
text
:
systemPrompt
.
content
+
textList
.
join
(
''
)
+
successResponse
.
map
((
item
)
=>
item
.
rawContent
).
join
(
''
)
});
generateQA
(
true
);
generateQA
(
true
);
generateVector
();
generateVector
();
}
catch
(
error
:
any
)
{
}
catch
(
error
:
any
)
{
...
...
src/service/events/pushBill.ts
View file @
144bed5a
...
@@ -22,9 +22,9 @@ export const pushChatBill = async ({
...
@@ -22,9 +22,9 @@ export const pushChatBill = async ({
try
{
try
{
// 计算 token 数量
// 计算 token 数量
const
tokens
=
encode
(
text
);
const
tokens
=
encode
(
text
)
.
length
;
console
.
log
(
`chat generate success. text len:
${
text
.
length
}
. token len:
${
tokens
.
length
}
`
);
console
.
log
(
`chat generate success. text len:
${
text
.
length
}
. token len:
${
tokens
}
`
);
if
(
isPay
)
{
if
(
isPay
)
{
await
connectToDatabase
();
await
connectToDatabase
();
...
@@ -33,7 +33,7 @@ export const pushChatBill = async ({
...
@@ -33,7 +33,7 @@ export const pushChatBill = async ({
const
modelItem
=
modelList
.
find
((
item
)
=>
item
.
model
===
modelName
);
const
modelItem
=
modelList
.
find
((
item
)
=>
item
.
model
===
modelName
);
// 计算价格
// 计算价格
const
unitPrice
=
modelItem
?.
price
||
5
;
const
unitPrice
=
modelItem
?.
price
||
5
;
const
price
=
unitPrice
*
tokens
.
length
;
const
price
=
unitPrice
*
tokens
;
console
.
log
(
`unit price:
${
unitPrice
}
, price:
${
formatPrice
(
price
)}
元`
);
console
.
log
(
`unit price:
${
unitPrice
}
, price:
${
formatPrice
(
price
)}
元`
);
try
{
try
{
...
@@ -44,7 +44,7 @@ export const pushChatBill = async ({
...
@@ -44,7 +44,7 @@ export const pushChatBill = async ({
modelName
,
modelName
,
chatId
,
chatId
,
textLen
:
text
.
length
,
textLen
:
text
.
length
,
tokenLen
:
tokens
.
length
,
tokenLen
:
tokens
,
price
price
});
});
billId
=
res
.
_id
;
billId
=
res
.
_id
;
...
@@ -66,11 +66,13 @@ export const pushChatBill = async ({
...
@@ -66,11 +66,13 @@ export const pushChatBill = async ({
export
const
pushSplitDataBill
=
async
({
export
const
pushSplitDataBill
=
async
({
isPay
,
isPay
,
userId
,
userId
,
tokenLen
,
text
,
text
,
type
type
}:
{
}:
{
isPay
:
boolean
;
isPay
:
boolean
;
userId
:
string
;
userId
:
string
;
tokenLen
:
number
;
text
:
string
;
text
:
string
;
type
:
DataType
;
type
:
DataType
;
})
=>
{
})
=>
{
...
@@ -79,12 +81,7 @@ export const pushSplitDataBill = async ({
...
@@ -79,12 +81,7 @@ export const pushSplitDataBill = async ({
let
billId
;
let
billId
;
try
{
try
{
// 计算 token 数量
console
.
log
(
`splitData generate success. text len:
${
text
.
length
}
. token len:
${
tokenLen
}
`
);
const
tokens
=
encode
(
text
);
console
.
log
(
`splitData generate success. text len:
${
text
.
length
}
. token len:
${
tokens
.
length
}
`
);
if
(
isPay
)
{
if
(
isPay
)
{
try
{
try
{
...
@@ -92,7 +89,7 @@ export const pushSplitDataBill = async ({
...
@@ -92,7 +89,7 @@ export const pushSplitDataBill = async ({
const
modelItem
=
modelList
.
find
((
item
)
=>
item
.
model
===
ChatModelNameEnum
.
GPT35
);
const
modelItem
=
modelList
.
find
((
item
)
=>
item
.
model
===
ChatModelNameEnum
.
GPT35
);
const
unitPrice
=
modelItem
?.
price
||
3
;
const
unitPrice
=
modelItem
?.
price
||
3
;
// 计算价格
// 计算价格
const
price
=
unitPrice
*
token
s
.
length
;
const
price
=
unitPrice
*
token
Len
;
console
.
log
(
`price:
${
formatPrice
(
price
)}
元`
);
console
.
log
(
`price:
${
formatPrice
(
price
)}
元`
);
...
@@ -102,7 +99,7 @@ export const pushSplitDataBill = async ({
...
@@ -102,7 +99,7 @@ export const pushSplitDataBill = async ({
type
,
type
,
modelName
:
ChatModelNameEnum
.
GPT35
,
modelName
:
ChatModelNameEnum
.
GPT35
,
textLen
:
text
.
length
,
textLen
:
text
.
length
,
tokenLen
:
tokens
.
length
,
tokenLen
,
price
price
});
});
billId
=
res
.
_id
;
billId
=
res
.
_id
;
...
@@ -124,27 +121,27 @@ export const pushSplitDataBill = async ({
...
@@ -124,27 +121,27 @@ export const pushSplitDataBill = async ({
export
const
pushGenerateVectorBill
=
async
({
export
const
pushGenerateVectorBill
=
async
({
isPay
,
isPay
,
userId
,
userId
,
text
text
,
tokenLen
}:
{
}:
{
isPay
:
boolean
;
isPay
:
boolean
;
userId
:
string
;
userId
:
string
;
text
:
string
;
text
:
string
;
tokenLen
:
number
;
})
=>
{
})
=>
{
await
connectToDatabase
();
await
connectToDatabase
();
let
billId
;
let
billId
;
try
{
try
{
// 计算 token 数量
console
.
log
(
`vector generate success. text len:
${
text
.
length
}
. token len:
${
tokenLen
}
`
);
const
tokens
=
encode
(
text
);
console
.
log
(
`vector generate success. text len:
${
text
.
length
}
. token len:
${
tokens
.
length
}
`
);
if
(
isPay
)
{
if
(
isPay
)
{
try
{
try
{
const
unitPrice
=
1
;
const
unitPrice
=
0.4
;
// 计算价格
// 计算价格. 至少为1
const
price
=
unitPrice
*
tokens
.
length
;
let
price
=
unitPrice
*
tokenLen
;
price
=
price
>
1
?
price
:
1
;
console
.
log
(
`price:
${
formatPrice
(
price
)}
元`
);
console
.
log
(
`price:
${
formatPrice
(
price
)}
元`
);
...
@@ -154,7 +151,7 @@ export const pushGenerateVectorBill = async ({
...
@@ -154,7 +151,7 @@ export const pushGenerateVectorBill = async ({
type
:
BillTypeEnum
.
vector
,
type
:
BillTypeEnum
.
vector
,
modelName
:
ChatModelNameEnum
.
VECTOR
,
modelName
:
ChatModelNameEnum
.
VECTOR
,
textLen
:
text
.
length
,
textLen
:
text
.
length
,
tokenLen
:
tokens
.
length
,
tokenLen
,
price
price
});
});
billId
=
res
.
_id
;
billId
=
res
.
_id
;
...
...
src/service/utils/openai.ts
View file @
144bed5a
import
axios
from
'axios'
;
import
{
getOpenAIApi
}
from
'@/service/utils/chat'
;
import
{
getOpenAIApi
}
from
'@/service/utils/chat'
;
import
{
httpsAgent
}
from
'./tools'
;
import
{
httpsAgent
}
from
'./tools'
;
import
{
User
}
from
'../models/user'
;
import
{
User
}
from
'../models/user'
;
...
@@ -75,7 +74,7 @@ export const openaiCreateEmbedding = async ({
...
@@ -75,7 +74,7 @@ export const openaiCreateEmbedding = async ({
const
chatAPI
=
getOpenAIApi
(
apiKey
);
const
chatAPI
=
getOpenAIApi
(
apiKey
);
// 把输入的内容转成向量
// 把输入的内容转成向量
const
vector
=
await
chatAPI
const
res
=
await
chatAPI
.
createEmbedding
(
.
createEmbedding
(
{
{
model
:
ChatModelNameEnum
.
VECTOR
,
model
:
ChatModelNameEnum
.
VECTOR
,
...
@@ -86,16 +85,20 @@ export const openaiCreateEmbedding = async ({
...
@@ -86,16 +85,20 @@ export const openaiCreateEmbedding = async ({
httpsAgent
httpsAgent
}
}
)
)
.
then
((
res
)
=>
res
?.
data
?.
data
?.[
0
]?.
embedding
||
[]);
.
then
((
res
)
=>
({
tokenLen
:
res
.
data
.
usage
.
total_tokens
||
0
,
vector
:
res
?.
data
?.
data
?.[
0
]?.
embedding
||
[]
}));
pushGenerateVectorBill
({
pushGenerateVectorBill
({
isPay
,
isPay
,
userId
,
userId
,
text
text
,
tokenLen
:
res
.
tokenLen
});
});
return
{
return
{
vector
,
vector
:
res
.
vector
,
chatAPI
chatAPI
};
};
};
};
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