diff --git a/src/lib/components/AssistantSettings.svelte b/src/lib/components/AssistantSettings.svelte index 6c1d2728550..9c13dfcea4a 100644 --- a/src/lib/components/AssistantSettings.svelte +++ b/src/lib/components/AssistantSettings.svelte @@ -96,9 +96,9 @@ : false; let tools = assistant?.tools ?? []; - const regex = /{{\s?url=(.+?)\s?}}/g; + const regex = /{{\s?(get|post|url)=(.*?)\s?}}/g; - $: templateVariables = [...systemPrompt.matchAll(regex)].map((match) => match[1]); + $: templateVariables = [...systemPrompt.matchAll(regex)]; $: selectedModel = models.find((m) => m.id === modelId); @@ -542,12 +542,18 @@
- Allow the use of template variables {"{{url=https://example.com/path}}"} + Allow the use of template variables {"{{get=https://example.com/path}}"} to insert dynamic content into your prompt by making GET requests to specified URLs on each - inference. + inference. You can also send the user's message as the body of a POST request, using {"{{post=https://example.com/path}}"}
diff --git a/src/lib/server/textGeneration/assistant.ts b/src/lib/server/textGeneration/assistant.ts index 3a4c1c07504..b8ab43c60f8 100644 --- a/src/lib/server/textGeneration/assistant.ts +++ b/src/lib/server/textGeneration/assistant.ts @@ -4,17 +4,32 @@ import { collections } from "$lib/server/database"; import type { Assistant } from "$lib/types/Assistant"; import type { ObjectId } from "mongodb"; -export async function processPreprompt(preprompt: string) { - const urlRegex = /{{\s?url=(.*?)\s?}}/g; +export async function processPreprompt(preprompt: string, user_message: string | undefined) { + const requestRegex = /{{\s?(get|post|url)=(.*?)\s?}}/g; - for (const match of preprompt.matchAll(urlRegex)) { + for (const match of preprompt.matchAll(requestRegex)) { + const method = match[1].toUpperCase(); + const urlString = match[2]; try { - const url = new URL(match[1]); + const url = new URL(urlString); 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 (method == "POST") { + res = await fetch(url.href, { + method: "POST", + body: user_message, + headers: { + "Content-Type": "text/plain", + }, + }); + } else if (method == "GET" || method == "URL") { + res = await fetch(url.href); + } else { + throw new Error("Invalid method " + method); + } if (!res.ok) { throw new Error("URL couldn't be fetched, error " + res.status); diff --git a/src/lib/server/textGeneration/index.ts b/src/lib/server/textGeneration/index.ts index b5c9bc3ecf9..bef84a283f3 100644 --- a/src/lib/server/textGeneration/index.ts +++ b/src/lib/server/textGeneration/index.ts @@ -68,7 +68,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; } diff --git a/src/routes/settings/(nav)/assistants/[assistantId]/+page.svelte b/src/routes/settings/(nav)/assistants/[assistantId]/+page.svelte index 57e276a63e7..35a6551cad1 100644 --- a/src/routes/settings/(nav)/assistants/[assistantId]/+page.svelte +++ b/src/routes/settings/(nav)/assistants/[assistantId]/+page.svelte @@ -259,8 +259,8 @@ > {#if assistant?.dynamicPrompt} {#each prepromptTags as tag} - {#if tag.startsWith("{{") && tag.endsWith("}}") && tag.includes("url=")} - {@const url = tag.split("url=")[1].split("}}")[0]} + {#if tag.startsWith("{{") && tag.endsWith("}}") && (tag.includes("get=") || tag.includes("post=") || tag.includes("url="))} + {@const url = tag.match(/(?:get|post|url)=(.*?)}}/)?.[1] ?? ""}