Files
MoodleGPT/src/background/core/get-response.ts
T
yoannchb-pro 3591aceba1 v1.1.5
2025-10-01 09:28:48 +02:00

57 lines
1.7 KiB
TypeScript

import type Config from '../types/config';
import type GPTAnswer from '../types/gpt-answer';
import normalizeText from 'background/utils/normalize-text';
import getContentWithHistory from './get-content-with-history';
import OpenAI from 'openai';
import { fixeO } from '../utils/fixe-o';
/**
* Get the response from chatGPT api
* @param config
* @param question
* @returns
*/
async function getChatGPTResponse(
config: Config,
questionElement: HTMLElement,
question: string
): Promise<GPTAnswer> {
const controller = new AbortController();
const timeoutControler = setTimeout(() => controller.abort(), 20 * 1000);
// Get the content to send to chatgpt
// Including the instructions to the AI, the images as base64 if needed, the question and the past conversation if history is set to true
const contentHandler = await getContentWithHistory(config, questionElement, question);
const client = new OpenAI({
apiKey: config.apiKey,
baseURL: config.baseURL,
dangerouslyAllowBrowser: true
});
const req = await client.chat.completions.create(
fixeO(config.model, {
model: config.model,
messages: contentHandler.messages,
max_completion_tokens: config.maxTokens || 2000 // Maximum length of the response,
}),
{ signal: config.timeout ? controller.signal : null }
);
clearTimeout(timeoutControler);
const response = req.choices[0].message.content ?? '';
// Save the response into the history
if (typeof contentHandler.saveResponse === 'function') contentHandler.saveResponse(response);
return {
question,
response,
normalizedResponse: normalizeText(response)
};
}
export default getChatGPTResponse;