Add support for custom embedding URL in PlaygroundEmbedding.svelte and HypaProcesser

This commit is contained in:
sub-hub
2024-04-27 18:21:44 +09:00
parent 091cf4f35c
commit e5a88c4ecb
2 changed files with 42 additions and 11 deletions

View File

@@ -8,6 +8,7 @@
let query = "";
let model = "MiniLM";
let customEmbeddingUrl = "";
let data:string[] = [];
let dataresult:[string, number][] = [];
let running = false;
@@ -15,7 +16,7 @@
const run = async () => {
if(running) return;
running = true;
const processer = new HypaProcesser(model as any);
const processer = new HypaProcesser(model as any, customEmbeddingUrl);
await processer.addText(data);
console.log(processer.vectors)
dataresult = await processer.similaritySearchScored(query);
@@ -29,8 +30,14 @@
<SelectInput bind:value={model}>
<OptionInput value="MiniLM">MiniLM L6 v2</OptionInput>
<OptionInput value="nomic">Nomic Embed Text v1.5</OptionInput>
<OptionInput value="custom">Custom (OpenAI-compatible)</OptionInput>
</SelectInput>
{#if model === "custom"}
<span class="text-textcolor text-lg">Custom Server URL</span>
<TextInput bind:value={customEmbeddingUrl} size="lg" fullwidth />
{/if}
<span class="text-textcolor text-lg">Query</span>
<TextInput bind:value={query} size="lg" fullwidth />

View File

@@ -1,20 +1,23 @@
import localforage from "localforage";
import { globalFetch } from "src/ts/storage/globalApi";
import { runEmbedding } from "../transformers";
import { alertError } from "src/ts/alert";
export class HypaProcesser{
oaikey:string
vectors:memoryVector[]
forage:LocalForage
model:'ada'|'MiniLM'|'nomic'
model:'ada'|'MiniLM'|'nomic'|'custom'
customEmbeddingUrl:string
constructor(model:'ada'|'MiniLM'|'nomic'){
constructor(model:'ada'|'MiniLM'|'nomic'|'custom',customEmbeddingUrl?:string){
this.forage = localforage.createInstance({
name: "hypaVector"
})
this.vectors = []
this.model = model
this.customEmbeddingUrl = customEmbeddingUrl
}
async embedDocuments(texts: string[]): Promise<VectorArray[]> {
@@ -40,15 +43,36 @@ export class HypaProcesser{
let results:Float32Array[] = await runEmbedding(inputs, this.model === 'nomic' ? 'nomic-ai/nomic-embed-text-v1.5' : 'Xenova/all-MiniLM-L6-v2')
return results
}
const gf = await globalFetch("https://api.openai.com/v1/embeddings", {
headers: {
"Authorization": "Bearer " + this.oaikey
},
body: {
"input": input,
"model": "text-embedding-ada-002"
let gf = null;
if(this.model === 'custom'){
if(!this.customEmbeddingUrl){
alertError('Custom model requires a Custom Server URL')
return [0]
}
})
let replaceUrl = new URL(this.customEmbeddingUrl)
if(replaceUrl.pathname !== '/embeddings'){
replaceUrl.pathname = '/embeddings'
}
gf = await globalFetch(replaceUrl.toString(), {
body:{
"input": input
},
})
}
if(this.model === 'ada'){
gf = await globalFetch("https://api.openai.com/v1/embeddings", {
headers: {
"Authorization": "Bearer " + this.oaikey
},
body: {
"input": input,
"model": "text-embedding-ada-002"
}
})
}
const data = gf.data