BxDolAIProviderUnaProxyTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * UNA proxy model: HMAC headers, and OpenAI tool_calls chunks parsed as a Neuron tool message.
  4. */
  5. class BxDolAIProviderUnaProxyTest extends \PHPUnit\Framework\TestCase
  6. {
  7. public function testMatchingKeyAndSecret()
  8. {
  9. $sBody = '{"model":"una-proxy","messages":[{"role":"user","content":"hi"}]}';
  10. $sSecret = 'right-secret';
  11. $oClient = new BxDolAIUnaProxyHttpClient('site-key', $sSecret);
  12. $aHeaders = $oClient->headersForBody($sBody);
  13. $this->assertSame('site-key', $aHeaders['X-Una-Key']);
  14. $this->assertSame(hash_hmac('sha256', $sBody, $sSecret), $aHeaders['X-Una-Sign']);
  15. $this->assertArrayNotHasKey('Authorization', $aHeaders);
  16. $this->assertNotContains($sSecret, $aHeaders);
  17. }
  18. public function testWrongSecretDoesNotMatch()
  19. {
  20. $sBody = '{"model":"una-proxy","messages":[{"role":"user","content":"hi"}]}';
  21. $oClient = new BxDolAIUnaProxyHttpClient('site-key', 'wrong-secret');
  22. $aHeaders = $oClient->headersForBody($sBody);
  23. $this->assertNotSame(hash_hmac('sha256', $sBody, 'right-secret'), $aHeaders['X-Una-Sign']);
  24. $this->assertArrayNotHasKey('Authorization', $aHeaders);
  25. }
  26. public function testEmptyKeyDoesNotBuildRequest()
  27. {
  28. $oHttp = $this->createMock(NeuronAI\HttpClient\HttpClientInterface::class);
  29. $oHttp->expects($this->never())->method('request');
  30. $oHttp->expects($this->never())->method('stream');
  31. $oHttp->expects($this->never())->method('withBaseUri');
  32. $oHttp->expects($this->never())->method('withHeaders');
  33. $this->expectException(Exception::class);
  34. new BxDolAIProviderUnaProxy('', 'secret', 'una-proxy', $oHttp);
  35. }
  36. public function testToolCallChunkBecomesNeuronToolMessage()
  37. {
  38. $aLines = [
  39. 'data: ' . json_encode([
  40. 'id' => 'chatcmpl-1',
  41. 'choices' => [[
  42. 'index' => 0,
  43. 'delta' => [
  44. 'tool_calls' => [[
  45. 'index' => 0,
  46. 'id' => 'call_1',
  47. 'type' => 'function',
  48. 'function' => [
  49. 'name' => 'content_search',
  50. 'arguments' => '',
  51. ],
  52. ]],
  53. ],
  54. ]],
  55. ]) . "\n",
  56. 'data: ' . json_encode([
  57. 'id' => 'chatcmpl-1',
  58. 'choices' => [[
  59. 'index' => 0,
  60. 'delta' => [
  61. 'tool_calls' => [[
  62. 'index' => 0,
  63. 'function' => [
  64. 'arguments' => '{"keyword":"cats"}',
  65. ],
  66. ]],
  67. ],
  68. ]],
  69. ]) . "\n",
  70. 'data: ' . json_encode([
  71. 'id' => 'chatcmpl-1',
  72. 'choices' => [[
  73. 'index' => 0,
  74. 'delta' => new stdClass(),
  75. 'finish_reason' => 'tool_calls',
  76. ]],
  77. ]) . "\n",
  78. "data: [DONE]\n",
  79. ];
  80. $oProvider = new BxDolAIProviderUnaProxy(
  81. 'site-key',
  82. 'site-secret',
  83. 'una-proxy',
  84. new BxDolAIProxyTestHttpClient(new BxDolAIProxyTestStream($aLines))
  85. );
  86. $oProvider->setTools([
  87. NeuronAI\Tools\Tool::make('content_search', 'Search content'),
  88. ]);
  89. $oGen = $oProvider->stream(new NeuronAI\Chat\Messages\UserMessage('find cats'));
  90. foreach ($oGen as $oChunk) {
  91. }
  92. $oMessage = $oGen->getReturn();
  93. $this->assertInstanceOf(NeuronAI\Chat\Messages\ToolCallMessage::class, $oMessage);
  94. $aTools = $oMessage->getTools();
  95. $this->assertCount(1, $aTools);
  96. $this->assertSame('content_search', $aTools[0]->getName());
  97. $this->assertSame(['keyword' => 'cats'], $aTools[0]->getInputs());
  98. }
  99. }
  100. class BxDolAIProxyTestStream implements NeuronAI\HttpClient\StreamInterface
  101. {
  102. private int $iLine = 0;
  103. public function __construct(private array $aLines)
  104. {
  105. }
  106. public function eof(): bool
  107. {
  108. return $this->iLine >= count($this->aLines);
  109. }
  110. public function read(int $length): string
  111. {
  112. return '';
  113. }
  114. public function readLine(): string
  115. {
  116. if ($this->eof())
  117. return '';
  118. $sLine = $this->aLines[$this->iLine];
  119. $this->iLine++;
  120. return $sLine;
  121. }
  122. public function close(): void
  123. {
  124. }
  125. }
  126. class BxDolAIProxyTestHttpClient implements NeuronAI\HttpClient\HttpClientInterface
  127. {
  128. public function __construct(private NeuronAI\HttpClient\StreamInterface $oStream)
  129. {
  130. }
  131. public function request(NeuronAI\HttpClient\HttpRequest $request): NeuronAI\HttpClient\HttpResponse
  132. {
  133. throw new Exception('request');
  134. }
  135. public function stream(NeuronAI\HttpClient\HttpRequest $request): NeuronAI\HttpClient\StreamInterface
  136. {
  137. return $this->oStream;
  138. }
  139. public function withBaseUri(string $baseUri): NeuronAI\HttpClient\HttpClientInterface
  140. {
  141. return $this;
  142. }
  143. public function withHeaders(array $headers): NeuronAI\HttpClient\HttpClientInterface
  144. {
  145. return $this;
  146. }
  147. public function withTimeout(float $timeout): NeuronAI\HttpClient\HttpClientInterface
  148. {
  149. return $this;
  150. }
  151. }