BxDolAiAgentRetrievalTest.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * An agent with no knowledge store must not read NeuronAI's embeddings provider.
  4. * A una-proxy store searches the master and does not need a local embedding model.
  5. */
  6. class BxDolAiAgentRetrievalTest extends \PHPUnit\Framework\TestCase
  7. {
  8. public function testAgentWithoutStoreSkipsEmbeddings()
  9. {
  10. $o = new BxDolAiAgent(['id' => 1, 'vector_store_id' => 0]);
  11. $oRetrieval = $o->resolveRetrieval();
  12. $this->assertInstanceOf(BxDolAiRetrievalNone::class, $oRetrieval);
  13. $this->assertSame([], $oRetrieval->retrieve(new NeuronAI\Chat\Messages\UserMessage('hello')));
  14. }
  15. public function testUnaProxyStoreSkipsEmbeddings()
  16. {
  17. $o = new BxDolAiAgent([
  18. 'id' => 1,
  19. 'vector_store_id' => 9,
  20. 'vector_store_type' => 'una-proxy',
  21. ]);
  22. $this->assertInstanceOf(BxDolAiRetrievalUnaProxy::class, $o->resolveRetrieval());
  23. }
  24. public function testStoreWithoutEmbeddingModelIsRejected()
  25. {
  26. $o = new BxDolAiAgent(['id' => 1, 'vector_store_id' => 9]);
  27. try {
  28. $o->resolveRetrieval();
  29. $this->fail('Retrieval must not run without an embedding model');
  30. } catch (Exception $oException) {
  31. $this->assertSame('Vector store 9 has no embedding model', $oException->getMessage());
  32. }
  33. }
  34. public function testConfiguredEmbeddingModelUsesSimilarityRetrieval()
  35. {
  36. $o = new BxDolAiAgent(['id' => 1, 'vector_store_id' => 0]);
  37. $o->setEmbeddingsProvider(new BxDolAiAgentRetrievalTestEmbeddings());
  38. $this->assertInstanceOf(
  39. NeuronAI\RAG\Retrieval\SimilarityRetrieval::class,
  40. $o->resolveRetrieval()
  41. );
  42. }
  43. }
  44. class BxDolAiAgentRetrievalTestEmbeddings implements NeuronAI\RAG\Embeddings\EmbeddingsProviderInterface
  45. {
  46. public function embedText(string $text): array
  47. {
  48. return [0.0];
  49. }
  50. public function embedDocument(NeuronAI\RAG\Document $document): NeuronAI\RAG\Document
  51. {
  52. $document->embedding = [0.0];
  53. return $document;
  54. }
  55. public function embedDocuments(array $documents): array
  56. {
  57. foreach ($documents as $document)
  58. $this->embedDocument($document);
  59. return $documents;
  60. }
  61. }