prompt.js 2.6 KB
Newer Older
1 2 3 4 5 6 7 8
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) {
9 10 11 12 13 14 15 16
            let tempContext = context[id].page_content
            if (tempContext) {
                const regex = /(<([^>]+)>)/ig;
                tempContext =  tempContext.replace(regex, ' ');

                if (tempContext.length + contextContent.length < max_token) {
                    contextContent  = contextContent + '\n' + tempContext
                }
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
            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) {
43
        
44 45 46 47 48 49 50
        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]
51 52 53 54 55 56 57
                if(!item?.ignore??false) {
                    message.push({
                        "role": item.user === 'AI' ? "system" : "user",
                        "content": item.message
                    })
                }
                
58 59 60 61 62
            }
        }
        if (config?.prompt_template) {
            const contextContent = getContextContent(context, config?.max_request_len??1024)
            message.unshift({
63
                "role": "system",
64 65 66
                "content": config.prompt_template.replace(/\{question\}/ig, '').replace(/\{context\}/ig, contextContent).replace(/\{user_call_name\}/ig, config.user_call_name)
            })
        }
67 68 69 70
        
        if (message.length > 0 && config.api_prompt_prefix) {
            message[message.length - 1].content = config.api_prompt_prefix + message[message.length - 1].content
        }
71 72 73
        return message
    }
}