Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added currentDate variable to Dynamic Prompt #1648

Merged
merged 3 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading