mirror of
https://github.com/ikechan8370/chatgpt-plugin.git
synced 2025-12-17 13:57:10 +00:00
fix: post processors
This commit is contained in:
parent
20195ecfdf
commit
5add41c982
4 changed files with 138 additions and 42 deletions
55
utils/postprocessors/ReasonerProcessor.js
Normal file
55
utils/postprocessors/ReasonerProcessor.js
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
import { AbstractPostProcessor } from './BasicProcessor.js'
|
||||
|
||||
export class ReasonerProcessor extends AbstractPostProcessor {
|
||||
constructor () {
|
||||
super()
|
||||
this.name = 'ReasonerPostProcessor'
|
||||
this.type = 'post'
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {{
|
||||
* text: string,
|
||||
* thinking_text?: string
|
||||
* }} input
|
||||
* @returns {Promise<{
|
||||
* text: string,
|
||||
* thinking_text?: string
|
||||
* }>}
|
||||
*/
|
||||
async processInner (input) {
|
||||
// eslint-disable-next-line camelcase
|
||||
const { text, thinking_text } = extractThinkingTextAndText(input.text)
|
||||
return {
|
||||
text,
|
||||
// eslint-disable-next-line camelcase
|
||||
thinking_text: input.thinking_text + thinking_text
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* written by gpt-4o
|
||||
* @param str
|
||||
* @returns {{thinkingText: string, text: *}|{thinkingText: *, text: *}}
|
||||
*/
|
||||
const extractThinkingTextAndText = (str) => {
|
||||
// 使用正则表达式提取think标签内容
|
||||
const thinkRegex = /<think>(.*?)<\/think>/s
|
||||
const match = str.match(thinkRegex)
|
||||
|
||||
// 如果找到了<think>标签内容
|
||||
if (match) {
|
||||
// thinking_text就是<think>标签内的内容
|
||||
const thinkingText = match[1].trim()
|
||||
|
||||
// text就是</think>标签后的部分
|
||||
const text = str.slice(match.index + match[0].length).trim()
|
||||
|
||||
return { thinkingText, text }
|
||||
}
|
||||
|
||||
// 如果没有<think>标签内容,返回空或原始内容
|
||||
return { thinkingText: '', text: str.trim() }
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue