From 66488538d56d1a8b2a24ab0f46b00fad054c6f12 Mon Sep 17 00:00:00 2001 From: Ethan Yu Date: Mon, 1 Jul 2024 14:58:18 -0700 Subject: [PATCH 1/6] add option to send user message as part of request body in assistant invokations --- .env | 3 ++- src/lib/server/textGeneration/assistant.ts | 19 +++++++++++++++---- src/lib/server/textGeneration/index.ts | 4 ++-- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/.env b/.env index 93fc4af51bc..6c8812e92b7 100644 --- a/.env +++ b/.env @@ -149,6 +149,7 @@ EXPOSE_API=true ENABLE_ASSISTANTS=false #set to true to enable assistants feature ENABLE_ASSISTANTS_RAG=false # /!\ This will let users specify arbitrary URLs that the server will then request. Make sure you have the proper firewall rules in place. +ENABLE_ASSISTANTS_POST=false #set to true to have assistants send query message in request body REQUIRE_FEATURED_ASSISTANTS=false ENABLE_LOCAL_FETCH=false #set to true to disable the blocklist for local fetches. Only enable this if you have the proper firewall rules to prevent SSRF attacks and understand the implications. ALTERNATIVE_REDIRECT_URLS=`[]` #valide alternative redirect URL for OAuth @@ -162,4 +163,4 @@ ALLOW_INSECURE_COOKIES=false # recommended to keep this to false but set to true METRICS_ENABLED=false METRICS_PORT=5565 LOG_LEVEL=info -BODY_SIZE_LIMIT=15728640 +BODY_SIZE_LIMIT=15728640 \ No newline at end of file diff --git a/src/lib/server/textGeneration/assistant.ts b/src/lib/server/textGeneration/assistant.ts index 4423db5db03..d3b88f2d827 100644 --- a/src/lib/server/textGeneration/assistant.ts +++ b/src/lib/server/textGeneration/assistant.ts @@ -4,7 +4,7 @@ import { collections } from "$lib/server/database"; import type { Assistant } from "$lib/types/Assistant"; import type { ObjectId } from "mongodb"; -export async function processPreprompt(preprompt: string) { +export async function processPreprompt(preprompt: string, user_message: string | undefined) { const urlRegex = /{{\s?url=(.*?)\s?}}/g; for (const match of preprompt.matchAll(urlRegex)) { @@ -13,8 +13,19 @@ export async function processPreprompt(preprompt: string) { if ((await isURLLocal(url)) && env.ENABLE_LOCAL_FETCH !== "true") { throw new Error("URL couldn't be fetched, it resolved to a local address."); } - - const res = await fetch(url.href); + + let res; + if (env.ENABLE_ASSISTANTS_POST === "true") { + res = await fetch(url.href, { + method: 'POST', + body: user_message, + headers: { + 'Content-Type': 'text/plain' + } + }); + } else { + res = await fetch(url.href); + } if (!res.ok) { throw new Error("URL couldn't be fetched, error " + res.status); @@ -50,4 +61,4 @@ export function assistantHasWebSearch(assistant?: Pick | null) export function assistantHasDynamicPrompt(assistant?: Pick) { return env.ENABLE_ASSISTANTS_RAG === "true" && Boolean(assistant?.dynamicPrompt); -} +} \ No newline at end of file diff --git a/src/lib/server/textGeneration/index.ts b/src/lib/server/textGeneration/index.ts index e64c25abe9f..9fc5d83c39e 100644 --- a/src/lib/server/textGeneration/index.ts +++ b/src/lib/server/textGeneration/index.ts @@ -56,7 +56,7 @@ async function* textGenerationWithoutTitle( let preprompt = conv.preprompt; if (assistantHasDynamicPrompt(assistant) && preprompt) { - preprompt = await processPreprompt(preprompt); + preprompt = await processPreprompt(preprompt, messages.at(-1)?.content); if (messages[0].from === "system") messages[0].content = preprompt; } @@ -70,4 +70,4 @@ async function* textGenerationWithoutTitle( const processedMessages = await preprocessMessages(messages, webSearchResult, convId); yield* generate({ ...ctx, messages: processedMessages }, toolResults, preprompt); -} +} \ No newline at end of file From 221b45e3dc99c18be6036458a38fdcc6d66b315a Mon Sep 17 00:00:00 2001 From: Ethan Yu Date: Sun, 22 Dec 2024 16:21:24 -0800 Subject: [PATCH 2/6] test --- .env | 1 - src/lib/components/AssistantSettings.svelte | 10 +++---- src/lib/server/textGeneration/assistant.ts | 26 +++++++++++-------- .../assistants/[assistantId]/+page.svelte | 13 ++++++++-- 4 files changed, 31 insertions(+), 19 deletions(-) diff --git a/.env b/.env index 92dbbc983b1..ef7b6d028a3 100644 --- a/.env +++ b/.env @@ -129,7 +129,6 @@ PUBLIC_APPLE_APP_ID=#1234567890 / Leave empty to disable LLM_SUMMARIZATION=true # generate conversation titles with LLMs ENABLE_ASSISTANTS=false #set to true to enable assistants feature ENABLE_ASSISTANTS_RAG=false # /!\ This will let users specify arbitrary URLs that the server will then request. Make sure you have the proper firewall rules in place. -ENABLE_ASSISTANTS_POST=false #set to true to have assistants send query message in request body REQUIRE_FEATURED_ASSISTANTS=false # require featured assistants to show in the list COMMUNITY_TOOLS=false # set to true to enable community tools EXPOSE_API=true # make the /api routes available diff --git a/src/lib/components/AssistantSettings.svelte b/src/lib/components/AssistantSettings.svelte index 6c1d2728550..66fbc2081a4 100644 --- a/src/lib/components/AssistantSettings.svelte +++ b/src/lib/components/AssistantSettings.svelte @@ -96,7 +96,7 @@ : false; let tools = assistant?.tools ?? []; - const regex = /{{\s?url=(.+?)\s?}}/g; + const regex = /{{\s?(get|post)=(.*?)\s?}}/g; $: templateVariables = [...systemPrompt.matchAll(regex)].map((match) => match[1]); $: selectedModel = models.find((m) => m.id === modelId); @@ -542,8 +542,8 @@ {/if} - + diff --git a/src/routes/settings/(nav)/assistants/[assistantId]/+page.svelte b/src/routes/settings/(nav)/assistants/[assistantId]/+page.svelte index b7caed4c1c2..0b698495e4a 100644 --- a/src/routes/settings/(nav)/assistants/[assistantId]/+page.svelte +++ b/src/routes/settings/(nav)/assistants/[assistantId]/+page.svelte @@ -259,7 +259,7 @@ > {#if assistant?.dynamicPrompt} {#each prepromptTags as tag} - {#if tag.startsWith("{{") && tag.endsWith("}}") && tag.includes("get=")} + {#if tag.startsWith("{{") && tag.endsWith("}}") && (tag.includes("get=") || tag.includes("post="))} {@const url = tag.split("get=")[1].split("}}")[0]} {tag} - {:else if tag.startsWith("{{") && tag.endsWith("}}") && tag.includes("post=")} - {@const url = tag.split("post=")[1].split("}}")[0]} - - {tag} {:else} {tag} {/if} From 59a3c9c90345a4e999c3aaa65e0196e0f48d8be6 Mon Sep 17 00:00:00 2001 From: Ethan Yu Date: Mon, 23 Dec 2024 10:44:02 -0800 Subject: [PATCH 4/6] fix linting problem --- src/lib/components/AssistantSettings.svelte | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/components/AssistantSettings.svelte b/src/lib/components/AssistantSettings.svelte index 686e3cfd9cf..dfd06368d1e 100644 --- a/src/lib/components/AssistantSettings.svelte +++ b/src/lib/components/AssistantSettings.svelte @@ -556,7 +556,7 @@ fetch(match[2], { method: "POST", headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ message: "" }) // Hardcoded dummy body + body: JSON.stringify({ message: "" }), // Hardcoded dummy body }) .then((response) => { if (response.ok) { @@ -576,7 +576,7 @@ {/if} - +