Skip to content

Commit 194f7a4

Browse files
committed
ref
1 parent 2d7297f commit 194f7a4

File tree

6 files changed

+76
-57
lines changed

6 files changed

+76
-57
lines changed

examples/.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ VOYAGE_API_KEY=
1616
REPLICATE_API_KEY=
1717

1818
# For using Ollama
19-
OLLAMA_HOST_URL=http://localhost:11434
19+
OLLAMA_HOST_URL=http://127.0.0.1:11434
2020
OLLAMA_LLM=llama3.2
2121
OLLAMA_EMBEDDINGS=nomic-embed-text
2222

examples/ollama/chat-llama-with-cache.php renamed to examples/misc/ollama-chat-with-cache.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@
1010
*/
1111

1212
use Symfony\AI\Agent\Agent;
13-
use Symfony\AI\Platform\Bridge\Ollama\Ollama;
1413
use Symfony\AI\Platform\Bridge\Ollama\PlatformFactory;
14+
use Symfony\AI\Platform\CachedPlatform;
1515
use Symfony\AI\Platform\Message\Message;
1616
use Symfony\AI\Platform\Message\MessageBag;
1717
use Symfony\Component\Cache\Adapter\ArrayAdapter;
1818

1919
require_once dirname(__DIR__).'/bootstrap.php';
2020

21-
$platform = PlatformFactory::create(env('OLLAMA_HOST_URL'), http_client(), cache: new ArrayAdapter());
22-
$model = new Ollama('llama');
21+
$platform = PlatformFactory::create(env('OLLAMA_HOST_URL'), http_client());
22+
$cachedPlatform = new CachedPlatform($platform, new ArrayAdapter());
2323

