Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ yarn-error.log*
next-env.d.ts

# env
.env
.env
package-lock.json
6 changes: 5 additions & 1 deletion src/components/chat/chat-bottombar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ export default function ChatBottombar({

const [tokenLimit, setTokenLimit] = React.useState<number>(4096);
React.useEffect(() => {
getTokenLimit(basePath).then((limit) => setTokenLimit(limit));
getTokenLimit(basePath)
.then((limit) => setTokenLimit(limit))
.catch((error) => {
console.error("Failed to get token limit:", error);
});
}, [hasMounted]);

const tokenCount = React.useMemo(
Expand Down
11 changes: 9 additions & 2 deletions src/components/chat/chat-topbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,25 @@ export default function ChatTopbar({

const data = await res.json();
// Extract the "name" field from each model object and store them in the state
if (!data.data || !Array.isArray(data.data) || data.data.length === 0) {
throw new Error("No models available from vLLM server");
}
const modelNames = data.data.map((model: any) => model.id);
// save the first and only model in the list as selectedModel in localstorage
setChatOptions({ ...chatOptions, selectedModel: modelNames[0] });
} catch (error) {
setChatOptions({ ...chatOptions, selectedModel: undefined });
toast.error(error as string);
toast.error(error instanceof Error ? error.message : String(error));
}
};

useEffect(() => {
fetchData();
getTokenLimit(basePath).then((limit) => setTokenLimit(limit));
getTokenLimit(basePath)
.then((limit) => setTokenLimit(limit))
.catch((error) => {
console.error("Failed to get token limit:", error);
});
}, [hasMounted]);

if (!hasMounted) {
Expand Down
22 changes: 14 additions & 8 deletions src/lib/token-counter.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import { CoreMessage, Message } from "ai";
import llama3Tokenizer from "llama3-tokenizer-js";

export const getTokenLimit = async (basePath: string) => {
const res = await fetch(basePath + "/api/settings");
const DEFAULT_TOKEN_LIMIT = 4096;

if (!res.ok) {
const errorResponse = await res.json();
const errorMessage = `Connection to vLLM server failed: ${errorResponse.error} [${res.status} ${res.statusText}]`;
throw new Error(errorMessage);
export const getTokenLimit = async (basePath: string): Promise<number> => {
try {
const res = await fetch(basePath + "/api/settings");

if (!res.ok) {
console.error(`Failed to fetch settings: ${res.status} ${res.statusText}`);
return DEFAULT_TOKEN_LIMIT;
}
const data = await res.json();
return data.tokenLimit ?? DEFAULT_TOKEN_LIMIT;
} catch (error) {
console.error("Failed to fetch token limit:", error);
return DEFAULT_TOKEN_LIMIT;
}
const data = await res.json();
return data.tokenLimit;
};

export const encodeChat = (messages: Message[] | CoreMessage[]): number => {
Expand Down
Loading