@sconedev/ai_toolkit
Version:
Simplify AI integration in web apps with local and offline model support
27 lines (26 loc) • 831 B
JavaScript
import { OpenAI } from 'openai';
export class OpenAIProvider {
constructor(apiKey, defaultModel) {
this.client = new OpenAI({ apiKey });
this.defaultModel = defaultModel;
}
async chat(messages, options) {
const completion = await this.client.chat.completions.create({
model: this.defaultModel,
messages,
...options,
});
const content = completion.choices[0].message.content;
if (content === null) {
throw new Error('No content in response from OpenAI');
}
return content;
}
async generateImage(description, options) {
const response = await this.client.images.generate({
prompt: description,
...options,
});
return response.data[0].url || '';
}
}