UNPKG

p4u-client-ai

Version:

Empower Your Apps with AI: Access Multiple Models, Automate Tasks, Create AI Agents Easily

126 lines 4.66 kB
import axios from 'axios'; export { GPTModelsEnum } from './enums/gptModels.enum.js'; export { AudioModelsEnum } from './enums/audioModels.enum.js'; export { ImageModelsEnum } from './enums/imageModels.enum.js'; export { VideoModelsEnum } from './enums/videoModels.enum.js'; export default class AIClient { constructor() { this.headers = { 'content-type': 'application/json', Authorization: '', }; this.apiUrl = 'https://api.programmers4u.com'; } async login(userName, password) { const data = { username: userName, password: password }; const res = await this.makeRequest('POST', '/auth/login', data); if (res.status >= 300) throw new Error(res.statusText); this.headers.Authorization = `Bearer ${res.data.access_token}` || ''; } async makeRequest(method, endpoint, data) { try { const isFormData = data && typeof data.append === 'function'; const headers = { ...this.headers }; if (isFormData) { delete headers['content-type']; } return await axios({ method, url: `${this.debugUrl ? this.debugUrl : this.apiUrl}${endpoint}`, headers, data: isFormData ? data : data ? JSON.stringify(data) : undefined, }); } catch (err) { throw err; } } async pingPong() { return this.makeRequest('GET', '/ping'); } async listTasks() { return this.makeRequest('GET', '/products/tasks'); } async runTask(request) { return this.makeRequest('POST', '/products/tasks/query', request); } async deleteTask(request) { const { idTask } = request; return this.makeRequest('DELETE', `/products/tasks/${idTask}`); } async createTask(request) { return this.makeRequest('PUT', '/products/tasks/', request); } setLocalUrl(url) { this.debugUrl = url; } async listAgents() { return this.makeRequest('GET', '/products/agents'); } async getAgent(id) { return this.makeRequest('GET', `/products/agents/one/${id}`); } async createAgent(request) { return this.makeRequest('PUT', '/products/agents', request); } async updateAgent(request) { return this.makeRequest('PATCH', '/products/agents', request); } async deleteAgent(id) { return this.makeRequest('DELETE', `/products/agents/${id}`); } async queryAgent(body) { return this.makeRequest('POST', '/products/agents/query', body); } async getAllAgentRag(query) { const qs = query ? `?${new URLSearchParams(query).toString()}` : ''; return this.makeRequest('GET', `/agent-rag${qs}`); } async getAgentRag(id) { return this.makeRequest('GET', `/agent-rag/${id}`); } async createAgentRag(data) { const form = new FormData(); Object.entries(data).forEach(([k, v]) => { if (v !== undefined && v !== null) form.append(k, String(v)); }); return this.makeRequest('POST', '/agent-rag', form); } async deleteAgentRag(id) { return this.makeRequest('DELETE', `/agent-rag/${id}`); } async addKnowledge(data) { return this.makeRequest('POST', '/agent-rag/knowledge', data); } async addKnowledgeBatch(data) { return this.makeRequest('POST', '/agent-rag/knowledge/batch', data); } async addKnowledgeFromFile(agentRagId, file, metadata) { const form = new FormData(); form.append('file', file); if (metadata) form.append('metadata', JSON.stringify(metadata)); return this.makeRequest('POST', `/agent-rag/knowledge/file/${agentRagId}`, form); } async searchKnowledge(data) { return this.makeRequest('POST', '/agent-rag/knowledge/search', data); } async getKnowledgeById(id) { return this.makeRequest('GET', `/agent-rag/knowledge/item/${id}`); } async getKnowledgeStatistics(agentRagId) { return this.makeRequest('GET', `/agent-rag/knowledge/statistics/${agentRagId}`); } async getKnowledgeByAgentRagId(agentRagId) { return this.makeRequest('GET', `/agent-rag/knowledge/${agentRagId}`); } async excludeKnowledge(data) { return this.makeRequest('DELETE', '/agent-rag/knowledge', data); } async searchKnowledgeByMetadata(data) { return this.makeRequest('POST', '/agent-rag/knowledge/search-by-metadata', data); } } //# sourceMappingURL=index.js.map