Skip to content

Commit

Permalink
Merge pull request #7 from MaestroError/dynamic-model-support
Browse files Browse the repository at this point in the history
Dynamic model setting for agents
  • Loading branch information
MaestroError authored Mar 2, 2025
2 parents e324eb8 + 8f78232 commit 8d75060
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 9 deletions.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ protected $temperature;
protected $message;
```

The agent also provides two core methods that you can override:
The agent also provides three core methods that you can override:

```php
/**
Expand All @@ -343,6 +343,15 @@ public function prompt(string $message)
{
return $message;
}

/**
* Decide which model to use dynamically with custom logic
* Or use property $model to statically set the model
*/
public function model()
{
return $this->model;
}
```

Example:
Expand Down Expand Up @@ -410,6 +419,12 @@ public function message(string $message);
*/
public function withImages(array $imageUrls);

/**
* Decide model dynamically in your controller
* @param string $model Model identifier (e.g., 'gpt-4o', 'gpt-3.5-turbo')
*/
public function withModel(string $model);

/**
* Clear the chat history
* This removes all messages from the chat history
Expand Down
31 changes: 27 additions & 4 deletions src/Agent.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class Agent
// Driver configs

/** @var string */
protected $model = 'gpt-4o-mini';
protected $model;

/** @var int */
protected $contextWindowSize;
Expand Down Expand Up @@ -156,6 +156,8 @@ public function respond(?string $message = null): string|array
$this->message($message);
}

$this->setupBeforeRespond();

$this->onConversationStart();

$message = Message::user($this->prompt($this->message));
Expand Down Expand Up @@ -193,6 +195,16 @@ public function instructions()
return $this->instructions;
}

/**
* Get the model for the agent
*
* @return string The agent's model
*/
public function model()
{
return $this->model;
}

/**
* Process a message before sending to the agent
*
Expand Down Expand Up @@ -353,14 +365,21 @@ public function temperature(float $temp): static
return $this;
}

public function withModel(string $model): static
{
$this->model = $model;

return $this;
}

/**
* Convert Agent to DTO
* // @todo mention DTO in the documentation as state for events
*/
public function toDTO(): AgentDTO
{
$driverConfigs = array_filter([
'model' => $this->model,
'model' => $this->model(),
'contextWindowSize' => $this->contextWindowSize ?? null,
'maxCompletionTokens' => $this->maxCompletionTokens ?? null,
'temperature' => $this->temperature ?? null,
Expand Down Expand Up @@ -399,7 +418,7 @@ protected function buildSessionId(string $id)
return sprintf(
'%s_%s_%s',
class_basename(static::class),
$this->model,
$this->model(),
$id
);
}
Expand Down Expand Up @@ -462,7 +481,7 @@ protected function setupAgent(): void
protected function buildConfigsForLaragent()
{
$config = [
'model' => $this->model,
'model' => $this->model(),
];
if (isset($this->maxCompletionTokens)) {
$config['max_completion_tokens'] = $this->maxCompletionTokens;
Expand Down Expand Up @@ -550,6 +569,10 @@ protected function setup(): void
$this->setupProviderData();
$chatHistory = $this->createChatHistory($this->getChatSessionId());
$this->setChatHistory($chatHistory);
}

protected function setupBeforeRespond(): void
{
$this->setupAgent();
$this->registerEvents();
}
Expand Down
9 changes: 5 additions & 4 deletions test-agent.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ enum Unit: string

class WeatherAgent extends LarAgent\Agent
{
protected $model = 'gpt-4o-mini';

protected $provider = 'default';

Expand Down Expand Up @@ -124,7 +123,9 @@ public static function weatherToolForNewYork(Unit $unit)

echo WeatherAgent::for('test_chat')->respond('What\'s the weather like in Boston and Los Angeles? I prefer fahrenheit');
echo "\n---\n";
// Using "celsus" instead of "celsius" to check correct pick of enum value
echo WeatherAgent::for('test_chat')->respond('Thanks for the info. What about New York? I prefer celsus');
// // Using "celsus" instead of "celsius" to check correct pick of enum value
// echo WeatherAgent::for('test_chat')->respond('Thanks for the info. What about New York? I prefer celsus');
// echo "\n---\n";
// echo WeatherAgent::for('test_chat')->message('Where am I now?')->respond();
echo "\n---\n";
echo WeatherAgent::for('test_chat')->message('Where am I now?')->respond();
echo WeatherAgent::for('test_chat')->model();
16 changes: 16 additions & 0 deletions tests/AgentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,19 @@ protected function afterToolExecution($tool, &$result)
'image_url' => ['url' => 'http://example.com/image2.jpg'],
]);
});

it('can dynamically change model', function () {
$agent = new TestAgent('test_session');

// Check default model
expect($agent->model())->toBe('gpt-4o-mini');

// Change model dynamically
$agent->withModel('gpt-3.5-turbo');

// Verify model was changed
expect($agent->model())->toBe('gpt-3.5-turbo');

// Verify chainable method returns agent instance
expect($agent->withModel('gpt-4'))->toBeInstanceOf(Agent::class);
});

0 comments on commit 8d75060

Please sign in to comment.