Skip to content
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
30 changes: 30 additions & 0 deletions src/Agents/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ abstract class Adapter
*/
protected int $outputTokens = 0;

/**
* Request timeout in seconds
*
* @var int
*/
protected int $timeout = 90;

/**
* Get the adapter name
*
Expand Down Expand Up @@ -145,4 +152,27 @@ public function getTotalTokens(): int
{
return $this->inputTokens + $this->outputTokens;
}

/**
* Set timeout in seconds
*
* @param int $timeout
* @return self
*/
public function setTimeout(int $timeout): self
{
$this->timeout = $timeout;

return $this;
}

/**
* Get timeout in seconds
*
* @return int
*/
public function getTimeout(): int
{
return $this->timeout;
}
}
7 changes: 5 additions & 2 deletions src/Agents/Adapters/Anthropic.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,21 @@ class Anthropic extends Adapter
* @param string $model
* @param int $maxTokens
* @param float $temperature
* @param int $timeout
*
* @throws \Exception
*/
public function __construct(
string $apiKey,
string $model = self::MODEL_CLAUDE_3_SONNET,
int $maxTokens = 1024,
float $temperature = 1.0
float $temperature = 1.0,
int $timeout = 90
) {
$this->apiKey = $apiKey;
$this->maxTokens = $maxTokens;
$this->temperature = $temperature;
$this->timeout = $timeout;
$this->setModel($model);
}

Expand All @@ -89,7 +92,7 @@ public function send(array $messages, ?callable $listener = null): Message

$client = new Client();
$client
->setTimeout(90)
->setTimeout($this->timeout)
->addHeader('x-api-key', $this->apiKey)
->addHeader('anthropic-version', '2023-06-01')
->addHeader('content-type', Client::CONTENT_TYPE_APPLICATION_JSON);
Expand Down
12 changes: 10 additions & 2 deletions src/Agents/Adapters/Deepseek.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,33 @@ class Deepseek extends Adapter
*/
protected float $temperature;

/**
* @var int
*/
protected int $timeout;

/**
* Create a new Deepseek adapter
*
* @param string $apiKey
* @param string $model
* @param int $maxTokens
* @param float $temperature
* @param int $timeout
*
* @throws \Exception
*/
public function __construct(
string $apiKey,
string $model = self::MODEL_DEEPSEEK_CHAT,
int $maxTokens = 1024,
float $temperature = 1.0
float $temperature = 1.0,
int $timeout = 90
) {
$this->apiKey = $apiKey;
$this->maxTokens = $maxTokens;
$this->temperature = $temperature;
$this->timeout = $timeout;
$this->setModel($model);
}

Expand All @@ -79,7 +87,7 @@ public function send(array $messages, ?callable $listener = null): Message

$client = new Client();
$client
->setTimeout(90)
->setTimeout($this->timeout)
->addHeader('authorization', 'Bearer '.$this->apiKey)
->addHeader('content-type', Client::CONTENT_TYPE_APPLICATION_JSON);

Expand Down
12 changes: 10 additions & 2 deletions src/Agents/Adapters/OpenAI.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ class OpenAI extends Adapter
*/
protected string $endpoint;

/**
* @var int
*/
protected int $timeout;

/**
* Create a new OpenAI adapter
*
Expand All @@ -63,6 +68,7 @@ class OpenAI extends Adapter
* @param int $maxTokens
* @param float $temperature
* @param string|null $endpoint
* @param int $timeout
*
* @throws \Exception
*/
Expand All @@ -71,12 +77,14 @@ public function __construct(
string $model = self::MODEL_GPT_3_5_TURBO,
int $maxTokens = 1024,
float $temperature = 1.0,
?string $endpoint = null
?string $endpoint = null,
int $timeout = 90
) {
$this->apiKey = $apiKey;
$this->maxTokens = $maxTokens;
$this->temperature = $temperature;
$this->endpoint = $endpoint ?? self::ENDPOINT;
$this->timeout = $timeout;
$this->setModel($model);
}

Expand All @@ -97,7 +105,7 @@ public function send(array $messages, ?callable $listener = null): Message

$client = new Client();
$client
->setTimeout(90)
->setTimeout($this->timeout)
->addHeader('authorization', 'Bearer '.$this->apiKey)
->addHeader('content-type', Client::CONTENT_TYPE_APPLICATION_JSON);

Expand Down
7 changes: 5 additions & 2 deletions src/Agents/Adapters/Perplexity.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Perplexity extends OpenAI
* @param int $maxTokens
* @param float $temperature
* @param string|null $endpoint
* @param int $timeout
*
* @throws \Exception
*/
Expand All @@ -50,14 +51,16 @@ public function __construct(
string $model = self::MODEL_SONAR,
int $maxTokens = 1024,
float $temperature = 1.0,
?string $endpoint = null
?string $endpoint = null,
int $timeout = 90
) {
parent::__construct(
$apiKey,
$model,
$maxTokens,
$temperature,
$endpoint ?? self::ENDPOINT
$endpoint ?? self::ENDPOINT,
$timeout
);
}

Expand Down
7 changes: 5 additions & 2 deletions src/Agents/Adapters/XAI.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class XAI extends OpenAI
* @param int $maxTokens
* @param float $temperature
* @param string|null $endpoint
* @param int $timeout
*
* @throws \Exception
*/
Expand All @@ -35,14 +36,16 @@ public function __construct(
string $model = self::MODEL_GROK_2_LATEST,
int $maxTokens = 1024,
float $temperature = 1.0,
?string $endpoint = null
?string $endpoint = null,
int $timeout = 90
) {
parent::__construct(
$apiKey,
$model,
$maxTokens,
$temperature,
$endpoint ?? self::ENDPOINT
$endpoint ?? self::ENDPOINT,
$timeout
);
}

Expand Down