BxDolAiRetrievalUnaProxyTest.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * Remote knowledge retrieval signs the raw JSON body and maps master documents.
  4. */
  5. class BxDolAiRetrievalUnaProxyTest extends BxDolTestCase
  6. {
  7. public function testSignedBodyMapsDocuments()
  8. {
  9. $sSecret = 'client-secret';
  10. $sSeenBody = '';
  11. $aSeenHeaders = [];
  12. $o = new BxDolAiRetrievalUnaProxy('site-key', $sSecret, function (string $sBody, array $aHeaders) use (&$sSeenBody, &$aSeenHeaders) {
  13. $sSeenBody = $sBody;
  14. $aSeenHeaders = $aHeaders;
  15. return [
  16. 'code' => 200,
  17. 'body' => json_encode([
  18. 'documents' => [[
  19. 'content' => 'Pages are blocks.',
  20. 'sourceType' => 'file',
  21. 'sourceName' => 'pages.md',
  22. 'score' => 0.82,
  23. 'embedding' => [0.1, 0.2],
  24. ]],
  25. ]),
  26. ];
  27. });
  28. $aDocuments = $o->retrieve(new NeuronAI\Chat\Messages\UserMessage('how do pages work'));
  29. $sExpected = BxDolAIUnaProxyHttpClient::encodeBody(['query' => 'how do pages work']);
  30. $this->assertSame($sExpected, $sSeenBody);
  31. $this->assertContains('X-Una-Key: site-key', $aSeenHeaders);
  32. $this->assertContains('X-Una-Sign: ' . BxDolAIUnaProxyHttpClient::sign($sExpected, $sSecret), $aSeenHeaders);
  33. $this->assertNotContains($sSecret, $aSeenHeaders);
  34. $this->assertCount(1, $aDocuments);
  35. $this->assertSame('Pages are blocks.', $aDocuments[0]->getContent());
  36. $this->assertSame('file', $aDocuments[0]->getSourceType());
  37. $this->assertSame('pages.md', $aDocuments[0]->getSourceName());
  38. $this->assertEqualsWithDelta(0.82, $aDocuments[0]->getScore(), 0.0001);
  39. $this->assertSame([], $aDocuments[0]->getEmbedding());
  40. }
  41. public function testFailedResponseReturnsNoDocuments()
  42. {
  43. $o = new BxDolAiRetrievalUnaProxy('site-key', 'secret', function () {
  44. return ['code' => 503, 'body' => ''];
  45. });
  46. $this->assertSame([], $o->retrieve(new NeuronAI\Chat\Messages\UserMessage('how do pages work')));
  47. }
  48. public function testEmptySecretDoesNotPost()
  49. {
  50. $bPosted = false;
  51. $o = new BxDolAiRetrievalUnaProxy('site-key', '', function () use (&$bPosted) {
  52. $bPosted = true;
  53. return ['code' => 200, 'body' => '{"documents":[]}'];
  54. });
  55. $this->assertSame([], $o->retrieve(new NeuronAI\Chat\Messages\UserMessage('how do pages work')));
  56. $this->assertFalse($bPosted);
  57. }
  58. }