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
7db87c2d
authored
Mar 10, 2023
by
Archer
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'dev1.1' into beian
parents
f023f631
453f3be8
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
69 additions
and
55 deletions
+69
-55
src/components/Markdown/index.module.scss
+1
-1
src/pages/api/chat/chatGpt.ts
+33
-38
src/pages/chat/index.tsx
+3
-3
src/service/mongo.ts
+22
-12
src/types/index.d.ts
+9
-0
tsconfig.json
+1
-1
No files found.
src/components/Markdown/index.module.scss
View file @
7db87c2d
...
@@ -8,7 +8,7 @@
...
@@ -8,7 +8,7 @@
animation
:
blink
0
.6s
infinite
;
animation
:
blink
0
.6s
infinite
;
}
}
.animation
{
.animation
{
:last-child::after
{
>
:last-child::after
{
display
:
inline-block
;
display
:
inline-block
;
content
:
''
;
content
:
''
;
width
:
4px
;
width
:
4px
;
...
...
src/pages/api/chat/chatGpt.ts
View file @
7db87c2d
...
@@ -69,56 +69,51 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -69,56 +69,51 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
stream
:
true
stream
:
true
},
},
{
{
timeout
:
20000
,
responseType
:
'stream'
,
responseType
:
'stream'
,
httpsAgent
:
openaiProxy
?.
httpsAgent
httpsAgent
:
openaiProxy
?.
httpsAgent
}
}
);
);
console
.
log
(
'response success'
);
let
AIResponse
=
''
;
let
AIResponse
=
''
;
// 解析数据
// 解析数据
const
decoder
=
new
TextDecoder
();
const
decoder
=
new
TextDecoder
();
new
ReadableStream
({
const
onParse
=
async
(
event
:
ParsedEvent
|
ReconnectInterval
)
=>
{
async
start
(
controller
)
{
if
(
event
.
type
===
'event'
)
{
// callback
const
data
=
event
.
data
;
async
function
onParse
(
event
:
ParsedEvent
|
ReconnectInterval
)
{
if
(
data
===
'[DONE]'
)
{
if
(
event
.
type
===
'event'
)
{
// 存入库
const
data
=
event
.
data
;
await
ChatWindow
.
findByIdAndUpdate
(
windowId
,
{
if
(
data
===
'[DONE]'
)
{
$push
:
{
controller
.
close
();
content
:
{
res
.
write
(
'event: done\ndata: \n\n'
);
obj
:
'AI'
,
res
.
end
();
value
:
AIResponse
// 存入库
}
await
ChatWindow
.
findByIdAndUpdate
(
windowId
,
{
},
$push
:
{
updateTime
:
Date
.
now
()
content
:
{
});
obj
:
'AI'
,
res
.
write
(
'event: done\ndata: \n\n'
);
value
:
AIResponse
res
.
end
();
}
return
;
},
updateTime
:
Date
.
now
()
});
return
;
}
try
{
const
json
=
JSON
.
parse
(
data
);
const
content
:
string
=
json
.
choices
[
0
].
delta
.
content
||
''
;
res
.
write
(
`event: responseData\ndata:
${
content
.
replace
(
/
\n
/g
,
'<br/>'
)}
\n\n`
);
AIResponse
+=
content
;
}
catch
(
e
)
{
// maybe parse error
controller
.
error
(
e
);
res
.
end
();
}
}
}
}
try
{
const
parser
=
createParser
(
onParse
);
const
json
=
JSON
.
parse
(
data
);
for
await
(
const
chunk
of
chatResponse
.
data
as
any
)
{
const
content
:
string
=
json
.
choices
[
0
].
delta
.
content
||
''
;
parser
.
feed
(
decoder
.
decode
(
chunk
));
// console.log('content:', content)
res
.
write
(
`event: responseData\ndata:
${
content
.
replace
(
/
\n
/g
,
'<br/>'
)}
\n\n`
);
AIResponse
+=
content
;
}
catch
(
e
)
{
res
.
end
();
}
}
}
}
});
};
const
parser
=
createParser
(
onParse
);
for
await
(
const
chunk
of
chatResponse
.
data
as
any
)
{
parser
.
feed
(
decoder
.
decode
(
chunk
));
}
}
catch
(
err
:
any
)
{
}
catch
(
err
:
any
)
{
let
errorText
=
err
;
let
errorText
=
err
;
if
(
err
.
code
===
'ECONNRESET'
)
{
if
(
err
.
code
===
'ECONNRESET'
)
{
...
...
src/pages/chat/index.tsx
View file @
7db87c2d
...
@@ -127,14 +127,14 @@ const Chat = () => {
...
@@ -127,14 +127,14 @@ const Chat = () => {
let
timer
=
setTimeout
(()
=>
{
let
timer
=
setTimeout
(()
=>
{
event
.
close
();
event
.
close
();
reject
(
'服务器超时'
);
reject
(
'服务器超时'
);
},
30000
0
);
},
30000
);
event
.
addEventListener
(
'responseData'
,
({
data
})
=>
{
event
.
addEventListener
(
'responseData'
,
({
data
})
=>
{
/* 重置定时器 */
/* 重置定时器 */
clearTimeout
(
timer
);
clearTimeout
(
timer
);
timer
=
setTimeout
(()
=>
{
timer
=
setTimeout
(()
=>
{
event
.
close
();
event
.
close
();
reject
(
'服务器超时'
);
reject
(
'服务器超时'
);
},
30000
0
);
},
30000
);
const
msg
=
data
.
replace
(
/<br
\/
>/g
,
'\n'
);
const
msg
=
data
.
replace
(
/<br
\/
>/g
,
'\n'
);
setChatList
((
state
)
=>
setChatList
((
state
)
=>
...
@@ -264,7 +264,7 @@ const Chat = () => {
...
@@ -264,7 +264,7 @@ const Chat = () => {
const
reEdit
=
useCallback
(
async
()
=>
{
const
reEdit
=
useCallback
(
async
()
=>
{
if
(
chatList
[
chatList
.
length
-
1
]?.
obj
!==
'Human'
)
return
;
if
(
chatList
[
chatList
.
length
-
1
]?.
obj
!==
'Human'
)
return
;
// 删除数据库最后一句
// 删除数据库最后一句
delLastMessage
(
windowId
);
await
delLastMessage
(
windowId
);
const
val
=
chatList
[
chatList
.
length
-
1
].
value
;
const
val
=
chatList
[
chatList
.
length
-
1
].
value
;
setInputVal
(
val
);
setInputVal
(
val
);
...
...
src/service/mongo.ts
View file @
7db87c2d
import
mongoose
from
'mongoose'
;
import
mongoose
from
'mongoose'
;
import
type
{
Mongoose
}
from
'mongoose'
;
let
cachedClient
:
Mongoose
;
/**
* 连接 MongoDB 数据库
export
async
function
connectToDatabase
()
{
*/
if
(
cachedClient
&&
cachedClient
.
connection
.
readyState
===
1
)
{
export
async
function
connectToDatabase
():
Promise
<
void
>
{
return
cachedClient
;
// @ts-ignore
if
(
global
.
mongodb
)
{
return
;
}
// @ts-ignore
global
.
mongodb
=
'connecting'
;
console
.
log
(
'connect mongo'
);
try
{
// @ts-ignore
global
.
mongodb
=
await
mongoose
.
connect
(
process
.
env
.
MONGODB_URI
as
string
,
{
dbName
:
'doc_gpt'
,
maxPoolSize
:
10
,
minPoolSize
:
1
});
}
catch
(
error
)
{
console
.
error
(
'mongo connect error'
);
// @ts-ignore
global
.
mongodb
=
null
;
}
}
cachedClient
=
await
mongoose
.
connect
(
process
.
env
.
MONGODB_URI
as
string
,
{
dbName
:
'doc_gpt'
});
return
cachedClient
;
}
}
export
*
from
'./models/authCode'
;
export
*
from
'./models/authCode'
;
...
...
src/types/index.d.ts
0 → 100644
View file @
7db87c2d
import
type
{
Mongoose
}
from
'mongoose'
;
declare
global
{
interface
Global
{
mongodb
:
Mongoose
;
}
}
export
type
a
=
string
;
tsconfig.json
View file @
7db87c2d
...
@@ -19,6 +19,6 @@
...
@@ -19,6 +19,6 @@
"@/*"
:
[
"./src/*"
]
"@/*"
:
[
"./src/*"
]
}
}
},
},
"include"
:
[
"next-env.d.ts"
,
"**/*.ts"
,
"**/*.tsx"
],
"include"
:
[
"next-env.d.ts"
,
"**/*.ts"
,
"**/*.tsx"
,
"**/*.d.ts"
],
"exclude"
:
[
"node_modules"
]
"exclude"
:
[
"node_modules"
]
}
}
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