contaigents
Version:
Modular AI Content Ecosystem with Audio Generation
145 lines (144 loc) • 4.88 kB
JavaScript
import { BaseLLM } from "./BaseLLM.js";
export class OpenAIProvider extends BaseLLM {
constructor() {
super(OpenAIProvider.providerConfig);
}
async executePrompt(prompt, options = {}) {
if (!(await this.isConfigured())) {
throw new Error("OpenAI provider is not configured");
}
const response = await fetch("https://api.openai.com/v1/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${this.config.apiKey}`,
},
body: JSON.stringify({
model: this.config.model,
messages: [{ role: "user", content: prompt }],
temperature: options.temperature ?? 0.7,
max_tokens: options.maxTokens,
}),
});
if (!response.ok) {
const error = await response.json();
throw new Error(`OpenAI API error: ${error.error?.message || "Unknown error"}`);
}
const data = await response.json();
return {
content: data.choices[0].message.content,
raw: data,
};
}
async executeConversation(messages, options = {}) {
if (!(await this.isConfigured())) {
throw new Error("OpenAI provider is not configured");
}
// Convert ConversationMessage to OpenAI format
const openAIMessages = messages.map(msg => {
// Handle text-only messages
if (!msg.images || msg.images.length === 0) {
return {
role: msg.role,
content: msg.content
};
}
// Handle messages with images (GPT-4V format)
const content = [
{
type: "text",
text: msg.content
}
];
// Add images
for (const image of msg.images) {
content.push({
type: "image_url",
image_url: {
url: `data:${image.mimeType};base64,${image.base64Data}`
}
});
}
return {
role: msg.role,
content
};
});
const response = await fetch("https://api.openai.com/v1/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${this.config.apiKey}`,
},
body: JSON.stringify({
model: this.config.model,
messages: openAIMessages,
temperature: options.temperature ?? 0.7,
max_tokens: options.maxTokens,
}),
});
if (!response.ok) {
const error = await response.json();
throw new Error(`OpenAI API error: ${error.error?.message || "Unknown error"}`);
}
const data = await response.json();
return {
content: data.choices[0].message.content,
raw: data,
};
}
async imagine(prompt, options = {}) {
if (!(await this.isConfigured())) {
throw new Error("OpenAI provider is not configured");
}
const modelName = options.model || "dall-e-3";
const size = options.aspect_ratio || "1024x1024";
const n = options.number_of_images || 1;
const response = await fetch("https://api.openai.com/v1/images/generations", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${this.config.apiKey}`,
},
body: JSON.stringify({
model: modelName,
prompt,
n,
size,
response_format: "url",
}),
});
if (!response.ok) {
const error = await response.json();
throw new Error(`OpenAI DALL-E API error: ${error.error?.message || "Unknown error"}`);
}
const data = await response.json();
return {
urls: data.data.map((image) => image.url),
raw: data,
};
}
}
OpenAIProvider.providerConfig = {
name: "OpenAI",
configFields: [
{
name: "apiKey",
required: true,
},
{
name: "model",
required: true,
options: [
"gpt-4o",
"gpt-4",
"gpt-3.5-turbo",
"gpt-4-turbo-preview",
"gpt-4-0125-preview",
"gpt-4-1106-preview",
"gpt-3.5-turbo-0125",
],
default: "gpt-4",
},
],
};