Support attachments in developer API (#2373)

* support attachments in developer api

* lint

---------

Co-authored-by: Timothy Carambat <rambat1010@gmail.com>
This commit is contained in:
Sean Hatfield 2024-09-25 13:44:26 -07:00 committed by GitHub
parent e2195a96d1
commit e6c4eb3f1c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 52 additions and 6 deletions

View File

@ -546,7 +546,14 @@ function apiWorkspaceEndpoints(app) {
example: { example: {
message: "What is AnythingLLM?", message: "What is AnythingLLM?",
mode: "query | chat", mode: "query | chat",
sessionId: "identifier-to-partition-chats-by-external-id" sessionId: "identifier-to-partition-chats-by-external-id",
attachments: [
{
name: "image.png",
mime: "image/png",
contentString: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..."
}
]
} }
} }
} }
@ -576,7 +583,12 @@ function apiWorkspaceEndpoints(app) {
*/ */
try { try {
const { slug } = request.params; const { slug } = request.params;
const { message, mode = "query", sessionId = null } = reqBody(request); const {
message,
mode = "query",
sessionId = null,
attachments = [],
} = reqBody(request);
const workspace = await Workspace.get({ slug: String(slug) }); const workspace = await Workspace.get({ slug: String(slug) });
if (!workspace) { if (!workspace) {
@ -612,6 +624,7 @@ function apiWorkspaceEndpoints(app) {
user: null, user: null,
thread: null, thread: null,
sessionId: !!sessionId ? String(sessionId) : null, sessionId: !!sessionId ? String(sessionId) : null,
attachments,
}); });
await Telemetry.sendTelemetry("sent_chat", { await Telemetry.sendTelemetry("sent_chat", {
@ -655,7 +668,14 @@ function apiWorkspaceEndpoints(app) {
example: { example: {
message: "What is AnythingLLM?", message: "What is AnythingLLM?",
mode: "query | chat", mode: "query | chat",
sessionId: "identifier-to-partition-chats-by-external-id" sessionId: "identifier-to-partition-chats-by-external-id",
attachments: [
{
name: "image.png",
mime: "image/png",
contentString: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..."
}
]
} }
} }
} }
@ -706,7 +726,12 @@ function apiWorkspaceEndpoints(app) {
*/ */
try { try {
const { slug } = request.params; const { slug } = request.params;
const { message, mode = "query", sessionId = null } = reqBody(request); const {
message,
mode = "query",
sessionId = null,
attachments = [],
} = reqBody(request);
const workspace = await Workspace.get({ slug: String(slug) }); const workspace = await Workspace.get({ slug: String(slug) });
if (!workspace) { if (!workspace) {
@ -749,6 +774,7 @@ function apiWorkspaceEndpoints(app) {
user: null, user: null,
thread: null, thread: null,
sessionId: !!sessionId ? String(sessionId) : null, sessionId: !!sessionId ? String(sessionId) : null,
attachments,
}); });
await Telemetry.sendTelemetry("sent_chat", { await Telemetry.sendTelemetry("sent_chat", {
LLMSelection: LLMSelection:

View File

@ -1958,7 +1958,14 @@
"example": { "example": {
"message": "What is AnythingLLM?", "message": "What is AnythingLLM?",
"mode": "query | chat", "mode": "query | chat",
"sessionId": "identifier-to-partition-chats-by-external-id" "sessionId": "identifier-to-partition-chats-by-external-id",
"attachments": [
{
"name": "image.png",
"mime": "image/png",
"contentString": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..."
}
]
} }
} }
} }
@ -2053,7 +2060,14 @@
"example": { "example": {
"message": "What is AnythingLLM?", "message": "What is AnythingLLM?",
"mode": "query | chat", "mode": "query | chat",
"sessionId": "identifier-to-partition-chats-by-external-id" "sessionId": "identifier-to-partition-chats-by-external-id",
"attachments": [
{
"name": "image.png",
"mime": "image/png",
"contentString": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..."
}
]
} }
} }
} }

View File

@ -29,6 +29,7 @@ const { Telemetry } = require("../../models/telemetry");
* user: import("@prisma/client").users|null, * user: import("@prisma/client").users|null,
* thread: import("@prisma/client").workspace_threads|null, * thread: import("@prisma/client").workspace_threads|null,
* sessionId: string|null, * sessionId: string|null,
* attachments: { name: string; mime: string; contentString: string }[],
* }} parameters * }} parameters
* @returns {Promise<ResponseObject>} * @returns {Promise<ResponseObject>}
*/ */
@ -39,6 +40,7 @@ async function chatSync({
user = null, user = null,
thread = null, thread = null,
sessionId = null, sessionId = null,
attachments = [],
}) { }) {
const uuid = uuidv4(); const uuid = uuidv4();
const chatMode = mode ?? "chat"; const chatMode = mode ?? "chat";
@ -251,6 +253,7 @@ async function chatSync({
userPrompt: message, userPrompt: message,
contextTexts, contextTexts,
chatHistory, chatHistory,
attachments,
}, },
rawHistory rawHistory
); );
@ -301,6 +304,7 @@ async function chatSync({
* user: import("@prisma/client").users|null, * user: import("@prisma/client").users|null,
* thread: import("@prisma/client").workspace_threads|null, * thread: import("@prisma/client").workspace_threads|null,
* sessionId: string|null, * sessionId: string|null,
* attachments: { name: string; mime: string; contentString: string }[],
* }} parameters * }} parameters
* @returns {Promise<VoidFunction>} * @returns {Promise<VoidFunction>}
*/ */
@ -312,6 +316,7 @@ async function streamChat({
user = null, user = null,
thread = null, thread = null,
sessionId = null, sessionId = null,
attachments = [],
}) { }) {
const uuid = uuidv4(); const uuid = uuidv4();
const chatMode = mode ?? "chat"; const chatMode = mode ?? "chat";
@ -536,6 +541,7 @@ async function streamChat({
userPrompt: message, userPrompt: message,
contextTexts, contextTexts,
chatHistory, chatHistory,
attachments,
}, },
rawHistory rawHistory
); );