ai-embedapi
Version:
A powerful JavaScript SDK to interact with multiple AI models (OpenAI, Anthropic, VertexAI, XAI) for text generation and other AI capabilities using the EmbedAPI service.
77 lines (67 loc) • 2.05 kB
JavaScript
// AI Embed API Package - JavaScript SDK
const fetch = require('node-fetch');
class AIEmbedAPI {
constructor(apiKey) {
if (!apiKey) {
throw new Error('API key is required to instantiate AIEmbedAPI.');
}
this.apiKey = apiKey;
this.baseUrl = 'https://embedapi.com/generate';
}
async generateText(options) {
const {
service,
model,
messages,
maxTokens = 1000,
temperature = 0.7,
topP,
frequencyPenalty,
presencePenalty,
stopSequences,
...otherParams
} = options;
// Validate required fields
if (!service || !model || !messages) {
throw new Error('Required parameters: service, model, and messages.');
}
// Setup request body
const body = {
service,
model,
messages,
maxTokens,
temperature,
topP,
frequencyPenalty,
presencePenalty,
stopSequences,
...otherParams,
};
try {
// Make request to OneAI API
const response = await fetch(this.baseUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': this.apiKey,
},
body: JSON.stringify(body),
});
if (!response.ok) {
const error = await response.json();
throw new Error(`API Error: ${error.error} (code: ${error.code})`);
}
const result = await response.json();
return {
data: result.data,
tokenUsage: result.tokenUsage,
cost: result.cost,
};
} catch (error) {
console.error('Error generating text:', error.message);
throw error;
}
}
}
module.exports = AIEmbedAPI;