AI.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { Button, Input } from "antd";
  2. import { useState } from "react";
  3. interface IOpenAi {
  4. model: string;
  5. messages: Message[];
  6. prompt: string;
  7. temperature: number;
  8. stream: boolean;
  9. }
  10. interface Message {
  11. role: string;
  12. content: string;
  13. }
  14. const AI = () => {
  15. const [url, setUrl] = useState(
  16. "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions"
  17. );
  18. const [key, setKey] = useState("");
  19. const [model, setModel] = useState("qwen-max-latest");
  20. const [concurrent, setConcurrent] = useState(10);
  21. return (
  22. <>
  23. <Input
  24. placeholder="url"
  25. value={url}
  26. onChange={(event) => {
  27. setUrl(event.target.value);
  28. }}
  29. />
  30. <Input
  31. placeholder="key"
  32. value={key}
  33. onChange={(event) => {
  34. setKey(event.target.value);
  35. }}
  36. />
  37. <Input
  38. placeholder="model"
  39. value={model}
  40. onChange={(event) => {
  41. setModel(event.target.value);
  42. }}
  43. />
  44. <Input
  45. placeholder="并发请求数量"
  46. value={concurrent}
  47. onChange={(event) => {
  48. setConcurrent(parseInt(event.target.value));
  49. }}
  50. />
  51. <Button
  52. onClick={() => {
  53. console.info("model", model);
  54. // 模拟请求数据,这里假设是向模型发送的文本等请求内容,实际按接口要求调整
  55. const requests: IOpenAi[] = Array.from(
  56. { length: concurrent },
  57. (_, _i) => ({
  58. model: model,
  59. messages: [
  60. {
  61. role: "system",
  62. content: "you are translate assistant",
  63. },
  64. {
  65. role: "user",
  66. content:
  67. "巴利文:‘‘ Sammā mānābhisamayā antamakāsi dukkhassā ’’ tiādīsu ( a . ni .7.9) pahānaṃ .\n中文译文:\n“通过正确(sammā)地觉悟(mānābhisamayā),他终结了(antamakāsi)痛苦(dukkhassa)”等(iti ādīsu)句子中,放弃(pahānaṃ)的含义被阐明。\n请从不同角度分析和评价译文,指出翻译错误,并给出建议的译文。\n",
  68. },
  69. ],
  70. prompt:
  71. "巴利文:‘‘ Sammā mānābhisamayā antamakāsi dukkhassā ’’ tiādīsu ( a . ni .7.9) pahānaṃ .\n中文译文:\n“通过正确(sammā)地觉悟(mānābhisamayā),他终结了(antamakāsi)痛苦(dukkhassa)”等(iti ādīsu)句子中,放弃(pahānaṃ)的含义被阐明。\n请从不同角度分析和评价译文,指出翻译错误,并给出建议的译文。\n",
  72. temperature: 0.7,
  73. stream: false,
  74. })
  75. );
  76. // 定义一个函数来发送单个 POST 请求
  77. const sendRequest = async (request: IOpenAi) => {
  78. try {
  79. console.info("ai post");
  80. const response = await fetch(url, {
  81. method: "POST",
  82. headers: {
  83. "Content-Type": "application/json",
  84. Authorization: `Bearer ${key}`,
  85. },
  86. body: JSON.stringify(request),
  87. });
  88. if (!response.ok) {
  89. throw new Error(`HTTP error! status: ${response.status}`);
  90. }
  91. const result = await response.json();
  92. return { status: "fulfilled", value: result };
  93. } catch (error) {
  94. return { status: "rejected", reason: error };
  95. }
  96. };
  97. // 并发执行 n 个请求
  98. (async () => {
  99. const results = await Promise.allSettled(requests.map(sendRequest));
  100. const successfulResults = results.filter(
  101. (result) =>
  102. result.status === "fulfilled" &&
  103. result.value.status === "fulfilled"
  104. );
  105. const failedResults = results.filter(
  106. (result) =>
  107. result.status === "fulfilled" &&
  108. result.value.status === "rejected"
  109. );
  110. console.log("Successful requests:", successfulResults);
  111. console.log("Failed requests:", failedResults);
  112. })();
  113. }}
  114. >
  115. AI run
  116. </Button>
  117. </>
  118. );
  119. };
  120. export default AI;