prompt.js 2.1 KB
Newer Older
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 55 56 57 58 59
const default_max_token = 1024

const getContextContent = (context, max_token=default_max_token) => {
    if (context && context.length > 0) {
        let id = 0
        let len = context.length
        let contextContent = ''
        while(contextContent.length < max_token && id < len) {
            if (context[id].page_content.length + contextContent.length < max_token) {
                contextContent  = contextContent + '\n' + context[id].page_content
            }
            id++
        }
        return contextContent
    } else {
        return ''
    }
}

export default {
    
    getPromptByTemplate: (config, context, prompt, history) => {

        if (config.api_prompt_prefix) {
            prompt = config?.api_prompt_prefix + ' ' + prompt
        }
        if (config?.prompt_template) {

            const contextContent = getContextContent(context, config?.max_request_len??1024)
            return config?.prompt_template.replace(/\{question\}/ig, prompt).replace(/\{context\}/ig, contextContent)
        } else {
            return prompt
        }
    },
    getPromptByChatMode (config, context, history) {

        const history_length = Math.min(Math.max(parseInt(config?.history_length??4), 4), 10)
        let message = []
        if (history && history.length >= 2) {
            const end = history.length - 2 // 结束位置
            const start = Math.max(history.length - 2 - history_length + 1, 0) // 开始位置
            for(let id = start; id <= end; id++) {
                const item = history[id]
                message.push({
                    "role": item.user === 'AI' ? "system" : "user",
                    "content": item.message
                })
            }
        }
        if (config?.prompt_template) {
            const contextContent = getContextContent(context, config?.max_request_len??1024)
            message.unshift({
                "role": "user",
                "content": config.prompt_template.replace(/\{question\}/ig, '').replace(/\{context\}/ig, contextContent).replace(/\{user_call_name\}/ig, config.user_call_name)
            })
        }
        return message
    }
}