mirror of
https://github.com/pionxzh/chatgpt-exporter.git
synced 2026-07-23 09:00:51 -05:00
chore: add interfaces and other fixes
This commit is contained in:
@@ -1,49 +1,95 @@
|
||||
function extractConversation(data: any): any[] {
|
||||
const current_node = data.current_node
|
||||
interface Message {
|
||||
parent?: string
|
||||
message?: {
|
||||
author: {
|
||||
role: string
|
||||
}
|
||||
create_time: number
|
||||
content: {
|
||||
parts: string[]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
interface MessageMapping {
|
||||
[key: string]: Message
|
||||
}
|
||||
|
||||
interface ConversationData {
|
||||
current_node: string
|
||||
mapping: MessageMapping
|
||||
}
|
||||
|
||||
interface TavernMessage {
|
||||
name: string
|
||||
is_user: boolean
|
||||
is_name: boolean
|
||||
send_date: number
|
||||
mes: string
|
||||
swipes: string[]
|
||||
swipe_id: number
|
||||
}
|
||||
|
||||
interface NameMessage {
|
||||
user_name: string
|
||||
character_name: string
|
||||
}
|
||||
|
||||
interface OobaData {
|
||||
internal: [string, string][]
|
||||
visible: [string, string][]
|
||||
}
|
||||
|
||||
function extractConversation(data: ConversationData): Message[] {
|
||||
const currentNode = data.current_node
|
||||
const mapping = data.mapping
|
||||
|
||||
const messagesReversed: any[] = []
|
||||
let node_id: any = current_node
|
||||
const messagesReversed: Message[] = []
|
||||
let nodeId: string | undefined = currentNode
|
||||
|
||||
while (node_id !== null && node_id !== undefined) {
|
||||
const current_message = mapping[node_id]
|
||||
messagesReversed.push(current_message)
|
||||
node_id = current_message.parent
|
||||
while (nodeId !== null && nodeId !== undefined) {
|
||||
const currentMessage: Message = mapping[nodeId]
|
||||
messagesReversed.push(currentMessage)
|
||||
nodeId = currentMessage.parent
|
||||
}
|
||||
|
||||
return messagesReversed.reverse()
|
||||
}
|
||||
|
||||
function convertMessageToTavern(message_data: any): any | null {
|
||||
if (!message_data.message) {
|
||||
function convertMessageToTavern(messageData: Message): TavernMessage | null {
|
||||
if (!messageData.message) {
|
||||
return null
|
||||
}
|
||||
|
||||
const sender_role: string = message_data.message.author.role
|
||||
if (sender_role === 'system') {
|
||||
const senderRole: string = messageData.message.author.role
|
||||
if (senderRole === 'system') {
|
||||
return null
|
||||
}
|
||||
|
||||
const is_assistant = sender_role === 'assistant'
|
||||
const create_time = Number.parseInt(message_data.message.create_time, 10)
|
||||
const text: string = message_data.message.content.parts[0]
|
||||
const isAssistant = senderRole === 'assistant'
|
||||
const createTime: number = messageData.message.create_time
|
||||
const text: string = messageData.message.content.parts[0]
|
||||
|
||||
return {
|
||||
name: is_assistant ? 'Assistant' : 'You',
|
||||
is_user: !is_assistant,
|
||||
is_name: is_assistant,
|
||||
send_date: create_time,
|
||||
name: isAssistant ? 'Assistant' : 'You',
|
||||
is_user: !isAssistant,
|
||||
is_name: isAssistant,
|
||||
send_date: createTime,
|
||||
mes: text,
|
||||
swipes: [text],
|
||||
swipe_id: 0,
|
||||
}
|
||||
}
|
||||
|
||||
export function getTavernString(jsonData: any): string {
|
||||
function jsonlStringify(messageArray: any[]): string {
|
||||
return messageArray.map((msg: any) => JSON.stringify(msg)).join('\n')
|
||||
}
|
||||
|
||||
export function getTavernString(jsonData: ConversationData): string {
|
||||
// Takes the OAI JSON data as input, outputs the JSONL string
|
||||
const conversation = extractConversation(jsonData)
|
||||
|
||||
const convertedConvo: any[] = [{
|
||||
const convertedConvo: (TavernMessage | NameMessage)[] = [{
|
||||
user_name: 'You',
|
||||
character_name: 'Assistant',
|
||||
}]
|
||||
@@ -55,13 +101,12 @@ export function getTavernString(jsonData: any): string {
|
||||
}
|
||||
})
|
||||
// This _has_ to be stringified without adding any indentation, due to the JSONL format.
|
||||
return convertedConvo.map(msg => JSON.stringify(msg)).join('\n')
|
||||
return jsonlStringify(convertedConvo)
|
||||
}
|
||||
|
||||
export function getOobaString(jsonData: any): string {
|
||||
export function getOobaString(jsonData: ConversationData): string {
|
||||
// Takes the OAI JSON data as input, outputs the serialized JSON
|
||||
const messages = extractConversation(jsonData)
|
||||
const oobaData: any = {}
|
||||
const pairs: any[] = []
|
||||
let idx = 0
|
||||
|
||||
@@ -70,6 +115,11 @@ export function getOobaString(jsonData: any): string {
|
||||
const nextMessage = messages[idx + 1]
|
||||
let role: string, text: string, nextRole: string, nextText: string
|
||||
|
||||
if (!message.message || !nextMessage.message) {
|
||||
idx += 1
|
||||
continue
|
||||
}
|
||||
|
||||
try {
|
||||
role = message.message.author.role
|
||||
text = message.message.content.parts[0]
|
||||
@@ -107,9 +157,10 @@ export function getOobaString(jsonData: any): string {
|
||||
idx += 1
|
||||
}
|
||||
}
|
||||
|
||||
oobaData.internal = pairs
|
||||
oobaData.visible = JSON.parse(JSON.stringify(pairs))
|
||||
const oobaData: OobaData = {
|
||||
internal: pairs,
|
||||
visible: JSON.parse(JSON.stringify(pairs)),
|
||||
}
|
||||
|
||||
if (oobaData.visible[0] && oobaData.visible[0][0] === '<|BEGIN-VISIBLE-CHAT|>') {
|
||||
oobaData.visible[0][0] = ''
|
||||
|
||||
Reference in New Issue
Block a user