2024-01-22 23:14:01 +01:00
// Helpers that convert workspace chats to some supported format
// for external use by the user.
const { Workspace } = require ( "../../../models/workspace" ) ;
const { WorkspaceChats } = require ( "../../../models/workspaceChats" ) ;
2024-07-16 01:05:53 +02:00
const { safeJsonParse } = require ( "../../http" ) ;
2024-01-22 23:14:01 +01:00
2024-02-13 19:12:59 +01:00
async function convertToCSV ( preparedData ) {
2024-02-13 20:33:05 +01:00
const rows = [ "id,username,workspace,prompt,response,sent_at,rating" ] ;
2024-02-13 19:12:59 +01:00
for ( const item of preparedData ) {
const record = [
item . id ,
escapeCsv ( item . username ) ,
escapeCsv ( item . workspace ) ,
escapeCsv ( item . prompt ) ,
escapeCsv ( item . response ) ,
item . sent _at ,
2024-02-13 20:33:05 +01:00
item . feedback ,
2024-02-13 19:12:59 +01:00
] . join ( "," ) ;
rows . push ( record ) ;
2024-01-22 23:14:01 +01:00
}
return rows . join ( "\n" ) ;
}
2024-03-01 02:04:59 +01:00
async function convertToJSON ( preparedData ) {
return JSON . stringify ( preparedData , null , 4 ) ;
2024-02-16 21:35:53 +01:00
}
// ref: https://raw.githubusercontent.com/gururise/AlpacaDataCleaned/main/alpaca_data.json
async function convertToJSONAlpaca ( preparedData ) {
return JSON . stringify ( preparedData , null , 4 ) ;
2024-01-22 23:14:01 +01:00
}
async function convertToJSONL ( workspaceChatsMap ) {
return Object . values ( workspaceChatsMap )
. map ( ( workspaceChats ) => JSON . stringify ( workspaceChats ) )
. join ( "\n" ) ;
}
2024-02-13 19:12:59 +01:00
async function prepareWorkspaceChatsForExport ( format = "jsonl" ) {
if ( ! exportMap . hasOwnProperty ( format ) )
throw new Error ( "Invalid export type." ) ;
2024-01-22 23:14:01 +01:00
const chats = await WorkspaceChats . whereWithData ( { } , null , null , {
id : "asc" ,
} ) ;
2024-02-13 19:12:59 +01:00
2024-03-01 02:04:59 +01:00
if ( format === "csv" || format === "json" ) {
2024-02-13 19:12:59 +01:00
const preparedData = chats . map ( ( chat ) => {
const responseJson = JSON . parse ( chat . response ) ;
return {
id : chat . id ,
2024-08-22 00:25:47 +02:00
username : chat . user
? chat . user . username
: chat . api _session _id !== null
? "API"
: "unknown user" ,
2024-02-13 19:12:59 +01:00
workspace : chat . workspace ? chat . workspace . name : "unknown workspace" ,
prompt : chat . prompt ,
response : responseJson . text ,
sent _at : chat . createdAt ,
2024-02-13 20:33:05 +01:00
feedback :
chat . feedbackScore === null
? "--"
: chat . feedbackScore
? "GOOD"
: "BAD" ,
2024-02-13 19:12:59 +01:00
} ;
} ) ;
return preparedData ;
}
2024-07-16 01:05:53 +02:00
const workspaceIds = [ ... new Set ( chats . map ( ( chat ) => chat . workspaceId ) ) ] ;
const workspacesWithPrompts = await Promise . all (
workspaceIds . map ( ( id ) => Workspace . get ( { id : Number ( id ) } ) )
) ;
const workspacePromptsMap = workspacesWithPrompts . reduce ( ( acc , workspace ) => {
acc [ workspace . id ] = workspace . openAiPrompt ;
return acc ;
} , { } ) ;
2024-02-16 21:35:53 +01:00
if ( format === "jsonAlpaca" ) {
const preparedData = chats . map ( ( chat ) => {
const responseJson = JSON . parse ( chat . response ) ;
return {
2024-07-16 01:05:53 +02:00
instruction : buildSystemPrompt (
chat ,
workspacePromptsMap [ chat . workspaceId ]
) ,
input : chat . prompt ,
2024-02-16 21:35:53 +01:00
output : responseJson . text ,
} ;
} ) ;
return preparedData ;
}
2024-01-22 23:14:01 +01:00
const workspaceChatsMap = chats . reduce ( ( acc , chat ) => {
const { prompt , response , workspaceId } = chat ;
const responseJson = JSON . parse ( response ) ;
if ( ! acc [ workspaceId ] ) {
acc [ workspaceId ] = {
messages : [
{
role : "system" ,
content :
workspacePromptsMap [ workspaceId ] ||
"Given the following conversation, relevant context, and a follow up question, reply with an answer to the current question the user is asking. Return only your response to the question given the above information following the users instructions as needed." ,
} ,
] ,
} ;
}
acc [ workspaceId ] . messages . push (
{
role : "user" ,
content : prompt ,
} ,
{
role : "assistant" ,
content : responseJson . text ,
}
) ;
return acc ;
} , { } ) ;
return workspaceChatsMap ;
}
const exportMap = {
json : {
contentType : "application/json" ,
func : convertToJSON ,
} ,
csv : {
contentType : "text/csv" ,
func : convertToCSV ,
} ,
jsonl : {
contentType : "application/jsonl" ,
func : convertToJSONL ,
} ,
2024-02-16 21:35:53 +01:00
jsonAlpaca : {
contentType : "application/json" ,
func : convertToJSONAlpaca ,
} ,
2024-01-22 23:14:01 +01:00
} ;
2024-02-13 19:12:59 +01:00
function escapeCsv ( str ) {
return ` " ${ str . replace ( /"/g , '""' ) . replace ( /\n/g , " " ) } " ` ;
}
2024-01-22 23:14:01 +01:00
async function exportChatsAsType ( workspaceChatsMap , format = "jsonl" ) {
const { contentType , func } = exportMap . hasOwnProperty ( format )
? exportMap [ format ]
: exportMap . jsonl ;
return {
contentType ,
data : await func ( workspaceChatsMap ) ,
} ;
}
2024-07-16 01:05:53 +02:00
const STANDARD _PROMPT =
"Given the following conversation, relevant context, and a follow up question, reply with an answer to the current question the user is asking. Return only your response to the question given the above information following the users instructions as needed." ;
function buildSystemPrompt ( chat , prompt = null ) {
const sources = safeJsonParse ( chat . response ) ? . sources || [ ] ;
const contextTexts = sources . map ( ( source ) => source . text ) ;
const context =
sources . length > 0
? "\nContext:\n" +
contextTexts
. map ( ( text , i ) => {
return ` [CONTEXT ${ i } ]: \n ${ text } \n [END CONTEXT ${ i } ] \n \n ` ;
} )
. join ( "" )
: "" ;
return ` ${ prompt ? ? STANDARD _PROMPT } ${ context } ` ;
}
2024-01-22 23:14:01 +01:00
module . exports = {
prepareWorkspaceChatsForExport ,
exportChatsAsType ,
} ;