Thomas G. Lopes
commited on
Commit
·
0ff4ef8
1
Parent(s):
39318e7
add fuzzy searching
Browse files- src/lib/components/InferencePlayground/InferencePlaygroundModelSelectorModal.svelte +3 -6
- src/lib/components/InferencePlayground/InferencePlaygroundProviderSelect.svelte +0 -1
- src/lib/components/InferencePlayground/inferencePlaygroundUtils.ts +2 -3
- src/lib/utils/model.ts +1 -1
- src/lib/utils/search.ts +64 -0
src/lib/components/InferencePlayground/InferencePlaygroundModelSelectorModal.svelte
CHANGED
@@ -7,6 +7,7 @@
|
|
7 |
import IconSearch from "../Icons/IconSearch.svelte";
|
8 |
import IconStar from "../Icons/IconStar.svelte";
|
9 |
import { getTrending } from "$lib/utils/model";
|
|
|
10 |
|
11 |
export let conversation: Conversation;
|
12 |
|
@@ -20,12 +21,8 @@
|
|
20 |
|
21 |
$: trendingModels = getTrending($models);
|
22 |
|
23 |
-
$: featuredModels =
|
24 |
-
|
25 |
-
});
|
26 |
-
$: otherModels = $models.filter(m => {
|
27 |
-
return m.id.toLocaleLowerCase().includes(query.toLocaleLowerCase().trim());
|
28 |
-
});
|
29 |
|
30 |
onMount(() => {
|
31 |
if (featuredModels.findIndex(model => model.id === conversation.model.id) !== -1) {
|
|
|
7 |
import IconSearch from "../Icons/IconSearch.svelte";
|
8 |
import IconStar from "../Icons/IconStar.svelte";
|
9 |
import { getTrending } from "$lib/utils/model";
|
10 |
+
import fuzzysearch from "$lib/utils/search";
|
11 |
|
12 |
export let conversation: Conversation;
|
13 |
|
|
|
21 |
|
22 |
$: trendingModels = getTrending($models);
|
23 |
|
24 |
+
$: featuredModels = fuzzysearch({ needle: query, haystack: trendingModels, property: "id" });
|
25 |
+
$: otherModels = fuzzysearch({ needle: query, haystack: $models, property: "id" });
|
|
|
|
|
|
|
|
|
26 |
|
27 |
onMount(() => {
|
28 |
if (featuredModels.findIndex(model => model.id === conversation.model.id) !== -1) {
|
src/lib/components/InferencePlayground/InferencePlaygroundProviderSelect.svelte
CHANGED
@@ -4,7 +4,6 @@
|
|
4 |
import { randomPick } from "$lib/utils/array";
|
5 |
import { cn } from "$lib/utils/cn";
|
6 |
import { createSelect, createSync } from "@melt-ui/svelte";
|
7 |
-
import { onMount } from "svelte";
|
8 |
import IconCaret from "../Icons/IconCaret.svelte";
|
9 |
import IconProvider from "../Icons/IconProvider.svelte";
|
10 |
|
|
|
4 |
import { randomPick } from "$lib/utils/array";
|
5 |
import { cn } from "$lib/utils/cn";
|
6 |
import { createSelect, createSync } from "@melt-ui/svelte";
|
|
|
7 |
import IconCaret from "../Icons/IconCaret.svelte";
|
8 |
import IconProvider from "../Icons/IconProvider.svelte";
|
9 |
|
src/lib/components/InferencePlayground/inferencePlaygroundUtils.ts
CHANGED
@@ -1,9 +1,8 @@
|
|
1 |
-
import { type ChatCompletionOutputMessage } from "@huggingface/tasks";
|
2 |
-
import type { InferenceSnippet, ModelDataMinimal } from "@huggingface/tasks";
|
3 |
import type { Conversation, ModelWithTokenizer } from "$lib/types";
|
|
|
|
|
4 |
|
5 |
import { HfInference, snippets, type InferenceProvider } from "@huggingface/inference";
|
6 |
-
import { keys } from "$lib/utils/object";
|
7 |
|
8 |
export async function handleStreamingResponse(
|
9 |
hf: HfInference,
|
|
|
|
|
|
|
1 |
import type { Conversation, ModelWithTokenizer } from "$lib/types";
|
2 |
+
import type { InferenceSnippet } from "@huggingface/tasks";
|
3 |
+
import { type ChatCompletionOutputMessage } from "@huggingface/tasks";
|
4 |
|
5 |
import { HfInference, snippets, type InferenceProvider } from "@huggingface/inference";
|
|
|
6 |
|
7 |
export async function handleStreamingResponse(
|
8 |
hf: HfInference,
|
src/lib/utils/model.ts
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
import type {
|
2 |
|
3 |
export function getTrending(models: ModelWithTokenizer[], limit = 5) {
|
4 |
return models.toSorted((a, b) => b.trendingScore - a.trendingScore).slice(0, limit);
|
|
|
1 |
+
import type { ModelWithTokenizer } from "$lib/types";
|
2 |
|
3 |
export function getTrending(models: ModelWithTokenizer[], limit = 5) {
|
4 |
return models.toSorted((a, b) => b.trendingScore - a.trendingScore).slice(0, limit);
|
src/lib/utils/search.ts
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* Generic fuzzy search function that searches through arrays and returns matching items
|
3 |
+
*
|
4 |
+
* @param options Configuration object for the fuzzy search
|
5 |
+
* @returns Array of items that match the search criteria
|
6 |
+
*/
|
7 |
+
export default function fuzzysearch<T>(options: {
|
8 |
+
needle: string;
|
9 |
+
haystack: T[];
|
10 |
+
property: keyof T | ((item: T) => string);
|
11 |
+
}): T[] {
|
12 |
+
const { needle, haystack, property } = options;
|
13 |
+
|
14 |
+
if (!Array.isArray(haystack)) {
|
15 |
+
throw new Error("Haystack must be an array");
|
16 |
+
}
|
17 |
+
|
18 |
+
if (!property) {
|
19 |
+
throw new Error("Property selector is required");
|
20 |
+
}
|
21 |
+
|
22 |
+
// Convert needle to lowercase for case-insensitive matching
|
23 |
+
const lowerNeedle = needle.toLowerCase();
|
24 |
+
|
25 |
+
// Filter the haystack to find matching items
|
26 |
+
return haystack.filter(item => {
|
27 |
+
// Extract the string value from the item based on the property selector
|
28 |
+
const value = typeof property === "function" ? property(item) : String(item[property]);
|
29 |
+
|
30 |
+
// Convert to lowercase for case-insensitive matching
|
31 |
+
const lowerValue = value.toLowerCase();
|
32 |
+
|
33 |
+
// Perform the fuzzy search
|
34 |
+
return fuzzyMatchString(lowerNeedle, lowerValue);
|
35 |
+
});
|
36 |
+
}
|
37 |
+
|
38 |
+
/**
|
39 |
+
* Internal helper function that performs the actual fuzzy string matching
|
40 |
+
*/
|
41 |
+
function fuzzyMatchString(needle: string, haystack: string): boolean {
|
42 |
+
const hlen = haystack.length;
|
43 |
+
const nlen = needle.length;
|
44 |
+
|
45 |
+
if (nlen > hlen) {
|
46 |
+
return false;
|
47 |
+
}
|
48 |
+
|
49 |
+
if (nlen === hlen) {
|
50 |
+
return needle === haystack;
|
51 |
+
}
|
52 |
+
|
53 |
+
outer: for (let i = 0, j = 0; i < nlen; i++) {
|
54 |
+
const nch = needle.charCodeAt(i);
|
55 |
+
while (j < hlen) {
|
56 |
+
if (haystack.charCodeAt(j++) === nch) {
|
57 |
+
continue outer;
|
58 |
+
}
|
59 |
+
}
|
60 |
+
return false;
|
61 |
+
}
|
62 |
+
|
63 |
+
return true;
|
64 |
+
}
|