Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(weave): load model default properly #3096

Merged
merged 5 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,68 @@ export const LLM_MAX_TOKENS = {
};

export type LLMMaxTokensKey = keyof typeof LLM_MAX_TOKENS;

export const LLM_MAX_TOKENS_KEYS: LLMMaxTokensKey[] = Object.keys(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export const LLM_MAX_TOKENS_KEYS: LLMMaxTokensKey[] = Object.keys(
export const LLM_MAX_TOKENS_KEYS = Object.keys(

LLM_MAX_TOKENS
) as LLMMaxTokensKey[];

// Helper function to calculate string similarity using Levenshtein distance
const getLevenshteinDistance = (str1: string, str2: string): number => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we are using js-levenshtein in the app, can you use that?

const track = Array(str2.length + 1)
.fill(null)
.map(() => Array(str1.length + 1).fill(null));

for (let i = 0; i <= str1.length; i++) {
track[0][i] = i;
}
for (let j = 0; j <= str2.length; j++) {
track[j][0] = j;
}

for (let j = 1; j <= str2.length; j++) {
for (let i = 1; i <= str1.length; i++) {
const indicator = str1[i - 1] === str2[j - 1] ? 0 : 1;
track[j][i] = Math.min(
track[j][i - 1] + 1, // deletion
track[j - 1][i] + 1, // insertion
track[j - 1][i - 1] + indicator // substitution
);
}
}
return track[str2.length][str1.length];
};

// Main function to find most similar LLM name
export const findMostSimilarLLMName = (
input: string,
llmList: LLMMaxTokensKey[]
): string => {
const normalizedInput = input.toLowerCase().trim();

// If exact match exists, return it
if (llmList.includes(normalizedInput as LLMMaxTokensKey)) {
return normalizedInput;
}

let closestMatch = llmList[0];
let smallestDistance = Infinity;

llmList.forEach(llmName => {
const distance = getLevenshteinDistance(
normalizedInput,
llmName.toLowerCase()
);

if (distance < smallestDistance) {
smallestDistance = distance;
closestMatch = llmName;
}

if (llmName.includes(normalizedInput)) {
closestMatch = llmName;
smallestDistance = 0;
}
});

return closestMatch;
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import {toast} from '@wandb/weave/common/components/elements/Toast';
import {SetStateAction, useCallback, useState} from 'react';

import {LLMMaxTokensKey} from './llmMaxTokens';
import {
findMostSimilarLLMName,
LLM_MAX_TOKENS_KEYS,
LLMMaxTokensKey,
} from './llmMaxTokens';
import {
OptionalTraceCallSchema,
PlaygroundResponseFormats,
Expand Down Expand Up @@ -34,7 +39,7 @@ const DEFAULT_PLAYGROUND_STATE = {
presencePenalty: 0,
// nTimes: 1,
maxTokensLimit: 16384,
model: 'gpt-4o-mini' as LLMMaxTokensKey,
model: 'gpt-4o-mini-2024-07-18' as LLMMaxTokensKey,
};

export const usePlaygroundState = () => {
Expand Down Expand Up @@ -106,6 +111,20 @@ export const usePlaygroundState = () => {
if (inputs.presence_penalty) {
newState.presencePenalty = parseFloat(inputs.presence_penalty);
}
if (inputs.model) {
if (LLM_MAX_TOKENS_KEYS.includes(inputs.model as LLMMaxTokensKey)) {
newState.model = inputs.model as LLMMaxTokensKey;
} else {
const closestModel = findMostSimilarLLMName(
inputs.model,
LLM_MAX_TOKENS_KEYS
);
toast(
`We currently don't support ${inputs.model}, in the playground. We will default to ${closestModel}`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we always want to default to the most similar model? can it ever be a typo? I would expect this case to most likely happen with custom models, which they might not want to have auto-selected.

);
newState.model = closestModel as LLMMaxTokensKey;
}
}
return [newState];
});
},
Expand Down
Loading