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
3ea2cf1d
authored
Apr 21, 2023
by
archer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
perf: chat上下文截断;QA提示词
parent
4397a0ad
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
63 additions
and
34 deletions
+63
-34
src/constants/model.ts
+1
-1
src/pages/api/chat/chatGpt.ts
+18
-7
src/pages/api/chat/vectorGpt.ts
+9
-7
src/pages/api/model/data/splitData.ts
+2
-2
src/pages/api/openapi/chat/chatGpt.ts
+9
-4
src/pages/api/openapi/chat/vectorGpt.ts
+16
-6
src/pages/chat/index.tsx
+1
-1
src/pages/model/detail/components/SelectFileModal.tsx
+1
-1
src/pages/model/detail/components/SelectUrlModal.tsx
+1
-1
src/service/events/generateQA.ts
+5
-4
No files found.
src/constants/model.ts
View file @
3ea2cf1d
...
@@ -35,7 +35,7 @@ export const modelList: ModelConstantsData[] = [
...
@@ -35,7 +35,7 @@ export const modelList: ModelConstantsData[] = [
model
:
ChatModelNameEnum
.
GPT35
,
model
:
ChatModelNameEnum
.
GPT35
,
trainName
:
''
,
trainName
:
''
,
maxToken
:
4000
,
maxToken
:
4000
,
contextMaxToken
:
7
5
00
,
contextMaxToken
:
7
0
00
,
maxTemperature
:
1.5
,
maxTemperature
:
1.5
,
price
:
3
price
:
3
},
},
...
...
src/pages/api/chat/chatGpt.ts
View file @
3ea2cf1d
...
@@ -61,7 +61,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -61,7 +61,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}
}
// 控制在 tokens 数量,防止超出
// 控制在 tokens 数量,防止超出
//
const filterPrompts = openaiChatFilter(prompts, modelConstantsData.contextMaxToken);
const
filterPrompts
=
openaiChatFilter
(
prompts
,
modelConstantsData
.
contextMaxToken
);
// 格式化文本内容成 chatgpt 格式
// 格式化文本内容成 chatgpt 格式
const
map
=
{
const
map
=
{
...
@@ -69,14 +69,25 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -69,14 +69,25 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
AI
:
ChatCompletionRequestMessageRoleEnum
.
Assistant
,
AI
:
ChatCompletionRequestMessageRoleEnum
.
Assistant
,
SYSTEM
:
ChatCompletionRequestMessageRoleEnum
.
System
SYSTEM
:
ChatCompletionRequestMessageRoleEnum
.
System
};
};
const
formatPrompts
:
ChatCompletionRequestMessage
[]
=
prompts
.
map
((
item
:
ChatItemType
)
=>
({
const
formatPrompts
:
ChatCompletionRequestMessage
[]
=
filterPrompts
.
map
(
role
:
map
[
item
.
obj
],
(
item
:
ChatItemType
)
=>
({
content
:
item
.
value
role
:
map
[
item
.
obj
],
}));
content
:
item
.
value
// console.log(formatPrompts);
})
);
// 计算温度
// 计算温度
const
temperature
=
modelConstantsData
.
maxTemperature
*
(
model
.
temperature
/
10
);
const
temperature
=
modelConstantsData
.
maxTemperature
*
(
model
.
temperature
/
10
);
// console.log({
// model: model.service.chatModel,
// temperature: temperature,
// // max_tokens: modelConstantsData.maxToken,
// messages: formatPrompts,
// frequency_penalty: 0.5, // 越大,重复内容越少
// presence_penalty: -0.5, // 越大,越容易出现新内容
// stream: true,
// stop: ['.!?。']
// });
// 获取 chatAPI
// 获取 chatAPI
const
chatAPI
=
getOpenAIApi
(
userApiKey
||
systemKey
);
const
chatAPI
=
getOpenAIApi
(
userApiKey
||
systemKey
);
// 发出请求
// 发出请求
...
...
src/pages/api/chat/vectorGpt.ts
View file @
3ea2cf1d
import
type
{
NextApiRequest
,
NextApiResponse
}
from
'next'
;
import
type
{
NextApiRequest
,
NextApiResponse
}
from
'next'
;
import
{
connectToDatabase
}
from
'@/service/mongo'
;
import
{
connectToDatabase
}
from
'@/service/mongo'
;
import
{
authChat
}
from
'@/service/utils/chat'
;
import
{
authChat
}
from
'@/service/utils/chat'
;
import
{
httpsAgent
,
systemPromptFilter
}
from
'@/service/utils/tools'
;
import
{
httpsAgent
,
systemPromptFilter
,
openaiChatFilter
}
from
'@/service/utils/tools'
;
import
{
ChatCompletionRequestMessage
,
ChatCompletionRequestMessageRoleEnum
}
from
'openai'
;
import
{
ChatCompletionRequestMessage
,
ChatCompletionRequestMessageRoleEnum
}
from
'openai'
;
import
{
ChatItemType
}
from
'@/types/chat'
;
import
{
ChatItemType
}
from
'@/types/chat'
;
import
{
jsonRes
}
from
'@/service/response'
;
import
{
jsonRes
}
from
'@/service/response'
;
...
@@ -79,7 +79,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -79,7 +79,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
`vector <=> '[
${
promptVector
}
]' <
${
similarity
}
`
`vector <=> '[
${
promptVector
}
]' <
${
similarity
}
`
],
],
order
:
[{
field
:
'vector'
,
mode
:
`<=> '[
${
promptVector
}
]'`
}],
order
:
[{
field
:
'vector'
,
mode
:
`<=> '[
${
promptVector
}
]'`
}],
limit
:
3
0
limit
:
2
0
});
});
const
formatRedisPrompt
:
string
[]
=
vectorSearch
.
rows
.
map
((
item
)
=>
`
${
item
.
q
}
\n
${
item
.
a
}
`
);
const
formatRedisPrompt
:
string
[]
=
vectorSearch
.
rows
.
map
((
item
)
=>
`
${
item
.
q
}
\n
${
item
.
a
}
`
);
...
@@ -116,7 +116,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -116,7 +116,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}
}
// 控制在 tokens 数量,防止超出
// 控制在 tokens 数量,防止超出
//
const filterPrompts = openaiChatFilter(prompts, modelConstantsData.contextMaxToken);
const
filterPrompts
=
openaiChatFilter
(
prompts
,
modelConstantsData
.
contextMaxToken
);
// 格式化文本内容成 chatgpt 格式
// 格式化文本内容成 chatgpt 格式
const
map
=
{
const
map
=
{
...
@@ -124,10 +124,12 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -124,10 +124,12 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
AI
:
ChatCompletionRequestMessageRoleEnum
.
Assistant
,
AI
:
ChatCompletionRequestMessageRoleEnum
.
Assistant
,
SYSTEM
:
ChatCompletionRequestMessageRoleEnum
.
System
SYSTEM
:
ChatCompletionRequestMessageRoleEnum
.
System
};
};
const
formatPrompts
:
ChatCompletionRequestMessage
[]
=
prompts
.
map
((
item
:
ChatItemType
)
=>
({
const
formatPrompts
:
ChatCompletionRequestMessage
[]
=
filterPrompts
.
map
(
role
:
map
[
item
.
obj
],
(
item
:
ChatItemType
)
=>
({
content
:
item
.
value
role
:
map
[
item
.
obj
],
}));
content
:
item
.
value
})
);
// console.log(formatPrompts);
// console.log(formatPrompts);
// 计算温度
// 计算温度
const
temperature
=
modelConstantsData
.
maxTemperature
*
(
model
.
temperature
/
10
);
const
temperature
=
modelConstantsData
.
maxTemperature
*
(
model
.
temperature
/
10
);
...
...
src/pages/api/model/data/splitData.ts
View file @
3ea2cf1d
...
@@ -41,11 +41,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -41,11 +41,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
const
tokens
=
encode
(
splitText
+
chunk
).
length
;
const
tokens
=
encode
(
splitText
+
chunk
).
length
;
if
(
tokens
>=
4000
)
{
if
(
tokens
>=
4000
)
{
// 超过 4000,不要这块内容
// 超过 4000,不要这块内容
textList
.
push
(
splitText
);
splitText
&&
textList
.
push
(
splitText
);
splitText
=
chunk
;
splitText
=
chunk
;
}
else
if
(
tokens
>=
3000
)
{
}
else
if
(
tokens
>=
3000
)
{
// 超过 3000,取内容
// 超过 3000,取内容
textList
.
push
(
splitText
+
chunk
);
splitText
&&
textList
.
push
(
splitText
+
chunk
);
splitText
=
''
;
splitText
=
''
;
}
else
{
}
else
{
//没超过 3000,继续添加
//没超过 3000,继续添加
...
...
src/pages/api/openapi/chat/chatGpt.ts
View file @
3ea2cf1d
...
@@ -74,16 +74,21 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -74,16 +74,21 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
});
});
}
}
// 控制在 tokens 数量,防止超出
const
filterPrompts
=
openaiChatFilter
(
prompts
,
modelConstantsData
.
contextMaxToken
);
// 格式化文本内容成 chatgpt 格式
// 格式化文本内容成 chatgpt 格式
const
map
=
{
const
map
=
{
Human
:
ChatCompletionRequestMessageRoleEnum
.
User
,
Human
:
ChatCompletionRequestMessageRoleEnum
.
User
,
AI
:
ChatCompletionRequestMessageRoleEnum
.
Assistant
,
AI
:
ChatCompletionRequestMessageRoleEnum
.
Assistant
,
SYSTEM
:
ChatCompletionRequestMessageRoleEnum
.
System
SYSTEM
:
ChatCompletionRequestMessageRoleEnum
.
System
};
};
const
formatPrompts
:
ChatCompletionRequestMessage
[]
=
prompts
.
map
((
item
:
ChatItemType
)
=>
({
const
formatPrompts
:
ChatCompletionRequestMessage
[]
=
filterPrompts
.
map
(
role
:
map
[
item
.
obj
],
(
item
:
ChatItemType
)
=>
({
content
:
item
.
value
role
:
map
[
item
.
obj
],
}));
content
:
item
.
value
})
);
// console.log(formatPrompts);
// console.log(formatPrompts);
// 计算温度
// 计算温度
const
temperature
=
modelConstantsData
.
maxTemperature
*
(
model
.
temperature
/
10
);
const
temperature
=
modelConstantsData
.
maxTemperature
*
(
model
.
temperature
/
10
);
...
...
src/pages/api/openapi/chat/vectorGpt.ts
View file @
3ea2cf1d
import
type
{
NextApiRequest
,
NextApiResponse
}
from
'next'
;
import
type
{
NextApiRequest
,
NextApiResponse
}
from
'next'
;
import
{
connectToDatabase
,
Model
}
from
'@/service/mongo'
;
import
{
connectToDatabase
,
Model
}
from
'@/service/mongo'
;
import
{
httpsAgent
,
systemPromptFilter
,
authOpenApiKey
}
from
'@/service/utils/tools'
;
import
{
httpsAgent
,
systemPromptFilter
,
authOpenApiKey
,
openaiChatFilter
}
from
'@/service/utils/tools'
;
import
{
ChatCompletionRequestMessage
,
ChatCompletionRequestMessageRoleEnum
}
from
'openai'
;
import
{
ChatCompletionRequestMessage
,
ChatCompletionRequestMessageRoleEnum
}
from
'openai'
;
import
{
ChatItemType
}
from
'@/types/chat'
;
import
{
ChatItemType
}
from
'@/types/chat'
;
import
{
jsonRes
}
from
'@/service/response'
;
import
{
jsonRes
}
from
'@/service/response'
;
...
@@ -93,7 +98,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -93,7 +98,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
`vector <=> '[
${
promptVector
}
]' <
${
similarity
}
`
`vector <=> '[
${
promptVector
}
]' <
${
similarity
}
`
],
],
order
:
[{
field
:
'vector'
,
mode
:
`<=> '[
${
promptVector
}
]'`
}],
order
:
[{
field
:
'vector'
,
mode
:
`<=> '[
${
promptVector
}
]'`
}],
limit
:
3
0
limit
:
2
0
});
});
const
formatRedisPrompt
:
string
[]
=
vectorSearch
.
rows
.
map
((
item
)
=>
`
${
item
.
q
}
\n
${
item
.
a
}
`
);
const
formatRedisPrompt
:
string
[]
=
vectorSearch
.
rows
.
map
((
item
)
=>
`
${
item
.
q
}
\n
${
item
.
a
}
`
);
...
@@ -134,16 +139,21 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -134,16 +139,21 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
});
});
}
}
// 控制在 tokens 数量,防止超出
const
filterPrompts
=
openaiChatFilter
(
prompts
,
modelConstantsData
.
contextMaxToken
);
// 格式化文本内容成 chatgpt 格式
// 格式化文本内容成 chatgpt 格式
const
map
=
{
const
map
=
{
Human
:
ChatCompletionRequestMessageRoleEnum
.
User
,
Human
:
ChatCompletionRequestMessageRoleEnum
.
User
,
AI
:
ChatCompletionRequestMessageRoleEnum
.
Assistant
,
AI
:
ChatCompletionRequestMessageRoleEnum
.
Assistant
,
SYSTEM
:
ChatCompletionRequestMessageRoleEnum
.
System
SYSTEM
:
ChatCompletionRequestMessageRoleEnum
.
System
};
};
const
formatPrompts
:
ChatCompletionRequestMessage
[]
=
prompts
.
map
((
item
:
ChatItemType
)
=>
({
const
formatPrompts
:
ChatCompletionRequestMessage
[]
=
filterPrompts
.
map
(
role
:
map
[
item
.
obj
],
(
item
:
ChatItemType
)
=>
({
content
:
item
.
value
role
:
map
[
item
.
obj
],
}));
content
:
item
.
value
})
);
// console.log(formatPrompts);
// console.log(formatPrompts);
// 计算温度
// 计算温度
const
temperature
=
modelConstantsData
.
maxTemperature
*
(
model
.
temperature
/
10
);
const
temperature
=
modelConstantsData
.
maxTemperature
*
(
model
.
temperature
/
10
);
...
...
src/pages/chat/index.tsx
View file @
3ea2cf1d
...
@@ -88,7 +88,7 @@ const Chat = ({ chatId }: { chatId: string }) => {
...
@@ -88,7 +88,7 @@ const Chat = ({ chatId }: { chatId: string }) => {
throttle
(()
=>
{
throttle
(()
=>
{
if
(
!
ChatBox
.
current
)
return
;
if
(
!
ChatBox
.
current
)
return
;
const
isBottom
=
const
isBottom
=
ChatBox
.
current
.
scrollTop
+
ChatBox
.
current
.
clientHeight
+
8
0
>=
ChatBox
.
current
.
scrollTop
+
ChatBox
.
current
.
clientHeight
+
15
0
>=
ChatBox
.
current
.
scrollHeight
;
ChatBox
.
current
.
scrollHeight
;
isBottom
&&
scrollToBottom
(
'auto'
);
isBottom
&&
scrollToBottom
(
'auto'
);
...
...
src/pages/model/detail/components/SelectFileModal.tsx
View file @
3ea2cf1d
...
@@ -86,7 +86,7 @@ const SelectFileModal = ({
...
@@ -86,7 +86,7 @@ const SelectFileModal = ({
await
postModelDataSplitData
({
await
postModelDataSplitData
({
modelId
,
modelId
,
text
:
fileText
.
replace
(
/
\\
n/g
,
'\n'
).
replace
(
/
\n
+/g
,
'\n'
),
text
:
fileText
.
replace
(
/
\\
n/g
,
'\n'
).
replace
(
/
\n
+/g
,
'\n'
),
prompt
:
`下面是
${
prompt
||
'一段长文本'
}
`
prompt
:
`下面是
"
${
prompt
||
'一段长文本'
}
"
`
});
});
toast
({
toast
({
title
:
'导入数据成功,需要一段拆解和训练'
,
title
:
'导入数据成功,需要一段拆解和训练'
,
...
...
src/pages/model/detail/components/SelectUrlModal.tsx
View file @
3ea2cf1d
...
@@ -45,7 +45,7 @@ const SelectUrlModal = ({
...
@@ -45,7 +45,7 @@ const SelectUrlModal = ({
await
postModelDataSplitData
({
await
postModelDataSplitData
({
modelId
,
modelId
,
text
:
webText
,
text
:
webText
,
prompt
:
`下面是
${
prompt
||
'一段长文本'
}
`
prompt
:
`下面是
"
${
prompt
||
'一段长文本'
}
"
`
});
});
toast
({
toast
({
title
:
'导入数据成功,需要一段拆解和训练'
,
title
:
'导入数据成功,需要一段拆解和训练'
,
...
...
src/service/events/generateQA.ts
View file @
3ea2cf1d
...
@@ -69,9 +69,9 @@ export async function generateQA(next = false): Promise<any> {
...
@@ -69,9 +69,9 @@ export async function generateQA(next = false): Promise<any> {
const
chatAPI
=
getOpenAIApi
(
userApiKey
||
systemKey
);
const
chatAPI
=
getOpenAIApi
(
userApiKey
||
systemKey
);
const
systemPrompt
:
ChatCompletionRequestMessage
=
{
const
systemPrompt
:
ChatCompletionRequestMessage
=
{
role
:
'system'
,
role
:
'system'
,
content
:
`
${
content
:
`
你是出题官.
${
dataItem
.
prompt
||
'下面是
一段长文本
'
dataItem
.
prompt
||
'下面是
"一段长文本"
'
}
,
请从中提取出5至30个问题和答案,并按以下
格式返回: Q1:\nA1:\nQ2:\nA2:\n`
}
,
从中选出5至20个题目和答案,题目包含问答题,计算题,代码题等.答案要详细.按
格式返回: Q1:\nA1:\nQ2:\nA2:\n`
};
};
// 请求 chatgpt 获取回答
// 请求 chatgpt 获取回答
...
@@ -114,7 +114,8 @@ export async function generateQA(next = false): Promise<any> {
...
@@ -114,7 +114,8 @@ export async function generateQA(next = false): Promise<any> {
};
};
})
})
.
catch
((
err
)
=>
{
.
catch
((
err
)
=>
{
console
.
log
(
'QA 拆分错误'
,
err
);
console
.
log
(
'QA拆分错误'
);
console
.
log
(
err
.
response
?.
status
,
err
.
response
?.
statusText
,
err
.
response
?.
data
);
return
Promise
.
reject
(
err
);
return
Promise
.
reject
(
err
);
})
})
)
)
...
...
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