2024-02-27 21:52:57 +01:00
const { EventLogs } = require ( "../../../models/eventLogs" ) ;
2023-08-24 04:15:07 +02:00
const { SystemSettings } = require ( "../../../models/systemSettings" ) ;
2024-06-11 17:14:13 +02:00
const { purgeDocument } = require ( "../../../utils/files/purgeDocument" ) ;
2023-08-24 04:15:07 +02:00
const { getVectorDbClass } = require ( "../../../utils/helpers" ) ;
2024-02-27 21:52:57 +01:00
const {
prepareWorkspaceChatsForExport ,
exportChatsAsType ,
} = require ( "../../../utils/helpers/chat/convertTo" ) ;
2023-08-24 04:15:07 +02:00
const { dumpENV , updateENV } = require ( "../../../utils/helpers/updateENV" ) ;
const { reqBody } = require ( "../../../utils/http" ) ;
const { validApiKey } = require ( "../../../utils/middleware/validApiKey" ) ;
function apiSystemEndpoints ( app ) {
if ( ! app ) return ;
app . get ( "/v1/system/env-dump" , async ( _ , response ) => {
2023-09-21 21:04:17 +02:00
/ *
2023-08-24 04:15:07 +02:00
# swagger . tags = [ 'System Settings' ]
# swagger . description = 'Dump all settings to file storage'
# swagger . responses [ 403 ] = {
schema : {
"$ref" : "#/definitions/InvalidAPIKey"
}
}
* /
try {
if ( process . env . NODE _ENV !== "production" )
return response . sendStatus ( 200 ) . end ( ) ;
2024-06-21 05:50:14 +02:00
dumpENV ( ) ;
2023-08-24 04:15:07 +02:00
response . sendStatus ( 200 ) . end ( ) ;
} catch ( e ) {
2024-07-04 01:39:33 +02:00
console . error ( e . message , e ) ;
2023-08-24 04:15:07 +02:00
response . sendStatus ( 500 ) . end ( ) ;
}
} ) ;
app . get ( "/v1/system" , [ validApiKey ] , async ( _ , response ) => {
2023-09-21 21:04:17 +02:00
/ *
2023-08-24 04:15:07 +02:00
# swagger . tags = [ 'System Settings' ]
# swagger . description = 'Get all current system settings that are defined.'
# swagger . responses [ 200 ] = {
content : {
"application/json" : {
schema : {
type : 'object' ,
example : {
"settings" : {
"VectorDB" : "pinecone" ,
"PineConeKey" : true ,
"PineConeIndex" : "my-pinecone-index" ,
"LLMProvider" : "azure" ,
"[KEY_NAME]" : "KEY_VALUE" ,
}
}
}
2023-09-21 21:04:17 +02:00
}
2023-08-24 04:15:07 +02:00
}
2023-09-21 21:04:17 +02:00
}
2023-08-24 04:15:07 +02:00
# swagger . responses [ 403 ] = {
schema : {
"$ref" : "#/definitions/InvalidAPIKey"
}
}
* /
try {
const settings = await SystemSettings . currentSettings ( ) ;
response . status ( 200 ) . json ( { settings } ) ;
} catch ( e ) {
2024-07-04 01:39:33 +02:00
console . error ( e . message , e ) ;
2023-08-24 04:15:07 +02:00
response . sendStatus ( 500 ) . end ( ) ;
}
} ) ;
app . get ( "/v1/system/vector-count" , [ validApiKey ] , async ( _ , response ) => {
2023-09-21 21:04:17 +02:00
/ *
2023-08-24 04:15:07 +02:00
# swagger . tags = [ 'System Settings' ]
# swagger . description = 'Number of all vectors in connected vector database'
# swagger . responses [ 200 ] = {
content : {
"application/json" : {
schema : {
type : 'object' ,
example : {
"vectorCount" : 5450
}
}
2023-09-21 21:04:17 +02:00
}
2023-08-24 04:15:07 +02:00
}
2023-09-21 21:04:17 +02:00
}
2023-08-24 04:15:07 +02:00
# swagger . responses [ 403 ] = {
schema : {
"$ref" : "#/definitions/InvalidAPIKey"
}
}
* /
try {
const VectorDb = getVectorDbClass ( ) ;
2023-09-21 21:04:17 +02:00
const vectorCount = await VectorDb . totalVectors ( ) ;
2023-08-24 04:15:07 +02:00
response . status ( 200 ) . json ( { vectorCount } ) ;
} catch ( e ) {
2024-07-04 01:39:33 +02:00
console . error ( e . message , e ) ;
2023-08-24 04:15:07 +02:00
response . sendStatus ( 500 ) . end ( ) ;
}
} ) ;
app . post (
"/v1/system/update-env" ,
[ validApiKey ] ,
async ( request , response ) => {
2023-09-21 21:04:17 +02:00
/ *
2023-08-24 04:15:07 +02:00
# swagger . tags = [ 'System Settings' ]
# swagger . description = 'Update a system setting or preference.'
# swagger . requestBody = {
description : 'Key pair object that matches a valid setting and value. Get keys from GET /v1/system or refer to codebase.' ,
required : true ,
content : {
"application/json" : {
example : {
VectorDB : "lancedb" ,
AnotherKey : "updatedValue"
}
}
}
}
# swagger . responses [ 200 ] = {
content : {
"application/json" : {
schema : {
type : 'object' ,
example : {
newValues : { "[ENV_KEY]" : 'Value' } ,
error : 'error goes here, otherwise null'
}
}
2023-09-21 21:04:17 +02:00
}
2023-08-24 04:15:07 +02:00
}
2023-09-21 21:04:17 +02:00
}
2023-08-24 04:15:07 +02:00
# swagger . responses [ 403 ] = {
schema : {
"$ref" : "#/definitions/InvalidAPIKey"
}
}
* /
try {
const body = reqBody ( request ) ;
2024-01-17 21:59:25 +01:00
const { newValues , error } = await updateENV ( body ) ;
2023-08-24 04:15:07 +02:00
response . status ( 200 ) . json ( { newValues , error } ) ;
} catch ( e ) {
2024-07-04 01:39:33 +02:00
console . error ( e . message , e ) ;
2023-08-24 04:15:07 +02:00
response . sendStatus ( 500 ) . end ( ) ;
}
}
) ;
2024-02-27 21:52:57 +01:00
app . get (
"/v1/system/export-chats" ,
[ validApiKey ] ,
async ( request , response ) => {
/ *
# swagger . tags = [ 'System Settings' ]
# swagger . description = 'Export all of the chats from the system in a known format. Output depends on the type sent. Will be send with the correct header for the output.'
# swagger . parameters [ 'type' ] = {
in : 'query' ,
description : "Export format jsonl, json, csv, jsonAlpaca" ,
required : false ,
type : 'string'
}
# swagger . responses [ 200 ] = {
content : {
"application/json" : {
schema : {
type : 'object' ,
example : [
{
"role" : "user" ,
"content" : "What is AnythinglLM?"
} ,
{
"role" : "assistant" ,
"content" : "AnythingLLM is a knowledge graph and vector database management system built using NodeJS express server. It provides an interface for handling all interactions, including vectorDB management and LLM (Language Model) interactions."
} ,
]
}
}
}
}
# swagger . responses [ 403 ] = {
schema : {
"$ref" : "#/definitions/InvalidAPIKey"
}
}
* /
try {
const { type = "jsonl" } = request . query ;
const chats = await prepareWorkspaceChatsForExport ( type ) ;
const { contentType , data } = await exportChatsAsType ( chats , type ) ;
await EventLogs . logEvent ( "exported_chats" , {
type ,
} ) ;
response . setHeader ( "Content-Type" , contentType ) ;
response . status ( 200 ) . send ( data ) ;
} catch ( e ) {
2024-07-04 01:39:33 +02:00
console . error ( e . message , e ) ;
2024-02-27 21:52:57 +01:00
response . sendStatus ( 500 ) . end ( ) ;
}
}
) ;
2024-06-07 18:22:03 +02:00
app . delete (
"/v1/system/remove-documents" ,
[ validApiKey ] ,
async ( request , response ) => {
/ *
# swagger . tags = [ 'System Settings' ]
# swagger . description = 'Permanently remove documents from the system.'
# swagger . requestBody = {
description : 'Array of document names to be removed permanently.' ,
required : true ,
content : {
"application/json" : {
schema : {
type : 'object' ,
properties : {
names : {
type : 'array' ,
items : {
type : 'string'
} ,
example : [
"custom-documents/file.txt-fc4beeeb-e436-454d-8bb4-e5b8979cb48f.json"
]
}
}
}
}
}
}
# swagger . responses [ 200 ] = {
description : 'Documents removed successfully.' ,
content : {
"application/json" : {
schema : {
type : 'object' ,
example : {
success : true ,
message : 'Documents removed successfully'
}
}
}
}
}
# swagger . responses [ 403 ] = {
description : 'Forbidden' ,
schema : {
"$ref" : "#/definitions/InvalidAPIKey"
}
}
# swagger . responses [ 500 ] = {
description : 'Internal Server Error'
}
* /
try {
const { names } = reqBody ( request ) ;
for await ( const name of names ) await purgeDocument ( name ) ;
2024-06-07 19:09:35 +02:00
response
. status ( 200 )
. json ( { success : true , message : "Documents removed successfully" } )
. end ( ) ;
2024-06-07 18:22:03 +02:00
} catch ( e ) {
2024-07-04 01:39:33 +02:00
console . error ( e . message , e ) ;
2024-06-07 18:22:03 +02:00
response . sendStatus ( 500 ) . end ( ) ;
}
}
) ;
2023-08-24 04:15:07 +02:00
}
module . exports = { apiSystemEndpoints } ;