Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
chenpangpang
open-webui
Commits
27d5eb46
Commit
27d5eb46
authored
Jan 17, 2024
by
Brandon Hulston
Browse files
Add chatGPT chat history Import functionality
parent
5e32db1c
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
73 additions
and
2 deletions
+73
-2
src/lib/components/chat/SettingsModal.svelte
src/lib/components/chat/SettingsModal.svelte
+4
-1
src/lib/utils/index.ts
src/lib/utils/index.ts
+68
-0
src/routes/(app)/c/[id]/+page.svelte
src/routes/(app)/c/[id]/+page.svelte
+1
-1
No files found.
src/lib/components/chat/SettingsModal.svelte
View file @
27d5eb46
...
@@ -21,7 +21,7 @@
...
@@ -21,7 +21,7 @@
import { WEB_UI_VERSION, WEBUI_API_BASE_URL } from '$lib/constants';
import { WEB_UI_VERSION, WEBUI_API_BASE_URL } from '$lib/constants';
import { config, models, settings, user, chats } from '$lib/stores';
import { config, models, settings, user, chats } from '$lib/stores';
import { splitStream, getGravatarURL } from '$lib/utils';
import { splitStream, getGravatarURL
, getImportOrigin, convertGptChats
} from '$lib/utils';
import Advanced from './Settings/Advanced.svelte';
import Advanced from './Settings/Advanced.svelte';
import Modal from '../common/Modal.svelte';
import Modal from '../common/Modal.svelte';
...
@@ -132,6 +132,9 @@
...
@@ -132,6 +132,9 @@
reader.onload = (event) => {
reader.onload = (event) => {
let chats = JSON.parse(event.target.result);
let chats = JSON.parse(event.target.result);
console.log(chats);
console.log(chats);
if (getImportOrigin(chats) == 'gpt') {
chats = convertGptChats(chats);
}
importChats(chats);
importChats(chats);
};
};
...
...
src/lib/utils/index.ts
View file @
27d5eb46
...
@@ -192,3 +192,71 @@ export const calculateSHA256 = async (file) => {
...
@@ -192,3 +192,71 @@ export const calculateSHA256 = async (file) => {
throw
error
;
throw
error
;
}
}
};
};
export
const
getImportOrigin
=
(
_chats
)
=>
{
// Check what external service chat imports are from
if
(
'
mapping
'
in
_chats
[
0
])
{
return
'
gpt
'
;
}
return
'
webui
'
;
}
const
convertGptMessages
=
(
convo
)
=>
{
// Parse OpenAI chat messages and create chat dictionary for creating new chats
const
mapping
=
convo
[
"
mapping
"
];
const
messages
=
[];
let
currentId
=
""
;
for
(
let
message_id
in
mapping
)
{
const
message
=
mapping
[
message_id
];
currentId
=
message_id
;
if
(
message
[
"
message
"
]
==
null
||
message
[
"
message
"
][
"
content
"
][
"
parts
"
][
0
]
==
""
)
{
// Skip chat messages with no content
continue
;
}
else
{
const
new_chat
=
{
"
id
"
:
message_id
,
"
parentId
"
:
messages
.
length
>
0
?
message
[
"
parent
"
]
:
null
,
"
childrenIds
"
:
message
[
"
children
"
]
||
[],
"
role
"
:
message
[
"
message
"
]?.[
"
author
"
]?.[
"
role
"
]
!==
"
user
"
?
"
assistant
"
:
"
user
"
,
"
content
"
:
message
[
"
message
"
]?.[
"
content
"
]?.[
'
parts
'
]?.[
0
]
||
""
,
"
model
"
:
''
,
"
done
"
:
true
,
"
context
"
:
null
,
}
messages
.
push
(
new_chat
)
}
}
let
history
=
{};
messages
.
forEach
(
obj
=>
history
[
obj
.
id
]
=
obj
);
const
chat
=
{
"
history
"
:
{
"
currentId
"
:
currentId
,
"
messages
"
:
history
,
// Need to convert this to not a list and instead a json object
},
"
models
"
:
[
""
],
"
messages
"
:
messages
,
"
options
"
:
{},
"
timestamp
"
:
convo
[
"
create_time
"
],
"
title
"
:
convo
[
"
title
"
],
}
return
chat
;
}
export
const
convertGptChats
=
(
_chats
)
=>
{
// Create a list of dictionaries with each conversation from import
const
chats
=
[];
for
(
let
convo
of
_chats
)
{
const
chat
=
{
"
id
"
:
convo
[
"
id
"
],
"
user_id
"
:
''
,
"
title
"
:
convo
[
"
title
"
],
"
chat
"
:
convertGptMessages
(
convo
),
"
timestamp
"
:
convo
[
"
timestamp
"
],
}
chats
.
push
(
chat
)
}
return
chats
;
}
src/routes/(app)/c/[id]/+page.svelte
View file @
27d5eb46
...
@@ -696,7 +696,7 @@
...
@@ -696,7 +696,7 @@
<div class="min-h-screen w-full flex justify-center">
<div class="min-h-screen w-full flex justify-center">
<div class=" py-2.5 flex flex-col justify-between w-full">
<div class=" py-2.5 flex flex-col justify-between w-full">
<div class="max-w-2xl mx-auto w-full px-3 md:px-0 mt-10">
<div class="max-w-2xl mx-auto w-full px-3 md:px-0 mt-10">
<ModelSelector bind:selectedModels disabled={messages.length > 0} />
<ModelSelector bind:selectedModels disabled={messages.length > 0
&& !selectedModels.includes('')
} />
</div>
</div>
<div class=" h-full mt-10 mb-32 w-full flex flex-col">
<div class=" h-full mt-10 mb-32 w-full flex flex-col">
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment