feat: normalization embedding;feat: model top_p param config (#3723)

* edit form force close image select

* model config

* feat: normalization embedding

* perf: add share page title force refresh
This commit is contained in:
Archer
2025-02-08 12:16:46 +08:00
committed by GitHub
parent 42b2046f96
commit 51e17a47fa
30 changed files with 388 additions and 94 deletions

View File

@@ -56,7 +56,14 @@ export async function getVectorsByText({ model, input, type }: GetVectorProps) {
const [tokens, vectors] = await Promise.all([
countPromptTokens(input),
Promise.all(res.data.map((item) => unityDimensional(item.embedding)))
Promise.all(
res.data
.map((item) => unityDimensional(item.embedding))
.map((item) => {
if (model.normalization) return normalization(item);
return item;
})
)
]);
return {
@@ -87,3 +94,15 @@ function unityDimensional(vector: number[]) {
return resultVector.concat(zeroVector);
}
// normalization processing
function normalization(vector: number[]) {
if (vector.some((item) => item > 1)) {
// Calculate the Euclidean norm (L2 norm)
const norm = Math.sqrt(vector.reduce((sum, val) => sum + val * val, 0));
// Normalize the vector by dividing each component by the norm
return vector.map((val) => val / norm);
}
return vector;
}