BxDolAIProviderUnaProxyTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 testOperatorPromptIsPrependedToTheModelSystemPrompt()
  37. {
  38. $oProvider = new BxDolAIProviderUnaProxy(
  39. 'site-key',
  40. 'site-secret',
  41. 'una-proxy',
  42. new BxDolAIProxyTestHttpClient(new BxDolAIProxyTestStream([])),
  43. 'Follow the operator rules.'
  44. );
  45. $oProvider->systemPrompt("You are the client agent.\nAnswer briefly.");
  46. $oPrompt = new ReflectionProperty($oProvider, 'system');
  47. $this->assertSame("Follow the operator rules.\n\nYou are the client agent.\nAnswer briefly.", $oPrompt->getValue($oProvider));
  48. }
  49. public function testBlankOperatorPromptLeavesTheModelSystemPrompt()
  50. {
  51. $oProvider = new BxDolAIProviderUnaProxy(
  52. 'site-key',
  53. 'site-secret',
  54. 'una-proxy',
  55. new BxDolAIProxyTestHttpClient(new BxDolAIProxyTestStream([])),
  56. ' '
  57. );
  58. $oProvider->systemPrompt('You are the client agent.');
  59. $oPrompt = new ReflectionProperty($oProvider, 'system');
  60. $this->assertSame('You are the client agent.', $oPrompt->getValue($oProvider));
  61. }
  62. public function testSavedOperatorPromptIsPrependedWhenTheArgumentIsOmitted()
  63. {
  64. $this->withOperatorPrompt('Follow the operator rules.', function () {
  65. $oProvider = new BxDolAIProviderUnaProxy(
  66. 'site-key',
  67. 'site-secret',
  68. 'una-proxy',
  69. new BxDolAIProxyTestHttpClient(new BxDolAIProxyTestStream([]))
  70. );
  71. $oProvider->systemPrompt("You are the client agent.\nAnswer briefly.");
  72. $oPrompt = new ReflectionProperty($oProvider, 'system');
  73. $this->assertSame("Follow the operator rules.\n\nYou are the client agent.\nAnswer briefly.", $oPrompt->getValue($oProvider));
  74. });
  75. }
  76. public function testEmptyStringOverridesTheSavedOperatorPrompt()
  77. {
  78. $this->withOperatorPrompt('Follow the operator rules.', function () {
  79. $oProvider = new BxDolAIProviderUnaProxy(
  80. 'site-key',
  81. 'site-secret',
  82. 'una-proxy',
  83. new BxDolAIProxyTestHttpClient(new BxDolAIProxyTestStream([])),
  84. ''
  85. );
  86. $oProvider->systemPrompt('You are the client agent.');
  87. $oPrompt = new ReflectionProperty($oProvider, 'system');
  88. $this->assertSame('You are the client agent.', $oPrompt->getValue($oProvider));
  89. });
  90. }
  91. private function withOperatorPrompt(string $sPrompt, callable $fTest)
  92. {
  93. $sPrior = getParam('bx_ai_proxy_prompt');
  94. setParam('bx_ai_proxy_prompt', $sPrompt);
  95. try {
  96. return $fTest();
  97. }
  98. finally {
  99. setParam('bx_ai_proxy_prompt', $sPrior);
  100. }
  101. }
  102. public function testToolCallChunkBecomesNeuronToolMessage()
  103. {
  104. $aLines = [
  105. 'data: ' . json_encode([
  106. 'id' => 'chatcmpl-1',
  107. 'choices' => [[
  108. 'index' => 0,
  109. 'delta' => [
  110. 'tool_calls' => [[
  111. 'index' => 0,
  112. 'id' => 'call_1',
  113. 'type' => 'function',
  114. 'function' => [
  115. 'name' => 'content_search',
  116. 'arguments' => '',
  117. ],
  118. ]],
  119. ],
  120. ]],
  121. ]) . "\n",
  122. 'data: ' . json_encode([
  123. 'id' => 'chatcmpl-1',
  124. 'choices' => [[
  125. 'index' => 0,
  126. 'delta' => [
  127. 'tool_calls' => [[
  128. 'index' => 0,
  129. 'function' => [
  130. 'arguments' => '{"keyword":"cats"}',
  131. ],
  132. ]],
  133. ],
  134. ]],
  135. ]) . "\n",
  136. 'data: ' . json_encode([
  137. 'id' => 'chatcmpl-1',
  138. 'choices' => [[
  139. 'index' => 0,
  140. 'delta' => new stdClass(),
  141. 'finish_reason' => 'tool_calls',
  142. ]],
  143. ]) . "\n",
  144. "data: [DONE]\n",
  145. ];
  146. $oProvider = new BxDolAIProviderUnaProxy(
  147. 'site-key',
  148. 'site-secret',
  149. 'una-proxy',
  150. new BxDolAIProxyTestHttpClient(new BxDolAIProxyTestStream($aLines))
  151. );
  152. $oProvider->setTools([
  153. NeuronAI\Tools\Tool::make('content_search', 'Search content'),
  154. ]);
  155. $oGen = $oProvider->stream(new NeuronAI\Chat\Messages\UserMessage('find cats'));
  156. foreach ($oGen as $oChunk) {
  157. }
  158. $oMessage = $oGen->getReturn();
  159. $this->assertInstanceOf(NeuronAI\Chat\Messages\ToolCallMessage::class, $oMessage);
  160. $aTools = $oMessage->getTools();
  161. $this->assertCount(1, $aTools);
  162. $this->assertSame('content_search', $aTools[0]->getName());
  163. $this->assertSame(['keyword' => 'cats'], $aTools[0]->getInputs());
  164. }
  165. }
  166. class BxDolAIProxyTestStream implements NeuronAI\HttpClient\StreamInterface
  167. {
  168. private int $iLine = 0;
  169. public function __construct(private array $aLines)
  170. {
  171. }
  172. public function eof(): bool
  173. {
  174. return $this->iLine >= count($this->aLines);
  175. }
  176. public function read(int $length): string
  177. {
  178. return '';
  179. }
  180. public function readLine(): string
  181. {
  182. if ($this->eof())
  183. return '';
  184. $sLine = $this->aLines[$this->iLine];
  185. $this->iLine++;
  186. return $sLine;
  187. }
  188. public function close(): void
  189. {
  190. }
  191. }
  192. class BxDolAIProxyTestHttpClient implements NeuronAI\HttpClient\HttpClientInterface
  193. {
  194. public function __construct(private NeuronAI\HttpClient\StreamInterface $oStream)
  195. {
  196. }
  197. public function request(NeuronAI\HttpClient\HttpRequest $request): NeuronAI\HttpClient\HttpResponse
  198. {
  199. throw new Exception('request');
  200. }
  201. public function stream(NeuronAI\HttpClient\HttpRequest $request): NeuronAI\HttpClient\StreamInterface
  202. {
  203. return $this->oStream;
  204. }
  205. public function withBaseUri(string $baseUri): NeuronAI\HttpClient\HttpClientInterface
  206. {
  207. return $this;
  208. }
  209. public function withHeaders(array $headers): NeuronAI\HttpClient\HttpClientInterface
  210. {
  211. return $this;
  212. }
  213. public function withTimeout(float $timeout): NeuronAI\HttpClient\HttpClientInterface
  214. {
  215. return $this;
  216. }
  217. }