Skip to content

Commit 2b79a41

Browse files
committed
Allow LiteLLM usage with or without api key
1 parent 10a17c7 commit 2b79a41

File tree

4 files changed

+10
-2
lines changed

4 files changed

+10
-2
lines changed

examples/.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ LMSTUDIO_HOST_URL=http://127.0.0.1:1234
114114

115115
# For using LiteLLM
116116
LITELLM_HOST_URL=http://127.0.0.1:4000
117+
LITELLM_API_KEY=sk-1234
117118

118119
# Qdrant (store)
119120
QDRANT_HOST=http://127.0.0.1:6333

examples/litellm/chat.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
require_once dirname(__DIR__).'/bootstrap.php';
1717

18-
$platform = PlatformFactory::create(env('LITELLM_HOST_URL'), http_client());
18+
$platform = PlatformFactory::create(env('LITELLM_HOST_URL'), env('LITELLM_API_KEY'), http_client());
1919

2020
$messages = new MessageBag(
2121
Message::forSystem('You are a pirate and you write funny.'),

src/platform/src/Bridge/LiteLlm/ModelClient.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ final class ModelClient implements ModelClientInterface
2727
public function __construct(
2828
HttpClientInterface $httpClient,
2929
private readonly string $hostUrl,
30+
private readonly ?string $apiKey = null,
3031
) {
3132
$this->httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
3233
}
@@ -38,8 +39,13 @@ public function supports(Model $model): bool
3839

3940
public function request(Model $model, array|string $payload, array $options = []): RawHttpResult
4041
{
42+
$authorizationHeader = null !== $this->apiKey ? ['Authorization' => 'Bearer '.$this->apiKey] : [];
43+
4144
return new RawHttpResult($this->httpClient->request('POST', \sprintf('%s/v1/chat/completions', $this->hostUrl), [
4245
'json' => array_merge($options, $payload),
46+
'headers' => [
47+
...$authorizationHeader,
48+
],
4349
]));
4450
}
4551
}

src/platform/src/Bridge/LiteLlm/PlatformFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class PlatformFactory
2525
{
2626
public static function create(
2727
string $hostUrl = 'http://localhost:4000',
28+
?string $apiKey = null,
2829
?HttpClientInterface $httpClient = null,
2930
ModelCatalogInterface $modelCatalog = new ModelCatalog(),
3031
?Contract $contract = null,
@@ -34,7 +35,7 @@ public static function create(
3435

3536
return new Platform(
3637
[
37-
new ModelClient($httpClient, $hostUrl),
38+
new ModelClient($httpClient, $hostUrl, $apiKey),
3839
],
3940
[
4041
new ResultConverter(),

0 commit comments

Comments
 (0)