@acaw-ai/generative
Version:
AI generative client for Acaw AI
56 lines (49 loc) • 1.54 kB
JavaScript
import axios from 'axios';
import models from './models.js';
class AI {
constructor(baseURL = 'https://www.acaw.my.id/api/v1') {
this.baseURL = baseURL;
this.generative = {};
for (const alias in models) {
const actualModel = models[alias];
this.generative[alias] = async (message) => {
if (typeof message !== 'string') throw new Error('Message must be string');
try {
const res = await axios.post(`${this.baseURL}/${actualModel}`, { message }, {
headers: {
accept: 'application/json',
'Content-Type': 'application/json'
}
});
return res.data;
} catch (err) {
throw new Error(err.response?.status
? `API error status ${err.response.status}`
: err.message);
}
};
}
this.generative.showAllModels = () => {
return Object.entries(models).map(([alias, real]) => ({
alias,
model: real
}));
};
this.generative = new Proxy(this.generative, {
get: (target, prop) => {
if (prop in target) {
return target[prop];
}
return async () => {
return Promise.reject(
new Error(`Model alias "${prop.toString()}" tidak ditemukan. Gunakan ai.generative.showAllModels() untuk daftar alias dan model.`)
);
};
}
});
return this.generative;
}
}
const aiInstance = new AI();
export default { generative: aiInstance };
export { AI, models };