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

Better options handling #41

Merged
merged 3 commits into from
Sep 25, 2024
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
8 changes: 6 additions & 2 deletions examples/chat-gpt-openai.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@
require_once dirname(__DIR__).'/vendor/autoload.php';

$platform = new OpenAI(HttpClient::create(), getenv('OPENAI_API_KEY'));
$llm = new Gpt($platform, Version::gpt4oMini());
$llm = new Gpt($platform, Version::gpt4oMini(), [
'temperature' => 0.5, // default options for the model
]);

$chain = new Chain($llm);
$messages = new MessageBag(
Message::forSystem('You are a pirate and you write funny.'),
Message::ofUser('What is the Symfony framework?'),
);
$response = $chain->call($messages);
$response = $chain->call($messages, [
'max_tokens' => 500, // specific options just for this call
]);

echo $response.PHP_EOL;
18 changes: 11 additions & 7 deletions src/Anthropic/Model/Claude.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,31 @@

final class Claude implements LanguageModel
{
/**
* @param array<mixed> $options The default options for the model usage
*/
public function __construct(
private readonly ClaudePlatform $platform,
private ?Version $version = null,
private readonly float $temperature = 1.0,
private readonly int $maxTokens = 1000,
private readonly array $options = ['temperature' => 1.0, 'max_tokens' => 1000],
) {
$this->version ??= Version::sonnet35();
}

/**
* @param array<mixed> $options The options to be used for this specific call.
* Can overwrite default options.
*/
public function call(MessageBag $messages, array $options = []): Response
{
$system = $messages->getSystemMessage();
$body = [
$body = array_merge($this->options, $options, [
'model' => $this->version->name,
'temperature' => $this->temperature,
'max_tokens' => $this->maxTokens,
'system' => $system->content,
'messages' => $messages->withoutSystemMessage(),
];
]);

$response = $this->platform->request(array_merge($body, $options));
$response = $this->platform->request($body);

return new Response(new Choice($response['content'][0]['text']));
}
Expand Down
16 changes: 11 additions & 5 deletions src/OpenAI/Model/Gpt.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,29 @@

final class Gpt implements LanguageModel
{
/**
* @param array<mixed> $options The default options for the model usage
*/
public function __construct(
private readonly Platform $platform,
private ?Version $version = null,
private readonly float $temperature = 1.0,
private readonly array $options = ['temperature' => 1.0],
) {
$this->version ??= Version::gpt4o();
}

/**
* @param array<mixed> $options The options to be used for this specific call.
* Can overwrite default options.
*/
public function call(MessageBag $messages, array $options = []): Response
{
$body = [
$body = array_merge($this->options, $options, [
'model' => $this->version->name,
'temperature' => $this->temperature,
'messages' => $messages,
];
]);

$response = $this->platform->request('chat/completions', array_merge($body, $options));
$response = $this->platform->request('chat/completions', $body);

return new Response(...array_map([$this, 'convertChoice'], $response['choices']));
}
Expand Down