anything-llm/server/endpoints/embed/index.js

102 lines
3.0 KiB
JavaScript
Raw Permalink Normal View History

2024-02-01 20:24:42 +01:00
const { v4: uuidv4 } = require("uuid");
const { reqBody, multiUserMode } = require("../../utils/http");
const { Telemetry } = require("../../models/telemetry");
const { writeResponseChunk } = require("../../utils/chats/stream");
2024-02-01 20:24:42 +01:00
const { streamChatWithForEmbed } = require("../../utils/chats/embed");
const { convertToChatHistory } = require("../../utils/chats");
const { EmbedChats } = require("../../models/embedChats");
const {
validEmbedConfig,
canRespond,
setConnectionMeta,
} = require("../../utils/middleware/embedMiddleware");
2024-02-01 20:24:42 +01:00
function embeddedEndpoints(app) {
if (!app) return;
app.post(
"/embed/:embedId/stream-chat",
[validEmbedConfig, setConnectionMeta, canRespond],
async (request, response) => {
try {
const embed = response.locals.embedConfig;
const {
sessionId,
message,
// optional keys for override of defaults if enabled.
prompt = null,
model = null,
temperature = null,
} = reqBody(request);
2024-02-01 20:24:42 +01:00
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Content-Type", "text/event-stream");
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Connection", "keep-alive");
response.flushHeaders();
2024-02-01 20:24:42 +01:00
await streamChatWithForEmbed(response, embed, message, sessionId, {
prompt,
model,
temperature,
2024-02-01 20:24:42 +01:00
});
await Telemetry.sendTelemetry("embed_sent_chat", {
multiUserMode: multiUserMode(response),
LLMSelection: process.env.LLM_PROVIDER || "openai",
Embedder: process.env.EMBEDDING_ENGINE || "inherit",
VectorDbSelection: process.env.VECTOR_DB || "pinecone",
});
response.end();
} catch (e) {
console.error(e);
writeResponseChunk(response, {
2024-02-01 20:24:42 +01:00
id: uuidv4(),
type: "abort",
textResponse: null,
close: true,
error: e.message,
2024-02-01 20:24:42 +01:00
});
response.end();
2024-02-01 20:24:42 +01:00
}
}
);
2024-02-01 20:24:42 +01:00
app.get(
"/embed/:embedId/:sessionId",
[validEmbedConfig],
async (request, response) => {
try {
const { sessionId } = request.params;
const embed = response.locals.embedConfig;
2024-02-01 20:24:42 +01:00
const history = await EmbedChats.forEmbedByUser(embed.id, sessionId);
response.status(200).json({
history: convertToChatHistory(history),
});
} catch (e) {
console.log(e.message, e);
response.sendStatus(500).end();
}
2024-02-01 20:24:42 +01:00
}
);
2024-02-02 01:20:44 +01:00
app.delete(
"/embed/:embedId/:sessionId",
[validEmbedConfig],
async (request, response) => {
try {
const { sessionId } = request.params;
const embed = response.locals.embedConfig;
await EmbedChats.markHistoryInvalid(embed.id, sessionId);
response.status(200).end();
} catch (e) {
console.log(e.message, e);
response.sendStatus(500).end();
}
}
);
2024-02-01 20:24:42 +01:00
}
module.exports = { embeddedEndpoints };