Spaces:
Runtime error
Runtime error
add option to send user message as part of request body in assistant … (#1322)
Browse files* add option to send user message as part of request body in assistant invokations
* test
* finish
* fix linting problem
* fix: add support for legacy url param
* fix: remove code to send POST requests on the frontend
---------
Co-authored-by: Ethan Yu <etyu@tesla.com>
Co-authored-by: Nathan Sarrazin <sarrazin.nathan@gmail.com>
src/lib/components/AssistantSettings.svelte
CHANGED
|
@@ -96,9 +96,9 @@
|
|
| 96 |
: false;
|
| 97 |
|
| 98 |
let tools = assistant?.tools ?? [];
|
| 99 |
-
const regex = /{{\s?url=(
|
| 100 |
|
| 101 |
-
$: templateVariables = [...systemPrompt.matchAll(regex)]
|
| 102 |
$: selectedModel = models.find((m) => m.id === modelId);
|
| 103 |
</script>
|
| 104 |
|
|
@@ -542,12 +542,18 @@
|
|
| 542 |
<div
|
| 543 |
class="invisible absolute right-0 top-6 z-10 rounded-lg border bg-white p-2 text-xs shadow-lg peer-focus:visible hover:visible sm:w-96"
|
| 544 |
>
|
| 545 |
-
Will perform a GET request and inject the response into the prompt. Works
|
| 546 |
-
plain text, csv or json content.
|
| 547 |
{#each templateVariables as match}
|
| 548 |
-
<
|
| 549 |
-
|
| 550 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 551 |
{/each}
|
| 552 |
</div>
|
| 553 |
</div>
|
|
@@ -557,9 +563,9 @@
|
|
| 557 |
<input type="checkbox" name="dynamicPrompt" bind:checked={dynamicPrompt} />
|
| 558 |
Dynamic Prompt
|
| 559 |
<p class="mb-2 text-xs font-normal text-gray-500">
|
| 560 |
-
Allow the use of template variables {"{{
|
| 561 |
to insert dynamic content into your prompt by making GET requests to specified URLs on each
|
| 562 |
-
inference.
|
| 563 |
</p>
|
| 564 |
</label>
|
| 565 |
|
|
|
|
| 96 |
: false;
|
| 97 |
|
| 98 |
let tools = assistant?.tools ?? [];
|
| 99 |
+
const regex = /{{\s?(get|post|url)=(.*?)\s?}}/g;
|
| 100 |
|
| 101 |
+
$: templateVariables = [...systemPrompt.matchAll(regex)];
|
| 102 |
$: selectedModel = models.find((m) => m.id === modelId);
|
| 103 |
</script>
|
| 104 |
|
|
|
|
| 542 |
<div
|
| 543 |
class="invisible absolute right-0 top-6 z-10 rounded-lg border bg-white p-2 text-xs shadow-lg peer-focus:visible hover:visible sm:w-96"
|
| 544 |
>
|
| 545 |
+
Will perform a GET or POST request and inject the response into the prompt. Works
|
| 546 |
+
better with plain text, csv or json content.
|
| 547 |
{#each templateVariables as match}
|
| 548 |
+
<div>
|
| 549 |
+
<a
|
| 550 |
+
href={match[1].toLowerCase() === "get" ? match[2] : "#"}
|
| 551 |
+
target={match[1].toLowerCase() === "get" ? "_blank" : ""}
|
| 552 |
+
class="text-gray-500 underline decoration-gray-300"
|
| 553 |
+
>
|
| 554 |
+
{match[1].toUpperCase()}: {match[2]}
|
| 555 |
+
</a>
|
| 556 |
+
</div>
|
| 557 |
{/each}
|
| 558 |
</div>
|
| 559 |
</div>
|
|
|
|
| 563 |
<input type="checkbox" name="dynamicPrompt" bind:checked={dynamicPrompt} />
|
| 564 |
Dynamic Prompt
|
| 565 |
<p class="mb-2 text-xs font-normal text-gray-500">
|
| 566 |
+
Allow the use of template variables {"{{get=https://example.com/path}}"}
|
| 567 |
to insert dynamic content into your prompt by making GET requests to specified URLs on each
|
| 568 |
+
inference. You can also send the user's message as the body of a POST request, using {"{{post=https://example.com/path}}"}
|
| 569 |
</p>
|
| 570 |
</label>
|
| 571 |
|
src/lib/server/textGeneration/assistant.ts
CHANGED
|
@@ -4,17 +4,32 @@ import { collections } from "$lib/server/database";
|
|
| 4 |
import type { Assistant } from "$lib/types/Assistant";
|
| 5 |
import type { ObjectId } from "mongodb";
|
| 6 |
|
| 7 |
-
export async function processPreprompt(preprompt: string) {
|
| 8 |
-
const
|
| 9 |
|
| 10 |
-
for (const match of preprompt.matchAll(
|
|
|
|
|
|
|
| 11 |
try {
|
| 12 |
-
const url = new URL(
|
| 13 |
if ((await isURLLocal(url)) && env.ENABLE_LOCAL_FETCH !== "true") {
|
| 14 |
throw new Error("URL couldn't be fetched, it resolved to a local address.");
|
| 15 |
}
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
if (!res.ok) {
|
| 20 |
throw new Error("URL couldn't be fetched, error " + res.status);
|
|
|
|
| 4 |
import type { Assistant } from "$lib/types/Assistant";
|
| 5 |
import type { ObjectId } from "mongodb";
|
| 6 |
|
| 7 |
+
export async function processPreprompt(preprompt: string, user_message: string | undefined) {
|
| 8 |
+
const requestRegex = /{{\s?(get|post|url)=(.*?)\s?}}/g;
|
| 9 |
|
| 10 |
+
for (const match of preprompt.matchAll(requestRegex)) {
|
| 11 |
+
const method = match[1].toUpperCase();
|
| 12 |
+
const urlString = match[2];
|
| 13 |
try {
|
| 14 |
+
const url = new URL(urlString);
|
| 15 |
if ((await isURLLocal(url)) && env.ENABLE_LOCAL_FETCH !== "true") {
|
| 16 |
throw new Error("URL couldn't be fetched, it resolved to a local address.");
|
| 17 |
}
|
| 18 |
|
| 19 |
+
let res;
|
| 20 |
+
if (method == "POST") {
|
| 21 |
+
res = await fetch(url.href, {
|
| 22 |
+
method: "POST",
|
| 23 |
+
body: user_message,
|
| 24 |
+
headers: {
|
| 25 |
+
"Content-Type": "text/plain",
|
| 26 |
+
},
|
| 27 |
+
});
|
| 28 |
+
} else if (method == "GET" || method == "URL") {
|
| 29 |
+
res = await fetch(url.href);
|
| 30 |
+
} else {
|
| 31 |
+
throw new Error("Invalid method " + method);
|
| 32 |
+
}
|
| 33 |
|
| 34 |
if (!res.ok) {
|
| 35 |
throw new Error("URL couldn't be fetched, error " + res.status);
|
src/lib/server/textGeneration/index.ts
CHANGED
|
@@ -68,7 +68,7 @@ async function* textGenerationWithoutTitle(
|
|
| 68 |
|
| 69 |
let preprompt = conv.preprompt;
|
| 70 |
if (assistantHasDynamicPrompt(assistant) && preprompt) {
|
| 71 |
-
preprompt = await processPreprompt(preprompt);
|
| 72 |
if (messages[0].from === "system") messages[0].content = preprompt;
|
| 73 |
}
|
| 74 |
|
|
|
|
| 68 |
|
| 69 |
let preprompt = conv.preprompt;
|
| 70 |
if (assistantHasDynamicPrompt(assistant) && preprompt) {
|
| 71 |
+
preprompt = await processPreprompt(preprompt, messages.at(-1)?.content);
|
| 72 |
if (messages[0].from === "system") messages[0].content = preprompt;
|
| 73 |
}
|
| 74 |
|
src/routes/settings/(nav)/assistants/[assistantId]/+page.svelte
CHANGED
|
@@ -259,8 +259,8 @@
|
|
| 259 |
>
|
| 260 |
{#if assistant?.dynamicPrompt}
|
| 261 |
{#each prepromptTags as tag}
|
| 262 |
-
{#if tag.startsWith("{{") && tag.endsWith("}}") && tag.includes("url=")}
|
| 263 |
-
{@const url = tag.
|
| 264 |
<a
|
| 265 |
target="_blank"
|
| 266 |
href={url.startsWith("http") ? url : `//${url}`}
|
|
|
|
| 259 |
>
|
| 260 |
{#if assistant?.dynamicPrompt}
|
| 261 |
{#each prepromptTags as tag}
|
| 262 |
+
{#if tag.startsWith("{{") && tag.endsWith("}}") && (tag.includes("get=") || tag.includes("post=") || tag.includes("url="))}
|
| 263 |
+
{@const url = tag.match(/(?:get|post|url)=(.*?)}}/)?.[1] ?? ""}
|
| 264 |
<a
|
| 265 |
target="_blank"
|
| 266 |
href={url.startsWith("http") ? url : `//${url}`}
|