Skip to content

Commit 5f23d7d

Browse files
authored
Merge pull request #5 from utopia-php/make-timeouts-configurable
chore: update timeouts
2 parents ef0d2cf + 89301a1 commit 5f23d7d

File tree

6 files changed

+65
-10
lines changed

6 files changed

+65
-10
lines changed

src/Agents/Adapter.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ abstract class Adapter
2525
*/
2626
protected int $outputTokens = 0;
2727

28+
/**
29+
* Request timeout in seconds
30+
*
31+
* @var int
32+
*/
33+
protected int $timeout = 90;
34+
2835
/**
2936
* Get the adapter name
3037
*
@@ -145,4 +152,27 @@ public function getTotalTokens(): int
145152
{
146153
return $this->inputTokens + $this->outputTokens;
147154
}
155+
156+
/**
157+
* Set timeout in seconds
158+
*
159+
* @param int $timeout
160+
* @return self
161+
*/
162+
public function setTimeout(int $timeout): self
163+
{
164+
$this->timeout = $timeout;
165+
166+
return $this;
167+
}
168+
169+
/**
170+
* Get timeout in seconds
171+
*
172+
* @return int
173+
*/
174+
public function getTimeout(): int
175+
{
176+
return $this->timeout;
177+
}
148178
}

src/Agents/Adapters/Anthropic.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,21 @@ class Anthropic extends Adapter
5757
* @param string $model
5858
* @param int $maxTokens
5959
* @param float $temperature
60+
* @param int $timeout
6061
*
6162
* @throws \Exception
6263
*/
6364
public function __construct(
6465
string $apiKey,
6566
string $model = self::MODEL_CLAUDE_3_SONNET,
6667
int $maxTokens = 1024,
67-
float $temperature = 1.0
68+
float $temperature = 1.0,
69+
int $timeout = 90
6870
) {
6971
$this->apiKey = $apiKey;
7072
$this->maxTokens = $maxTokens;
7173
$this->temperature = $temperature;
74+
$this->timeout = $timeout;
7275
$this->setModel($model);
7376
}
7477

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

9093
$client = new Client();
9194
$client
92-
->setTimeout(90)
95+
->setTimeout($this->timeout)
9396
->addHeader('x-api-key', $this->apiKey)
9497
->addHeader('anthropic-version', '2023-06-01')
9598
->addHeader('content-type', Client::CONTENT_TYPE_APPLICATION_JSON);

src/Agents/Adapters/Deepseek.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,33 @@ class Deepseek extends Adapter
4040
*/
4141
protected float $temperature;
4242

43+
/**
44+
* @var int
45+
*/
46+
protected int $timeout;
47+
4348
/**
4449
* Create a new Deepseek adapter
4550
*
4651
* @param string $apiKey
4752
* @param string $model
4853
* @param int $maxTokens
4954
* @param float $temperature
55+
* @param int $timeout
5056
*
5157
* @throws \Exception
5258
*/
5359
public function __construct(
5460
string $apiKey,
5561
string $model = self::MODEL_DEEPSEEK_CHAT,
5662
int $maxTokens = 1024,
57-
float $temperature = 1.0
63+
float $temperature = 1.0,
64+
int $timeout = 90
5865
) {
5966
$this->apiKey = $apiKey;
6067
$this->maxTokens = $maxTokens;
6168
$this->temperature = $temperature;
69+
$this->timeout = $timeout;
6270
$this->setModel($model);
6371
}
6472

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

8088
$client = new Client();
8189
$client
82-
->setTimeout(90)
90+
->setTimeout($this->timeout)
8391
->addHeader('authorization', 'Bearer '.$this->apiKey)
8492
->addHeader('content-type', Client::CONTENT_TYPE_APPLICATION_JSON);
8593

src/Agents/Adapters/OpenAI.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ class OpenAI extends Adapter
5555
*/
5656
protected string $endpoint;
5757

58+
/**
59+
* @var int
60+
*/
61+
protected int $timeout;
62+
5863
/**
5964
* Create a new OpenAI adapter
6065
*
@@ -63,6 +68,7 @@ class OpenAI extends Adapter
6368
* @param int $maxTokens
6469
* @param float $temperature
6570
* @param string|null $endpoint
71+
* @param int $timeout
6672
*
6773
* @throws \Exception
6874
*/
@@ -71,12 +77,14 @@ public function __construct(
7177
string $model = self::MODEL_GPT_3_5_TURBO,
7278
int $maxTokens = 1024,
7379
float $temperature = 1.0,
74-
?string $endpoint = null
80+
?string $endpoint = null,
81+
int $timeout = 90
7582
) {
7683
$this->apiKey = $apiKey;
7784
$this->maxTokens = $maxTokens;
7885
$this->temperature = $temperature;
7986
$this->endpoint = $endpoint ?? self::ENDPOINT;
87+
$this->timeout = $timeout;
8088
$this->setModel($model);
8189
}
8290

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

98106
$client = new Client();
99107
$client
100-
->setTimeout(90)
108+
->setTimeout($this->timeout)
101109
->addHeader('authorization', 'Bearer '.$this->apiKey)
102110
->addHeader('content-type', Client::CONTENT_TYPE_APPLICATION_JSON);
103111

src/Agents/Adapters/Perplexity.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class Perplexity extends OpenAI
4242
* @param int $maxTokens
4343
* @param float $temperature
4444
* @param string|null $endpoint
45+
* @param int $timeout
4546
*
4647
* @throws \Exception
4748
*/
@@ -50,14 +51,16 @@ public function __construct(
5051
string $model = self::MODEL_SONAR,
5152
int $maxTokens = 1024,
5253
float $temperature = 1.0,
53-
?string $endpoint = null
54+
?string $endpoint = null,
55+
int $timeout = 90
5456
) {
5557
parent::__construct(
5658
$apiKey,
5759
$model,
5860
$maxTokens,
5961
$temperature,
60-
$endpoint ?? self::ENDPOINT
62+
$endpoint ?? self::ENDPOINT,
63+
$timeout
6164
);
6265
}
6366

src/Agents/Adapters/XAI.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class XAI extends OpenAI
2727
* @param int $maxTokens
2828
* @param float $temperature
2929
* @param string|null $endpoint
30+
* @param int $timeout
3031
*
3132
* @throws \Exception
3233
*/
@@ -35,14 +36,16 @@ public function __construct(
3536
string $model = self::MODEL_GROK_2_LATEST,
3637
int $maxTokens = 1024,
3738
float $temperature = 1.0,
38-
?string $endpoint = null
39+
?string $endpoint = null,
40+
int $timeout = 90
3941
) {
4042
parent::__construct(
4143
$apiKey,
4244
$model,
4345
$maxTokens,
4446
$temperature,
45-
$endpoint ?? self::ENDPOINT
47+
$endpoint ?? self::ENDPOINT,
48+
$timeout
4649
);
4750
}
4851

0 commit comments

Comments
 (0)