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

Add vector search API endpoint #2812

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

Closed
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
122 changes: 121 additions & 1 deletion server/endpoints/api/workspace/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { Telemetry } = require("../../../models/telemetry");
const { DocumentVectors } = require("../../../models/vectors");
const { Workspace } = require("../../../models/workspace");
const { WorkspaceChats } = require("../../../models/workspaceChats");
const { getVectorDbClass } = require("../../../utils/helpers");
const { getVectorDbClass, getLLMProvider } = require("../../../utils/helpers");
const { multiUserMode, reqBody } = require("../../../utils/http");
const { validApiKey } = require("../../../utils/middleware/validApiKey");
const { VALID_CHAT_MODE } = require("../../../utils/chats/stream");
Expand Down Expand Up @@ -841,6 +841,126 @@ function apiWorkspaceEndpoints(app) {
}
}
);

app.post(
"/v1/workspace/:slug/vector-search",
[validApiKey],
async (request, response) => {
/*
#swagger.tags = ['Workspaces']
#swagger.description = 'Perform a vector similarity search in a workspace'
#swagger.parameters['slug'] = {
in: 'path',
description: 'Unique slug of workspace to search in',
required: true,
type: 'string'
}
#swagger.requestBody = {
description: 'Query to perform vector search with and optional parameters',
required: true,
content: {
"application/json": {
example: {
query: "What is the meaning of life?",
topN: 4,
scoreThreshold: 0.75
}
}
}
}
#swagger.responses[200] = {
content: {
"application/json": {
schema: {
type: 'object',
example: {
results: [
{
id: "5a6bee0a-306c-47fc-942b-8ab9bf3899c4",
text: "Document chunk content...",
metadata: {
url: "file://document.txt",
title: "document.txt",
author: "no author specified",
description: "no description found",
docSource: "post:123456",
chunkSource: "document.txt",
published: "12/1/2024, 11:39:39 AM",
wordCount: 8,
tokenCount: 9
},
distance: 0.541887640953064,
score: 0.45811235904693604
}
]
}
}
}
}
}
*/
try {
const { slug } = request.params;
const { query, topN, scoreThreshold } = reqBody(request);
const workspace = await Workspace.get({ slug: String(slug) });

if (!workspace) {
response.status(400).json({
message: `Workspace ${slug} is not a valid workspace.`,
});
return;
}

if (!query?.length) {
response.status(400).json({
message: "Query parameter cannot be empty.",
});
return;
}

const VectorDb = getVectorDbClass();
const hasVectorizedSpace = await VectorDb.hasNamespace(workspace.slug);
const embeddingsCount = await VectorDb.namespaceCount(workspace.slug);

if (!hasVectorizedSpace || embeddingsCount === 0) {
response.status(200).json({ results: [] });
return;
}

const LLMConnector = getLLMProvider();
const results = await VectorDb.performSimilaritySearch({
namespace: workspace.slug,
input: query,
LLMConnector,
similarityThreshold: scoreThreshold ?? workspace?.similarityThreshold,
topN: topN ?? workspace?.topN ?? 4,
});

response.status(200).json({
results: results.sources.map((source) => ({
id: source.id,
text: source.text,
metadata: {
url: source.url,
title: source.title,
author: source.docAuthor,
description: source.description,
docSource: source.docSource,
chunkSource: source.chunkSource,
published: source.published,
wordCount: source.wordCount,
tokenCount: source.token_count_estimate
},
distance: source._distance,
score: source.score
})),
});
} catch (e) {
console.error(e.message, e);
response.sendStatus(500).end();
}
}
);
}

module.exports = { apiWorkspaceEndpoints };