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:
parent
f6239a39f8
commit
e01c0dff04
@ -55,6 +55,21 @@ const client = new OpenAI({
|
|||||||
console.log({ message });
|
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
|
// Vector DB functionality
|
||||||
console.log("Fetching /vector_stores");
|
console.log("Fetching /vector_stores");
|
||||||
const vectorDBList = await client.beta.vectorStores.list();
|
const vectorDBList = await client.beta.vectorStores.list();
|
||||||
|
|||||||
@ -207,7 +207,7 @@ function apiOpenAICompatibleEndpoints(app) {
|
|||||||
content: {
|
content: {
|
||||||
"application/json": {
|
"application/json": {
|
||||||
example: {
|
example: {
|
||||||
inputs: [
|
input: [
|
||||||
"This is my first string to embed",
|
"This is my first string to embed",
|
||||||
"This is my second string to embed",
|
"This is my second string to embed",
|
||||||
],
|
],
|
||||||
@ -223,13 +223,23 @@ function apiOpenAICompatibleEndpoints(app) {
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
try {
|
try {
|
||||||
const { inputs = [] } = reqBody(request);
|
const body = reqBody(request);
|
||||||
const validArray = inputs.every((input) => typeof input === "string");
|
// Support input or "inputs" (for backwards compatibility) as an array of strings or a single string
|
||||||
if (!validArray)
|
// TODO: "inputs" key support will eventually be fully removed.
|
||||||
throw new Error("All inputs to be embedded must be strings.");
|
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 Embedder = getEmbeddingEngineSelection();
|
||||||
const embeddings = await Embedder.embedChunks(inputs);
|
const embeddings = await Embedder.embedChunks(input);
|
||||||
const data = [];
|
const data = [];
|
||||||
embeddings.forEach((embedding, index) => {
|
embeddings.forEach((embedding, index) => {
|
||||||
data.push({
|
data.push({
|
||||||
|
|||||||
@ -3541,7 +3541,7 @@
|
|||||||
"content": {
|
"content": {
|
||||||
"application/json": {
|
"application/json": {
|
||||||
"example": {
|
"example": {
|
||||||
"inputs": [
|
"input": [
|
||||||
"This is my first string to embed",
|
"This is my first string to embed",
|
||||||
"This is my second string to embed"
|
"This is my second string to embed"
|
||||||
],
|
],
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user