Skip to content

Commit

Permalink
use separate session to process string array items
Browse files Browse the repository at this point in the history
  • Loading branch information
t83714 committed Jan 15, 2025
1 parent c7e6077 commit a5f35b3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/libs/EmbeddingEncoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,23 @@ class EmbeddingEncoder {
async encode(
sentences: string | string[],
model: string = this.defaultModel
) {
if (typeof sentences === "string") {
sentences = [sentences];
}
let tokenSize = 0;
const embeddings: number[][] = [];
for (let i = 0; i < sentences.length; i++) {
const output = await this.doEncode(sentences[i], model);
tokenSize += output.tokenSize;
embeddings.push(output.embeddings[0]);
}
return { embeddings, tokenSize };
}

async doEncode(
sentences: string | string[],
model: string = this.defaultModel
) {
const { extraction_config } = this.getModelByName(model);

Expand Down
5 changes: 4 additions & 1 deletion src/routes/v1/embeddings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ const embeddings: FastifyPluginAsync = async (
? request.body.input
: [request.body.input];
if (debugFlag) {
console.log("Received encode request. inputItems: ", inputItems);
console.log(
"Received encode request. inputItems: ",
JSON.stringify(inputItems)
);
}
const results = await this.embeddingEncoderWorker.exec("encode", [
inputItems,
Expand Down

0 comments on commit a5f35b3

Please sign in to comment.