@lobehub/chat
Version:
Lobe Chat - an open-source, high-performance chatbot framework that supports speech synthesis, multimodal, and extensible Function Call plugin system. Supports one-click free deployment of your private ChatGPT/LLM web application.
18 lines (13 loc) • 609 B
text/typescript
import { get } from 'lodash-es';
export const hydrationPrompt = (prompt: string, context: any) => {
const regex = /{{([\S\s]+?)}}/g;
// Use String.prototype.replace with a replacer function
return prompt.replaceAll(regex, (match, key) => {
const trimmedKey = key.trim();
// Safely get the value from the context, including nested paths
const value = get(context, trimmedKey);
// If the value exists (is not undefined), convert it to string and return.
// Otherwise, return an empty string to replace the placeholder.
return value !== undefined ? String(value) : '';
});
};