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
2b2c70e5
authored
Mar 30, 2023
by
archer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 模型数据导入
parent
f32c557b
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
16 changed files
with
415 additions
and
76 deletions
+415
-76
src/api/model.ts
+11
-6
src/constants/model.ts
+2
-3
src/constants/redis.ts
+2
-0
src/hooks/usePaging.ts
+2
-1
src/pages/api/model/data/delModelDataById.ts
+4
-4
src/pages/api/model/data/pushModelDataInput.ts
+2
-1
src/pages/api/model/data/pushModelDataSelectData.ts
+57
-0
src/pages/api/model/data/putModelData.ts
+6
-6
src/pages/model/components/ModelDataCard.tsx
+254
-14
src/pages/model/detail.tsx
+1
-1
src/service/models/modelData.ts
+10
-9
src/service/mongo.ts
+0
-2
src/service/redis.ts
+37
-25
src/types/model.d.ts
+9
-0
src/types/mongoSchema.d.ts
+8
-4
src/types/redis.d.ts
+10
-0
No files found.
src/api/model.ts
View file @
2b2c70e5
import
{
GET
,
POST
,
DELETE
,
PUT
}
from
'./request'
;
import
type
{
ModelSchema
}
from
'@/types/mongoSchema'
;
import
type
{
ModelSchema
,
ModelDataSchema
}
from
'@/types/mongoSchema'
;
import
{
ModelUpdateParams
}
from
'@/types/model'
;
import
{
TrainingItemType
}
from
'../types/training'
;
import
{
PagingData
}
from
'@/types'
;
...
...
@@ -39,10 +39,15 @@ type GetModelDataListProps = RequestPaging & {
export
const
getModelDataList
=
(
props
:
GetModelDataListProps
)
=>
GET
(
`/model/data/getModelData?
${
Obj2Query
(
props
)}
`
);
export
const
postModelData
=
(
data
:
{
modelId
:
string
;
data
:
{
q
:
string
;
a
:
string
}[]
})
=>
POST
(
`/model/data/pushModelData`
,
data
);
export
const
postModelDataInput
=
(
data
:
{
modelId
:
string
;
data
:
{
text
:
ModelDataSchema
[
'text'
];
q
:
ModelDataSchema
[
'q'
]
}[];
})
=>
POST
(
`/model/data/pushModelDataInput`
,
data
);
export
const
postModelDataSelect
=
(
modelId
:
string
,
dataIds
:
string
[])
=>
POST
(
`/model/data/pushModelDataSelectData`
,
{
modelId
,
dataIds
});
export
const
putModelDataById
=
(
data
:
{
modelId
:
string
;
answer
:
string
})
=>
export
const
putModelDataById
=
(
data
:
{
dataId
:
string
;
text
:
string
})
=>
PUT
(
'/model/data/putModelData'
,
data
);
export
const
DelOneModelData
=
(
model
Id
:
string
)
=>
DELETE
(
`/model/data/delModelDataById?
modelId=
${
model
Id
}
`
);
export
const
delOneModelData
=
(
data
Id
:
string
)
=>
DELETE
(
`/model/data/delModelDataById?
dataId=
${
data
Id
}
`
);
src/constants/model.ts
View file @
2b2c70e5
...
...
@@ -75,10 +75,9 @@ export const formatModelStatus = {
}
};
export
const
ModelDataStatusMap
:
Record
<
ModelDataType
,
string
>
=
{
export
const
ModelDataStatusMap
=
{
0
:
'训练完成'
,
1
:
'等待训练'
,
2
:
'训练中'
1
:
'训练中'
};
export
const
defaultModel
:
ModelSchema
=
{
...
...
src/constants/redis.ts
0 → 100644
View file @
2b2c70e5
export
const
ModelDataIndex
=
'model:data'
;
export
const
VecModelDataIndex
=
'vec:model:data'
;
src/hooks/usePaging.ts
View file @
2b2c70e5
...
...
@@ -75,6 +75,7 @@ export const usePaging = <T = any>({
requesting
,
isLoadAll
,
nextPage
,
initRequesting
initRequesting
,
setData
};
};
src/pages/api/model/data/delModelDataById.ts
View file @
2b2c70e5
...
...
@@ -5,8 +5,8 @@ import { authToken } from '@/service/utils/tools';
export
default
async
function
handler
(
req
:
NextApiRequest
,
res
:
NextApiResponse
<
any
>
)
{
try
{
let
{
model
Id
}
=
req
.
query
as
{
model
Id
:
string
;
let
{
data
Id
}
=
req
.
query
as
{
data
Id
:
string
;
};
const
{
authorization
}
=
req
.
headers
;
...
...
@@ -14,7 +14,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
throw
new
Error
(
'无权操作'
);
}
if
(
!
model
Id
)
{
if
(
!
data
Id
)
{
throw
new
Error
(
'缺少参数'
);
}
...
...
@@ -24,7 +24,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
await
connectToDatabase
();
await
ModelData
.
deleteOne
({
model
Id
,
_id
:
data
Id
,
userId
});
...
...
src/pages/api/model/data/pushModelData.ts
→
src/pages/api/model/data/pushModelData
Input
.ts
View file @
2b2c70e5
...
...
@@ -2,12 +2,13 @@ import type { NextApiRequest, NextApiResponse } from 'next';
import
{
jsonRes
}
from
'@/service/response'
;
import
{
connectToDatabase
,
ModelData
,
Model
}
from
'@/service/mongo'
;
import
{
authToken
}
from
'@/service/utils/tools'
;
import
{
ModelDataSchema
}
from
'@/types/mongoSchema'
;
export
default
async
function
handler
(
req
:
NextApiRequest
,
res
:
NextApiResponse
<
any
>
)
{
try
{
const
{
modelId
,
data
}
=
req
.
body
as
{
modelId
:
string
;
data
:
{
q
:
string
;
a
:
string
}[];
data
:
{
text
:
ModelDataSchema
[
'text'
];
q
:
ModelDataSchema
[
'q'
]
}[];
};
const
{
authorization
}
=
req
.
headers
;
...
...
src/pages/api/model/data/pushModelDataSelectData.ts
0 → 100644
View file @
2b2c70e5
import
type
{
NextApiRequest
,
NextApiResponse
}
from
'next'
;
import
{
jsonRes
}
from
'@/service/response'
;
import
{
connectToDatabase
,
DataItem
,
ModelData
}
from
'@/service/mongo'
;
import
{
authToken
}
from
'@/service/utils/tools'
;
import
{
customAlphabet
}
from
'nanoid'
;
const
nanoid
=
customAlphabet
(
'abcdefghijklmnopqrstuvwxyz1234567890'
,
12
);
export
default
async
function
handler
(
req
:
NextApiRequest
,
res
:
NextApiResponse
)
{
try
{
let
{
dataIds
,
modelId
}
=
req
.
body
as
{
dataIds
:
string
[];
modelId
:
string
};
if
(
!
dataIds
)
{
throw
new
Error
(
'参数错误'
);
}
await
connectToDatabase
();
const
{
authorization
}
=
req
.
headers
;
const
userId
=
await
authToken
(
authorization
);
const
dataItems
=
(
await
Promise
.
all
(
dataIds
.
map
((
dataId
)
=>
DataItem
.
find
<
{
_id
:
string
;
result
:
{
q
:
string
}[];
text
:
string
}
>
(
{
userId
,
dataId
},
'result text'
)
)
)
).
flat
();
// push data
await
ModelData
.
insertMany
(
dataItems
.
map
((
item
)
=>
({
modelId
:
modelId
,
userId
,
text
:
item
.
text
,
q
:
item
.
result
.
map
((
item
)
=>
({
id
:
nanoid
(),
text
:
item
.
q
}))
}))
);
jsonRes
(
res
,
{
data
:
dataItems
});
}
catch
(
err
)
{
jsonRes
(
res
,
{
code
:
500
,
error
:
err
});
}
}
src/pages/api/model/data/putModelData.ts
View file @
2b2c70e5
...
...
@@ -5,9 +5,9 @@ import { authToken } from '@/service/utils/tools';
export
default
async
function
handler
(
req
:
NextApiRequest
,
res
:
NextApiResponse
<
any
>
)
{
try
{
let
{
modelId
,
answer
}
=
req
.
body
as
{
model
Id
:
string
;
answer
:
string
;
let
{
dataId
,
text
}
=
req
.
body
as
{
data
Id
:
string
;
text
:
string
;
};
const
{
authorization
}
=
req
.
headers
;
...
...
@@ -15,7 +15,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
throw
new
Error
(
'无权操作'
);
}
if
(
!
model
Id
)
{
if
(
!
data
Id
)
{
throw
new
Error
(
'缺少参数'
);
}
...
...
@@ -26,11 +26,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
await
ModelData
.
updateOne
(
{
model
Id
,
_id
:
data
Id
,
userId
},
{
a
:
answer
text
}
);
...
...
src/pages/model/components/ModelDataCard.tsx
View file @
2b2c70e5
This diff is collapsed.
Click to expand it.
src/pages/model/detail.tsx
View file @
2b2c70e5
...
...
@@ -255,7 +255,7 @@ const ModelDetail = ({ modelId }: { modelId: string }) => {
<Training model={model} />
</Card>
)} */
}
<
Card
p=
{
4
}
height=
{
'
4
00px'
}
gridColumnStart=
{
1
}
gridColumnEnd=
{
3
}
>
<
Card
p=
{
4
}
height=
{
'
5
00px'
}
gridColumnStart=
{
1
}
gridColumnEnd=
{
3
}
>
{
model
.
_id
&&
<
ModelDataCard
model=
{
model
}
/>
}
</
Card
>
</
Grid
>
...
...
src/service/models/modelData.ts
View file @
2b2c70e5
...
...
@@ -13,22 +13,23 @@ const ModelDataSchema = new Schema({
ref
:
'user'
,
required
:
true
},
q
:
{
text
:
{
type
:
String
,
required
:
true
},
a
:
{
type
:
String
,
default
:
''
q
:
{
type
:
[
{
id
:
String
,
// 对应redis的key
text
:
String
}
],
default
:
[]
},
status
:
{
type
:
Number
,
enum
:
[
0
,
1
,
2
],
enum
:
[
0
,
1
],
// 1 训练ing
default
:
1
},
createTime
:
{
type
:
Date
,
default
:
()
=>
new
Date
()
}
});
...
...
src/service/mongo.ts
View file @
2b2c70e5
import
mongoose
from
'mongoose'
;
import
{
generateQA
}
from
'./events/generateQA'
;
import
{
generateAbstract
}
from
'./events/generateAbstract'
;
import
{
connectRedis
}
from
'./redis'
;
/**
* 连接 MongoDB 数据库
...
...
@@ -29,7 +28,6 @@ export async function connectToDatabase(): Promise<void> {
generateQA
();
generateAbstract
();
// connectRedis();
}
export
*
from
'./models/authCode'
;
...
...
src/service/redis.ts
View file @
2b2c70e5
import
{
createClient
,
SchemaFieldTypes
}
from
'redis'
;
import
{
ModelDataIndex
}
from
'@/constants/redis'
;
import
{
customAlphabet
}
from
'nanoid'
;
const
nanoid
=
customAlphabet
(
'abcdefghijklmnopqrstuvwxyz1234567890'
,
10
);
export
const
connectRedis
=
async
()
=>
{
// 断开了,重连
...
...
@@ -6,12 +9,12 @@ export const connectRedis = async () => {
await
global
.
redisClient
.
disconnect
();
}
else
if
(
global
.
redisClient
)
{
// 没断开,不再连接
return
;
return
global
.
redisClient
;
}
try
{
global
.
redisClient
=
createClient
({
url
:
'redis://:121914yu@120.76.193.200:8100'
url
:
'redis://
default
:121914yu@120.76.193.200:8100'
});
global
.
redisClient
.
on
(
'error'
,
(
err
)
=>
{
...
...
@@ -31,31 +34,35 @@ export const connectRedis = async () => {
await
global
.
redisClient
.
select
(
0
);
// 创建索引
await
global
.
redisClient
.
ft
.
create
(
'vec:question'
,
{
'$.vector'
:
SchemaFieldTypes
.
VECTOR
,
'$.modelId'
:
{
type
:
SchemaFieldTypes
.
TEXT
,
AS
:
'modelId'
try
{
await
global
.
redisClient
.
ft
.
create
(
ModelDataIndex
,
{
// '$.vector': SchemaFieldTypes.VECTOR,
'$.modelId'
:
{
type
:
SchemaFieldTypes
.
TEXT
,
AS
:
'modelId'
},
'$.userId'
:
{
type
:
SchemaFieldTypes
.
TEXT
,
AS
:
'userId'
},
'$.status'
:
{
type
:
SchemaFieldTypes
.
NUMERIC
,
AS
:
'status'
}
},
'$.userId'
:
{
type
:
SchemaFieldTypes
.
TEXT
,
AS
:
'userId'
},
'$.status'
:
{
type
:
SchemaFieldTypes
.
NUMERIC
,
AS
:
'status'
{
ON
:
'JSON'
,
PREFIX
:
'model:data'
}
},
{
ON
:
'JSON'
,
PREFIX
:
'fastgpt:modeldata'
}
);
);
}
catch
(
error
)
{
console
.
log
(
'创建索引失败'
,
error
);
}
// await global.redisClient.json.set('fastgpt:modeldata:
1
', '$', {
// vector: [],
// await global.redisClient.json.set('fastgpt:modeldata:
2
', '$', {
// vector: [
124, 214, 412, 4, 124, 1, 4, 1, 4, 3, 423
],
// modelId: 'daf',
// userId: 'adfd',
// q: 'fasf',
...
...
@@ -63,12 +70,17 @@ export const connectRedis = async () => {
// status: 0,
// createTime: new Date()
// });
// const value = await global.redisClient.
get('fastgpt:modeldata:1
');
// const value = await global.redisClient.
json.get('fastgpt:modeldata:2
');
// console.log(value);
return
global
.
redisClient
;
}
catch
(
error
)
{
console
.
log
(
error
,
'=='
);
global
.
redisClient
=
null
;
return
Promise
.
reject
(
'redis 连接失败'
);
}
};
export
const
getKey
=
(
prefix
=
''
)
=>
{
return
`
${
prefix
}
:
${
nanoid
()}
`
;
};
src/types/model.d.ts
View file @
2b2c70e5
...
...
@@ -8,3 +8,12 @@ export interface ModelUpdateParams {
service
:
ModelSchema
.
service
;
security
:
ModelSchema
.
security
;
}
export
interface
ModelDataItemType
{
id
:
string
;
status
:
0
|
1
;
// 1代表向量生成完毕
q
:
string
;
// 提问词
a
:
string
;
// 原文
modelId
:
string
;
userId
:
string
;
}
src/types/mongoSchema.d.ts
View file @
2b2c70e5
...
...
@@ -51,13 +51,17 @@ export interface ModelPopulate extends ModelSchema {
userId
:
UserModelSchema
;
}
export
type
ModelDataType
=
0
|
1
|
2
;
export
type
ModelDataType
=
0
|
1
;
export
interface
ModelDataSchema
{
_id
:
string
;
q
:
string
;
a
:
string
;
modelId
:
string
;
userId
:
string
;
text
:
string
;
q
:
{
id
:
string
;
text
:
string
;
}[];
status
:
ModelDataType
;
createTime
:
Date
;
}
export
interface
TrainingSchema
{
...
...
src/types/redis.d.ts
0 → 100644
View file @
2b2c70e5
export
interface
RedisModelDataItemType
{
id
:
string
;
value
:
{
vector
:
number
[];
q
:
string
;
// 提问词
a
:
string
;
// 原文
modelId
:
string
;
userId
:
string
;
};
}
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