-
Notifications
You must be signed in to change notification settings - Fork 716
/
shared.mjs
54 lines (46 loc) · 1.97 KB
/
shared.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
export const getChatSystemPromptBase = async () => {
return `You are a helpful, creative, clever, and very friendly assistant. You are familiar with various languages in the world.`
}
export const getCompletionPromptBase = async () => {
return (
`The following is a conversation with an AI assistant.` +
`The assistant is helpful, creative, clever, and very friendly. The assistant is familiar with various languages in the world.\n\n` +
`Human: Hello, who are you?\n` +
`AI: I am an AI assistant. How can I help you today?\n`
)
}
export const getCustomApiPromptBase = async () => {
return `I am a helpful, creative, clever, and very friendly assistant. I am familiar with various languages in the world.`
}
export function setAbortController(port, onStop, onDisconnect) {
const controller = new AbortController()
const messageListener = (msg) => {
if (msg.stop) {
port.onMessage.removeListener(messageListener)
console.debug('stop generating')
port.postMessage({ done: true })
controller.abort()
if (onStop) onStop()
}
}
port.onMessage.addListener(messageListener)
const disconnectListener = () => {
port.onDisconnect.removeListener(disconnectListener)
console.debug('port disconnected')
controller.abort()
if (onDisconnect) onDisconnect()
}
port.onDisconnect.addListener(disconnectListener)
const cleanController = () => {
port.onMessage.removeListener(messageListener)
port.onDisconnect.removeListener(disconnectListener)
}
return { controller, cleanController, messageListener, disconnectListener }
}
export function pushRecord(session, question, answer) {
const recordLength = session.conversationRecords.length
let lastRecord
if (recordLength > 0) lastRecord = session.conversationRecords[recordLength - 1]
if (session.isRetry && lastRecord && lastRecord.question === question) lastRecord.answer = answer
else session.conversationRecords.push({ question: question, answer: answer })
}