θΏ™ζ˜―indexlocζδΎ›ηš„ζœεŠ‘οΌŒδΈθ¦θΎ“ε…₯任何密码
Skip to content

Fix embedding endpoint for OpenAI compatible API #3467

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions server/endpoints/api/openai/compatibility-test-script.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
22 changes: 16 additions & 6 deletions server/endpoints/api/openai/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
Expand All @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion server/swagger/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -3541,7 +3541,7 @@
"content": {
"application/json": {
"example": {
"inputs": [
"input": [
"This is my first string to embed",
"This is my second string to embed"
],
Expand Down