|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Lugha package. |
| 5 | + * |
| 6 | + * (c) Bernard Ngandu <[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 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace Devscast\Lugha\Model\Completion; |
| 15 | + |
| 16 | +use Devscast\Lugha\Exception\ServiceIntegrationException; |
| 17 | +use Devscast\Lugha\Model\Completion\Chat\History; |
| 18 | +use Devscast\Lugha\Model\Completion\Chat\Message; |
| 19 | +use Devscast\Lugha\Model\Completion\Chat\Role; |
| 20 | +use Devscast\Lugha\Model\Completion\Prompt\PromptTemplate; |
| 21 | +use Devscast\Lugha\Provider\Service\HasCompletionSupport; |
| 22 | +use Devscast\Lugha\Retrieval\VectorStore\VectorStoreInterface; |
| 23 | + |
| 24 | +/** |
| 25 | + * Class RetrievalAugmentedChatter. |
| 26 | + * |
| 27 | + * @author bernard-ng <[email protected]> |
| 28 | + */ |
| 29 | +final readonly class RetrievalAugmentedChat implements RetrievalAugmentedInterface |
| 30 | +{ |
| 31 | + public function __construct( |
| 32 | + private HasCompletionSupport $client, |
| 33 | + private CompletionConfig $config, |
| 34 | + private VectorStoreInterface $vectorStore, |
| 35 | + ) { |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @throws ServiceIntegrationException |
| 40 | + */ |
| 41 | + #[\Override] |
| 42 | + public function augmentedCompletion(string $query, PromptTemplate $prompt): string |
| 43 | + { |
| 44 | + $prompt->setParameter(':CONTEXT', $this->createContext($query)); |
| 45 | + |
| 46 | + $history = History::fromMessages([ |
| 47 | + new Message((string) $prompt, Role::SYSTEM), |
| 48 | + new Message($query, Role::USER), |
| 49 | + ]); |
| 50 | + |
| 51 | + return $this->client->completion($history, $this->config)->completion; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @throws ServiceIntegrationException |
| 56 | + */ |
| 57 | + #[\Override] |
| 58 | + public function augmentedCompletionWithHistory(string $query, PromptTemplate $prompt, History $history): string |
| 59 | + { |
| 60 | + $prompt->setParameter(':CONTEXT', $this->createContext($query)); |
| 61 | + |
| 62 | + // TODO: not sure if a chat history can have multiple system messages |
| 63 | + // TODO: further investigation needed |
| 64 | + $history->append(new Message((string) $prompt, Role::SYSTEM)); |
| 65 | + $history->append(new Message($query, Role::USER)); |
| 66 | + |
| 67 | + return $this->client->completion($history, $this->config)->completion; |
| 68 | + } |
| 69 | + |
| 70 | + private function createContext(string $query): string |
| 71 | + { |
| 72 | + $documents = $this->vectorStore->similaritySearch( |
| 73 | + query: $query, |
| 74 | + k: $this->config->similarityK, |
| 75 | + distance: $this->config->similarityDistance |
| 76 | + ); |
| 77 | + |
| 78 | + $context = ''; |
| 79 | + foreach ($documents as $document) { |
| 80 | + $context .= $document->content . "\n"; |
| 81 | + } |
| 82 | + |
| 83 | + return $context; |
| 84 | + } |
| 85 | +} |
0 commit comments