UNPKG

fi_sdk_js

Version:

Feedback Intelligence SKD

202 lines (178 loc) 5.74 kB
interface MessageDict { role: string; text: string; prompt?: string | null; date?: string | null; context?: ContextDict | null; feedback?: FeedbackDict | null; } interface ContextDict { context_id?: number; text?: string; } interface FeedbackDict { thumbs_up?: number; rating?: number; message?: string; } export class FeedbackIntelligenceSDK { private baseUrl: string; private headers: { [key: string]: string }; constructor(apiKey?: string, baseUrl?: string) { this.baseUrl = baseUrl || 'https://api.feedbackintelligence.ai'; this.headers = { 'Content-Type': 'application/json', }; if (apiKey) { this.headers['Authorization'] = `Bearer ${apiKey}`; } } async addContext(projectId: number, context: string, contextId: number | null = null): Promise<any> { const url = `${this.baseUrl}/data/${projectId}/context/add`; const payload: { context: string; id?: number } = {context}; if (contextId !== null) { payload['id'] = contextId; } return this._post(url, payload); } async addChat( projectId: number, chatId: string, messages: Message[], userId: string | null = null, version: string | null = null ): Promise<any> { const url = `${this.baseUrl}/data/${projectId}/chat/add`; const payload: { chat_id: string; messages: MessageDict[]; user_id?: string; version?: string } = { chat_id: chatId, messages: messages.map((msg) => msg.toDict()), }; if (userId !== null) payload['user_id'] = userId; if (version !== null) payload['version'] = version; console.log(payload); return this._post(url, payload); } async addFeedback( projectId: number, message: string, source: string, userId: string | null = null, chatId: string | null = null, date: string | null = null ): Promise<any> { const url = `${this.baseUrl}/data/${projectId}/feedback/add`; const payload: { message: string; source: string; user_id?: string; chat_id?: string; date?: string } = { message, source, }; if (userId !== null) payload['user_id'] = userId; if (chatId !== null) payload['chat_id'] = chatId; if (date !== null) payload['date'] = date; return this._post(url, payload); } // Private method to handle POST requests private async _post(url: string, payload: object): Promise<any> { const response = await fetch(url, { method: 'POST', headers: this.headers, body: JSON.stringify(payload), }); if (!response.ok) { console.log(response) throw new Error(`HTTP error! status: ${response.status}`); } return response.json(); } } // Message Class export class Message { role: string; text: string; prompt: string | null; date: string | null; context: Context | null; feedback: Feedback | null; constructor({ role, text, prompt = null, date = null, context = null, feedback = null }: { role: string; text: string; prompt?: string | null; date?: string | null; context?: Context | null; feedback?: Feedback | null; }) { this.role = role; this.text = text; this.prompt = prompt; this.date = date; this.context = context; this.feedback = feedback; } toDict(): MessageDict { const payload: any = { role: this.role, text: this.text, }; if (this.prompt !== null) payload["prompt"] = this.prompt; if (this.date !== null) payload["date"] = this.date; if (this.context !== null) payload["context"] = this.context.toDict(); if (this.feedback !== null) payload["feedback"] = this.feedback.toDict(); return payload; } } // Context Class export class Context { contextId: number | null; text: string | null; constructor({ contextId = null, text = null }: { contextId?: number | null; text?: string | null; } = {}) { if (contextId === null && text === null) { throw new Error('Either contextId or text must be provided'); } this.contextId = contextId; this.text = text; } toDict(): ContextDict { const data: ContextDict = {}; if (this.contextId !== null) data['context_id'] = this.contextId; if (this.text !== null) data['text'] = this.text; return data; } } // Feedback Class export class Feedback { thumbsUp: number | null = null; rating: number | null = null; message: string | null = null; constructor({ thumbsUp = null, rating = null, message = null }: { thumbsUp?: number | null; rating?: number | null; message?: string | null; } = {}) { this.thumbsUp = thumbsUp; this.rating = rating; this.message = message; } toDict(): FeedbackDict { const data: FeedbackDict = {}; if (this.thumbsUp !== null) data['thumbs_up'] = this.thumbsUp; if (this.rating !== null) data['rating'] = this.rating; if (this.message !== null) data['message'] = this.message; return data; } }