Skip to content
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

Open
Oskar-V opened this issue Mar 18, 2024 · 2 comments
Open

Add prompt templating functionality #14

Oskar-V opened this issue Mar 18, 2024 · 2 comments

Comments

@Oskar-V
Copy link
Contributor

Oskar-V commented Mar 18, 2024

Function:

Messages could have a built-in content substitution functionality to make it easier to use message templates ie:

// Example 1
$user_message = "Hi my name is Bob"
$template = "User question: [user_message]
Reply with a valid JSON object in with the following format:
{
\"prompt\": \"[user_message]\"
\"answer\": _your_answer_
\"something_else\": _random_number_
}"

// Examle 2
$assistant_data = [
"name" => "Bob",
"age" => "25",
"gender" => "Male"
]
$system_template = "You're a [age] year old human [gender] named [name] and you're talking to another human about..."

Current overwrite example:

public function user(string $content, ?string $name = null, ?array $substitutions = null): self
{
	$message = [
		'role' => 'user',
		'content' => $content,
	];
		# If any substitutions were provided swap them into the user message
	$message = $this->substitute($message, $substitutions);

	$message = $this->addOptionalProperty($message, 'name', $name);
	$this->messages[] = $message;

	return $this;
}

$messages = (new Messages)
	->system($system_message_template, null, $system_substitutions)
	->user($user_message_template, null, $user_substitutions);
@allantatter
Copy link
Contributor

allantatter commented Mar 19, 2024

sprintf function might do the trick for you: https://www.php.net/manual/en/function.sprintf.php

$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!'),
);

@Oskar-V
Copy link
Contributor Author

Oskar-V commented Mar 20, 2024

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.

@allantatter allantatter reopened this Mar 21, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants