Fix embedding endpoint for OpenAI compatible API (#3467)

* fix embedding endpoint for openai compatible api

* remove unused imports

* add backwards compat for embedding

---------

Co-authored-by: timothycarambat <rambat1010@gmail.com>
This commit is contained in:
Sean Hatfield 2025-03-17 18:10:24 -07:00 committed by GitHub
parent f6239a39f8
commit e01c0dff04
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 32 additions and 7 deletions

View File

@ -55,6 +55,21 @@ const client = new OpenAI({
console.log({ message });
}
// Test embeddings creation
console.log("Creating embeddings");
const embedding = await client.embeddings.create({
model: null, // model is optional for AnythingLLM
input: "This is a test string for embedding",
encoding_format: "float",
});
console.log("Embedding created successfully:");
console.log(`Dimensions: ${embedding.data[0].embedding.length}`);
console.log(
`First few values:`,
embedding.data[0].embedding.slice(0, 5),
`+ ${embedding.data[0].embedding.length - 5} more`
);
// Vector DB functionality
console.log("Fetching /vector_stores");
const vectorDBList = await client.beta.vectorStores.list();

View File

@ -207,7 +207,7 @@ function apiOpenAICompatibleEndpoints(app) {
content: {
"application/json": {
example: {
inputs: [
input: [
"This is my first string to embed",
"This is my second string to embed",
],
@ -223,13 +223,23 @@ function apiOpenAICompatibleEndpoints(app) {
}
*/
try {
const { inputs = [] } = reqBody(request);
const validArray = inputs.every((input) => typeof input === "string");
if (!validArray)
throw new Error("All inputs to be embedded must be strings.");
const body = reqBody(request);
// Support input or "inputs" (for backwards compatibility) as an array of strings or a single string
// TODO: "inputs" key support will eventually be fully removed.
let input = body?.input || body?.inputs || [];
// if input is not an array, make it an array and force to string content
if (!Array.isArray(input)) input = [String(input)];
if (Array.isArray(input)) {
if (input.length === 0)
throw new Error("Input array cannot be empty.");
const validArray = input.every((text) => typeof text === "string");
if (!validArray)
throw new Error("All inputs to be embedded must be strings.");
}
const Embedder = getEmbeddingEngineSelection();
const embeddings = await Embedder.embedChunks(inputs);
const embeddings = await Embedder.embedChunks(input);
const data = [];
embeddings.forEach((embedding, index) => {
data.push({

View File

@ -3541,7 +3541,7 @@
"content": {
"application/json": {
"example": {
"inputs": [
"input": [
"This is my first string to embed",
"This is my second string to embed"
],