Skip to content

Commit

Permalink
feat: add {model} and {model_name} to variable
Browse files Browse the repository at this point in the history
  • Loading branch information
pionxzh committed Mar 30, 2023
1 parent 1369f14 commit 057fe9f
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 13 deletions.
20 changes: 17 additions & 3 deletions packages/userscript/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ interface ApiSession {
}
}

type ModelSlug = 'text-davinci-002-render-sha' | 'text-davinci-002-render-paid' | 'gpt-4'

interface MessageMeta {
finish_details?: {
stop: string
type: string
type: 'stop' | 'interrupted' & (string & {})
}
model_slug?: 'text-davinci-002-render-sha' | 'text-davinci-002-render-paid' | 'gpt-4' & (string & {})
model_slug?: ModelSlug & (string & {})
timestamp_: 'absolute' & (string & {})
}

Expand All @@ -41,7 +43,6 @@ interface ConversationNode {
id: string
metadata?: MessageMeta
recipient: 'all' & (string & {})
update_time: string | null
weight: number
}
parent?: string
Expand All @@ -55,6 +56,7 @@ export interface ApiConversation {
}
moderation_results: unknown[]
title: string
update_time: number
}

export type ApiConversationWithId = ApiConversation & {
Expand Down Expand Up @@ -160,13 +162,23 @@ class LinkedListItem<T> {
export interface ConversationResult {
id: string
title: string
modelSlug: string
model: string
createTime: number
conversationNodes: ConversationNode[]
}

const modelMapping: { [key in ModelSlug]: string } = {
'text-davinci-002-render-sha': 'GTP-3.5',
'text-davinci-002-render-paid': 'GTP-3.5',
'gpt-4': 'GPT-4',
}

export function processConversation(conversation: ApiConversationWithId, conversationChoices: Array<number | null> = []): ConversationResult {
const title = conversation.title || 'ChatGPT Conversation'
const createTime = conversation.create_time
const modelSlug = Object.values(conversation.mapping).find(node => node.message?.metadata?.model_slug)?.message?.metadata?.model_slug || ''
const model = modelSlug ? (modelMapping[modelSlug] || '') : ''

const result: ConversationNode[] = []
const nodes = Object.values(conversation.mapping)
Expand Down Expand Up @@ -205,6 +217,8 @@ export function processConversation(conversation: ApiConversationWithId, convers
return {
id: conversation.id,
title,
modelSlug,
model,
createTime,
conversationNodes: result,
}
Expand Down
4 changes: 3 additions & 1 deletion packages/userscript/src/exporter/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export async function exportAllToHtml(fileNameFormat: string, apiConversations:
}

function conversationToHtml(conversation: ConversationResult, avatar: string, metaList?: ExportMeta[]) {
const { id, title, conversationNodes } = conversation
const { id, title, model, modelSlug, conversationNodes } = conversation

const conversationHtml = conversationNodes.map((item) => {
const author = item.message?.author.role === 'assistant' ? 'ChatGPT' : 'You'
Expand Down Expand Up @@ -109,6 +109,8 @@ function conversationToHtml(conversation: ConversationResult, avatar: string, me
.replace('{date}', date)
.replace('{timestamp}', timestamp())
.replace('{source}', source)
.replace('{model}', model)
.replace('{mode_name}', modelSlug)

return [name, val] as const
})
Expand Down
7 changes: 5 additions & 2 deletions packages/userscript/src/exporter/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ export async function exportAllToMarkdown(fileNameFormat: string, apiConversatio
}

function conversationToMarkdown(conversation: ConversationResult, metaList?: ExportMeta[]) {
const { id, title, conversationNodes } = conversation
const { id, title, model, modelSlug, conversationNodes } = conversation
const source = `${baseUrl}/chat/${id}`

const _metaList = metaList
?.filter(x => !!x.name)
Expand All @@ -52,7 +53,9 @@ function conversationToMarkdown(conversation: ConversationResult, metaList?: Exp
.replace('{title}', title)
.replace('{date}', dateStr())
.replace('{timestamp}', timestamp())
.replace('{source}', `${baseUrl}/chat/${id}`)
.replace('{source}', source)
.replace('{model}', model)
.replace('{modelSlug}', modelSlug)

return `${name}: ${val}`
})
Expand Down
4 changes: 4 additions & 0 deletions packages/userscript/src/styles/missing-tailwind.css
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
white-space: nowrap;
}

.whitespace-nowrap {
white-space: nowrap;
}

@media (min-width:768px) {
/* md */
}
Expand Down
1 change: 1 addition & 0 deletions packages/userscript/src/ui/Dialog.css
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
width: 90vw;
max-width: 450px;
max-height: 85vh;
overflow-x: hidden;
overflow-y: auto;
padding: 16px 24px;
z-index: 1001;
Expand Down
23 changes: 16 additions & 7 deletions packages/userscript/src/ui/SettingDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import { IconCross, IconTrash } from './Icons'
import { useMetaDataContext } from './MetaContext'
import type { FC } from '../type'

const Variable = ({ name, title }: { name: string; title: string }) => (
<strong className="cursor-help select-all whitespace-nowrap" title={title}>{name}</strong>
)

export const SettingDialog: FC = ({ children }) => {
const { format, setFormat } = useFormatContext()
const { enableMeta, setEnableMeta, exportMetaList, setExportMetaList } = useMetaDataContext()
Expand Down Expand Up @@ -44,11 +48,11 @@ export const SettingDialog: FC = ({ children }) => {
<dd>
<p className="mt-2 text-sm text-gray-700 dark:text-gray-300">
Available variables:&nbsp;
<strong className="cursor-help select-all" title={title}>{'{title}'}</strong>
<Variable name="{title}" title={title} />
,&nbsp;
<strong className="cursor-help select-all" title={date}>{'{date}'}</strong>
<Variable name="{date}" title={date} />
,&nbsp;
<strong className="cursor-help select-all" title={timestamp}>{'{timestamp}'}</strong>
<Variable name="{timestamp}" title={timestamp} />
</p>
<input className="Input mt-1" id="filename" value={format} onChange={e => setFormat(e.currentTarget.value)} />
<p className="mt-1 text-sm text-gray-700 dark:text-gray-300">
Expand All @@ -73,13 +77,18 @@ export const SettingDialog: FC = ({ children }) => {
<>
<p className="mt-2 text-sm text-gray-700 dark:text-gray-300">
Available variables:&nbsp;
<strong className="cursor-help select-all" title={title}>{'{title}'}</strong>
<Variable name="{title}" title={title} />
,&nbsp;
<Variable name="{date}" title={date} />
,&nbsp;
<Variable name="{timestamp}" title={timestamp} />
,&nbsp;
<strong className="cursor-help select-all" title={date}>{'{date}'}</strong>
<Variable name="{source}" title={source} />
,&nbsp;
<strong className="cursor-help select-all" title={timestamp}>{'{timestamp}'}</strong>
<br />
<Variable name="{model}" title={'ChatGPT-3.5'} />
,&nbsp;
<strong className="cursor-help select-all" title={source}>{'{source}'}</strong>
<Variable name="{model_name}" title={'text-davinci-002-render-sha'} />
</p>
{exportMetaList.map((meta, i) => (
<div className="flex items-center mt-2" key={i}>
Expand Down

0 comments on commit 057fe9f

Please sign in to comment.