Skip to content

Commit

Permalink
add option to send user message as part of request body in assistant … (
Browse files Browse the repository at this point in the history
#1322)

* 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 <[email protected]>
Co-authored-by: Nathan Sarrazin <[email protected]>
  • Loading branch information
3 people authored Jan 2, 2025
1 parent e6dc3de commit 8bae553
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 17 deletions.
24 changes: 15 additions & 9 deletions src/lib/components/AssistantSettings.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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);
</script>

Expand Down Expand Up @@ -542,12 +542,18 @@
<div
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"
>
Will perform a GET request and inject the response into the prompt. Works better with
plain text, csv or json content.
Will perform a GET or POST request and inject the response into the prompt. Works
better with plain text, csv or json content.
{#each templateVariables as match}
<a href={match} target="_blank" class="text-gray-500 underline decoration-gray-300"
>{match}</a
>
<div>
<a
href={match[1].toLowerCase() === "get" ? match[2] : "#"}
target={match[1].toLowerCase() === "get" ? "_blank" : ""}
class="text-gray-500 underline decoration-gray-300"
>
{match[1].toUpperCase()}: {match[2]}
</a>
</div>
{/each}
</div>
</div>
Expand All @@ -557,9 +563,9 @@
<input type="checkbox" name="dynamicPrompt" bind:checked={dynamicPrompt} />
Dynamic Prompt
<p class="mb-2 text-xs font-normal text-gray-500">
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}}"}
</p>
</label>

Expand Down
25 changes: 20 additions & 5 deletions src/lib/server/textGeneration/assistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/server/textGeneration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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] ?? ""}
<a
target="_blank"
href={url.startsWith("http") ? url : `//${url}`}
Expand Down

0 comments on commit 8bae553

Please sign in to comment.