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
fc7edcb5
authored
Apr 06, 2023
by
archer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
perf: log和向量对话
parent
87d35042
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
8 additions
and
53 deletions
+8
-53
src/pages/api/chat/vectorGpt.ts
+6
-41
src/pages/model/detail/components/SelectFileModal.tsx
+0
-3
src/service/events/pushBill.ts
+0
-5
src/service/response.ts
+2
-4
No files found.
src/pages/api/chat/vectorGpt.ts
View file @
fc7edcb5
...
@@ -14,6 +14,7 @@ import { connectRedis } from '@/service/redis';
...
@@ -14,6 +14,7 @@ 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
}
from
'@/service/utils/openai'
;
import
{
openaiCreateEmbedding
}
from
'@/service/utils/openai'
;
import
{
gpt35StreamResponse
}
from
'@/service/utils/openai'
;
/* 发送提示词 */
/* 发送提示词 */
export
default
async
function
handler
(
req
:
NextApiRequest
,
res
:
NextApiResponse
)
{
export
default
async
function
handler
(
req
:
NextApiRequest
,
res
:
NextApiResponse
)
{
...
@@ -152,49 +153,13 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -152,49 +153,13 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
console
.
log
(
'api response time:'
,
`
${(
Date
.
now
()
-
startTime
)
/
1000
}
s`
);
console
.
log
(
'api response time:'
,
`
${(
Date
.
now
()
-
startTime
)
/
1000
}
s`
);
// 创建响应流
res
.
setHeader
(
'Content-Type'
,
'text/event-stream;charset-utf-8'
);
res
.
setHeader
(
'Access-Control-Allow-Origin'
,
'*'
);
res
.
setHeader
(
'X-Accel-Buffering'
,
'no'
);
res
.
setHeader
(
'Cache-Control'
,
'no-cache, no-transform'
);
step
=
1
;
step
=
1
;
let
responseContent
=
''
;
const
{
responseContent
}
=
await
gpt35StreamResponse
({
stream
.
pipe
(
res
);
res
,
stream
,
const
onParse
=
async
(
event
:
ParsedEvent
|
ReconnectInterval
)
=>
{
chatResponse
if
(
event
.
type
!==
'event'
)
return
;
});
const
data
=
event
.
data
;
if
(
data
===
'[DONE]'
)
return
;
try
{
const
json
=
JSON
.
parse
(
data
);
const
content
:
string
=
json
?.
choices
?.[
0
].
delta
.
content
||
''
;
if
(
!
content
||
(
responseContent
===
''
&&
content
===
'\n'
))
return
;
responseContent
+=
content
;
// console.log('content:', content)
!
stream
.
destroyed
&&
stream
.
push
(
content
.
replace
(
/
\n
/g
,
'<br/>'
));
}
catch
(
error
)
{
error
;
}
};
const
decoder
=
new
TextDecoder
();
try
{
for
await
(
const
chunk
of
chatResponse
.
data
as
any
)
{
if
(
stream
.
destroyed
)
{
// 流被中断了,直接忽略后面的内容
break
;
}
const
parser
=
createParser
(
onParse
);
parser
.
feed
(
decoder
.
decode
(
chunk
));
}
}
catch
(
error
)
{
console
.
log
(
'pipe error'
,
error
);
}
// close stream
!
stream
.
destroyed
&&
stream
.
push
(
null
);
stream
.
destroy
();
const
promptsContent
=
formatPrompts
.
map
((
item
)
=>
item
.
content
).
join
(
''
);
const
promptsContent
=
formatPrompts
.
map
((
item
)
=>
item
.
content
).
join
(
''
);
// 只有使用平台的 key 才计费
// 只有使用平台的 key 才计费
...
...
src/pages/model/detail/components/SelectFileModal.tsx
View file @
fc7edcb5
...
@@ -14,7 +14,6 @@ import {
...
@@ -14,7 +14,6 @@ import {
}
from
'@chakra-ui/react'
;
}
from
'@chakra-ui/react'
;
import
{
useToast
}
from
'@/hooks/useToast'
;
import
{
useToast
}
from
'@/hooks/useToast'
;
import
{
useSelectFile
}
from
'@/hooks/useSelectFile'
;
import
{
useSelectFile
}
from
'@/hooks/useSelectFile'
;
import
{
customAlphabet
}
from
'nanoid'
;
import
{
encode
}
from
'gpt-token-utils'
;
import
{
encode
}
from
'gpt-token-utils'
;
import
{
useConfirm
}
from
'@/hooks/useConfirm'
;
import
{
useConfirm
}
from
'@/hooks/useConfirm'
;
import
{
readTxtContent
,
readPdfContent
,
readDocContent
}
from
'@/utils/tools'
;
import
{
readTxtContent
,
readPdfContent
,
readDocContent
}
from
'@/utils/tools'
;
...
@@ -22,8 +21,6 @@ import { useMutation } from '@tanstack/react-query';
...
@@ -22,8 +21,6 @@ import { useMutation } from '@tanstack/react-query';
import
{
postModelDataSplitData
}
from
'@/api/model'
;
import
{
postModelDataSplitData
}
from
'@/api/model'
;
import
{
formatPrice
}
from
'@/utils/user'
;
import
{
formatPrice
}
from
'@/utils/user'
;
const
nanoid
=
customAlphabet
(
'abcdefghijklmnopqrstuvwxyz1234567890'
,
12
);
const
fileExtension
=
'.txt,.doc,.docx,.pdf,.md'
;
const
fileExtension
=
'.txt,.doc,.docx,.pdf,.md'
;
const
SelectFileModal
=
({
const
SelectFileModal
=
({
...
...
src/service/events/pushBill.ts
View file @
fc7edcb5
...
@@ -34,7 +34,6 @@ export const pushChatBill = async ({
...
@@ -34,7 +34,6 @@ export const pushChatBill = async ({
// 计算价格
// 计算价格
const
unitPrice
=
modelItem
?.
price
||
5
;
const
unitPrice
=
modelItem
?.
price
||
5
;
const
price
=
unitPrice
*
tokens
;
const
price
=
unitPrice
*
tokens
;
console
.
log
(
`unit price:
${
unitPrice
}
, price:
${
formatPrice
(
price
)}
元`
);
try
{
try
{
// 插入 Bill 记录
// 插入 Bill 记录
...
@@ -91,8 +90,6 @@ export const pushSplitDataBill = async ({
...
@@ -91,8 +90,6 @@ export const pushSplitDataBill = async ({
// 计算价格
// 计算价格
const
price
=
unitPrice
*
tokenLen
;
const
price
=
unitPrice
*
tokenLen
;
console
.
log
(
`price:
${
formatPrice
(
price
)}
元`
);
// 插入 Bill 记录
// 插入 Bill 记录
const
res
=
await
Bill
.
create
({
const
res
=
await
Bill
.
create
({
userId
,
userId
,
...
@@ -143,8 +140,6 @@ export const pushGenerateVectorBill = async ({
...
@@ -143,8 +140,6 @@ export const pushGenerateVectorBill = async ({
let
price
=
unitPrice
*
tokenLen
;
let
price
=
unitPrice
*
tokenLen
;
price
=
price
>
1
?
price
:
1
;
price
=
price
>
1
?
price
:
1
;
console
.
log
(
`price:
${
formatPrice
(
price
)}
元`
);
// 插入 Bill 记录
// 插入 Bill 记录
const
res
=
await
Bill
.
create
({
const
res
=
await
Bill
.
create
({
userId
,
userId
,
...
...
src/service/response.ts
View file @
fc7edcb5
...
@@ -30,13 +30,11 @@ export const jsonRes = <T = any>(
...
@@ -30,13 +30,11 @@ export const jsonRes = <T = any>(
}
else
if
(
openaiError
[
error
?.
response
?.
statusText
])
{
}
else
if
(
openaiError
[
error
?.
response
?.
statusText
])
{
msg
=
openaiError
[
error
.
response
.
statusText
];
msg
=
openaiError
[
error
.
response
.
statusText
];
}
}
console
.
log
(
'error->'
);
console
.
log
(
`error-> msg:
${
msg
}
`
);
console
.
log
(
'code:'
,
error
.
code
);
console
.
log
(
'msg:'
,
msg
);
// request 时候报错
// request 时候报错
if
(
error
?.
response
)
{
if
(
error
?.
response
)
{
console
.
log
(
'statusText:'
,
error
?.
response
?.
statusText
);
console
.
log
(
'statusText:'
,
error
?.
response
?.
statusText
);
console
.
log
(
'
type:'
,
error
?.
response
?.
data
?.
error
?.
type
);
console
.
log
(
'
error data:'
,
error
?.
response
?.
data
);
}
}
}
}
...
...
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