Skip to content

Commit

Permalink
Added currentDate variable to Dynamic Prompt (#1648)
Browse files Browse the repository at this point in the history
* Added ability to include the current date in the assistant dynamic prompt

* feat: rename tag to {{today}}

---------

Co-authored-by: Nathan Sarrazin <[email protected]>
  • Loading branch information
evalstate and nsarrazin authored Jan 16, 2025
1 parent 03a28b2 commit b493e99
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/lib/components/AssistantSettings.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
: false;
let tools = assistant?.tools ?? [];
const regex = /{{\s?(get|post|url)=(.*?)\s?}}/g;
const regex = /{{\s?(get|post|url|today)(=.*?)?\s?}}/g;
$: templateVariables = [...systemPrompt.matchAll(regex)];
$: selectedModel = models.find((m) => m.id === modelId);
Expand Down Expand Up @@ -565,7 +565,8 @@
<p class="mb-2 text-xs font-normal text-gray-500">
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. You can also send the user's message as the body of a POST request, using {"{{post=https://example.com/path}}"}
inference. You can also send the user's message as the body of a POST request, using {"{{post=https://example.com/path}}"}.
Use {"{{today}}"} to include the current date.
</p>
</label>

Expand Down
8 changes: 8 additions & 0 deletions src/lib/server/textGeneration/assistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ import type { Assistant } from "$lib/types/Assistant";
import type { ObjectId } from "mongodb";

export async function processPreprompt(preprompt: string, user_message: string | undefined) {
// Replace {{today}} with formatted date
const today = new Intl.DateTimeFormat("en-US", {
weekday: "long",
day: "numeric",
month: "long",
year: "numeric",
}).format(new Date());
preprompt = preprompt.replaceAll("{{today}}", today);
const requestRegex = /{{\s?(get|post|url)=(.*?)\s?}}/g;

for (const match of preprompt.matchAll(requestRegex)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@
>
{#if assistant?.dynamicPrompt}
{#each prepromptTags as tag}
{#if tag.startsWith("{{") && tag.endsWith("}}") && (tag.includes("get=") || tag.includes("post=") || tag.includes("url="))}
{#if (tag.startsWith("{{") && tag.endsWith("}}") && (tag.includes("get=") || tag.includes("post=") || tag.includes("url="))) || tag.includes("today")}
{@const url = tag.match(/(?:get|post|url)=(.*?)}}/)?.[1] ?? ""}
<a
target="_blank"
Expand Down

0 comments on commit b493e99

Please sign in to comment.