Skip to content

Commit c8113d2

Browse files
authored
bug fix for file upload & session switch (#67)
* fix aws file upload bugs Signed-off-by: cbh778899 <[email protected]> * edit styles of code section Signed-off-by: cbh778899 <[email protected]> * add padding for user input element Signed-off-by: cbh778899 <[email protected]> * add MIN_TOKENS Signed-off-by: cbh778899 <[email protected]> * if max_tokens set to 0, there will be no limitations Signed-off-by: cbh778899 <[email protected]> * remove output to console Signed-off-by: cbh778899 <[email protected]> * add special value to bypass checkValue Signed-off-by: cbh778899 <[email protected]> * change default max_tokens to 1024 as we have gpu inference now Signed-off-by: cbh778899 <[email protected]> * fix engine switching bugs, engines are not shared between sessions any more Signed-off-by: cbh778899 <[email protected]> --------- Signed-off-by: cbh778899 <[email protected]>
1 parent 28881aa commit c8113d2

File tree

10 files changed

+53
-30
lines changed

10 files changed

+53
-30
lines changed

preloader/node-llama-cpp-preloader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@ async function chatCompletions(latest_message, cb=null) {
106106
const options = {
107107
signal: stop_signal.signal,
108108
stopOnAbortSignal: true,
109-
maxTokens: max_tokens,
110109
topP: top_p,
111110
temperature
112111
}
112+
if(max_tokens) options.maxTokens = max_tokens
113113
let resp_text = ''
114114
if(cb) options.onTextChunk = chunk => {
115115
resp_text += chunk;

src/components/chat/UserMessage.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export default function UserMessage({ uid, enable_send, file_available, abort_co
1616
event.preventDefault();
1717
send(message, files);
1818
setMessage('');
19-
setFiles('');
19+
setFiles([]);
2020
}
2121

2222
// update when uid changed, means we entered a new conversation
@@ -41,7 +41,7 @@ export default function UserMessage({ uid, enable_send, file_available, abort_co
4141
<input
4242
type="file" className="clickable"
4343
title={files.length ? `Append file ${files.map(e=>e.name).join('; ')}` : "Select file to append"}
44-
onChange={evt=>setFiles(evt.target.files.length ? evt.target.files[0] : null)} />
44+
onChange={evt=>setFiles([...evt.target.files])} />
4545
</div>
4646
}
4747
<input type="text" ref={inputRef} value={message} onChange={evt=>setMessage(evt.target.value)}/>

src/components/chat/index.jsx

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { useEffect, useState } from "react";
1+
import { useEffect, useRef, useState } from "react";
22
import Tickets from "./Tickets";
33
// import Conversation from "./Conversation";
44
import useIDB from "../../utils/idb";
55
import DeleteConfirm from "./DeleteConfirm";
66
import ChatPage from "./ChatPage";
7-
import { useRef } from "react";
87
import { getCompletionFunctions } from "../../utils/workers";
8+
import { getPlatformSettings } from "../../utils/general_settings";
99

1010
export default function Chat() {
1111

@@ -20,8 +20,8 @@ export default function Chat() {
2020
const [pending_message, setPendingMessage] = useState(null);
2121

2222
const idb = useIDB();
23-
// const settings = useRef(getCompletionFunctions());
24-
const settings = useRef(getCompletionFunctions());
23+
const platform = useRef(getPlatformSettings().enabled_platform);
24+
const [session_setting, setSessionSetting] = useState({});
2525

2626
async function sendMessage(message, files) {
2727
// save user messages
@@ -59,11 +59,11 @@ export default function Chat() {
5959

6060
// start inference
6161
const send_message = (
62-
settings.current.formator ?
63-
await settings.current.formator(history_save, files) : history_save
62+
session_setting.formator ?
63+
await session_setting.formator(history_save, files) : history_save
6464
)
6565
setPendingMessage('')
66-
await settings.current.completions(send_message, cb)
66+
await session_setting.completions(send_message, cb)
6767
}
6868

6969
function updateChatClient(client) {
@@ -127,27 +127,29 @@ export default function Chat() {
127127
message_history = messages;
128128
setChatHistory(messages)
129129
}).finally(()=>{
130-
const client = settings.current.initClient(chat.client || null, message_history)
130+
const ss = getCompletionFunctions(chat.platform);
131+
const client = ss.initClient(chat.client || null, message_history)
131132
if(!chat.client) {
132133
updateChatClient(client)
133134
}
135+
setSessionSetting(ss);
134136
})
135137
}
136138
// eslint-disable-next-line
137139
}, [chat])
138140

139141
return (
140-
settings.current ?
142+
platform.current ?
141143
<div className="chat">
142144
<Tickets
143145
selectChat={selectChat} current_chat={chat}
144146
setHistory={setTickets} history={tickets}
145-
deleteHistory={requestDelete} platform={settings.current.platform}
147+
deleteHistory={requestDelete} platform={platform.current}
146148
/>
147149
<ChatPage
148150
updateTitle={updateTitle}
149151
chat={chat} chat_history={chat_history}
150-
pending_message={pending_message} abort={settings.current.abort}
152+
pending_message={pending_message} abort={session_setting.abort}
151153
sendMessage={sendMessage}
152154
/>
153155
<DeleteConfirm

src/components/settings/ModelSettings.jsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { useEffect, useState } from "react";
22
import ScrollBarComponent from "./components/ScrollBarComponent";
33
import SettingSection from "./SettingSection";
44
import { getModelSettings, updateModelSettings } from "../../utils/general_settings";
5+
import { MIN_TOKENS } from "../../utils/types";
56

67
export default function ModelSettings({ trigger, updateState }) {
78

@@ -10,8 +11,11 @@ export default function ModelSettings({ trigger, updateState }) {
1011
const [temperature, setTemperature] = useState(0);
1112

1213
function saveSettings() {
14+
let validate_max_token = max_tokens;
15+
console.log(max_tokens)
16+
if(max_tokens < MIN_TOKENS && max_tokens !== 0) validate_max_token = MIN_TOKENS;
1317
updateModelSettings({
14-
max_tokens, top_p, temperature
18+
max_tokens: validate_max_token, top_p, temperature
1519
})
1620
updateState()
1721
}
@@ -32,9 +36,9 @@ export default function ModelSettings({ trigger, updateState }) {
3236
<SettingSection title={'General Model Settings'}>
3337
<ScrollBarComponent
3438
title={'Set Max Tokens'}
35-
description={'The max tokens AI can generate'}
39+
description={'The max tokens AI can generate, if set to 0 there will be no limitations.'}
3640
value={max_tokens} cb={setMaxTokens}
37-
max={2048} min={32}
41+
max={2048} min={32} special={0}
3842
/>
3943
<ScrollBarComponent
4044
title={'Set Top P'}

src/components/settings/components/ScrollBarComponent.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { useEffect, useState } from "react"
22

3-
export default function ScrollBarComponent({ cb, value, disabled, title, description, min, max, times_10, step }) {
3+
export default function ScrollBarComponent({ cb, value, disabled, title, description, min, max, times_10, step, special }) {
44

55
const [scrollValue, setScrollValue] = useState((times_10 ? 10 : 1) * value);
66
const [textValue, setTextValue] = useState(value);
77

88
function checkValue(v) {
99
v = v || +textValue;
10-
return v <= max && v >= min;
10+
return (v <= max && v >= min) || v === special;
1111
}
1212

1313
function setValue(value, is_scroll = false) {

src/styles/chat.css

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,21 @@
248248
padding: 10px;
249249
overflow: auto;
250250
}
251-
.chat > .conversation-main > .bubbles > .bubble :not(pre) code {
251+
252+
.chat > .conversation-main > .bubbles > .bubble code {
252253
padding: 0px 5px;
253254
background-color: rgb(227, 227, 227);
254255
border-radius: 5px;
255256
}
256257

258+
.chat > .conversation-main > .bubbles > .bubble.user code {
259+
background-color: rgb(22, 113, 203);
260+
}
261+
262+
.chat > .conversation-main > .bubbles > .bubble pre code {
263+
background-color: unset;
264+
}
265+
257266
@keyframes dotAnimation {
258267
0% { color: rgb(90, 90, 90); }
259268
50% { color: rgb(150, 150, 150); }
@@ -290,7 +299,7 @@ input[type="text"] {
290299
height: 100%;
291300
position: relative;
292301
border: none;
293-
padding: 0px var(--elem-size) 0px 10px;
302+
padding: 0px calc(var(--elem-size) + 5px) 0px 10px;
294303
}
295304

296305
.chat > .conversation-main > .send-message-form > .input-container >

src/utils/general_settings.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const MODEL_SETTINGS_KEY = 'general-model-settings'
3434
* @property {Number} temperature
3535
*/
3636
const DEFAULT_MODEL_SETTINGS = {
37-
max_tokens: 128,
37+
max_tokens: 1024,
3838
top_p: 0.9,
3939
temperature: 0.7
4040
}

src/utils/types.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ export const LOAD_FINISHED = 1;
66
export const LOAD_SET_SETTINGS = 2;
77
export const LOAD_SKIP_SETTINGS = 3;
88

9-
export const DEFAULT_LLAMA_CPP_MODEL_URL = "https://huggingface.co/aisuko/Phi-3-mini-4k-instruct-gguf/resolve/main/Phi3-mini-4k-instruct-Q4.gguf"
9+
export const DEFAULT_LLAMA_CPP_MODEL_URL = "https://huggingface.co/aisuko/Phi-3-mini-4k-instruct-gguf/resolve/main/Phi3-mini-4k-instruct-Q4.gguf"
10+
11+
export const MIN_TOKENS = 32;

src/utils/workers/aws-worker.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,19 @@ export async function chatCompletions(messages, cb = null) {
125125
}
126126
})
127127

128-
const { max_tokens:maxTokens, top_p:topP, temperature } = getModelSettings();
128+
const { max_tokens, top_p:topP, temperature } = getModelSettings();
129129
const input = {
130130
modelId: aws_model_id,
131131
messages: normal_messages,
132132
inferenceConfig: {
133-
maxTokens, temperature, topP
133+
temperature, topP
134134
}
135135
}
136136

137+
if(max_tokens) {
138+
input.inferenceConfig.maxTokens = max_tokens
139+
}
140+
137141
if(system.length) input.system = system;
138142
let response_text = '', usage = {}
139143

@@ -195,9 +199,9 @@ export async function formator(messages, files = []) {
195199
if(files.length) {
196200
for(const file of files) {
197201
const file_info = file.name.split('.')
198-
const extension = file_info.pop();
202+
const extension = file_info.pop().toLowerCase();
199203
const filename = file_info.join('_');
200-
const bytes = await file.arrayBuffer()
204+
const bytes = new Uint8Array(await file.arrayBuffer())
201205

202206
if(/^image\/.+/.test(file.type)) {
203207
common_messages[common_messages.length - 1].content.push(
@@ -209,11 +213,12 @@ export async function formator(messages, files = []) {
209213
}
210214
)
211215
} else {
216+
const is_valid_format = /^(docx|csv|html|txt|pdf|md|doc|xlsx|xls)$/.test(extension)
212217
common_messages[common_messages.length - 1].content.push(
213218
{
214219
document: {
215-
name: filename,
216-
format: extension,
220+
name: filename + (is_valid_format ? '' : `_${extension}`),
221+
format: is_valid_format ? extension : 'txt' ,
217222
source: { bytes }
218223
}
219224
}

src/utils/workers/wllama-worker.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@ export function loadModelSamplingSettings() {
3535
n_threads: wllama_threads,
3636
n_batch: wllama_batch_size,
3737
n_ctx: wllama_context_length,
38-
nPredict: max_tokens,
3938
temp: temperature,
4039
top_p
4140
}
41+
42+
if(max_tokens) model_sampling_settings.nPredict = max_tokens;
4243
}
4344
loadModelSamplingSettings();
4445

0 commit comments

Comments
 (0)