Add support for chatting via the API (#261)

This commit is contained in:
Timothy Carambat 2023-09-29 22:45:35 +02:00 committed by GitHub
parent 62d39eb4fb
commit 3f5b419601
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 160 additions and 1 deletions

View File

@ -3,7 +3,10 @@ const { Telemetry } = require("../../../models/telemetry");
const { DocumentVectors } = require("../../../models/vectors");
const { Workspace } = require("../../../models/workspace");
const { WorkspaceChats } = require("../../../models/workspaceChats");
const { convertToChatHistory } = require("../../../utils/chats");
const {
convertToChatHistory,
chatWithWorkspace,
} = require("../../../utils/chats");
const { getVectorDbClass } = require("../../../utils/helpers");
const { multiUserMode, reqBody } = require("../../../utils/http");
const { validApiKey } = require("../../../utils/middleware/validApiKey");
@ -427,6 +430,78 @@ function apiWorkspaceEndpoints(app) {
}
}
);
app.post(
"/v1/workspace/:slug/chat",
[validApiKey],
async (request, response) => {
/*
#swagger.tags = ['Workspaces']
#swagger.description = 'Execute a chat with a workspace'
#swagger.requestBody = {
description: 'prompt to send to the workspace and the type of conversation (query or chat).',
required: true,
type: 'object',
content: {
"application/json": {
example: {
message: "What is AnythingLLM?",
mode: "query | chat"
}
}
}
}
#swagger.responses[200] = {
content: {
"application/json": {
schema: {
type: 'object',
example: {
id: 'chat-uuid',
type: "abort | textResponse",
textResponse: "Response to your query",
sources: [{title: "anythingllm.txt", chunk: "This is a context chunk used in the answer of the prompt by the LLM,"}],
close: true,
error: "null | text string of the failure mode."
}
}
}
}
}
#swagger.responses[403] = {
schema: {
"$ref": "#/definitions/InvalidAPIKey"
}
}
*/
try {
const { slug } = request.params;
const { message, mode = "query" } = reqBody(request);
const workspace = await Workspace.get({ slug });
if (!workspace) {
response.sendStatus(400).end();
return;
}
const result = await chatWithWorkspace(workspace, message, mode);
await Telemetry.sendTelemetry("sent_chat", {
LLMSelection: process.env.LLM_PROVIDER || "openai",
VectorDbSelection: process.env.VECTOR_DB || "pinecone",
});
response.status(200).json({ ...result });
} catch (e) {
response.status(500).json({
id: uuidv4(),
type: "abort",
textResponse: null,
sources: [],
close: true,
error: e.message,
});
}
}
);
}
module.exports = { apiWorkspaceEndpoints };

View File

@ -1531,6 +1531,90 @@
}
}
},
"/v1/workspace/{slug}/chat": {
"post": {
"tags": [
"Workspaces"
],
"description": "Execute a chat with a workspace",
"parameters": [
{
"name": "slug",
"in": "path",
"required": true,
"schema": {
"type": "string"
}
},
{
"name": "Authorization",
"in": "header",
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"type": "object",
"example": {
"id": "chat-uuid",
"type": "abort | textResponse",
"textResponse": "Response to your query",
"sources": [
{
"title": "anythingllm.txt",
"chunk": "This is a context chunk used in the answer of the prompt by the LLM,"
}
],
"close": true,
"error": "null | text string of the failure mode."
}
}
}
}
},
"400": {
"description": "Bad Request"
},
"403": {
"description": "Forbidden",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/InvalidAPIKey"
}
},
"application/xml": {
"schema": {
"$ref": "#/components/schemas/InvalidAPIKey"
}
}
}
},
"500": {
"description": "Internal Server Error"
}
},
"requestBody": {
"description": "prompt to send to the workspace and the type of conversation (query or chat).",
"required": true,
"type": "object",
"content": {
"application/json": {
"example": {
"message": "What is AnythingLLM?",
"mode": "query | chat"
}
}
}
}
}
},
"/v1/system/env-dump": {
"get": {
"tags": [