@prexo/ai-chat-sdk
Version:
AI Chat Component with Persistent History
71 lines (70 loc) • 2.01 kB
JavaScript
import { BASE_API_ENDPOINT } from "../../lib/utils.js";
class IntVector {
namespace;
apiKey;
BASE_API = `${BASE_API_ENDPOINT}/context`;
constructor(namespace, apiKey) {
this.namespace = namespace;
this.apiKey = apiKey;
}
async addContext(input) {
if (!input.options) input.options = {};
input.options.namespace = this.namespace;
const response = await fetch(`${this.BASE_API}/add`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${this.apiKey}`
},
body: JSON.stringify(input)
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
}
async removeContext(ids) {
const response = await fetch(`${this.BASE_API}/delete`, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${this.apiKey}`
},
body: JSON.stringify({ ids, namespace: this.namespace })
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
}
async getContext(payload) {
const response = await fetch(`${this.BASE_API}/get`, {
method: "GET",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${this.apiKey}`
},
body: JSON.stringify({ ...payload, namespace: this.namespace })
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
}
async resetContext() {
const response = await fetch(`${this.BASE_API}/reset`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${this.apiKey}`
},
body: JSON.stringify({ namespace: this.namespace })
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
}
}
export {
IntVector
};