MockOpenAIController.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Http\JsonResponse;
  5. use Illuminate\Support\Str;
  6. class MockOpenAIController extends Controller
  7. {
  8. /**
  9. * 模拟 Chat Completions API
  10. */
  11. public function chatCompletions(Request $request): JsonResponse
  12. {
  13. $delay = $request->query('delay', 'true');
  14. if ($delay === "true") {
  15. // 随机延迟
  16. $this->randomDelay();
  17. }
  18. $error = $request->query('error', "true");
  19. // 随机返回错误
  20. if ($error === "true") {
  21. if ($errorResponse = $this->randomError()) {
  22. return $errorResponse;
  23. }
  24. }
  25. $model = $request->input('model', 'gpt-3.5-turbo');
  26. $messages = $request->input('messages', []);
  27. return response()->json([
  28. 'id' => 'chatcmpl-' . Str::random(29),
  29. 'object' => 'chat.completion',
  30. 'created' => time(),
  31. 'model' => $model,
  32. 'choices' => [
  33. [
  34. 'index' => 0,
  35. 'message' => [
  36. 'role' => 'assistant',
  37. 'content' => $this->generateMockResponse($messages)
  38. ],
  39. 'finish_reason' => 'stop'
  40. ]
  41. ],
  42. 'usage' => [
  43. 'prompt_tokens' => rand(10, 100),
  44. 'completion_tokens' => rand(20, 200),
  45. 'total_tokens' => rand(30, 300)
  46. ]
  47. ]);
  48. }
  49. /**
  50. * 模拟 Completions API
  51. */
  52. public function completions(Request $request): JsonResponse
  53. {
  54. $delay = $request->query('delay', true);
  55. if ($delay === true) {
  56. // 随机延迟
  57. $this->randomDelay();
  58. }
  59. $error = $request->query('error', true);
  60. // 随机返回错误
  61. if ($error === true) {
  62. if ($errorResponse = $this->randomError()) {
  63. return $errorResponse;
  64. }
  65. }
  66. $model = $request->input('model', 'text-davinci-003');
  67. $prompt = $request->input('prompt', '');
  68. return response()->json([
  69. 'id' => 'cmpl-' . Str::random(29),
  70. 'object' => 'text_completion',
  71. 'created' => time(),
  72. 'model' => $model,
  73. 'choices' => [
  74. [
  75. 'text' => $this->generateMockTextResponse($prompt),
  76. 'index' => 0,
  77. 'logprobs' => null,
  78. 'finish_reason' => 'stop'
  79. ]
  80. ],
  81. 'usage' => [
  82. 'prompt_tokens' => rand(10, 100),
  83. 'completion_tokens' => rand(20, 200),
  84. 'total_tokens' => rand(30, 300)
  85. ]
  86. ]);
  87. }
  88. /**
  89. * 模拟 Models API
  90. */
  91. public function models(Request $request): JsonResponse
  92. {
  93. // 随机延迟
  94. $this->randomDelay();
  95. // 随机返回错误
  96. if ($errorResponse = $this->randomError()) {
  97. return $errorResponse;
  98. }
  99. return response()->json([
  100. 'object' => 'list',
  101. 'data' => [
  102. [
  103. 'id' => 'gpt-4',
  104. 'object' => 'model',
  105. 'created' => 1687882411,
  106. 'owned_by' => 'openai'
  107. ],
  108. [
  109. 'id' => 'gpt-3.5-turbo',
  110. 'object' => 'model',
  111. 'created' => 1677610602,
  112. 'owned_by' => 'openai'
  113. ],
  114. [
  115. 'id' => 'text-davinci-003',
  116. 'object' => 'model',
  117. 'created' => 1669599635,
  118. 'owned_by' => 'openai-internal'
  119. ]
  120. ]
  121. ]);
  122. }
  123. /**
  124. * 随机延迟
  125. */
  126. private function randomDelay(): void
  127. {
  128. // 90% 概率 1-3秒延迟
  129. // 10% 概率 60-100秒延迟
  130. if (rand(1, 100) <= 10) {
  131. sleep(rand(60, 100));
  132. } else {
  133. sleep(rand(1, 3));
  134. }
  135. }
  136. /**
  137. * 随机返回错误响应
  138. */
  139. private function randomError(): ?JsonResponse
  140. {
  141. // 20% 概率返回错误
  142. if (rand(1, 100) <= 20) {
  143. $errorType = rand(1, 3);
  144. switch ($errorType) {
  145. case 1:
  146. return $this->badRequestError();
  147. case 2:
  148. return $this->internalServerError();
  149. case 3:
  150. return $this->rateLimitError();
  151. }
  152. }
  153. return null;
  154. }
  155. /**
  156. * 400 错误响应
  157. */
  158. private function badRequestError(): JsonResponse
  159. {
  160. return response()->json([
  161. 'error' => [
  162. 'message' => 'Invalid request: missing required parameter',
  163. 'type' => 'invalid_request_error',
  164. 'param' => null,
  165. 'code' => null
  166. ]
  167. ], 400);
  168. }
  169. /**
  170. * 500 错误响应
  171. */
  172. private function internalServerError(): JsonResponse
  173. {
  174. return response()->json([
  175. 'error' => [
  176. 'message' => 'The server had an error while processing your request. Sorry about that!',
  177. 'type' => 'server_error',
  178. 'param' => null,
  179. 'code' => null
  180. ]
  181. ], 500);
  182. }
  183. /**
  184. * 429 限流错误响应
  185. */
  186. private function rateLimitError(): JsonResponse
  187. {
  188. return response()->json([
  189. 'error' => [
  190. 'message' => 'Rate limit reached for requests',
  191. 'type' => 'requests',
  192. 'param' => null,
  193. 'code' => 'rate_limit_exceeded'
  194. ]
  195. ], 429);
  196. }
  197. /**
  198. * 生成模拟聊天响应
  199. */
  200. private function generateMockResponse(array $messages): string
  201. {
  202. $responses = [
  203. "这是一个模拟的AI响应。我正在模拟OpenAI的API服务器。",
  204. "感谢您的问题!这是一个测试响应,用于模拟真实的AI助手。",
  205. "我是一个模拟的AI助手。您的请求已被处理,这是模拟生成的回复。",
  206. "模拟Hello! This is a mock response from the simulated OpenAI API server.",
  207. "模拟Thank you for your message. This is a simulated response for testing purposes.",
  208. "模拟I understand your question. This is a mock reply generated by the test API server.",
  209. ];
  210. return $responses[array_rand($responses)] . " (响应时间: " . date('Y-m-d H:i:s') . ")";
  211. }
  212. /**
  213. * 生成模拟文本补全响应
  214. */
  215. private function generateMockTextResponse(string $prompt): string
  216. {
  217. $responses = [
  218. " 这是对您提示的模拟补全回复。",
  219. " Mock completion response for your prompt.",
  220. " 模拟的文本补全结果,用于测试目的。",
  221. " This is a simulated text completion.",
  222. " 基于您的输入生成的模拟响应。",
  223. ];
  224. return $responses[array_rand($responses)];
  225. }
  226. }