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
e49a831c
authored
Jul 21, 2023
by
archer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
perf: response key
parent
fdcf53ea
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
67 additions
and
48 deletions
+67
-48
client/src/constants/app.ts
+3
-2
client/src/constants/chat.ts
+24
-0
client/src/constants/flow/ModuleTemplate.ts
+3
-3
client/src/pages/api/app/modules/chat/gpt.ts
+2
-2
client/src/pages/api/app/modules/kb/search.ts
+8
-5
client/src/pages/api/openapi/v1/chat/completions.ts
+14
-32
client/src/service/api/request.ts
+4
-4
client/src/service/models/chat.ts
+9
-0
No files found.
client/src/constants/app.ts
View file @
e49a831c
...
...
@@ -14,8 +14,9 @@ export enum SystemInputEnum {
'history'
=
'history'
,
'userChatInput'
=
'userChatInput'
}
export
enum
SpecificInputEnum
{
'answerText'
=
'answerText'
// answer module text key
export
enum
TaskResponseKeyEnum
{
'answerText'
=
'answerText'
,
// answer module text key
'responseData'
=
'responseData'
}
export
enum
VariableInputEnum
{
input
=
'input'
,
...
...
client/src/constants/chat.ts
View file @
e49a831c
...
...
@@ -24,6 +24,30 @@ export const ChatRoleMap = {
}
};
export
enum
ChatSourceEnum
{
'test'
=
'test'
,
online
=
'online'
,
share
=
'share'
,
api
=
'api'
}
export
const
ChatSourceMap
=
{
[
ChatSourceEnum
.
test
]:
{
name
:
'调试测试'
},
[
ChatSourceEnum
.
online
]:
{
name
:
'在线使用'
},
[
ChatSourceEnum
.
share
]:
{
name
:
'链接分享'
},
[
ChatSourceEnum
.
api
]:
{
name
:
'API调用'
}
};
export
const
responseDataKey
=
'responseData'
;
export
const
rawSearchKey
=
'rawSearch'
;
export
const
quoteLenKey
=
'quoteLen'
;
...
...
client/src/constants/flow/ModuleTemplate.ts
View file @
e49a831c
import
{
AppModuleItemTypeEnum
,
SystemInputEnum
,
SpecificInput
Enum
}
from
'../app'
;
import
{
AppModuleItemTypeEnum
,
SystemInputEnum
,
TaskResponseKey
Enum
}
from
'../app'
;
import
{
FlowModuleTypeEnum
,
FlowInputItemTypeEnum
,
FlowOutputItemTypeEnum
}
from
'./index'
;
import
type
{
AppItemType
,
AppModuleTemplateItemType
}
from
'@/types/app'
;
import
{
chatModelList
}
from
'@/store/static'
;
...
...
@@ -177,7 +177,7 @@ export const ChatModule: AppModuleTemplateItemType = {
],
outputs: [
{
key:
SpecificInput
Enum.answerText,
key:
TaskResponseKey
Enum.answerText,
label: '模型回复',
description: '直接响应,无需配置',
type: FlowOutputItemTypeEnum.hidden,
...
...
@@ -264,7 +264,7 @@ export const AnswerModule: AppModuleTemplateItemType = {
inputs: [
Input_Template_TFSwitch,
{
key:
SpecificInput
Enum.answerText,
key:
TaskResponseKey
Enum.answerText,
value: '',
type: FlowInputItemTypeEnum.textarea,
label: '回复的内容'
...
...
client/src/pages/api/app/modules/chat/gpt.ts
View file @
e49a831c
...
...
@@ -10,7 +10,7 @@ import type { ChatItemType } from '@/types/chat';
import
{
ChatRoleEnum
,
sseResponseEventEnum
}
from
'@/constants/chat'
;
import
{
parseStreamChunk
,
textAdaptGptResponse
}
from
'@/utils/adapt'
;
import
{
getOpenAIApi
,
axiosConfig
}
from
'@/service/ai/openai'
;
import
{
SpecificInput
Enum
}
from
'@/constants/app'
;
import
{
TaskResponseKey
Enum
}
from
'@/constants/app'
;
import
{
getChatModel
}
from
'@/service/utils/data'
;
import
{
countModelPrice
,
pushTaskBillListItem
}
from
'@/service/events/pushBill'
;
import
{
authUser
}
from
'@/service/utils/auth'
;
...
...
@@ -27,7 +27,7 @@ export type Props = {
limitPrompt
?:
string
;
billId
?:
string
;
};
export
type
Response
=
{
[
SpecificInput
Enum
.
answerText
]:
string
;
totalTokens
:
number
};
export
type
Response
=
{
[
TaskResponseKey
Enum
.
answerText
]:
string
;
totalTokens
:
number
};
export
default
async
function
handler
(
req
:
NextApiRequest
,
res
:
NextApiResponse
)
{
let
{
model
,
stream
}
=
req
.
body
as
Props
;
...
...
client/src/pages/api/app/modules/kb/search.ts
View file @
e49a831c
...
...
@@ -3,7 +3,7 @@ import { jsonRes } from '@/service/response';
import
{
PgClient
}
from
'@/service/pg'
;
import
{
withNextCors
}
from
'@/service/utils/tools'
;
import
type
{
ChatItemType
}
from
'@/types/chat'
;
import
{
ChatRoleEnum
,
rawSearchKey
}
from
'@/constants/chat'
;
import
{
ChatRoleEnum
,
rawSearchKey
,
responseDataKey
}
from
'@/constants/chat'
;
import
{
modelToolMap
}
from
'@/utils/plugin'
;
import
{
getVector
}
from
'@/pages/api/openapi/plugin/vector'
;
import
{
countModelPrice
,
pushTaskBillListItem
}
from
'@/service/events/pushBill'
;
...
...
@@ -29,7 +29,9 @@ type Props = {
billId
?:
string
;
};
type
Response
=
{
[
rawSearchKey
]:
QuoteItemType
[];
[
responseDataKey
]:
{
[
rawSearchKey
]:
QuoteItemType
[];
};
isEmpty
?:
boolean
;
quotePrompt
?:
string
;
};
...
...
@@ -112,7 +114,6 @@ export async function kbSearch({
// filter part quote by maxToken
const
sliceResult
=
modelToolMap
.
tokenSlice
({
model
:
'gpt-3.5-turbo'
,
maxToken
,
messages
:
searchRes
.
map
((
item
,
i
)
=>
({
obj
:
ChatRoleEnum
.
System
,
...
...
@@ -128,7 +129,9 @@ export async function kbSearch({
return
{
isEmpty
:
rawSearch
.
length
===
0
?
true
:
undefined
,
rawSearch
,
quotePrompt
:
sliceResult
?
`知识库:\n
${
sliceResult
}
`
:
undefined
quotePrompt
:
sliceResult
?
`知识库:\n
${
sliceResult
}
`
:
undefined
,
responseData
:
{
rawSearch
}
};
}
client/src/pages/api/openapi/v1/chat/completions.ts
View file @
e49a831c
...
...
@@ -10,7 +10,7 @@ import { getChatHistory } from './getHistory';
import
{
saveChat
}
from
'@/pages/api/chat/saveChat'
;
import
{
sseResponse
}
from
'@/service/utils/tools'
;
import
{
type
ChatCompletionRequestMessage
}
from
'openai'
;
import
{
SpecificInput
Enum
,
AppModuleItemTypeEnum
}
from
'@/constants/app'
;
import
{
TaskResponseKey
Enum
,
AppModuleItemTypeEnum
}
from
'@/constants/app'
;
import
{
Types
}
from
'mongoose'
;
import
{
moduleFetch
}
from
'@/service/api/request'
;
import
{
AppModuleItemType
,
RunningModuleItemType
}
from
'@/types/app'
;
...
...
@@ -223,34 +223,23 @@ export async function dispatchModules({
})
{
const
runningModules
=
loadModules
(
modules
,
variables
);
let
storeData
:
Record
<
string
,
any
>
=
{};
// after module used
let
responseData
:
Record
<
string
,
any
>
=
{};
// response request and save to database
//
let storeData: Record<string, any> = {}; // after module used
let
chatResponse
:
Record
<
string
,
any
>
=
{};
// response request and save to database
let
answerText
=
''
;
// AI answer
function
pushStore
({
isResponse
=
false
,
answer
,
d
ata
=
{}
responseD
ata
=
{}
}:
{
isResponse
?:
boolean
;
answer
?:
string
;
d
ata
?:
Record
<
string
,
any
>
;
responseD
ata
?:
Record
<
string
,
any
>
;
})
{
if
(
isResponse
)
{
responseData
=
{
...
responseData
,
...
data
};
}
if
(
answer
)
{
answerText
+=
answer
;
}
storeData
=
{
...
storeData
,
...
data
chatResponse
=
{
...
chatResponse
,
...
responseData
};
answerText
+=
answer
;
}
function
moduleInput
(
module
:
RunningModuleItemType
,
...
...
@@ -282,20 +271,13 @@ export async function dispatchModules({
module
:
RunningModuleItemType
,
result
:
Record
<
string
,
any
>
=
{}
):
Promise
<
any
>
{
pushStore
(
result
);
return
Promise
.
all
(
module
.
outputs
.
map
((
outputItem
)
=>
{
if
(
result
[
outputItem
.
key
]
===
undefined
)
return
;
/* update output value */
outputItem
.
value
=
result
[
outputItem
.
key
];
pushStore
({
isResponse
:
outputItem
.
response
,
answer
:
outputItem
.
answer
?
outputItem
.
value
:
''
,
data
:
{
[
outputItem
.
key
]:
outputItem
.
value
}
});
/* update target */
return
Promise
.
all
(
outputItem
.
targets
.
map
((
target
:
any
)
=>
{
...
...
@@ -315,7 +297,7 @@ export async function dispatchModules({
// direct answer
if
(
module
.
type
===
AppModuleItemTypeEnum
.
answer
)
{
const
text
=
module
.
inputs
.
find
((
item
)
=>
item
.
key
===
SpecificInput
Enum
.
answerText
)?.
value
||
''
;
module
.
inputs
.
find
((
item
)
=>
item
.
key
===
TaskResponseKey
Enum
.
answerText
)?.
value
||
''
;
pushStore
({
answer
:
text
});
...
...
@@ -365,7 +347,7 @@ export async function dispatchModules({
await
Promise
.
all
(
initModules
.
map
((
module
)
=>
moduleInput
(
module
,
params
)));
return
{
responseData
,
responseData
:
chatResponse
,
answerText
};
}
...
...
@@ -402,7 +384,7 @@ function loadModules(
}),
outputs
:
module
.
outputs
.
map
((
item
)
=>
({
key
:
item
.
key
,
answer
:
item
.
key
===
SpecificInput
Enum
.
answerText
,
answer
:
item
.
key
===
TaskResponseKey
Enum
.
answerText
,
response
:
item
.
response
,
value
:
undefined
,
targets
:
item
.
targets
...
...
client/src/service/api/request.ts
View file @
e49a831c
...
...
@@ -3,7 +3,7 @@ import { getErrText } from '@/utils/tools';
import
{
parseStreamChunk
}
from
'@/utils/adapt'
;
import
{
NextApiResponse
}
from
'next'
;
import
{
sseResponse
}
from
'../utils/tools'
;
import
{
SpecificInput
Enum
}
from
'@/constants/app'
;
import
{
TaskResponseKey
Enum
}
from
'@/constants/app'
;
interface
Props
{
res
:
NextApiResponse
;
// 用于流转发
...
...
@@ -45,7 +45,7 @@ export const moduleFetch = ({ url, data, res }: Props) =>
const
reader
=
response
.
body
?.
getReader
();
let
chatResponse
:
Record
<
string
,
any
>
=
{
[
SpecificInput
Enum
.
answerText
]:
''
[
TaskResponseKey
Enum
.
answerText
]:
''
};
const
read
=
async
()
=>
{
...
...
@@ -85,8 +85,8 @@ export const moduleFetch = ({ url, data, res }: Props) =>
if
(
answer
)
{
chatResponse
=
{
...
chatResponse
,
[
SpecificInput
Enum
.
answerText
]:
chatResponse
[
SpecificInput
Enum
.
answerText
]
+
answer
[
TaskResponseKey
Enum
.
answerText
]:
chatResponse
[
TaskResponseKey
Enum
.
answerText
]
+
answer
};
}
...
...
client/src/service/models/chat.ts
View file @
e49a831c
import
{
Schema
,
model
,
models
,
Model
}
from
'mongoose'
;
import
{
ChatSchema
as
ChatType
}
from
'@/types/mongoSchema'
;
import
{
ChatRoleMap
}
from
'@/constants/chat'
;
import
{
ChatSourceEnum
,
ChatSourceMap
}
from
'@/constants/chat'
;
const
ChatSchema
=
new
Schema
({
userId
:
{
...
...
@@ -32,6 +33,14 @@ const ChatSchema = new Schema({
type
:
Object
,
default
:
{}
},
// source: {
// type: String,
// enum: Object.keys(ChatSourceMap),
// required: true
// },
// shareId: {
// type: String
// },
content
:
{
type
:
[
{
...
...
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