Skip to content

Commit abccc6c

Browse files
committed
refactor: getConversationPairs and promptBase
1 parent ab95a3e commit abccc6c

File tree

4 files changed

+39
-34
lines changed

4 files changed

+39
-34
lines changed

src/background/apis/custom-api.mjs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ import { getUserConfig, maxResponseTokenLength } from '../../config/index.mjs'
99
import { fetchSSE } from '../../utils/fetch-sse'
1010
import { getConversationPairs } from '../../utils/get-conversation-pairs'
1111
import { isEmpty } from 'lodash-es'
12-
import { pushRecord, setAbortController } from './shared.mjs'
13-
14-
const getCustomApiPromptBase = async () => {
15-
return `I am a helpful, creative, clever, and very friendly assistant. I am familiar with various languages in the world.`
16-
}
12+
import { getCustomApiPromptBase, pushRecord, setAbortController } from './shared.mjs'
1713

1814
/**
1915
* @param {Browser.Runtime.Port} port
@@ -25,7 +21,7 @@ const getCustomApiPromptBase = async () => {
2521
export async function generateAnswersWithCustomApi(port, question, session, apiKey, modelName) {
2622
const { controller, messageListener } = setAbortController(port)
2723

28-
const prompt = getConversationPairs(session.conversationRecords, true)
24+
const prompt = getConversationPairs(session.conversationRecords, false)
2925
prompt.unshift({ role: 'system', content: await getCustomApiPromptBase() })
3026
prompt.push({ role: 'user', content: question })
3127
const apiUrl = (await getUserConfig()).customModelApiUrl

src/background/apis/openai-api.mjs

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,12 @@ import { maxResponseTokenLength, Models, getUserConfig } from '../../config/inde
44
import { fetchSSE } from '../../utils/fetch-sse'
55
import { getConversationPairs } from '../../utils/get-conversation-pairs'
66
import { isEmpty } from 'lodash-es'
7-
import { pushRecord, setAbortController } from './shared.mjs'
8-
9-
const getChatgptPromptBase = async () => {
10-
return `You are a helpful, creative, clever, and very friendly assistant. You are familiar with various languages in the world.`
11-
}
12-
13-
const getGptPromptBase = async () => {
14-
return (
15-
`The following is a conversation with an AI assistant.` +
16-
`The assistant is helpful, creative, clever, and very friendly. The assistant is familiar with various languages in the world.\n\n` +
17-
`Human: Hello, who are you?\n` +
18-
`AI: I am an AI created by OpenAI. How can I help you today?\n` +
19-
`Human: 谢谢\n` +
20-
`AI: 不客气\n`
21-
)
22-
}
7+
import {
8+
getChatSystemPromptBase,
9+
getCompletionPromptBase,
10+
pushRecord,
11+
setAbortController,
12+
} from './shared.mjs'
2313

2414
/**
2515
* @param {Browser.Runtime.Port} port
@@ -38,9 +28,9 @@ export async function generateAnswersWithGptCompletionApi(
3828
const { controller, messageListener } = setAbortController(port)
3929

4030
const prompt =
41-
(await getGptPromptBase()) +
42-
getConversationPairs(session.conversationRecords, false) +
43-
`Human:${question}\nAI:`
31+
(await getCompletionPromptBase()) +
32+
getConversationPairs(session.conversationRecords, true) +
33+
`Human: ${question}\nAI: `
4434
const apiUrl = (await getUserConfig()).customOpenAiApiUrl
4535

4636
let answer = ''
@@ -101,8 +91,8 @@ export async function generateAnswersWithGptCompletionApi(
10191
export async function generateAnswersWithChatgptApi(port, question, session, apiKey, modelName) {
10292
const { controller, messageListener } = setAbortController(port)
10393

104-
const prompt = getConversationPairs(session.conversationRecords, true)
105-
prompt.unshift({ role: 'system', content: await getChatgptPromptBase() })
94+
const prompt = getConversationPairs(session.conversationRecords, false)
95+
prompt.unshift({ role: 'system', content: await getChatSystemPromptBase() })
10696
prompt.push({ role: 'user', content: question })
10797
const apiUrl = (await getUserConfig()).customOpenAiApiUrl
10898

src/background/apis/shared.mjs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
export const getChatSystemPromptBase = async () => {
2+
return `You are a helpful, creative, clever, and very friendly assistant. You are familiar with various languages in the world.`
3+
}
4+
5+
export const getCompletionPromptBase = async () => {
6+
return (
7+
`The following is a conversation with an AI assistant.` +
8+
`The assistant is helpful, creative, clever, and very friendly. The assistant is familiar with various languages in the world.\n\n` +
9+
`Human: Hello, who are you?\n` +
10+
`AI: I am an AI assistant. How can I help you today?\n` +
11+
`Human: 没什么\n` +
12+
`AI: 好的, 如果有什么需要, 随时告诉我\n`
13+
)
14+
}
15+
16+
export const getCustomApiPromptBase = async () => {
17+
return `I am a helpful, creative, clever, and very friendly assistant. I am familiar with various languages in the world.`
18+
}
19+
120
export function setAbortController(port, onStop, onDisconnect) {
221
const controller = new AbortController()
322
const messageListener = (msg) => {

src/utils/get-conversation-pairs.mjs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
export function getConversationPairs(records, isChatgpt) {
1+
export function getConversationPairs(records, isCompletion) {
22
let pairs
3-
if (isChatgpt) {
4-
pairs = []
3+
if (isCompletion) {
4+
pairs = ''
55
for (const record of records) {
6-
pairs.push({ role: 'user', content: record['question'] })
7-
pairs.push({ role: 'assistant', content: record['answer'] })
6+
pairs += 'Human: ' + record.question + '\nAI: ' + record.answer + '\n'
87
}
98
} else {
10-
pairs = ''
9+
pairs = []
1110
for (const record of records) {
12-
pairs += 'Human:' + record.question + '\nAI:' + record.answer + '\n'
11+
pairs.push({ role: 'user', content: record['question'] })
12+
pairs.push({ role: 'assistant', content: record['answer'] })
1313
}
1414
}
1515

0 commit comments

Comments
 (0)