Просмотр исходного кода

Merge pull request #2263 from visuddhinanda/development

获取大模型数据加timeout 五分钟
visuddhinanda 1 год назад
Родитель
Сommit
1ddaccfa7b

+ 8 - 6
api-v8/app/Console/Commands/MqAiTranslate.php

@@ -80,6 +80,7 @@ class MqAiTranslate extends Command
             $modelLog->request_data = json_encode($param, JSON_UNESCAPED_UNICODE);
             try {
                 $response = Http::withToken($message->model->key)
+                    ->timeout(300)
                     ->post($message->model->url, $param);
 
                 $response->throw(); // 触发异常(如果请求失败)
@@ -137,7 +138,7 @@ class MqAiTranslate extends Command
                 $message->sentence->content = $responseContent;
                 $sentData[] = $message->sentence;
                 Log::info($queue . " sentence update {$url}");
-                $response = Http::withToken($token)->post($url, [
+                $response = Http::timeout(10)->withToken($token)->post($url, [
                     'sentences' => $sentData,
                 ]);
                 if ($response->failed()) {
@@ -147,7 +148,8 @@ class MqAiTranslate extends Command
                     ]);
                     return 1;
                 } else {
-                    Log::info($queue . ' sentence update successful');
+                    $count = $response->json()['data']['count'];
+                    Log::info("{$queue} sentence update {$count} successful");
                 }
             }
 
@@ -169,7 +171,7 @@ class MqAiTranslate extends Command
                 'type' => 'discussion',
                 'notification' => false,
             ];
-            $response = Http::withToken($token)->post($url, $data);
+            $response = Http::timeout(10)->withToken($token)->post($url, $data);
             if ($response->failed()) {
                 Log::error($queue . ' discussion error', ['data' => $response->json()]);
             } else {
@@ -189,7 +191,7 @@ class MqAiTranslate extends Command
                     foreach ($topicChildren as  $content) {
                         $data['content'] = $content;
                         Log::debug($queue . ' discussion child request', ['url' => $url, 'data' => $data]);
-                        $response = Http::withToken($token)->post($url, $data);
+                        $response = Http::timeout(10)->withToken($token)->post($url, $data);
                         if ($response->failed()) {
                             Log::error($queue . ' discussion error', ['data' => $response->json()]);
                         } else {
@@ -215,7 +217,7 @@ class MqAiTranslate extends Command
                 'progress' => $progress,
             ];
             Log::debug($queue . ' task progress request', ['url' => $url, 'data' => $data]);
-            $response = Http::withToken($token)->patch($url, $data);
+            $response = Http::timeout(10)->withToken($token)->patch($url, $data);
             if ($response->failed()) {
                 Log::error($queue . ' task progress error', ['data' => $response->json()]);
             } else {
@@ -229,7 +231,7 @@ class MqAiTranslate extends Command
                     'status' => 'done',
                 ];
                 Log::debug($queue . ' task status request', ['url' => $url, 'data' => $data]);
-                $response = Http::withToken($token)->patch($url, $data);
+                $response = Http::timeout(10)->withToken($token)->patch($url, $data);
                 //判断状态码
                 if ($response->failed()) {
                     Log::error($queue . ' task status error', ['data' => $response->json()]);

+ 3 - 2
api-v8/app/Http/Controllers/SentenceController.php

@@ -322,11 +322,12 @@ class SentenceController extends Controller
         foreach ($request->get('sentences') as $key => $sent) {
             # 权限
             if (!$request->has('channel')) {
-                if (!$this->UserCanEdit(
+
+                if ($this->UserCanEdit(
                     $user["user_uid"],
                     $sent['channel_uid'],
                     $sent['book_id'],
-                    isset($sent['access_token']) ?? null
+                    isset($sent['access_token']) ? $sent['access_token'] : null
                 )) {
                     $destChannel = Channel::where('uid', $sent['channel_uid'])->first();;
                 } else {

+ 95 - 0
dashboard-v4/dashboard/src/components/nut/test/AI.tsx

@@ -0,0 +1,95 @@
+import { Button, Input } from "antd";
+
+const AI = () => {
+  interface IOpenAi {
+    model: string;
+    messages: Message[];
+    prompt: string;
+    temperature: number;
+    stream: boolean;
+  }
+
+  interface Message {
+    role: string;
+    content: string;
+  }
+
+  const url =
+    "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions";
+  const key = "sk-ab723c6d5f6a4a3ca3ec362b0042ed94";
+  const model = "qwen-max-latest";
+  return (
+    <>
+      <Input placeholder="并发请求数量" />
+      <Button
+        onClick={() => {
+          console.info("model", model);
+          // 模拟请求数据,这里假设是向模型发送的文本等请求内容,实际按接口要求调整
+          const requests: IOpenAi[] = Array.from({ length: 50 }, (_, i) => ({
+            model: model,
+            messages: [
+              {
+                role: "system",
+                content: "you are translate assistant",
+              },
+              {
+                role: "user",
+                content:
+                  "巴利文:‘‘ 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",
+              },
+            ],
+            prompt:
+              "巴利文:‘‘ 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",
+            temperature: 0.7,
+            stream: false,
+          }));
+
+          // 定义一个函数来发送单个 POST 请求
+          const sendRequest = async (request: IOpenAi) => {
+            try {
+              console.info("ai post");
+              const response = await fetch(url, {
+                method: "POST",
+                headers: {
+                  "Content-Type": "application/json",
+                  Authorization: `Bearer ${key}`,
+                },
+                body: JSON.stringify(request),
+              });
+
+              if (!response.ok) {
+                throw new Error(`HTTP error! status: ${response.status}`);
+              }
+
+              const result = await response.json();
+              return { status: "fulfilled", value: result };
+            } catch (error) {
+              return { status: "rejected", reason: error };
+            }
+          };
+
+          // 并发执行 n 个请求
+          (async () => {
+            const results = await Promise.allSettled(requests.map(sendRequest));
+            const successfulResults = results.filter(
+              (result) =>
+                result.status === "fulfilled" &&
+                result.value.status === "fulfilled"
+            );
+            const failedResults = results.filter(
+              (result) =>
+                result.status === "fulfilled" &&
+                result.value.status === "rejected"
+            );
+
+            console.log("Successful requests:", successfulResults);
+            console.log("Failed requests:", failedResults);
+          })();
+        }}
+      >
+        AI run
+      </Button>
+    </>
+  );
+};
+export default AI;

+ 2 - 1
dashboard-v4/dashboard/src/pages/nut/index.tsx

@@ -7,6 +7,7 @@ import FooterBar from "../../components/library/FooterBar";
 import HeadBar from "../../components/library/HeadBar";
 import Home from "../../components/nut/Home";
 import InnerDrawer from "../../components/nut/InnerDrawer";
+import AI from "../../components/nut/test/AI";
 
 interface IRandomPanel {
   v1: string;
@@ -36,7 +37,7 @@ const Widget = () => {
     <div>
       <HeadBar />
       <div>Home Page</div>
-
+      <AI />
       <InnerDrawer />
       <div>
         <h1>Mermaid</h1>