24-
$agent = new Agent($platform, $model, logger: logger());
24+
$agent = new Agent($cachedPlatform, 'gemma3n', logger: logger());
2525
$messages = new MessageBag(
2626
Message::forSystem('You are a helpful assistant.'),
2727
Message::ofUser('Tina has one brother and one sister. How many sisters do Tina\'s siblings have?'),
@@ -32,19 +32,8 @@
3232

3333
echo $result->getContent().\PHP_EOL;
3434

35-
assert($result->getMetadata()->get('cached'));
36-
assert('chat' === $result->getMetadata()->get('prompt_cache_key'));
37-
assert(0 !== $result->getMetadata()->get('cached_prompt_count'));
38-
assert(0 !== $result->getMetadata()->get('cached_completion_count'));
39-
4035
$secondResult = $agent->call($messages, [
4136
'prompt_cache_key' => 'chat',
4237
]);
4338

4439
echo $secondResult->getContent().\PHP_EOL;
45-
46-
assert($secondResult->getMetadata()->get('cached'));
47-
assert('chat' === $secondResult->getMetadata()->get('prompt_cache_key'));
48-
assert($result->getMetadata()->get('cached_prompt_count') === $secondResult->getMetadata()->get('cached_prompt_count'));
49-
assert($result->getMetadata()->get('cached_completion_count') === $secondResult->getMetadata()->get('cached_completion_count'));
50-
assert($result->getMetadata()->get('cached_time') === $secondResult->getMetadata()->get('cached_time'));

src/platform/src/Bridge/Ollama/OllamaClient.php

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@
1515
use Symfony\AI\Platform\Model;
1616
use Symfony\AI\Platform\ModelClientInterface;
1717
use Symfony\AI\Platform\Result\RawHttpResult;
18-
use Symfony\Component\HttpClient\Response\MockResponse;
19-
use Symfony\Contracts\Cache\CacheInterface;
2018
use Symfony\Contracts\HttpClient\HttpClientInterface;
21-
use Symfony\Contracts\HttpClient\ResponseInterface;
2219

2320
/**
2421
* @author Christopher Hertel <[email protected]>
@@ -28,7 +25,6 @@
2825
public function __construct(
2926
private HttpClientInterface $httpClient,
3027
private string $hostUrl,
31-
private ?CacheInterface $cache = null,
3228
) {
3329
}
3430

@@ -72,40 +68,10 @@ private function doCompletionRequest(array|string $payload, array $options = [])
7268
unset($options['response_format']);
7369
}
7470

75-
$requestCallback = fn ($options, $payload): ResponseInterface => $this->httpClient->request('POST', \sprintf('%s/api/chat', $this->hostUrl), [
76-
'headers' => [
77-
'Content-Type' => 'application/json',
78-
],
79-
'json' => [
80-
...$options,
81-
...$payload,
82-
],
83-
]);
84-
85-
if ($this->cache instanceof CacheInterface && (\array_key_exists('prompt_cache_key', $options) && '' !== $options['prompt_cache_key'])) {
86-
$cacheKey = \sprintf('%s_%s', $options['prompt_cache_key'], md5(\is_array($payload) ? json_encode($payload) : ['context' => $payload]));
87-
88-
unset($options['prompt_cache_key']);
89-
90-
$cachedResponse = $this->cache->get($cacheKey, static function () use ($requestCallback, $options, $payload): array {
91-
$response = $requestCallback($options, $payload);
92-
93-
return [
94-
'content' => $response->getContent(),
95-
'headers' => $response->getHeaders(),
96-
'http_code' => $response->getStatusCode(),
97-
];
98-
});
99-
100-
$mockedResponse = new MockResponse($cachedResponse['content'], [
101-
'http_code' => $cachedResponse['http_code'],
102-
'response_headers' => $cachedResponse['headers'],
103-
]);
104-
105-
return new RawHttpResult(MockResponse::fromRequest('POST', \sprintf('%s/api/chat', $this->hostUrl), $options, $mockedResponse));
106-
}
107-
108-
return new RawHttpResult($requestCallback($options, $payload));
71+
return new RawHttpResult($this->httpClient->request('POST', \sprintf('%s/api/chat', $this->hostUrl), [
72+
'headers' => ['Content-Type' => 'application/json'],
73+
'json' => array_merge($options, $payload),
74+
]));
10975
}
11076

11177
/**

src/platform/src/Bridge/Ollama/PlatformFactory.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Symfony\AI\Platform\ModelCatalog\ModelCatalogInterface;
1717
use Symfony\AI\Platform\Platform;
1818
use Symfony\Component\HttpClient\EventSourceHttpClient;
19-
use Symfony\Contracts\Cache\CacheInterface;
2019
use Symfony\Contracts\HttpClient\HttpClientInterface;
2120

2221
/**
@@ -29,12 +28,11 @@ public static function create(
2928
?HttpClientInterface $httpClient = null,
3029
ModelCatalogInterface $modelCatalog = new ModelCatalog(),
3130
?Contract $contract = null,
32-
?CacheInterface $cache = null,
3331
): Platform {
3432
$httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
3533

3634
return new Platform(
37-
[new OllamaClient($httpClient, $hostUrl, $cache)],
35+
[new OllamaClient($httpClient, $hostUrl)],
3836
[new OllamaResultConverter()],
3937
$modelCatalog,
4038
$contract ?? OllamaContract::create(),
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\AI\Platform;
13+
14+
use Symfony\AI\Platform\ModelCatalog\ModelCatalogInterface;
15+
use Symfony\AI\Platform\Result\ResultPromise;
16+
use Symfony\Contracts\Cache\CacheInterface;
17+
18+
/**
19+
* @author Guillaume Loulier <[email protected]>
20+
*/
21+
final readonly class CachedPlatform implements PlatformInterface
22+
{
23+
public function __construct(
24+
private PlatformInterface $platform,
25+
private CacheInterface $cache,
26+
) {
27+
}
28+
29+
public function invoke(string $model, object|array|string $input, array $options = []): ResultPromise
30+
{
31+
$invokeCall = fn (string $model, object|array|string $input, array $options = []): ResultPromise => $this->platform->invoke($model, $input, $options);
32+
33+
if ($this->cache instanceof CacheInterface && (\array_key_exists('prompt_cache_key', $options) && '' !== $options['prompt_cache_key'])) {
34+
$cacheKey = \sprintf('%s_%s', $options['prompt_cache_key'], md5($model));
35+
36+
unset($options['prompt_cache_key']);
37+
38+
return $this->cache->get($cacheKey, static fn (): ResultPromise => $invokeCall($model, $input, $options));
39+
}
40+
41+
return $invokeCall($model, $input, $options);
42+
}
43+
44+
public function getModelCatalog(): ModelCatalogInterface
45+
{
46+
return $this->platform->getModelCatalog();
47+
}
48+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\AI\Platform\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
16+
final class CachedPlatformTest extends TestCase
17+
{
18+
}

0 commit comments

Comments
 (0)