Skip to content

Commit cc5f431

Browse files
committed
ref
1 parent 55d8d64 commit cc5f431

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

examples/ollama/chat-llama-with-cache.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
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();
21+
$platform = PlatformFactory::create('http://localhost:11434', http_client(), cache: new ArrayAdapter());
22+
$model = new Ollama('anna');
2323

2424
$agent = new Agent($platform, $model, logger: logger());
2525
$messages = new MessageBag(
@@ -32,8 +32,14 @@
3232

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

35+
assert($result->getMetadata()->get('cached'));
36+
assert('chat' === $result->getMetadata()->get('prompt_cache_key'));
37+
3538
$secondResult = $agent->call($messages, [
3639
'prompt_cache_key' => 'chat',
3740
]);
3841

39-
echo $result->getContent().\PHP_EOL;
42+
echo $secondResult->getContent().\PHP_EOL;
43+
44+
assert($secondResult->getMetadata()->get('cached'));
45+
assert('chat' === $secondResult->getMetadata()->get('prompt_cache_key'));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ private function doCompletionRequest(array|string $payload, array $options = [])
8282
],
8383
]);
8484

85-
if ($this->cache instanceof CacheInterface && (\array_key_exists('prompt_cache_key', $options) && $options['prompt_cache_key'])) {
85+
if ($this->cache instanceof CacheInterface && (\array_key_exists('prompt_cache_key', $options) && '' !== $options['prompt_cache_key'])) {
8686
$cacheKey = \sprintf('%s_%s', $options['prompt_cache_key'], md5(\is_array($payload) ? json_encode($payload) : ['context' => $payload]));
8787

8888
unset($options['prompt_cache_key']);

src/platform/src/Bridge/Ollama/OllamaResultConverter.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ public function convert(RawResultInterface $result, array $options = []): Result
4747

4848
return \array_key_exists('embeddings', $data)
4949
? $this->doConvertEmbeddings($data)
50-
: $this->doConvertCompletion($data);
50+
: $this->doConvertCompletion($data, $options);
5151
}
5252

5353
/**
5454
* @param array<string, mixed> $data
5555
*/
56-
public function doConvertCompletion(array $data): ResultInterface
56+
public function doConvertCompletion(array $data, array $options): ResultInterface
5757
{
5858
if (!isset($data['message'])) {
5959
throw new RuntimeException('Response does not contain message.');
@@ -73,7 +73,16 @@ public function doConvertCompletion(array $data): ResultInterface
7373
return new ToolCallResult(...$toolCalls);
7474
}
7575

76-
return new TextResult($data['message']['content']);
76+
$result = new TextResult($data['message']['content']);
77+
78+
if (\array_key_exists('prompt_cache_key', $options)) {
79+
$metadata = $result->getMetadata();
80+
81+
$metadata->add('cached', true);
82+
$metadata->add('prompt_cache_key', $options['prompt_cache_key']);
83+
}
84+
85+
return $result;
7786
}
7887

7988
/**

0 commit comments

Comments
 (0)