now using the openai responses API. see #407

This commit is contained in:
Mic 2025-12-23 00:29:00 +01:00
parent 65ee959f0d
commit 713415c1d2
2 changed files with 41 additions and 17 deletions

View file

@ -27,13 +27,13 @@ export class OpenAI {
stream = false; stream = false;
store = false; store = false;
constructor({ constructor(
apiKey = '', apiKey = '',
model = '', model = '',
developer_messages = '', developer_messages = '',
stream = false, stream = false,
store = false store = false
} = {}) { ) {
this.apiKey = apiKey; this.apiKey = apiKey;
this.model = model; this.model = model;
this.developer_messages = developer_messages; this.developer_messages = developer_messages;
@ -78,14 +78,20 @@ export class OpenAI {
} }
} }
fetchResponse = async (messages, maxTokens = 0) => { fetchResponse = async (messages, maxTokens = 0, previous_response_id = null) => {
const input = messages.map(msg => ({
role: msg.role,
content: [{ type: "input_text", text: msg.content }]
}));
let request_body = { let request_body = {
model: this.model, model: this.model,
input: messages, input: input,
stream: this.stream, stream: this.stream,
store: this.store, store: this.store,
...(maxTokens > 0 ? { 'max_completion_tokens': parseInt(maxTokens) } : {}) ...(maxTokens > 0 ? { 'max_output_tokens': parseInt(maxTokens) } : {}),
...(previous_response_id && this.store ? { 'previous_response_id': previous_response_id } : {})
} }
if(this.developer_messages !== ''){ if(this.developer_messages !== ''){

View file

@ -29,11 +29,11 @@ let openai = null;
let stopStreaming = false; let stopStreaming = false;
let i18nStrings = null; let i18nStrings = null;
let do_debug = false; let do_debug = false;
let taLog = null let taLog = null;
let conversationHistory = []; let conversationHistory = [];
let assistantResponseAccumulator = ''; let assistantResponseAccumulator = '';
let previous_response_id = -1; let previous_response_id = null;
self.onmessage = async function(event) { self.onmessage = async function(event) {
if (event.data.type === 'init') { if (event.data.type === 'init') {
@ -43,10 +43,19 @@ self.onmessage = async function(event) {
do_debug = event.data.do_debug; do_debug = event.data.do_debug;
i18nStrings = event.data.i18nStrings; i18nStrings = event.data.i18nStrings;
taLog = new taLogger('model-worker-openai_responses', do_debug); taLog = new taLogger('model-worker-openai_responses', do_debug);
previous_response_id = null;
} else if (event.data.type === 'chatMessage') { } else if (event.data.type === 'chatMessage') {
conversationHistory.push({ role: 'user', content: event.data.message }); conversationHistory.push({ role: 'user', content: event.data.message });
const response = await openai.fetchResponse(conversationHistory); //4096); let messagesToSend = conversationHistory;
if (previous_response_id) {
messagesToSend = [conversationHistory[conversationHistory.length - 1]];
taLog.log("previous_response_id: " + previous_response_id);
} else {
taLog.log("no previous_response_id");
}
const response = await openai.fetchResponse(messagesToSend, 0, previous_response_id);
postMessage({ type: 'messageSent' }); postMessage({ type: 'messageSent' });
if (!response.ok) { if (!response.ok) {
@ -97,24 +106,33 @@ self.onmessage = async function(event) {
let parsedLines = []; let parsedLines = [];
try{ try{
parsedLines = lines parsedLines = lines
.map((line) => line.trim())
.filter((line) => line.startsWith("data:"))
.map((line) => line.replace(/^data: /, "").trim()) // Remove the "data: " prefix .map((line) => line.replace(/^data: /, "").trim()) // Remove the "data: " prefix
.filter((line) => line !== "" && line !== "[DONE]") // Remove empty lines and "[DONE]" .filter((line) => line !== "" && line !== "[DONE]") // Remove empty lines and "[DONE]"
// .map((line) => JSON.parse(line)); // Parse the JSON string // .map((line) => JSON.parse(line)); // Parse the JSON string
.map((line) => { .map((line) => {
taLog.log("line: " + JSON.stringify(line)); try {
return JSON.parse(line); taLog.log("line: " + JSON.stringify(line));
}); return JSON.parse(line);
} catch (e) {
taLog.warn("JSON parse warning, skipped line: " + line + " - " + e.message);
return null;
}
})
.filter((parsed) => parsed !== null);
}catch(e){ }catch(e){
taLog.error("Error parsing lines: " + e); taLog.error("Error parsing lines: " + e);
} }
for (const parsedLine of parsedLines) { for (const parsedLine of parsedLines) {
console.log(">>>>>>>>>> parsedLine: " + JSON.stringify(parsedLine)); if (parsedLine.type === 'response.created' && parsedLine.response && parsedLine.response.id){
const { content } = parsedLine; previous_response_id = parsedLine.response.id;
// Update the UI with the new content } else if (parsedLine.type === 'response.output_text.delta' && parsedLine.delta) {
if (content) { assistantResponseAccumulator += parsedLine.delta;
assistantResponseAccumulator += content; postMessage({ type: 'newToken', payload: { token: parsedLine.delta } });
postMessage({ type: 'newToken', payload: { token: content } }); // } else if (parsedLine.type === 'response.completed' && parsedLine.response && parsedLine.response.id) {
// previous_response_id = parsedLine.response.id;
} }
} }
} }