-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Add prompt templating functionality #14
Comments
$systemPrompt = sprintf("Hello
You're a %d year old human %s named %s and you're talking to another human about...", 25, 'female', 'Oskar');
OpenAI::chat()->create(
model: 'gpt-3.5-turbo',
messages: (new Messages)->system($systemPrompt)->user('Hello!'),
); |
The need is to replace concrete key-value pairs in a text, where the key itself exists in the initial template text in random positions which change and can't be known ahead of time. Hence can't use sprintf or other simple text replacements. // Example helper function to solve the problem
public static function substitute(string $message, ?array $substitutions): string
{
if ($substitutions === null) return $message
return str_replace(
array_map(fn ($val) => "[{$val}]", array_keys($substitutions)),
array_map(fn ($val) => is_array($val) ? implode($val) : $val, array_values($substitutions)),
$message
);
} This example is a decoupled version of the substitute function used in the initial issue description Ideally the built in helper would support turning more complex elements into proper text representations then the current implementation which is able to support up to 2d arrays of key-value substitution pairs. |
Function:
Messages could have a built-in content substitution functionality to make it easier to use message templates ie:
Current overwrite example:
The text was updated successfully, but these errors were encountered: