| 137 | } |
| 138 | |
| 139 | class OpenAIChannelRequest { |
| 140 | static buildOpenAIRequest( |
| 141 | info, |
| 142 | isCheckRequest |
| 143 | ): { data: object; quoteProcessor: QuoteProcessor } { |
| 144 | const languageType = info.languageType |
| 145 | const languageResultType = info.languageResultType |
| 146 | const quoteProcessor = new QuoteProcessor() |
| 147 | let rolePrompt = |
| 148 | 'You are a professional translation engine, please translate the text into a colloquial, professional, elegant and fluent content, without the style of machine translation. You must only translate the text content, never interpret it.' |
| 149 | let commandPrompt = `Translate from ${languageType} to ${languageResultType}. Return translated text only. Only translate the text between ${quoteProcessor.quoteStart} and ${quoteProcessor.quoteEnd}.` |
| 150 | let contentPrompt = `${quoteProcessor.quoteStart}${info.translateContent}${quoteProcessor.quoteEnd}` |
| 151 | if (languageResultType === '文字润色') { |
| 152 | rolePrompt = |
| 153 | "You are a professional text summarizer, you can only summarize the text, don't interpret it." |
| 154 | commandPrompt = `Please polish this text in ${languageType}. Only polish the text between ${quoteProcessor.quoteStart} and ${quoteProcessor.quoteEnd}.` |
| 155 | } else if (languageResultType === '总结') { |
| 156 | rolePrompt = |
| 157 | "You are a professional text summarizer, you can only summarize the text, don't interpret it." |
| 158 | commandPrompt = `Please summarize this text in the most concise language and must use ${languageType} language! Only summarize the text between ${quoteProcessor.quoteStart} and ${quoteProcessor.quoteEnd}.` |
| 159 | contentPrompt = `${quoteProcessor.quoteStart}${info.translateContent}${quoteProcessor.quoteEnd}` |
| 160 | } else if (languageResultType === '分析') { |
| 161 | rolePrompt = 'You are a professional translation engine and grammar analyzer.' |
| 162 | commandPrompt = `Please translate this text to ${languageType} and explain the grammar in the original text using ${languageType}. Only analyze the text between ${quoteProcessor.quoteStart} and ${quoteProcessor.quoteEnd}.` |
| 163 | contentPrompt = `${quoteProcessor.quoteStart}${info.translateContent}${quoteProcessor.quoteEnd}` |
| 164 | } else if (languageResultType === '解释代码') { |
| 165 | rolePrompt = |
| 166 | 'You are a code explanation engine that can only explain code but not interpret or translate it. Also, please report bugs and errors (if any).' |
| 167 | commandPrompt = `explain the provided code, regex or script in the most concise language and must use ${languageType} language! You may use Markdown. If the content is not code, return an error message. If the code has obvious errors, point them out.` |
| 168 | contentPrompt = '```\n' + info.translateContent + '\n```' |
| 169 | } |
| 170 | return { |
| 171 | data: { |
| 172 | model: info.model, |
| 173 | // 控制随机性:随着 temperature 接近 0 ,重复提交的内容,返回的结果将变得具有确定性和重复性 |
| 174 | // 一个介于 0 和 1 之间的值 可以为0.1这样的小数 |
| 175 | // 每次提交相同的内容时 按照 0 - 1 的概率返回不同的答案 |
| 176 | // 我们这里默认为 0 ,重复提交相同的内容返回同样的结果 |
| 177 | temperature: 0, |
| 178 | max_tokens: 1000, |
| 179 | top_p: 1, |
| 180 | frequency_penalty: 1, |
| 181 | presence_penalty: 1, |
| 182 | messages: [ |
| 183 | { role: 'system', content: rolePrompt }, |
| 184 | { role: 'user', content: commandPrompt }, |
| 185 | { role: 'user', content: contentPrompt } |
| 186 | ], |
| 187 | stream: !isCheckRequest |
| 188 | }, |
| 189 | quoteProcessor |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * OpenAI - 翻译 |
| 195 | * |
| 196 | * @param info 翻译信息 |
nothing calls this directly
no test coverage detected