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
a7459938
authored
May 09, 2023
by
archer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
perf: search prompt
parent
a837552b
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
53 additions
and
44 deletions
+53
-44
src/pages/api/chat/chat.ts
+3
-6
src/pages/api/openapi/chat/chat.ts
+3
-4
src/pages/api/openapi/chat/lafGpt.ts
+2
-2
src/service/plugins/searchKb.ts
+44
-31
src/service/utils/chat/claude.ts
+1
-1
No files found.
src/pages/api/chat/chat.ts
View file @
a7459938
...
@@ -55,7 +55,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -55,7 +55,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
// 使用了知识库搜索
// 使用了知识库搜索
if
(
model
.
chat
.
useKb
)
{
if
(
model
.
chat
.
useKb
)
{
const
{
code
,
searchPrompt
,
aiPrompt
}
=
await
searchKb
({
const
{
code
,
searchPrompt
s
}
=
await
searchKb
({
userOpenAiKey
,
userOpenAiKey
,
prompts
,
prompts
,
similarity
:
ModelVectorSearchModeMap
[
model
.
chat
.
searchMode
]?.
similarity
,
similarity
:
ModelVectorSearchModeMap
[
model
.
chat
.
searchMode
]?.
similarity
,
...
@@ -65,13 +65,10 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -65,13 +65,10 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
// search result is empty
// search result is empty
if
(
code
===
201
)
{
if
(
code
===
201
)
{
return
res
.
send
(
searchPrompt
?.
value
);
return
res
.
send
(
searchPrompt
s
[
0
]
?.
value
);
}
}
if
(
aiPrompt
)
{
prompts
.
splice
(
prompts
.
length
-
1
,
0
,
...
searchPrompts
);
prompts
.
splice
(
prompts
.
length
-
1
,
0
,
aiPrompt
);
}
searchPrompt
&&
prompts
.
unshift
(
searchPrompt
);
}
else
{
}
else
{
// 没有用知识库搜索,仅用系统提示词
// 没有用知识库搜索,仅用系统提示词
model
.
chat
.
systemPrompt
&&
model
.
chat
.
systemPrompt
&&
...
...
src/pages/api/openapi/chat/chat.ts
View file @
a7459938
...
@@ -73,7 +73,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -73,7 +73,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
if
(
model
.
chat
.
useKb
)
{
if
(
model
.
chat
.
useKb
)
{
const
similarity
=
ModelVectorSearchModeMap
[
model
.
chat
.
searchMode
]?.
similarity
||
0.22
;
const
similarity
=
ModelVectorSearchModeMap
[
model
.
chat
.
searchMode
]?.
similarity
||
0.22
;
const
{
code
,
searchPrompt
}
=
await
searchKb
({
const
{
code
,
searchPrompt
s
}
=
await
searchKb
({
prompts
,
prompts
,
similarity
,
similarity
,
model
,
model
,
...
@@ -82,10 +82,9 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -82,10 +82,9 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
// search result is empty
// search result is empty
if
(
code
===
201
)
{
if
(
code
===
201
)
{
return
res
.
send
(
searchPrompt
?.
value
);
return
res
.
send
(
searchPrompt
s
[
0
]
?.
value
);
}
}
prompts
.
splice
(
prompts
.
length
-
1
,
0
,
...
searchPrompts
);
searchPrompt
&&
prompts
.
unshift
(
searchPrompt
);
}
else
{
}
else
{
// 没有用知识库搜索,仅用系统提示词
// 没有用知识库搜索,仅用系统提示词
if
(
model
.
chat
.
systemPrompt
)
{
if
(
model
.
chat
.
systemPrompt
)
{
...
...
src/pages/api/openapi/chat/lafGpt.ts
View file @
a7459938
...
@@ -122,14 +122,14 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -122,14 +122,14 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
const
prompts
=
[
prompt
];
const
prompts
=
[
prompt
];
// 获取向量匹配到的提示词
// 获取向量匹配到的提示词
const
{
searchPrompt
}
=
await
searchKb
({
const
{
searchPrompt
s
}
=
await
searchKb
({
similarity
:
ModelVectorSearchModeMap
[
model
.
chat
.
searchMode
]?.
similarity
,
similarity
:
ModelVectorSearchModeMap
[
model
.
chat
.
searchMode
]?.
similarity
,
prompts
,
prompts
,
model
,
model
,
userId
userId
});
});
searchPrompt
&&
prompts
.
unshift
(
searchPrompt
);
prompts
.
splice
(
prompts
.
length
-
1
,
0
,
...
searchPrompts
);
// 计算温度
// 计算温度
const
temperature
=
(
modelConstantsData
.
maxTemperature
*
(
model
.
chat
.
temperature
/
10
)).
toFixed
(
const
temperature
=
(
modelConstantsData
.
maxTemperature
*
(
model
.
chat
.
temperature
/
10
)).
toFixed
(
...
...
src/service/plugins/searchKb.ts
View file @
a7459938
...
@@ -23,14 +23,10 @@ export const searchKb = async ({
...
@@ -23,14 +23,10 @@ export const searchKb = async ({
similarity
?:
number
;
similarity
?:
number
;
}):
Promise
<
{
}):
Promise
<
{
code
:
200
|
201
;
code
:
200
|
201
;
searchPrompt
?
:
{
searchPrompt
s
:
{
obj
:
`
${
ChatRoleEnum
.
System
}
`
;
obj
:
ChatRoleEnum
;
value
:
string
;
value
:
string
;
};
}[];
aiPrompt
?:
{
obj
:
`
${
ChatRoleEnum
.
AI
}
`
;
value
:
string
;
};
}
>
=>
{
}
>
=>
{
async
function
search
(
textArr
:
string
[]
=
[])
{
async
function
search
(
textArr
:
string
[]
=
[])
{
// 获取提示词的向量
// 获取提示词的向量
...
@@ -102,41 +98,58 @@ export const searchKb = async ({
...
@@ -102,41 +98,58 @@ export const searchKb = async ({
if
(
!
filterSystemPrompt
&&
model
.
chat
.
searchMode
===
ModelVectorSearchModeEnum
.
hightSimilarity
)
{
if
(
!
filterSystemPrompt
&&
model
.
chat
.
searchMode
===
ModelVectorSearchModeEnum
.
hightSimilarity
)
{
return
{
return
{
code
:
201
,
code
:
201
,
searchPrompt
:
{
searchPrompts
:
[
obj
:
ChatRoleEnum
.
System
,
{
value
:
'对不起,你的问题不在知识库中。'
obj
:
ChatRoleEnum
.
System
,
}
value
:
'对不起,你的问题不在知识库中。'
}
]
};
};
}
}
/* 高相似度+无上下文,不添加额外知识,仅用系统提示词 */
/* 高相似度+无上下文,不添加额外知识,仅用系统提示词 */
if
(
!
filterSystemPrompt
&&
model
.
chat
.
searchMode
===
ModelVectorSearchModeEnum
.
noContext
)
{
if
(
!
filterSystemPrompt
&&
model
.
chat
.
searchMode
===
ModelVectorSearchModeEnum
.
noContext
)
{
return
{
return
{
code
:
200
,
code
:
200
,
searchPrompt
:
model
.
chat
.
systemPrompt
searchPrompts
:
model
.
chat
.
systemPrompt
?
{
?
[
obj
:
ChatRoleEnum
.
System
,
{
value
:
model
.
chat
.
systemPrompt
obj
:
ChatRoleEnum
.
System
,
}
value
:
model
.
chat
.
systemPrompt
:
undefined
}
]
:
[]
};
};
}
}
/* 有匹配 */
/* 有匹配 */
return
{
return
{
code
:
200
,
code
:
200
,
searchPrompt
:
{
searchPrompts
:
[
obj
:
ChatRoleEnum
.
System
,
{
value
:
`知识库:'
${
filterSystemPrompt
}
'`
obj
:
ChatRoleEnum
.
System
,
},
value
:
`知识库:'
${
filterSystemPrompt
}
'`
aiPrompt
:
ModelVectorSearchModeEnum
.
hightSimilarity
},
?
{
...(
model
.
chat
.
systemPrompt
obj
:
'AI'
,
?
[
value
:
`我来玩一个问答游戏,规则为:
{
1.我完全忘记我已有的知识
obj
:
ChatRoleEnum
.
System
,
2.我只能回答关于"
${
model
.
chat
.
systemPrompt
||
model
.
name
}
"的问题
value
:
model
.
chat
.
systemPrompt
3.我只能从知识库中选择内容进行回答
}
4.如果问题不在知识库中,我会回答"我不知道。"`
]
}
:
[]),
:
undefined
...(
model
.
chat
.
searchMode
!==
ModelVectorSearchModeEnum
.
noContext
?
[
{
obj
:
ChatRoleEnum
.
System
,
value
:
`我们来玩问答游戏,规则为:
1.你完全忘记你已有的知识
2.你只能回答关于"
${
model
.
name
}
"的问题
3.你只能从知识库中选择内容进行回答
4.如果问题不在知识库中,你会回答"我不知道。"
务必遵守规则`
}
]
:
[])
]
};
};
};
};
src/service/utils/chat/claude.ts
View file @
a7459938
...
@@ -27,7 +27,7 @@ export const lafClaudChat = async ({
...
@@ -27,7 +27,7 @@ export const lafClaudChat = async ({
.
join
(
'\n'
);
.
join
(
'\n'
);
const
systemPromptText
=
systemPrompt
?
`\n知识库内容:'
${
systemPrompt
}
'\n`
:
''
;
const
systemPromptText
=
systemPrompt
?
`\n知识库内容:'
${
systemPrompt
}
'\n`
:
''
;
const
prompt
=
`
${
systemPromptText
}
我的问题
:'
${
messages
[
messages
.
length
-
1
].
value
}
'`
;
const
prompt
=
`
${
systemPromptText
}
\n我的问题是
:'
${
messages
[
messages
.
length
-
1
].
value
}
'`
;
const
lafResponse
=
await
axios
.
post
(
const
lafResponse
=
await
axios
.
post
(
'https://hnvacz.laf.run/claude-gpt'
,
'https://hnvacz.laf.run/claude-gpt'
,
...
...
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