From 27d5eb46847f71e0ec15c437c1064b950fca32fe Mon Sep 17 00:00:00 2001 From: Brandon Hulston Date: Wed, 17 Jan 2024 01:01:11 -0700 Subject: [PATCH 1/9] Add chatGPT chat history Import functionality --- src/lib/components/chat/SettingsModal.svelte | 5 +- src/lib/utils/index.ts | 68 ++++++++++++++++++++ src/routes/(app)/c/[id]/+page.svelte | 2 +- 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/src/lib/components/chat/SettingsModal.svelte b/src/lib/components/chat/SettingsModal.svelte index 35ad9f1aa..9da70add8 100644 --- a/src/lib/components/chat/SettingsModal.svelte +++ b/src/lib/components/chat/SettingsModal.svelte @@ -21,7 +21,7 @@ import { WEB_UI_VERSION, WEBUI_API_BASE_URL } from '$lib/constants'; 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 Modal from '../common/Modal.svelte'; @@ -132,6 +132,9 @@ reader.onload = (event) => { let chats = JSON.parse(event.target.result); console.log(chats); + if (getImportOrigin(chats) == 'gpt') { + chats = convertGptChats(chats); + } importChats(chats); }; diff --git a/src/lib/utils/index.ts b/src/lib/utils/index.ts index 129a1d12c..2c0906dc1 100644 --- a/src/lib/utils/index.ts +++ b/src/lib/utils/index.ts @@ -192,3 +192,71 @@ export const calculateSHA256 = async (file) => { 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; +} diff --git a/src/routes/(app)/c/[id]/+page.svelte b/src/routes/(app)/c/[id]/+page.svelte index c0db9c5f3..314cc7b6b 100644 --- a/src/routes/(app)/c/[id]/+page.svelte +++ b/src/routes/(app)/c/[id]/+page.svelte @@ -696,7 +696,7 @@
- 0} /> + 0 && !selectedModels.includes('')} />
From 415777eca2f7fafef84aa7922bfc553cbe8acbe7 Mon Sep 17 00:00:00 2001 From: Brandon Hulston Date: Wed, 17 Jan 2024 01:19:27 -0700 Subject: [PATCH 2/9] Add error catching for import --- src/lib/components/chat/SettingsModal.svelte | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/lib/components/chat/SettingsModal.svelte b/src/lib/components/chat/SettingsModal.svelte index 9da70add8..f5c2b06ed 100644 --- a/src/lib/components/chat/SettingsModal.svelte +++ b/src/lib/components/chat/SettingsModal.svelte @@ -133,7 +133,11 @@ let chats = JSON.parse(event.target.result); console.log(chats); if (getImportOrigin(chats) == 'gpt') { - chats = convertGptChats(chats); + try { + chats = convertGptChats(chats); + } catch (error) { + console.log("Unable to import chats:", error); + } } importChats(chats); }; From 81bfb97c91e72a24e74bddc7ec818d98a6f73920 Mon Sep 17 00:00:00 2001 From: Brandon Hulston Date: Wed, 17 Jan 2024 02:07:29 -0700 Subject: [PATCH 3/9] Make sure model is saved in DB for imported chats (or chats with no model saved already) --- src/routes/(app)/c/[id]/+page.svelte | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/routes/(app)/c/[id]/+page.svelte b/src/routes/(app)/c/[id]/+page.svelte index 314cc7b6b..593a7a78c 100644 --- a/src/routes/(app)/c/[id]/+page.svelte +++ b/src/routes/(app)/c/[id]/+page.svelte @@ -200,8 +200,15 @@ await chatId.set('local'); } await tick(); - } + } else if (chat.chat["models"][0] == "") { + // If model is not saved in DB, then save selectedmodel when message is sent + chat = await updateChatById(localStorage.token, $chatId, { + models: selectedModels + }); + await chats.set(await getChatList(localStorage.token)); + } + // Reset chat input textarea prompt = ''; files = []; From 1f66bbba98633133da7b6db82ad50b18feef51d9 Mon Sep 17 00:00:00 2001 From: Brandon Hulston Date: Wed, 17 Jan 2024 02:24:17 -0700 Subject: [PATCH 4/9] Better solution to handle edge cases --- src/routes/(app)/c/[id]/+page.svelte | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes/(app)/c/[id]/+page.svelte b/src/routes/(app)/c/[id]/+page.svelte index 593a7a78c..c4774aaae 100644 --- a/src/routes/(app)/c/[id]/+page.svelte +++ b/src/routes/(app)/c/[id]/+page.svelte @@ -200,7 +200,7 @@ await chatId.set('local'); } await tick(); - } else if (chat.chat["models"][0] == "") { + } else if (chat.chat["models"] != selectedModels) { // If model is not saved in DB, then save selectedmodel when message is sent chat = await updateChatById(localStorage.token, $chatId, { From 8e0552f7350c27938a7ee156612d2df81f11d528 Mon Sep 17 00:00:00 2001 From: Oliver Bob Reyes Lagumen Date: Wed, 17 Jan 2024 20:30:52 +0800 Subject: [PATCH 5/9] Idiot-Proof level HTTPS doc for apache --- docs/apache.md | 204 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 204 insertions(+) create mode 100644 docs/apache.md diff --git a/docs/apache.md b/docs/apache.md new file mode 100644 index 000000000..481fc7468 --- /dev/null +++ b/docs/apache.md @@ -0,0 +1,204 @@ +# Hosting UI and Models separately + +Sometimes, its beneficial to host Ollama, separate from the UI, but retain the RAG and RBAC support features shared across users: + +# Ollama WebUI Configuration + +## UI Configuration + +For the UI configuration, you can set up the Apache VirtualHost as follows: + +``` +# Assuming you have a website hosting this UI at "server.com" + + ServerName server.com + DocumentRoot /home/server/public_html + + ProxyPass / http://server.com:3000/ nocanon + ProxyPassReverse / http://server.com:3000/ + + +``` + +Enable the site first before you can request SSL: + +`a2ensite server.com.conf` # this will enable the site. a2ensite is short for "Apache 2 Enable Site" + + +``` +# For SSL + + ServerName server.com + DocumentRoot /home/server/public_html + + ProxyPass / http://server.com:3000/ nocanon + ProxyPassReverse / http://server.com:3000/ + + SSLEngine on + SSLCertificateFile /etc/ssl/virtualmin/170514456861234/ssl.cert + SSLCertificateKeyFile /etc/ssl/virtualmin/170514456861234/ssl.key + SSLProtocol all -SSLv2 -SSLv3 -TLSv1 -TLSv1.1 + + SSLProxyEngine on + SSLCACertificateFile /etc/ssl/virtualmin/170514456865864/ssl.ca + + +``` + +I'm using virtualmin here for my SSL clusters, but you can also use certbot directly or your preferred SSL method. To use SSL: + +### Prerequisites. + +Run the following commands: + +`snap install certbot --classic` +`snap apt install python3-certbot-apache` (this will install the apache plugin). + +Navigate to the apache sites-available directory: + +`cd /etc/apache2/sites-available/` + +Create server.com.conf if it is not yet already created, containing the above `` configuration (it should match your case. Modify as necessary). Use the one without the SSL: + +Once it's created, run `certbot --apache -d server.com`, this will request and add/create an SSL keys for you as well as create the server.com.le-ssl.conf + + +# Configuring Ollama Server + +On your latest installation of Ollama, make sure that you have setup your api server from the official Ollama reference: + +[Ollama FAQ](https://github.com/jmorganca/ollama/blob/main/docs/faq.md) + + +### TL;DR + +The guide doesn't seem to match the current updated service file on linux. So, we will address it here: + +Unless when you're compiling Ollama from source, installing with the standard install `curl https://ollama.ai/install.sh | sh` creates a file called `ollama.service` in /etc/systemd/system. You can use nano to edit the file: + +``` +sudo nano /etc/systemd/system/ollama.service +``` + +Add the following lines: +``` +Environment="OLLAMA_HOST=0.0.0.0:11434" # this line is mandatory. You can also specify +``` + +For instance: + +``` +[Unit] +Description=Ollama Service +After=network-online.target + +[Service] +ExecStart=/usr/local/bin/ollama serve +Environment="OLLAMA_HOST=0.0.0.0:11434" # this line is mandatory. You can also specify 192.168.254.109:DIFFERENT_PORT, format +Environment="OLLAMA_ORIGINS=http://192.168.254.106:11434,https://models.server.city" # this line is optional +User=ollama +Group=ollama +Restart=always +RestartSec=3 +Environment="PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/s> + +[Install] +WantedBy=default.target +``` + + +Save the file by pressing CTRL+S, then press CTRL+X + +When your computer restarts, the Ollama server will now be listening on the IP:PORT you specified, in this case 0.0.0.0:11434, or 192.168.254.106:11434 (whatever your local IP address is). Make sure that your router is correctly configured to serve pages from that local IP by forwarding 11434 to your local IP server. + + +# Ollama Model Configuration +## For the Ollama model configuration, use the following Apache VirtualHost setup: + + +Navigate to the apache sites-available directory: + +`cd /etc/apache2/sites-available/` + +`nano models.server.city.conf` # match this with your ollama server domain + +Add the folloing virtualhost containing this example (modify as needed): + +``` + +# Assuming you have a website hosting this UI at "models.server.city" + + + DocumentRoot "/var/www/html/" + ServerName models.server.city + + Options None + Require all granted + + + ProxyRequests Off + ProxyPreserveHost On + ProxyAddHeaders On + SSLProxyEngine on + + ProxyPass / http://server.city:1000/ nocanon # or port 11434 + ProxyPassReverse / http://server.city:1000/ # or port 11434 + + SSLCertificateFile /etc/letsencrypt/live/models.server.city/fullchain.pem + SSLCertificateKeyFile /etc/letsencrypt/live/models.server.city/privkey.pem + Include /etc/letsencrypt/options-ssl-apache.conf + + +``` + +You may need to enable the site first (if you haven't done so yet) before you can request SSL: + +`a2ensite models.server.city.conf` + +#### For the SSL part of Ollama server + +Run the following commands: + +Navigate to the apache sites-available directory: + +`cd /etc/apache2/sites-available/` +`certbot --apache -d server.com` + +``` + + DocumentRoot "/var/www/html/" + ServerName models.server.city + + Options None + Require all granted + + + ProxyRequests Off + ProxyPreserveHost On + ProxyAddHeaders On + SSLProxyEngine on + + ProxyPass / http://server.city:1000/ nocanon # or port 11434 + ProxyPassReverse / http://server.city:1000/ # or port 11434 + + RewriteEngine on + RewriteCond %{SERVER_NAME} =models.server.city + RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent] + + +``` + +Don't forget to restart/reload Apache with `systemctl reload apache2` + +Open your site at https://server.com! + +**Congratulations**, your _**Open-AI-like Chat-GPT style UI**_ is now serving AI with RAG, RBAC and multimodal features! Download Ollama models if you haven't yet done so! + +If you encounter any misconfiguration or errors, please file an issue or engage with our discussion. There are a lot of friendly developers here to assist you. + +Let's make this UI much more user friendly for everyone! + +Thanks for making ollama-webui your UI Choice for AI! + + +This doc is made by **Bob Reyes**, your **Ollama-Web-UI** fan from the Philippines. From 7f144795677edf21a96cae77376bf3b6d3eca41e Mon Sep 17 00:00:00 2001 From: i810697 Date: Wed, 17 Jan 2024 11:47:49 -0500 Subject: [PATCH 6/9] fixing md document upload bug --- src/lib/components/chat/MessageInput.svelte | 9 ++++++--- src/routes/(app)/documents/+page.svelte | 10 ++++++++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/lib/components/chat/MessageInput.svelte b/src/lib/components/chat/MessageInput.svelte index 3fae5a764..4830b98c7 100644 --- a/src/lib/components/chat/MessageInput.svelte +++ b/src/lib/components/chat/MessageInput.svelte @@ -301,7 +301,10 @@ const file = inputFiles[0]; if (['image/gif', 'image/jpeg', 'image/png'].includes(file['type'])) { reader.readAsDataURL(file); - } else if (SUPPORTED_FILE_TYPE.includes(file['type'])) { + } else if ( + SUPPORTED_FILE_TYPE.includes(file['type']) || + ['md'].includes(file.name.split('.').at(-1)) + ) { uploadDoc(file); filesInputElement.value = ''; } else { @@ -461,8 +464,8 @@ placeholder={chatInputPlaceholder !== '' ? chatInputPlaceholder : speechRecognitionListening - ? 'Listening...' - : 'Send a message'} + ? 'Listening...' + : 'Send a message'} bind:value={prompt} on:keypress={(e) => { if (e.keyCode == 13 && !e.shiftKey) { diff --git a/src/routes/(app)/documents/+page.svelte b/src/routes/(app)/documents/+page.svelte index d79056e24..9a0aa1301 100644 --- a/src/routes/(app)/documents/+page.svelte +++ b/src/routes/(app)/documents/+page.svelte @@ -67,7 +67,10 @@ if (inputFiles && inputFiles.length > 0) { const file = inputFiles[0]; - if (SUPPORTED_FILE_TYPE.includes(file['type'])) { + if ( + SUPPORTED_FILE_TYPE.includes(file['type']) || + ['md'].includes(file.name.split('.').at(-1)) + ) { uploadDoc(file); } else { toast.error(`Unsupported File Type '${file['type']}'.`); @@ -144,7 +147,10 @@ on:change={async (e) => { if (inputFiles && inputFiles.length > 0) { const file = inputFiles[0]; - if (SUPPORTED_FILE_TYPE.includes(file['type'])) { + if ( + SUPPORTED_FILE_TYPE.includes(file['type']) || + ['md'].includes(file.name.split('.').at(-1)) + ) { uploadDoc(file); } else { toast.error(`Unsupported File Type '${file['type']}'.`); From f40147ce58e746eb2825e6755cdf9a734a0b3cf4 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Wed, 17 Jan 2024 14:23:16 -0800 Subject: [PATCH 7/9] refac: gpt renamed to openai --- src/lib/components/chat/SettingsModal.svelte | 8 +- src/lib/utils/index.ts | 94 ++++++++++---------- 2 files changed, 51 insertions(+), 51 deletions(-) diff --git a/src/lib/components/chat/SettingsModal.svelte b/src/lib/components/chat/SettingsModal.svelte index f5c2b06ed..c79fe1068 100644 --- a/src/lib/components/chat/SettingsModal.svelte +++ b/src/lib/components/chat/SettingsModal.svelte @@ -21,7 +21,7 @@ import { WEB_UI_VERSION, WEBUI_API_BASE_URL } from '$lib/constants'; import { config, models, settings, user, chats } from '$lib/stores'; - import { splitStream, getGravatarURL, getImportOrigin, convertGptChats } from '$lib/utils'; + import { splitStream, getGravatarURL, getImportOrigin, convertOpenAIChats } from '$lib/utils'; import Advanced from './Settings/Advanced.svelte'; import Modal from '../common/Modal.svelte'; @@ -132,11 +132,11 @@ reader.onload = (event) => { let chats = JSON.parse(event.target.result); console.log(chats); - if (getImportOrigin(chats) == 'gpt') { + if (getImportOrigin(chats) == 'openai') { try { - chats = convertGptChats(chats); + chats = convertOpenAIChats(chats); } catch (error) { - console.log("Unable to import chats:", error); + console.log('Unable to import chats:', error); } } importChats(chats); diff --git a/src/lib/utils/index.ts b/src/lib/utils/index.ts index 2c0906dc1..c0a2f9ed6 100644 --- a/src/lib/utils/index.ts +++ b/src/lib/utils/index.ts @@ -195,68 +195,68 @@ export const calculateSHA256 = async (file) => { export const getImportOrigin = (_chats) => { // Check what external service chat imports are from - if ('mapping' in _chats[0]) { - return 'gpt'; - } - return 'webui'; -} + if ('mapping' in _chats[0]) { + return 'openai'; + } + return 'webui'; +}; -const convertGptMessages = (convo) => { +const convertOpenAIMessages = (convo) => { // Parse OpenAI chat messages and create chat dictionary for creating new chats - const mapping = convo["mapping"]; + const mapping = convo['mapping']; const messages = []; - let currentId = ""; + let currentId = ''; - for (let message_id in mapping) { - const message = mapping[message_id]; + for (let message_id in mapping) { + const message = mapping[message_id]; currentId = message_id; - if (message["message"] == null || message["message"]["content"]["parts"][0] == "") { + 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) + 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); + 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 + 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; -} + models: [''], + messages: messages, + options: {}, + timestamp: convo['create_time'], + title: convo['title'] + }; + return chat; +}; -export const convertGptChats = (_chats) => { +export const convertOpenAIChats = (_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) + const chats = []; + for (let convo of _chats) { + const chat = { + id: convo['id'], + user_id: '', + title: convo['title'], + chat: convertOpenAIMessages(convo), + timestamp: convo['timestamp'] + }; + chats.push(chat); } - return chats; -} + return chats; +}; From f9885a15e47e8b0045139444df09ecc2acba2a9b Mon Sep 17 00:00:00 2001 From: Mike Slinn Date: Wed, 17 Jan 2024 17:30:58 -0500 Subject: [PATCH 8/9] Can now run from any directory --- backend/start.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/backend/start.sh b/backend/start.sh index 44e0c5810..e6d09364e 100755 --- a/backend/start.sh +++ b/backend/start.sh @@ -1,4 +1,7 @@ #!/usr/bin/env bash +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) +cd "$SCRIPT_DIR" || exit + PORT="${PORT:-8080}" -uvicorn main:app --host 0.0.0.0 --port $PORT --forwarded-allow-ips '*' +uvicorn main:app --host 0.0.0.0 --port "$PORT" --forwarded-allow-ips '*' From b6ab357e8cb8422b224cf72dfa80c6d3950de7c3 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Wed, 17 Jan 2024 14:47:56 -0800 Subject: [PATCH 9/9] fix: more edge cases --- backend/apps/web/routers/chats.py | 40 +++++++++++++++++-------------- src/lib/utils/index.ts | 27 +++++++++++---------- 2 files changed, 37 insertions(+), 30 deletions(-) diff --git a/backend/apps/web/routers/chats.py b/backend/apps/web/routers/chats.py index f061b145b..e97e14730 100644 --- a/backend/apps/web/routers/chats.py +++ b/backend/apps/web/routers/chats.py @@ -17,7 +17,8 @@ from apps.web.models.chats import ( ) from utils.utils import ( - bearer_scheme, ) + bearer_scheme, +) from constants import ERROR_MESSAGES router = APIRouter() @@ -29,7 +30,8 @@ router = APIRouter() @router.get("/", response_model=List[ChatTitleIdResponse]) async def get_user_chats( - user=Depends(get_current_user), skip: int = 0, limit: int = 50): + user=Depends(get_current_user), skip: int = 0, limit: int = 50 +): return Chats.get_chat_lists_by_user_id(user.id, skip, limit) @@ -41,9 +43,8 @@ async def get_user_chats( @router.get("/all", response_model=List[ChatResponse]) async def get_all_user_chats(user=Depends(get_current_user)): return [ - ChatResponse(**{ - **chat.model_dump(), "chat": json.loads(chat.chat) - }) for chat in Chats.get_all_chats_by_user_id(user.id) + ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)}) + for chat in Chats.get_all_chats_by_user_id(user.id) ] @@ -54,8 +55,14 @@ async def get_all_user_chats(user=Depends(get_current_user)): @router.post("/new", response_model=Optional[ChatResponse]) async def create_new_chat(form_data: ChatForm, user=Depends(get_current_user)): - chat = Chats.insert_new_chat(user.id, form_data) - return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)}) + try: + chat = Chats.insert_new_chat(user.id, form_data) + return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)}) + except Exception as e: + print(e) + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT() + ) ############################ @@ -68,12 +75,11 @@ async def get_chat_by_id(id: str, user=Depends(get_current_user)): chat = Chats.get_chat_by_id_and_user_id(id, user.id) if chat: - return ChatResponse(**{ - **chat.model_dump(), "chat": json.loads(chat.chat) - }) + return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)}) else: - raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, - detail=ERROR_MESSAGES.NOT_FOUND) + raise HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.NOT_FOUND + ) ############################ @@ -82,17 +88,15 @@ async def get_chat_by_id(id: str, user=Depends(get_current_user)): @router.post("/{id}", response_model=Optional[ChatResponse]) -async def update_chat_by_id(id: str, - form_data: ChatForm, - user=Depends(get_current_user)): +async def update_chat_by_id( + id: str, form_data: ChatForm, user=Depends(get_current_user) +): chat = Chats.get_chat_by_id_and_user_id(id, user.id) if chat: updated_chat = {**json.loads(chat.chat), **form_data.chat} chat = Chats.update_chat_by_id(id, updated_chat) - return ChatResponse(**{ - **chat.model_dump(), "chat": json.loads(chat.chat) - }) + return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)}) else: raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, diff --git a/src/lib/utils/index.ts b/src/lib/utils/index.ts index c0a2f9ed6..e7dfba2c7 100644 --- a/src/lib/utils/index.ts +++ b/src/lib/utils/index.ts @@ -216,11 +216,11 @@ const convertOpenAIMessages = (convo) => { } else { const new_chat = { id: message_id, - parentId: messages.length > 0 ? message['parent'] : null, + parentId: messages.length > 0 && message['parent'] in mapping ? message['parent'] : null, childrenIds: message['children'] || [], role: message['message']?.['author']?.['role'] !== 'user' ? 'assistant' : 'user', content: message['message']?.['content']?.['parts']?.[0] || '', - model: '', + model: 'gpt-3.5-turbo', done: true, context: null }; @@ -236,11 +236,11 @@ const convertOpenAIMessages = (convo) => { currentId: currentId, messages: history // Need to convert this to not a list and instead a json object }, - models: [''], + models: ['gpt-3.5-turbo'], messages: messages, options: {}, timestamp: convo['create_time'], - title: convo['title'] + title: convo['title'] ?? 'New Chat' }; return chat; }; @@ -249,14 +249,17 @@ export const convertOpenAIChats = (_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: convertOpenAIMessages(convo), - timestamp: convo['timestamp'] - }; - chats.push(chat); + const chat = convertOpenAIMessages(convo); + + if (Object.keys(chat.history.messages).length > 0) { + chats.push({ + id: convo['id'], + user_id: '', + title: convo['title'], + chat: chat, + timestamp: convo['timestamp'] + }); + } } return chats; };