@yuxilabs/gptp-core
Version:
Core validation, formatting and execution logic for the GPTP file format.
45 lines (44 loc) • 1.46 kB
JavaScript
// src/engine/execute/runners/openai.ts
export async function callOpenAI(opts) {
if (process.env.MOCK === 'true') {
return {
choices: [
{
message: {
role: 'assistant',
content: `[Mocked response] Received ${opts.messages.length} messages. Model: ${opts.model}`,
},
},
],
};
}
// Placeholder for real API call
try {
const response = await fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.OPENAI_API_KEY}`,
},
body: JSON.stringify({
model: opts.model,
messages: opts.messages,
temperature: opts.temperature,
top_p: opts.top_p,
max_tokens: opts.max_tokens,
}),
});
if (!response.ok) {
throw new Error(`OpenAI API error: ${response.status} ${response.statusText}`);
}
const data = await response.json();
return data;
}
catch (error) {
console.error('Error calling OpenAI API:', error);
throw new Error('Failed to fetch response from OpenAI API.');
}
}
export function openaiRunner() {
// Add implementation here
}