mirror of
https://github.com/Jerryplusy/crystelf-plugin.git
synced 2026-01-29 01:07:27 +00:00
🛠️ fix(openaiChat): improve AI response parsing and error handling for better logging and response extraction (#13)
This commit is contained in:
parent
943a51be65
commit
e0b0b5bacf
@ -62,9 +62,32 @@ class OpenaiChat {
|
||||
presence_penalty: 0.2,
|
||||
stream:false
|
||||
});
|
||||
let parsedCompletion = completion;
|
||||
if (typeof completion === 'string') {
|
||||
try {
|
||||
parsedCompletion = JSON.parse(completion);
|
||||
} catch (parseError) {
|
||||
logger.error('[crystelf-ai] 响应JSON解析失败:', parseError);
|
||||
return { success: false };
|
||||
}
|
||||
}
|
||||
|
||||
const aiResponse = completion.choices[0].message.content;
|
||||
//logger.info(aiResponse);
|
||||
//logger.info("[DEBUG] 解析后的响应:", JSON.stringify(parsedCompletion));
|
||||
let aiResponse = null;
|
||||
|
||||
if (parsedCompletion && parsedCompletion.choices && Array.isArray(parsedCompletion.choices) && parsedCompletion.choices.length > 0) {
|
||||
const choice = parsedCompletion.choices[0];
|
||||
if (choice && choice.message && choice.message.content) {
|
||||
aiResponse = choice.message.content;
|
||||
}
|
||||
}
|
||||
|
||||
if (!aiResponse) {
|
||||
logger.error('[crystelf-ai] 无法从响应中提取AI回复内容:', parsedCompletion);
|
||||
return { success: false };
|
||||
}
|
||||
|
||||
logger.info("[DEBUG] AI响应内容:", aiResponse);
|
||||
return {
|
||||
success: true,
|
||||
aiResponse: aiResponse,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user