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
b13c3c4d
authored
Apr 04, 2023
by
archer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: 账单余额问题
parent
c12aa7fd
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
32 additions
and
45 deletions
+32
-45
src/constants/common.ts
+9
-0
src/service/events/generateAbstract.ts
+1
-1
src/service/events/generateQA.ts
+2
-2
src/service/events/generateVector.ts
+13
-1
src/service/events/pushBill.ts
+1
-1
src/service/utils/chat.ts
+1
-4
src/service/utils/openai.ts
+5
-36
No files found.
src/constants/common.ts
View file @
b13c3c4d
...
@@ -36,6 +36,15 @@ wx: YNyiqi
...
@@ -36,6 +36,15 @@ wx: YNyiqi
4. 使用该模型对话。
4. 使用该模型对话。
注意:使用知识库模型对话时,tokens 消耗会加快。
注意:使用知识库模型对话时,tokens 消耗会加快。
### 价格表
如果使用了自己的 Api Key,不会计费。可以在账号页,看到详细账单。单纯使用 chatGPT 模型进行对话,只有一个计费项目。使用知识库时,包含**对话**和**索引**生成两个计费项。
| 计费项 | 价格: 元/ 1K tokens(包含上下文)|
| --- | --- |
| chatgpt - 对话 | 0.003 |
| 知识库 - 对话 | 0.003 |
| 知识库 - 索引 | 0.001 |
| 文件拆分 | 0.003 |
`
;
`
;
export
const
chatProblem
=
`
export
const
chatProblem
=
`
...
...
src/service/events/generateAbstract.ts
View file @
b13c3c4d
...
@@ -39,7 +39,7 @@ export async function generateAbstract(next = false): Promise<any> {
...
@@ -39,7 +39,7 @@ export async function generateAbstract(next = false): Promise<any> {
// 获取 openapi Key
// 获取 openapi Key
let
userApiKey
,
systemKey
;
let
userApiKey
,
systemKey
;
try
{
try
{
const
key
=
await
getOpenApiKey
(
dataItem
.
userId
,
true
);
const
key
=
await
getOpenApiKey
(
dataItem
.
userId
);
userApiKey
=
key
.
userApiKey
;
userApiKey
=
key
.
userApiKey
;
systemKey
=
key
.
systemKey
;
systemKey
=
key
.
systemKey
;
}
catch
(
error
:
any
)
{
}
catch
(
error
:
any
)
{
...
...
src/service/events/generateQA.ts
View file @
b13c3c4d
...
@@ -38,7 +38,7 @@ export async function generateQA(next = false): Promise<any> {
...
@@ -38,7 +38,7 @@ export async function generateQA(next = false): Promise<any> {
// 获取 openapi Key
// 获取 openapi Key
let
userApiKey
,
systemKey
;
let
userApiKey
,
systemKey
;
try
{
try
{
const
key
=
await
getOpenApiKey
(
dataItem
.
userId
,
true
);
const
key
=
await
getOpenApiKey
(
dataItem
.
userId
);
userApiKey
=
key
.
userApiKey
;
userApiKey
=
key
.
userApiKey
;
systemKey
=
key
.
systemKey
;
systemKey
=
key
.
systemKey
;
}
catch
(
error
:
any
)
{
}
catch
(
error
:
any
)
{
...
@@ -48,7 +48,7 @@ export async function generateQA(next = false): Promise<any> {
...
@@ -48,7 +48,7 @@ export async function generateQA(next = false): Promise<any> {
textList
:
[],
textList
:
[],
errorText
:
error
.
message
errorText
:
error
.
message
});
});
throw
new
Error
(
'账号余额不足'
);
throw
new
Error
(
error
?.
message
);
}
}
throw
new
Error
(
'获取 openai key 失败'
);
throw
new
Error
(
'获取 openai key 失败'
);
...
...
src/service/events/generateVector.ts
View file @
b13c3c4d
...
@@ -39,7 +39,19 @@ export async function generateVector(next = false): Promise<any> {
...
@@ -39,7 +39,19 @@ export async function generateVector(next = false): Promise<any> {
};
};
// 获取 openapi Key
// 获取 openapi Key
const
{
userApiKey
,
systemKey
}
=
await
getOpenApiKey
(
dataItem
.
userId
);
let
userApiKey
,
systemKey
;
try
{
const
res
=
await
getOpenApiKey
(
dataItem
.
userId
);
userApiKey
=
res
.
userApiKey
;
systemKey
=
res
.
systemKey
;
}
catch
(
error
:
any
)
{
if
(
error
?.
code
===
501
)
{
await
redis
.
del
(
dataItem
.
id
);
throw
new
Error
(
error
?.
message
);
}
throw
new
Error
(
'获取 openai key 失败'
);
}
// 生成词向量
// 生成词向量
const
{
vector
}
=
await
openaiCreateEmbedding
({
const
{
vector
}
=
await
openaiCreateEmbedding
({
...
...
src/service/events/pushBill.ts
View file @
b13c3c4d
...
@@ -90,7 +90,7 @@ export const pushSplitDataBill = async ({
...
@@ -90,7 +90,7 @@ export const pushSplitDataBill = async ({
try
{
try
{
// 获取模型单价格, 都是用 gpt35 拆分
// 获取模型单价格, 都是用 gpt35 拆分
const
modelItem
=
modelList
.
find
((
item
)
=>
item
.
model
===
ChatModelNameEnum
.
GPT35
);
const
modelItem
=
modelList
.
find
((
item
)
=>
item
.
model
===
ChatModelNameEnum
.
GPT35
);
const
unitPrice
=
modelItem
?.
price
||
5
;
const
unitPrice
=
modelItem
?.
price
||
3
;
// 计算价格
// 计算价格
const
price
=
unitPrice
*
tokens
.
length
;
const
price
=
unitPrice
*
tokens
.
length
;
...
...
src/service/utils/chat.ts
View file @
b13c3c4d
...
@@ -36,10 +36,7 @@ export const authChat = async (chatId: string, authorization?: string) => {
...
@@ -36,10 +36,7 @@ export const authChat = async (chatId: string, authorization?: string) => {
}
}
// 获取 user 的 apiKey
// 获取 user 的 apiKey
const
{
user
,
userApiKey
,
systemKey
}
=
await
getOpenApiKey
(
const
{
user
,
userApiKey
,
systemKey
}
=
await
getOpenApiKey
(
chat
.
userId
as
unknown
as
string
);
chat
.
userId
as
unknown
as
string
,
false
);
// filter 掉被 deleted 的内容
// filter 掉被 deleted 的内容
chat
.
content
=
chat
.
content
.
filter
((
item
)
=>
item
.
deleted
!==
true
);
chat
.
content
=
chat
.
content
.
filter
((
item
)
=>
item
.
deleted
!==
true
);
...
...
src/service/utils/openai.ts
View file @
b13c3c4d
...
@@ -6,20 +6,6 @@ import { formatPrice } from '@/utils/user';
...
@@ -6,20 +6,6 @@ import { formatPrice } from '@/utils/user';
import
{
ChatModelNameEnum
}
from
'@/constants/model'
;
import
{
ChatModelNameEnum
}
from
'@/constants/model'
;
import
{
pushGenerateVectorBill
}
from
'../events/pushBill'
;
import
{
pushGenerateVectorBill
}
from
'../events/pushBill'
;
/* 判断 apikey 是否还有余额 */
export
const
checkKeyGrant
=
async
(
apiKey
:
string
)
=>
{
const
grant
=
await
axios
.
get
(
'https://api.openai.com/dashboard/billing/credit_grants'
,
{
headers
:
{
Authorization
:
`Bearer
${
apiKey
}
`
},
httpsAgent
});
if
(
grant
.
data
?.
total_available
<=
0.2
)
{
return
false
;
}
return
true
;
};
/* 获取用户 api 的 openai 信息 */
/* 获取用户 api 的 openai 信息 */
export
const
getUserApiOpenai
=
async
(
userId
:
string
)
=>
{
export
const
getUserApiOpenai
=
async
(
userId
:
string
)
=>
{
const
user
=
await
User
.
findById
(
userId
);
const
user
=
await
User
.
findById
(
userId
);
...
@@ -30,15 +16,6 @@ export const getUserApiOpenai = async (userId: string) => {
...
@@ -30,15 +16,6 @@ export const getUserApiOpenai = async (userId: string) => {
return
Promise
.
reject
(
'缺少ApiKey, 无法请求'
);
return
Promise
.
reject
(
'缺少ApiKey, 无法请求'
);
}
}
// 余额校验
const
hasGrant
=
await
checkKeyGrant
(
userApiKey
);
if
(
!
hasGrant
)
{
return
Promise
.
reject
({
code
:
501
,
message
:
'API 余额不足'
});
}
return
{
return
{
user
,
user
,
openai
:
getOpenAIApi
(
userApiKey
),
openai
:
getOpenAIApi
(
userApiKey
),
...
@@ -47,27 +24,19 @@ export const getUserApiOpenai = async (userId: string) => {
...
@@ -47,27 +24,19 @@ export const getUserApiOpenai = async (userId: string) => {
};
};
/* 获取 open api key,如果用户没有自己的key,就用平台的,用平台记得加账单 */
/* 获取 open api key,如果用户没有自己的key,就用平台的,用平台记得加账单 */
export
const
getOpenApiKey
=
async
(
userId
:
string
,
checkGrant
=
false
)
=>
{
export
const
getOpenApiKey
=
async
(
userId
:
string
)
=>
{
const
user
=
await
User
.
findById
(
userId
);
const
user
=
await
User
.
findById
(
userId
);
if
(
!
user
)
{
if
(
!
user
)
{
return
Promise
.
reject
(
'找不到用户'
);
return
Promise
.
reject
({
code
:
501
,
message
:
'找不到用户'
});
}
}
const
userApiKey
=
user
?.
accounts
?.
find
((
item
:
any
)
=>
item
.
type
===
'openai'
)?.
value
;
const
userApiKey
=
user
?.
accounts
?.
find
((
item
:
any
)
=>
item
.
type
===
'openai'
)?.
value
;
// 有自己的key
// 有自己的key
if
(
userApiKey
)
{
if
(
userApiKey
)
{
// api 余额校验
if
(
checkGrant
)
{
const
hasGrant
=
await
checkKeyGrant
(
userApiKey
);
if
(
!
hasGrant
)
{
return
Promise
.
reject
({
code
:
501
,
message
:
'API 余额不足'
});
}
}
return
{
return
{
user
,
user
,
userApiKey
,
userApiKey
,
...
...
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