diff --git a/server/endpoints/api/openai/compatibility-test-script.cjs b/server/endpoints/api/openai/compatibility-test-script.cjs index 7ae2b2dbf55623d71d1360970355f7559f309a21..96e56b0734aa9ae795b042e5af1c15b26ccb7525 100644 --- a/server/endpoints/api/openai/compatibility-test-script.cjs +++ b/server/endpoints/api/openai/compatibility-test-script.cjs @@ -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(); diff --git a/server/endpoints/api/openai/index.js b/server/endpoints/api/openai/index.js index ffd641b3e5c23a7f053164dbfe4d0833ad0ce41d..e5bd1771b6f9245ef861c421e101d088d1a52b83 100644 --- a/server/endpoints/api/openai/index.js +++ b/server/endpoints/api/openai/index.js @@ -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({ diff --git a/server/swagger/openapi.json b/server/swagger/openapi.json index 5c488c67d4895a74fe5ddb89f2a3f8be157db21b..f848bbe213923f856f3e93ceda8b457c516ec502 100644 --- a/server/swagger/openapi.json +++ b/server/swagger/openapi.json @@ -3541,7 +3541,7 @@ "content": { "application/json": { "example": { - "inputs": [ + "input": [ "This is my first string to embed", "This is my second string to embed" ],