-
-
Notifications
You must be signed in to change notification settings - Fork 110
[Platform][Ollama] Add prompt cache #416
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Symfony package. | ||
| * | ||
| * (c) Fabien Potencier <[email protected]> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| use Symfony\AI\Agent\Agent; | ||
| use Symfony\AI\Platform\Bridge\Ollama\PlatformFactory; | ||
| use Symfony\AI\Platform\CachedPlatform; | ||
| use Symfony\AI\Platform\Message\Message; | ||
| use Symfony\AI\Platform\Message\MessageBag; | ||
| use Symfony\Component\Cache\Adapter\ArrayAdapter; | ||
| use Symfony\Component\Cache\Adapter\TagAwareAdapter; | ||
|
|
||
| require_once dirname(__DIR__).'/bootstrap.php'; | ||
|
|
||
| $platform = PlatformFactory::create(env('OLLAMA_HOST_URL'), http_client()); | ||
| $cachedPlatform = new CachedPlatform($platform, new TagAwareAdapter(new ArrayAdapter())); | ||
|
|
||
| $agent = new Agent($cachedPlatform, 'qwen3:0.6b-q4_K_M'); | ||
| $messages = new MessageBag( | ||
| Message::forSystem('You are a helpful assistant.'), | ||
| Message::ofUser('Tina has one brother and one sister. How many sisters do Tina\'s siblings have?'), | ||
| ); | ||
| $result = $agent->call($messages, [ | ||
| 'prompt_cache_key' => 'chat', | ||
| ]); | ||
|
|
||
| assert($result->getMetadata()->has('cached')); | ||
|
|
||
| echo $result->getContent().\PHP_EOL; | ||
|
|
||
| $secondResult = $agent->call($messages, [ | ||
| 'prompt_cache_key' => 'chat', | ||
| ]); | ||
|
|
||
| assert($secondResult->getMetadata()->has('cached')); | ||
|
|
||
| echo $secondResult->getContent().\PHP_EOL; |
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. changes here would also belong into |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,7 +26,7 @@ | |
| /** | ||
| * @author Christopher Hertel <[email protected]> | ||
| */ | ||
| final readonly class OllamaResultConverter implements ResultConverterInterface | ||
| final class OllamaResultConverter implements ResultConverterInterface | ||
| { | ||
| public function supports(Model $model): bool | ||
| { | ||
|
|
@@ -43,13 +43,14 @@ public function convert(RawResultInterface $result, array $options = []): Result | |
|
|
||
| return \array_key_exists('embeddings', $data) | ||
| ? $this->doConvertEmbeddings($data) | ||
| : $this->doConvertCompletion($data); | ||
| : $this->doConvertCompletion($data, $options); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<string, mixed> $data | ||
| * @param array<string, mixed> $options | ||
| */ | ||
| public function doConvertCompletion(array $data): ResultInterface | ||
| public function doConvertCompletion(array $data, array $options): ResultInterface | ||
| { | ||
| if (!isset($data['message'])) { | ||
| throw new RuntimeException('Response does not contain message.'); | ||
|
|
@@ -69,7 +70,19 @@ public function doConvertCompletion(array $data): ResultInterface | |
| return new ToolCallResult(...$toolCalls); | ||
| } | ||
|
|
||
| return new TextResult($data['message']['content']); | ||
| $result = new TextResult($data['message']['content']); | ||
|
|
||
| if (\array_key_exists('prompt_cache_key', $options)) { | ||
| $metadata = $result->getMetadata(); | ||
|
|
||
| $metadata->add('cached', true); | ||
| $metadata->add('prompt_cache_key', $options['prompt_cache_key']); | ||
| $metadata->add('cached_prompt_count', $data['prompt_eval_count']); | ||
| $metadata->add('cached_completion_count', $data['eval_count']); | ||
|
Comment on lines
+78
to
+81
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't it make sense to group this data into a DTO, like there is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not convinced about the benefits of using an object here, we're only storing an integer, I don't see the benefits to be honest 🤔 @OskarStark @chr-hertel Any thoughts? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i agree, it would be great to have an object like |
||
| $metadata->add('cached_time', (new \DateTimeImmutable())->getTimestamp()); | ||
| } | ||
|
|
||
| return $result; | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Symfony package. | ||
| * | ||
| * (c) Fabien Potencier <[email protected]> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Symfony\AI\Platform; | ||
|
|
||
| use Symfony\AI\Platform\ModelCatalog\ModelCatalogInterface; | ||
| use Symfony\AI\Platform\Result\DeferredResult; | ||
| use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface; | ||
| use Symfony\Contracts\Cache\CacheInterface; | ||
| use Symfony\Contracts\Cache\ItemInterface; | ||
|
|
||
| /** | ||
| * @author Guillaume Loulier <[email protected]> | ||
| */ | ||
| final class CachedPlatform implements PlatformInterface | ||
| { | ||
| public function __construct( | ||
| private readonly PlatformInterface $platform, | ||
| private readonly (CacheInterface&TagAwareAdapterInterface)|null $cache = null, | ||
| private readonly ?string $cacheKey = null, | ||
| ) { | ||
| } | ||
|
|
||
| public function invoke(string $model, array|string|object $input, array $options = []): DeferredResult | ||
| { | ||
| $invokeCall = fn (string $model, array|string|object $input, array $options = []): DeferredResult => $this->platform->invoke($model, $input, $options); | ||
|
|
||
| if ($this->cache instanceof CacheInterface && (\array_key_exists('prompt_cache_key', $options) && '' !== $options['prompt_cache_key'])) { | ||
| $cacheKey = \sprintf('%s_%s', $this->cacheKey ?? $options['prompt_cache_key'], md5($model)); | ||
|
|
||
| unset($options['prompt_cache_key']); | ||
|
|
||
| return $this->cache->get($cacheKey, static function (ItemInterface $item) use ($invokeCall, $model, $input, $options, $cacheKey): DeferredResult { | ||
| $item->tag($model); | ||
|
|
||
| $result = $invokeCall($model, $input, $options); | ||
|
|
||
| $result = new DeferredResult( | ||
| $result->getResultConverter(), | ||
| $result->getRawResult(), | ||
| $options, | ||
| ); | ||
|
|
||
| $result->getMetadata()->set([ | ||
| 'cached' => true, | ||
| 'cache_key' => $cacheKey, | ||
| 'cached_at' => (new \DateTimeImmutable())->getTimestamp(), | ||
| ]); | ||
|
|
||
| return $result; | ||
| }); | ||
| } | ||
|
|
||
| return $invokeCall($model, $input, $options); | ||
| } | ||
|
|
||
| public function getModelCatalog(): ModelCatalogInterface | ||
| { | ||
| return $this->platform->getModelCatalog(); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might end with the same cache repeated again and again in every platform.
Should we introduce a cache config key at a higher level ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good question, IMHO, we should introduce a "root" key for it and allowing the override "per platform", @OskarStark @chr-hertel, any idea?