{"id":23648,"date":"2024-12-23T10:49:52","date_gmt":"2024-12-23T02:49:52","guid":{"rendered":"https:\/\/aif.amtbbs.org\/?p=23648"},"modified":"2024-12-23T10:49:52","modified_gmt":"2024-12-23T02:49:52","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-gemini-api-%e6%9e%84%e5%bb%ba%e8%a7%86%e9%a2%91%e5%ad%97%e5%b9%95%e7%94%9f%e6%88%90%e5%99%a8","status":"publish","type":"post","link":"https:\/\/aif.amtbbs.org\/index.php\/2024\/12\/23\/23648\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 Gemini API \u6784\u5efa\u89c6\u9891\u5b57\u5e55\u751f\u6210\u5668"},"content":{"rendered":"<div><img data-dominant-color=\"2876a4\" data-has-transparency=\"false\" style=\"--dominant-color: #2876a4;\" loading=\"lazy\" decoding=\"async\" class=\"not-transparent alignnone size-full wp-image-23650\" src=\"https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2024\/12\/a1203bdd-7a2d-4ec1-a891-f2f01ab22f94-300x167-1.png\" width=\"300\" height=\"167\" alt=\"\" srcset=\"https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2024\/12\/a1203bdd-7a2d-4ec1-a891-f2f01ab22f94-300x167-1.png 300w, https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2024\/12\/a1203bdd-7a2d-4ec1-a891-f2f01ab22f94-300x167-1-150x84.png 150w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/div>\n<div><\/div>\n<div class=\"article-desc\">\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u4f60\u5c06\u4f7f\u7528 Google \u7684 Gemini API \u6784\u5efa\u4eba\u5de5\u667a\u80fd\u9a71\u52a8\u7684\u5b57\u5e55\u751f\u6210\u5668\u3002\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\u201cAI-Subtitle-Generator\u201d\u7684\u9879\u76ee\uff0c\u8be5\u9879\u76ee\u7684\u524d\u7aef\u4f7f\u7528 React\uff0c\u540e\u7aef\u4f7f\u7528 Express\u3002\u51c6\u5907\u597d\u4e86\u5417\uff1f\u8ba9\u6211\u4eec\u9a6c\u4e0a\u51fa\u53d1\u5427\uff01<\/div>\n<div id=\"postspictures\" class=\"article-content\">\n<div id=\"container\" class=\"container am-engine\" data-v-1d7a5742=\"\" data-element=\"root\">\n<p>&nbsp;<\/p>\n<h3>\u5f00\u7bc7<\/h3>\n<p>\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u4f60\u5c06\u4f7f\u7528 Google \u7684 Gemini API \u6784\u5efa\u4eba\u5de5\u667a\u80fd\u9a71\u52a8\u7684\u5b57\u5e55\u751f\u6210\u5668\u3002\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\u201cAI-Subtitle-Generator\u201d\u7684\u9879\u76ee\uff0c\u8be5\u9879\u76ee\u7684\u524d\u7aef\u4f7f\u7528 React\uff0c\u540e\u7aef\u4f7f\u7528 Express\u3002\u51c6\u5907\u597d\u4e86\u5417\uff1f\u8ba9\u6211\u4eec\u9a6c\u4e0a\u51fa\u53d1\u5427\uff01<\/p>\n<p>&nbsp;<\/p>\n<p><img data-dominant-color=\"17191d\" data-has-transparency=\"true\" style=\"--dominant-color: #17191d;\" loading=\"lazy\" decoding=\"async\" class=\"has-transparency alignnone size-full wp-image-23651\" src=\"https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2024\/12\/b3ebb46201b0.png\" width=\"531\" height=\"299\" srcset=\"https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2024\/12\/b3ebb46201b0.png 531w, https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2024\/12\/b3ebb46201b0-300x169.png 300w, https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2024\/12\/b3ebb46201b0-150x84.png 150w\" sizes=\"auto, (max-width: 531px) 100vw, 531px\" \/><\/p>\n<p>&nbsp;<\/p>\n<h3>\u5148\u51b3\u6761\u4ef6<\/h3>\n<p>\u5728\u6784\u5efa\u9879\u76ee\u4e4b\u524d\uff0c\u4f60\u9700\u8981\u5bf9React \u548c Express\u00a0\u6709\u57fa\u672c\u4e86\u89e3\u3002<\/p>\n<h3>Gemini API \u662f\u4ec0\u4e48\uff1f<\/h3>\n<p>Google \u7684 Gemini API \u662f\u4e00\u6b3e\u529f\u80fd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u8ba9\u4f60\u5c06\u9ad8\u7ea7 AI \u529f\u80fd\u96c6\u6210\u5230\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\u3002 Gemini \u662f\u591a\u6a21\u6001\u6a21\u578b\uff0c\u5b83\u652f\u6301\u7528\u6237\u4f7f\u7528\u5404\u79cd\u7c7b\u578b\u7684\u8f93\u5165\uff0c\u4f8b\u5982\u6587\u672c\u3001\u56fe\u50cf\u3001\u97f3\u9891\u548c\u89c6\u9891\u7b49\u3002<\/p>\n<p>\u5b83\u64c5\u957f\u5206\u6790\u548c\u5904\u7406\u5927\u91cf\u6587\u672c\uff0c\u7279\u522b\u662f\u4ece\u89c6\u9891\u4e2d\u63d0\u53d6\u4fe1\u606f\u7684\u80fd\u529b\uff0c\u975e\u5e38\u9002\u5408\u5b57\u5e55\u751f\u6210\u5668\u7684\u9879\u76ee\u3002<\/p>\n<h3>\u5982\u4f55\u83b7\u53d6 API \u5bc6\u94a5<\/h3>\n<p>API \u5bc6\u94a5\u5145\u5f53\u552f\u4e00\u6807\u8bc6\u7b26\u5e76\u9a8c\u8bc1\u4f60\u5bf9\u670d\u52a1\u7684\u8bf7\u6c42\u3002\u5b83\u5bf9\u4e8e\u8bbf\u95ee\u548c\u4f7f\u7528 Gemini AI \u7684\u529f\u80fd\u81f3\u5173\u91cd\u8981\u3002\u8fd9\u4e2a\u5bc6\u94a5\u5c06\u5141\u8bb8\u6211\u4eec\u7684\u5e94\u7528\u7a0b\u5e8f\u4e0e Gemini \u8fdb\u884c\u901a\u4fe1\uff0c\u4e5f\u662f\u6784\u5efa\u8be5\u9879\u76ee\u7684\u5173\u952e\u56e0\u7d20\u3002<\/p>\n<p>\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u8fdb\u5165<a href=\"https:\/\/aistudio.google.com\/prompts\/new_chat\" target=\"_blank\" rel=\"noopener\"><u>Google AI Studio<\/u><\/a>\u00a0\uff0c\u7136\u540e\u70b9\u51fb\u201c\u83b7\u53d6 API \u5bc6\u94a5\u201d\uff08Get\u00a0API\u00a0key\uff09\uff1a<\/p>\n<p><img data-dominant-color=\"191b1e\" data-has-transparency=\"true\" style=\"--dominant-color: #191b1e;\" loading=\"lazy\" decoding=\"async\" class=\"has-transparency alignnone size-full wp-image-23652\" src=\"https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2024\/12\/b8c12b72727d.png\" width=\"591\" height=\"323\" srcset=\"https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2024\/12\/b8c12b72727d.png 591w, https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2024\/12\/b8c12b72727d-300x164.png 300w, https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2024\/12\/b8c12b72727d-150x82.png 150w\" sizes=\"auto, (max-width: 591px) 100vw, 591px\" \/><\/p>\n<p>\u5728\u6253\u5f00\u7684API KEY \u9875\u9762\u4e2d\uff0c\u5355\u51fb\u201c\u521b\u5efa API \u5bc6\u94a5\u201d\uff1a<\/p>\n<p><img data-dominant-color=\"191a1f\" data-has-transparency=\"true\" style=\"--dominant-color: #191a1f;\" loading=\"lazy\" decoding=\"async\" class=\"has-transparency alignnone size-full wp-image-23653\" src=\"https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2024\/12\/326e611696bb.png\" width=\"587\" height=\"336\" srcset=\"https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2024\/12\/326e611696bb.png 587w, https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2024\/12\/326e611696bb-300x172.png 300w, https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2024\/12\/326e611696bb-150x86.png 150w\" sizes=\"auto, (max-width: 587px) 100vw, 587px\" \/><\/p>\n<p>\u8be5\u64cd\u4f5c\u5c06\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 API \u5bc6\u94a5\uff0c\u5e76\u590d\u5236\u5bc6\u94a5\u5bf9\u5176\u8fdb\u884c\u59a5\u5584\u4fdd\u5b58\u3002<\/p>\n<p>\u8fd9\u4e2a\u5c31\u662f\u8bbf\u95eeGemini\u00a0API\u7684\u00a0\u5bc6\u94a5\u3002\u6b64\u5bc6\u94a5\u7528\u4e8e\u9a8c\u8bc1\u5e94\u7528\u7a0b\u5e8f\u5bf9 Gemini API \u7684\u8bf7\u6c42\u3002\u6bcf\u6b21\u5e94\u7528\u7a0b\u5e8f\u5411 Gemini \u53d1\u9001\u8bf7\u6c42\u65f6\uff0c\u90fd\u5fc5\u987b\u5305\u542b\u6b64\u5bc6\u94a5\u3002 Gemini \u4f7f\u7528\u6b64\u5bc6\u94a5\u6765\u9a8c\u8bc1\u8bf7\u6c42\u662f\u5426\u6765\u81ea\u6388\u6743\u6765\u6e90\u3002\u5982\u679c\u6ca1\u6709\u6b64 API \u5bc6\u94a5\uff0c\u8bf7\u6c42\u5c06\u88ab\u62d2\u7edd\uff0c\u540c\u65f6\u5e94\u7528\u4e5f\u65e0\u6cd5\u8bbf\u95ee Gemini \u7684\u670d\u52a1\u3002<\/p>\n<h3>\u9879\u76ee\u8bbe\u7f6e<\/h3>\n<p>\u9996\u5148\uff0c\u57fa\u4e8e\u9879\u76ee\u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u4ef6\u5939\u3002\u6211\u4eec\u79f0\u4e4b\u4e3aai-subtitle-generator \u3002<\/p>\n<p>\u5728ai-subtitle-generator\u6587\u4ef6\u5939\u5185\uff0c\u521b\u5efa\u4e24\u4e2a\u5b50\u6587\u4ef6\u5939\uff1a client\u548cserver \u3002 client\u6587\u4ef6\u5939\u5c06\u5305\u542b React \u524d\u7aef\uff0c server\u6587\u4ef6\u5939\u5c06\u5305\u542b Express \u540e\u7aef\u3002<\/p>\n<h3>\u524d\u7aef\u8bbe\u7f6e<\/h3>\n<p>\u6211\u4eec\u5148\u5c06\u91cd\u70b9\u5173\u6ce8\u653e\u5230\u524d\u7aef\uff0c\u5e76\u8bbe\u7f6e\u4e00\u4e2a\u57fa\u672c\u7684 React \u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p>\u901a\u8fc7\u5982\u4e0b\u547d\u4ee4\uff0c\u5bfc\u822a\u5230client\u6587\u4ef6\u5939\uff1a<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_0\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-plain\" tabindex=\"0\"><code class=\"language-plain\">cd client<\/code><\/pre>\n<ul id=\"code_id_0\" class=\"pre-numbering\">\n<li>1.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>\u4f7f\u7528Vite\u521b\u5efa\u4e00\u4e2a\u65b0\u7684React\u9879\u76ee\u3002\u6267\u884c\u5982\u4e0b\u547d\u4ee4\uff1a<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_1\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-plain\" tabindex=\"0\"><code class=\"language-plain\">npm create vite@latest .<\/code><\/pre>\n<ul id=\"code_id_1\" class=\"pre-numbering\">\n<li>1.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>\u6839\u636e\u63d0\u793a\u65f6\uff0c\u9009\u62e9\u201cReact\u201d\u3001\u201cReact + TS\u201d\u6216\u8005\u201cReact + JS\u201d\u3002\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u5c06\u4f7f\u7528 React + TS\u3002\u5f53\u7136\u4f60\u4e5f\u53ef\u4ee5\u6839\u636e\u559c\u597d\u9009\u62e9\u5176\u4ed6\u7684\u9009\u9879\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\u4f9d\u8d56\u9879\uff1a<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_2\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-plain\" tabindex=\"0\"><code class=\"language-plain\">npm install<\/code><\/pre>\n<ul id=\"code_id_2\" class=\"pre-numbering\">\n<li>1.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>\u7136\u540e\u542f\u52a8\u5f00\u53d1\u670d\u52a1\u5668\uff1a<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_3\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-plain\" tabindex=\"0\"><code class=\"language-plain\">npm run dev<\/code><\/pre>\n<ul id=\"code_id_3\" class=\"pre-numbering\">\n<li>1.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h4>\u5728\u524d\u7aef\u5904\u7406\u6587\u4ef6\u4e0a\u4f20<\/h4>\n<p>\u73b0\u5728\u5728client\/src\/App.tsx\u4e2d\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_4\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-plain\" tabindex=\"0\"><code class=\"language-plain\">\/\/  client\/src\/App.tsx\r\nconst App = () =&gt; {\r\n    const handleSubmit = async (e: React.FormEvent&lt;HTMLFormElement&gt;): Promise&lt;void&gt; =&gt; {\r\n    e.preventDefault();\r\n    try {\r\n      const formData = new FormData(e.currentTarget);\r\n      console.log(formData)\r\n    } catch (error) {\r\n      console.log(error);\r\n    }\r\n  };\r\n\r\n  return (\r\n    &lt;div&gt;\r\n      &lt;form onSubmit={handleSubmit}&gt;\r\n        &lt;input type=\"file\" accept=\"video\/*,.mkv\" name=\"video\" \/&gt;\r\n        &lt;input type=\"submit\" \/&gt;\r\n      &lt;\/form&gt;\r\n    &lt;\/div&gt;\r\n  );\r\n};\r\nexport default App;<\/code><\/pre>\n<ul id=\"code_id_4\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<li>9.<\/li>\n<li>10.<\/li>\n<li>11.<\/li>\n<li>12.<\/li>\n<li>13.<\/li>\n<li>14.<\/li>\n<li>15.<\/li>\n<li>16.<\/li>\n<li>17.<\/li>\n<li>18.<\/li>\n<li>19.<\/li>\n<li>20.<\/li>\n<li>21.<\/li>\n<li>22.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e00\u4e2a\u8f93\u5165\u6807\u7b7e\u6765\u63a5\u53d7\u89c6\u9891\u5e76\u5c06\u5176\u547d\u540d\u4e3avideo \u3002\u8be5\u540d\u79f0\u5c06\u9644\u52a0\u5230FormData\u5bf9\u8c61\u4e2d\u3002<\/p>\n<p>\u5728\u5c06\u89c6\u9891\u53d1\u9001\u5230\u670d\u52a1\u5668\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f7f\u7528\u5230\u4e86\u201c\u952e\u503c\u5bf9\u201c\u7684\u53d1\u9001\u65b9\u5f0f\uff0c\u5176\u4e2d\u201d\u952e\u201c\u662fvideo \uff0c\u503c\u662f\u6587\u4ef6\u6570\u636e\u3002<\/p>\n<p>\u4e3a\u4ec0\u4e48\u662f\u952e\u503c\u5bf9\uff1f\u56e0\u4e3a\u5f53\u670d\u52a1\u5668\u6536\u5230\u8bf7\u6c42\u65f6\uff0c\u5b83\u9700\u8981\u89e3\u6790\u4f20\u5165\u7684\u5757\u3002\u89e3\u6790\u540e\uff0c\u89c6\u9891\u6570\u636e\u5c06\u5728req.files[key]\u4e2d\u4f7f\u7528\uff0c\u5176\u4e2dkey\u662f\u524d\u7aef\u5206\u914d\u7684\u540d\u79f0\uff08\u5728\u672c\u4f8b\u4e2d\u4e3avideo \uff09\u3002<\/p>\n<p>\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u4f7f\u7528FormData\u5bf9\u8c61\u7684\u539f\u56e0\u3002\u5f53\u521b\u5efa\u4e00\u4e2a\u65b0\u7684FormData\u5b9e\u4f8b\u5e76\u5c06e.target\u4f20\u9012\u7ed9\u5b83\u65f6\uff0c\u6240\u6709\u8868\u5355\u5b57\u6bb5\u53ca\u5176\u540d\u79f0\u5c06\u81ea\u52a8\u751f\u6210\u952e\u503c\u5bf9\u7684\u5f62\u5f0f\u3002<\/p>\n<h3>\u670d\u52a1\u5668\u8bbe\u7f6e<\/h3>\n<p>\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u5df2\u7ecf\u83b7\u53d6\u4e86API \u5bc6\u94a5\uff0c\u63a5\u7740\u9700\u8981\u8bbe\u7f6e\u540e\u7aef\u670d\u52a1\u5668\u3002\u8be5\u670d\u52a1\u5668\u5c06\u5904\u7406\u6765\u81ea\u524d\u7aef\u4e0a\u4f20\u7684\u89c6\u9891\uff0c\u5e76\u4e0e Gemini API \u8fdb\u884c\u901a\u4fe1\u4ece\u800c\u751f\u6210\u5b57\u5e55\u3002<\/p>\n<p>\u901a\u8fc7\u5982\u4e0b\u547d\u4ee4\uff0c\u5bfc\u822a\u5230server\u6587\u4ef6\u5939\uff1a<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_5\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-plain\" tabindex=\"0\"><code class=\"language-plain\">cd server<\/code><\/pre>\n<ul id=\"code_id_5\" class=\"pre-numbering\">\n<li>1.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>\u5e76\u521d\u59cb\u5316\u9879\u76ee\uff1a<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_6\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-plain\" tabindex=\"0\"><code class=\"language-plain\">npm init -y<\/code><\/pre>\n<ul id=\"code_id_6\" class=\"pre-numbering\">\n<li>1.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>\u7136\u540e\u5b89\u88c5\u5fc5\u8981\u7684\u5305\uff1a<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_7\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-plain\" tabindex=\"0\"><code class=\"language-plain\">npm install express dotenv cors @google\/generative-ai express-fileupload nodemon<\/code><\/pre>\n<ul id=\"code_id_7\" class=\"pre-numbering\">\n<li>1.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>\u8fd9\u4e9b\u662f\u5728\u6b64\u9879\u76ee\u4e2d\u4f7f\u7528\u7684\u540e\u7aef\u4f9d\u8d56\u9879\uff1a<\/p>\n<ul data-id=\"u738a58b-mULfDiEG\">\n<li data-id=\"ld70c578-JWRLQE1N\">express\u00a0<strong>\uff1a<\/strong>\u7528\u4e8e\u521b\u5efa\u540e\u7aef API \u7684 Web \u6846\u67b6\u3002<\/li>\n<li data-id=\"ld70c578-I3Pdb0UI\">dotenv\u00a0<strong>\uff1a<\/strong>\u4ece.env\u6587\u4ef6\u52a0\u8f7d\u73af\u5883\u53d8\u91cf\u3002<\/li>\n<li data-id=\"ld70c578-bj18AQ48\">cors\u00a0<strong>\uff1a<\/strong>\u542f\u7528\u8de8\u6e90\u8d44\u6e90\u5171\u4eab\uff0c\u5141\u8bb8\u524d\u7aef\u4e0e\u540e\u7aef\u8fdb\u884c\u901a\u4fe1\u3002<\/li>\n<li data-id=\"ld70c578-Iep9VENB\">@google\/generative-ai\u00a0<strong>\uff1a<\/strong>\u7528\u4e8e\u4e0e Gemini API \u4ea4\u4e92\u7684 Google AI \u5e93\u3002<\/li>\n<li data-id=\"ld70c578-ZhaPmoBj\">express-fileupload\u00a0<strong>\uff1a<\/strong>\u5904\u7406\u6587\u4ef6\u4e0a\u4f20\uff0c\u53ef\u4ee5\u8f7b\u677e\u8bbf\u95ee\u670d\u52a1\u5668\u4e0a\u4e0a\u4f20\u7684\u6587\u4ef6\u3002<\/li>\n<li data-id=\"ld70c578-5MM1MaYc\">nodemon\u00a0<strong>\uff1a<\/strong>\u66f4\u6539\u4ee3\u7801\u65f6\uff0c\u81ea\u52a8\u91cd\u65b0\u542f\u52a8\u670d\u52a1\u5668\u3002<\/li>\n<\/ul>\n<h4>\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf<\/h4>\n<p>\u73b0\u5728\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a.env\u7684\u6587\u4ef6\u3002\u53ef\u4ee5\u5728\u6b64\u5904\u7ba1\u7406 API \u5bc6\u94a5\u3002<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_8\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-plain\" tabindex=\"0\"><code class=\"language-plain\">\/\/.env\r\nAPI_KEY = YOUR_API_API\r\nPORT = 3000<\/code><\/pre>\n<ul id=\"code_id_8\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h4>\u66f4\u65b0package.json<\/h4>\n<p>\u5bf9\u4e8e\u8be5\u9879\u76ee\uff0c\u6211\u4eec\u4f7f\u7528 ES6 \u6a21\u5757\u800c\u4e0d\u662f CommonJS\u3002\u8981\u542f\u7528\u6b64\u529f\u80fd\uff0c\u8bf7\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u66f4\u65b0\u4f60\u7684package.json\u6587\u4ef6\uff1a<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_9\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-plain\" tabindex=\"0\"><code class=\"language-plain\">{\r\n  \"name\": \"server\",\r\n  \"version\": \"1.0.0\",\r\n  \"main\": \"index.js\",\r\n  \"type\": \"module\",       \/\/Add \"type\": \"module\" to enable ES6 modules\r\n  \"scripts\": {\r\n    \"start\": \"node server.js\",\r\n    \"dev\": \"nodemon server.js\"    \/\/configure nodemon\r\n  },\r\n  \"keywords\": [],\r\n  \"author\": \"\",\r\n  \"license\": \"ISC\",\r\n  \"description\": \"\",\r\n  \"dependencies\": {\r\n    \"@google\/generative-ai\": \"^0.21.0\",\r\n    \"cors\": \"^2.8.5\",\r\n    \"dotenv\": \"^16.4.7\",\r\n    \"express\": \"^4.21.1\",\r\n    \"express-fileupload\": \"^1.5.1\",\r\n    \"nodemon\": \"^3.1.7\"\r\n  }\r\n}<\/code><\/pre>\n<ul id=\"code_id_9\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<li>9.<\/li>\n<li>10.<\/li>\n<li>11.<\/li>\n<li>12.<\/li>\n<li>13.<\/li>\n<li>14.<\/li>\n<li>15.<\/li>\n<li>16.<\/li>\n<li>17.<\/li>\n<li>18.<\/li>\n<li>19.<\/li>\n<li>20.<\/li>\n<li>21.<\/li>\n<li>22.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h4>Express \u7684\u57fa\u672c\u8bbe\u7f6e<\/h4>\n<p>\u521b\u5efa\u6587\u4ef6server.js \u3002\u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u8bbe\u7f6e\u4e00\u4e2a\u57fa\u672c\u7684 Express \u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_10\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-plain\" tabindex=\"0\"><code class=\"language-plain\">\/\/  server\/server.js\r\n\r\nimport express from \"express\";\r\nimport { configDotenv } from \"dotenv\";\r\nimport fileUpload from \"express-fileupload\";\r\nimport cors from \"cors\"\r\n\r\nconst app = express();\r\n\r\nconfigDotenv();           \/\/configure the env\r\napp.use(fileUpload());    \/\/it will parse the mutipart data\r\napp.use(express.json());  \/\/ Enable JSON parsing for request bodies\r\napp.use(cors())           \/\/configure cors\r\n\r\napp.use(\"\/api\/subs\",subRoutes);  \/\/ Use routes for the \"\/api\/subs\" endpoint\r\n\r\napp.listen(process.env.PORT, () =&gt; {   \/\/access the PORT from the .env\r\n  console.log(\"server started\");         \r\n});<\/code><\/pre>\n<ul id=\"code_id_10\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<li>9.<\/li>\n<li>10.<\/li>\n<li>11.<\/li>\n<li>12.<\/li>\n<li>13.<\/li>\n<li>14.<\/li>\n<li>15.<\/li>\n<li>16.<\/li>\n<li>17.<\/li>\n<li>18.<\/li>\n<li>19.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>\u5728\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a Express \u5e94\u7528\u7a0b\u5e8f\u5b9e\u4f8b\uff0c\u7136\u540e\u52a0\u8f7d\u73af\u5883\u53d8\u91cf\u3002\u8be5\u73af\u5883\u53d8\u91cf\u53ef\u4ee5\u7528\u6765\u4fdd\u5b58API \u5bc6\u94a5\u7b49\u654f\u611f\u6570\u636e\u3002\u63a5\u4e0b\u6765\uff0c\u5229\u7528\u4e2d\u95f4\u4ef6\uff1a fileUpload\u51c6\u5907\u670d\u52a1\u5668\u6765\u63a5\u6536\u4e0a\u4f20\u7684\u89c6\u9891\uff0c express.json\u5141\u8bb8\u63a5\u6536 JSON \u6570\u636e\uff0c cors\u5141\u8bb8\u524d\u7aef\u548c\u540e\u7aef\u4e4b\u95f4\u7684\u901a\u4fe1\u3002<\/p>\n<p>\u63a5\u7740\uff0c\u5b9a\u4e49\u8def\u7531(\/api\/subs)\u6765\u5904\u7406\u4e0e\u5b57\u5e55\u751f\u6210\u76f8\u5173\u7684\u6240\u6709\u8bf7\u6c42\u3002\u8def\u7531\u7684\u5177\u4f53\u903b\u8f91\u5c06\u5728subs.routes.js\u4e2d\u4f53\u73b0\u3002\u6700\u540e\uff0c\u542f\u52a8\u670d\u52a1\u5668\uff0c\u544a\u8bc9\u5b83\u76d1\u542c.env\u6587\u4ef6\u4e2d\u5b9a\u4e49\u7684\u7aef\u53e3\uff0c\u4ece\u800c\u4fdd\u8bc1\u8bf7\u6c42\u54cd\u5e94\u3002<\/p>\n<p>\u7136\u540e\uff0c\u521b\u5efa\u4e00\u7cfb\u5217\u6587\u4ef6\u5939\u6765\u7ba1\u7406\u4ee3\u7801\u3002\u5f53\u7136\uff0c\u4e5f\u53ef\u4ee5\u5728\u5355\u4e2a\u6587\u4ef6\u4e2d\u7ba1\u7406\u4ee3\u7801\uff0c\u4f46\u5c06\u5176\u6784\u5efa\u5230\u5355\u72ec\u7684\u6587\u4ef6\u5939\u8fdb\u884c\u7ba1\u7406\u4f1a\u66f4\u52a0\u65b9\u4fbf\u3001\u66f4\u52a0\u6e05\u6670\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u670d\u52a1\u5668\u7684\u6700\u7ec8\u6587\u4ef6\u5939\u7ed3\u6784\uff1a<\/p>\n<p>server\/<\/p>\n<p>\u251c\u2500\u2500 server.js<\/p>\n<p>\u251c\u2500\u2500 controller\/<\/p>\n<p>\u2502 \u2514\u2500\u2500 subs.controller.js<\/p>\n<p>\u251c\u2500\u2500 gemini\/<\/p>\n<p>\u2502 \u251c\u2500\u2500 gemini.config.js<\/p>\n<p>\u251c\u2500\u2500 routes\/<\/p>\n<p>\u2502 \u2514\u2500\u2500 subs.routes.js<\/p>\n<p>\u251c\u2500\u2500 uploads\/<\/p>\n<p>\u251c\u2500\u2500 utils\/<\/p>\n<p>\u2502 \u251c\u2500\u2500 fileUpload.js<\/p>\n<p>\u2502 \u2514\u2500\u2500 genContent.js<\/p>\n<p>\u2514\u2500\u2500 .env<\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong>\u73b0\u5728\u4e0d\u5fc5\u62c5\u5fc3\u521b\u5efa\u6b64\u6587\u4ef6\u5939\u7ed3\u6784\u3002\u8fd9\u4ec5\u4f9b\u53c2\u8003\u3002\u8ddf\u7740\u6587\u7ae0\u4e00\u6b65\u6b65\u6765\uff0c\u5728\u540e\u9762\u7684\u5185\u5bb9\u4e2d\u4f1a\u9010\u6b65\u642d\u5efa\u8fd9\u4e2a\u7ed3\u6784\u3002<\/p>\n<h4>\u521b\u5efa\u8def\u7531<\/h4>\n<p>\u521b\u5efa\u4e00\u4e2aroutes\u6587\u4ef6\u5939\uff0c\u7136\u540e\u521b\u5efasubs.routes.js\u6587\u4ef6\u5982\u4e0b\u00a0\uff1a<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_11\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-plain\" tabindex=\"0\"><code class=\"language-plain\">\/\/ server\/routes\/sub.routes.js\r\nimport express from \"express\"\r\nimport { uploadFile } from \"..\/controller\/subs.controller.js\"    \/\/ import the uploadFile function from the controller folder\r\n\r\nconst router = express.Router()\r\n\r\nrouter.post(\"\/\",uploadFile)    \/\/ define a POST route that calls the uploadFile function\r\n\r\nexport default router     \/\/ export the router to use in the main server.js file<\/code><\/pre>\n<ul id=\"code_id_11\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<li>9.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>\u6b64\u4ee3\u7801\u5b9a\u4e49\u4e86\u670d\u52a1\u5668\u7684\u8def\u7531\uff0c\u7279\u522b\u662f\u5904\u7406\u89c6\u9891\u4e0a\u4f20\u548c\u5b57\u5e55\u751f\u6210\u7684\u8def\u7531\u3002<\/p>\n<p>\u4f7f\u7528express.Router()\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u8def\u7531\u5668\u5b9e\u4f8b\u3002\u8fd9\u4f7f\u6211\u4eec\u80fd\u591f\u5b9a\u4e49\u65b0\u7684\u8def\u7531\uff0c\u8be5\u8def\u7531\u53ef\u4ee5\u72ec\u7acb\u4e8e\u4e3b\u670d\u52a1\u5668\u8def\u7531\uff0c\u4ece\u800c\u6539\u8fdb\u4ee3\u7801\u7ec4\u7ec7\u7ed3\u6784\uff0c\u4f7f\u4e4b\u66f4\u52a0\u6e05\u6670\u3002\u5728 API\u00a0\u63a5\u5165\u70b9\u7684\u6839\u8def\u5f84(&#8220;\/&#8221;)\u5904\u5b9a\u4e49 POST \u8def\u7531\u3002\u5f53\u5bf9\u6b64\u8def\u7531\u53d1\u51fa POST \u8bf7\u6c42\u65f6\uff08\u5f53\u7528\u6237\u5728\u524d\u7aef\u63d0\u4ea4\u89c6\u9891\u4e0a\u4f20\u8868\u5355\u65f6\u4f1a\u53d1\u751f\uff09\uff0c\u5c06\u8c03\u7528uploadFile\u51fd\u6570\u3002\u8be5\u51fd\u6570\u5c06\u5904\u7406\u5b9e\u9645\u7684\u4e0a\u4f20\u548c\u5b57\u5e55\u751f\u6210\u3002<\/p>\n<p>\u6700\u540e\uff0c\u6211\u4eec\u5bfc\u51fa\u8def\u7531\u5668\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u5728\u4e3b\u670d\u52a1\u5668\u6587\u4ef6(server.js)\u4e2d\u4f7f\u7528\u5b83\u6765\u5c06\u6b64\u8def\u7531\u8fde\u63a5\u5230\u4e3b\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<h4>\u914d\u7f6eGemini<\/h4>\n<p>\u63a5\u4e0b\u6765\u7684\u4efb\u52a1\u5c31\u662f\u914d\u7f6e\u5e94\u7528\u7a0b\u5e8f\u5982\u4f55\u4e0e Gemini \u4ea4\u4e92\u3002<\/p>\n<p>\u521b\u5efa\u4e00\u4e2agemini\u6587\u4ef6\u5939\uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u540d\u4e3agemini.config.js\u7684\u65b0\u6587\u4ef6\uff1a<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_12\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-plain\" tabindex=\"0\"><code class=\"language-plain\">\/\/  server\/gemini\/gemini.config.js\r\n\r\nimport {\r\n  GoogleGenerativeAI,\r\n  HarmBlockThreshold,\r\n  HarmCategory,\r\n} from \"@google\/generative-ai\";\r\nimport { configDotenv } from \"dotenv\";\r\nconfigDotenv();\r\n\r\nconst genAI = new GoogleGenerativeAI(process.env.API_KEY);  \/\/ Initialize Google Generative AI with the API key\r\n\r\nconst safetySettings = [\r\n  {\r\n    category: HarmCategory.HARM_CATEGORY_HARASSMENT,\r\n    threshold: HarmBlockThreshold.BLOCK_NONE,\r\n  },\r\n  {\r\n    category: HarmCategory.HARM_CATEGORY_HATE_SPEECH,\r\n    threshold: HarmBlockThreshold.BLOCK_NONE,\r\n  },\r\n  {\r\n    category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,\r\n    threshold: HarmBlockThreshold.BLOCK_NONE,\r\n  },\r\n  {\r\n    category: HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,\r\n    threshold: HarmBlockThreshold.BLOCK_NONE,\r\n  },\r\n];\r\n\r\nconst model = genAI.getGenerativeModel({\r\n  model: \"gemini-1.5-flash-001\",    \/\/choose the model\r\n  safetySettings: safetySettings,   \/\/optional safety settings\r\n});\r\nexport default model;    \/\/export the model<\/code><\/pre>\n<ul id=\"code_id_12\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<li>9.<\/li>\n<li>10.<\/li>\n<li>11.<\/li>\n<li>12.<\/li>\n<li>13.<\/li>\n<li>14.<\/li>\n<li>15.<\/li>\n<li>16.<\/li>\n<li>17.<\/li>\n<li>18.<\/li>\n<li>19.<\/li>\n<li>20.<\/li>\n<li>21.<\/li>\n<li>22.<\/li>\n<li>23.<\/li>\n<li>24.<\/li>\n<li>25.<\/li>\n<li>26.<\/li>\n<li>27.<\/li>\n<li>28.<\/li>\n<li>29.<\/li>\n<li>30.<\/li>\n<li>31.<\/li>\n<li>32.<\/li>\n<li>33.<\/li>\n<li>34.<\/li>\n<li>35.<\/li>\n<li>36.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c safetySettings\u662f\u53ef\u9009\u7684\u3002\u8fd9\u4e9b\u8bbe\u7f6e\u5141\u8bb8\u4f60\u5b9a\u4e49 Gemini \u8f93\u51fa\u4e2d\u6f5c\u5728\u6709\u5bb3\u5185\u5bb9\uff08\u4f8b\u5982\u4ec7\u6068\u8a00\u8bba\u3001\u66b4\u529b\u6216\u9732\u9aa8\u5185\u5bb9\uff09\u7684\u9608\u503c\u3002<\/p>\n<h4>\u521b\u5efa\u4e00\u4e2a\u63a7\u5236\u5668\u6765\u5904\u7406\u7aef\u70b9\u903b\u8f91<\/h4>\n<p>\u521b\u5efa\u4e00\u4e2acontroller\u6587\u4ef6\u5939\uff0c\u5e76\u5728\u5176\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u4e3asubs.controller.js\u7684\u6587\u4ef6\u3002\u5728\u6b64\u6587\u4ef6\u4e2d\uff0c\u4f60\u5c06\u5904\u7406\u4e0e Gemini \u6a21\u578b\u4ea4\u4e92\u7684\u7aef\u70b9\u903b\u8f91\u3002<\/p>\n<p>\u5728 server\/controller\/subs.controller.js \uff0c\u6dfb\u52a0\u8fd9\u6bb5\u4ee3\u7801\uff1a<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_13\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-plain\" tabindex=\"0\"><code class=\"language-plain\">\/\/ server\/controller\/subs.controller.js\r\n\r\nimport { fileURLToPath } from \"url\";\r\nimport path from \"path\";\r\nimport fs from \"fs\";\r\n\r\nconst __filename = fileURLToPath(import.meta.url);  \/\/converts the module URL to a file path\r\nconst __dirname = path.dirname(__filename);   \/\/get the current file directory\r\n\r\nexport const uploadFile = async (req, res) =&gt; {\r\n  try {\r\n    if (!req.files || !req.files.video) {   \/\/if there is no file available, return error to the client\r\n      return res.status(400).json({ error: \"No video uploaded\" });\r\n    }\r\n\r\n    const videoFile = req.files.video;   \/\/access the video\r\n    const uploadDir = path.join(__dirname, \"..\", \"uploads\");   \/\/path to upload the video temporarily\r\n\r\n    if (!fs.existsSync(uploadDir)) {   \/\/check if the directory exists\r\n      fs.mkdirSync(uploadDir);      \/\/if not create a new one\r\n    }\r\n\r\n    const uploadPath = path.join(uploadDir, videoFile.name);  \r\n\r\n    await videoFile.mv(uploadPath);  \/\/it moves the video from the buffer to the \"upload\" folder\r\n\r\n    return res.status(200).json({ message:\"file uploaded sucessfully\" });\r\n  } catch (error) {\r\n    return res\r\n      .status(500)\r\n      .json({ error: \"Internal server error: \" + error.message });\r\n  }\r\n};<\/code><\/pre>\n<ul id=\"code_id_13\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<li>9.<\/li>\n<li>10.<\/li>\n<li>11.<\/li>\n<li>12.<\/li>\n<li>13.<\/li>\n<li>14.<\/li>\n<li>15.<\/li>\n<li>16.<\/li>\n<li>17.<\/li>\n<li>18.<\/li>\n<li>19.<\/li>\n<li>20.<\/li>\n<li>21.<\/li>\n<li>22.<\/li>\n<li>23.<\/li>\n<li>24.<\/li>\n<li>25.<\/li>\n<li>26.<\/li>\n<li>27.<\/li>\n<li>28.<\/li>\n<li>29.<\/li>\n<li>30.<\/li>\n<li>31.<\/li>\n<li>32.<\/li>\n<li>33.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>\u7531\u4e8e\u6211\u4eec\u4f7f\u7528\u7684\u662f ES6 \u6a21\u5757\uff0c\u56e0\u6b64__dirname\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u4e0d\u53ef\u7528\u3002\u4e0e CommonJS \u76f8\u6bd4\uff0c\u6587\u4ef6\u5904\u7406\u673a\u5236\u6709\u6240\u4e0d\u540c\u3002\u56e0\u6b64\uff0c\u5c06\u4f7f\u7528fileURLToPath\u6765\u5904\u7406\u6587\u4ef6\u8def\u5f84\u3002<\/p>\n<p>\u5c06\u6587\u4ef6\u4ece\u9ed8\u8ba4\u7684\u4e34\u65f6\u4f4d\u7f6e\uff08\u7f13\u51b2\u533a\uff09\u79fb\u52a8\u5230uploads\u5939\u3002<\/p>\n<p>\u4f46\u6587\u4ef6\u4e0a\u4f20\u8fc7\u7a0b\u5c1a\u672a\u5b8c\u6210\u3002\u6211\u4eec\u4ecd\u7136\u9700\u8981\u5c06\u6587\u4ef6\u53d1\u9001\u5230Google AI\u6587\u4ef6\u7ba1\u7406\u5668\uff0c\u4e0a\u4f20\u540e\uff0c\u5b83\u4f1a\u8fd4\u56de\u4e00\u4e2aURI\uff0c\u6a21\u578b\u4f1a\u4f7f\u7528\u8fd9\u4e2aURI\u8fdb\u884c\u89c6\u9891\u5206\u6790\u3002<\/p>\n<h4>\u5982\u4f55\u5c06\u6587\u4ef6\u4e0a\u4f20\u5230 Google AI \u6587\u4ef6\u7ba1\u7406\u5668<\/h4>\n<p>\u521b\u5efa\u6587\u4ef6\u5939utils\u5e76\u521b\u5efa\u6587\u4ef6fileUpload.js \u3002\u4f60\u53ef\u4ee5\u53c2\u8003\u4e0a\u9762\u63d0\u4f9b\u7684\u6587\u4ef6\u5939\u7ed3\u6784\u3002<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_14\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-plain\" tabindex=\"0\"><code class=\"language-plain\">\/\/  server\/utils\/fileUpload.js\r\n\r\nimport { GoogleAIFileManager, FileState } from \"@google\/generative-ai\/server\";\r\nimport { configDotenv } from \"dotenv\";\r\nconfigDotenv();\r\n\r\nexport const fileManager = new GoogleAIFileManager(process.env.API_KEY);  \/\/create a new GoogleAIFileManager instance\r\n\r\nexport async function fileUpload(path, videoData) {  \r\n  try {\r\n    const uploadResponse = await fileManager.uploadFile(path, {   \/\/give the path as an argument\r\n      mimeType: videoData.mimetype,  \r\n      displayName: videoData.name,\r\n    });\r\n    const name = uploadResponse.file.name;\r\n    let file = await fileManager.getFile(name);    \r\n    while (file.state === FileState.PROCESSING) {     \/\/check the state of the file\r\n      process.stdout.write(\".\");\r\n      await new Promise((res) =&gt; setTimeout(res, 10000));   \/\/check every 10 second\r\n      file = await fileManager.getFile(name);\r\n    }\r\n    if (file.state === FileState.FAILED) {   \r\n      throw new Error(\"Video processing failed\");\r\n    }\r\n    return file;   \/\/ return the file object, containing the upload file information and the uri\r\n  } catch (error) {\r\n    throw error;\r\n  }\r\n}<\/code><\/pre>\n<ul id=\"code_id_14\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<li>9.<\/li>\n<li>10.<\/li>\n<li>11.<\/li>\n<li>12.<\/li>\n<li>13.<\/li>\n<li>14.<\/li>\n<li>15.<\/li>\n<li>16.<\/li>\n<li>17.<\/li>\n<li>18.<\/li>\n<li>19.<\/li>\n<li>20.<\/li>\n<li>21.<\/li>\n<li>22.<\/li>\n<li>23.<\/li>\n<li>24.<\/li>\n<li>25.<\/li>\n<li>26.<\/li>\n<li>27.<\/li>\n<li>28.<\/li>\n<li>29.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3afileUpload\u7684\u51fd\u6570\uff0c\u5b83\u5e26\u6709\u4e24\u4e2a\u53c2\u6570\u3002\u8fd9\u4e9b\u53c2\u6570\u5c06\u4ece\u63a7\u5236\u5668\u51fd\u6570\u4f20\u9012\uff0c\u6211\u4eec\u7a0d\u540e\u5c06\u5bf9\u5176\u8fdb\u884c\u8bbe\u7f6e\u3002<\/p>\n<p>fileUpload\u51fd\u6570\u4f7f\u7528fileManager.uploadFile\u65b9\u6cd5\u5c06\u89c6\u9891\u53d1\u9001\u5230 Google \u7684\u670d\u52a1\u5668\u3002\u6b64\u65b9\u6cd5\u9700\u8981\u4e24\u4e2a\u53c2\u6570\uff1a\u6587\u4ef6\u8def\u5f84\u548c\u5305\u542b\u6587\u4ef6\u5143\u6570\u636e\uff08\u5176 MIME \u7c7b\u578b\u548c\u663e\u793a\u540d\u79f0\uff09\u7684\u5bf9\u8c61\u3002<\/p>\n<p>\u7531\u4e8e Google \u670d\u52a1\u5668\u4e0a\u7684\u89c6\u9891\u5904\u7406\u9700\u8981\u65f6\u95f4\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u68c0\u67e5\u6587\u4ef6\u7684\u72b6\u6001\u3002\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u5faa\u73af\u6765\u6267\u884c\u6b64\u64cd\u4f5c\uff0c\u8be5\u5faa\u73af\u4f7f\u7528fileManager.getFile()\u6bcf 10 \u79d2\u68c0\u67e5\u4e00\u6b21\u6587\u4ef6\u7684\u72b6\u6001\u3002\u53ea\u8981\u6587\u4ef6\u7684\u72b6\u6001\u4e3aPROCESSING\uff0c\u5faa\u73af\u5c31\u4f1a\u7ee7\u7eed\u3002\u4e00\u65e6\u72b6\u6001\u66f4\u6539\u4e3aSUCCESS\u6216FAILED \uff0c\u5faa\u73af\u5c31\u4f1a\u505c\u6b62\u3002<\/p>\n<p>\u7136\u540e\uff0c\u8be5\u51fd\u6570\u68c0\u67e5\u5904\u7406\u662f\u5426\u6210\u529f\u3002\u5982\u679c\u662f\uff0c\u5219\u8fd4\u56de\u6587\u4ef6\u5bf9\u8c61\uff0c\u5176\u4e2d\u5305\u542b\u6709\u5173\u4e0a\u4f20\u548c\u5904\u7406\u7684\u89c6\u9891\u7684\u4fe1\u606f\uff0c\u5305\u62ec\u5176 URI\u3002\u5426\u5219\uff0c\u5982\u679c\u72b6\u6001\u4e3aFAILED \uff0c\u8be5\u51fd\u6570\u5c06\u5f15\u53d1\u9519\u8bef\u3002<\/p>\n<h4>\u5c06 URI \u4f20\u9012\u7ed9 Gemini \u6a21\u578b<\/h4>\n<p>\u5728utils\u6587\u4ef6\u5939\u4e2d\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3agenContent.js\u7684\u6587\u4ef6\uff1a<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_15\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-plain\" tabindex=\"0\"><code class=\"language-plain\">\/\/ server\/utils\/genContent.js\r\n\r\nimport model from \"..\/gemini\/gemini.config.js\";\r\nimport { configDotenv } from \"dotenv\";\r\nconfigDotenv();\r\n\r\nexport async function getContent(file) {\r\n  try {\r\n    const result = await model.generateContent([\r\n      {\r\n        fileData: {\r\n          mimeType: file.mimeType,\r\n          fileUri: file.uri,\r\n        },\r\n      },\r\n      {\r\n        text: \"You need to write a subtitle for this full video, write the subtitle in the SRT format, don't write anything else other than a subtitle in the response, create accurate subtitle.\",\r\n      },\r\n    ]);\r\n    return result.response.text();\r\n  } catch (error) {\r\n    throw error;\r\n  }\r\n}<\/code><\/pre>\n<ul id=\"code_id_15\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<li>9.<\/li>\n<li>10.<\/li>\n<li>11.<\/li>\n<li>12.<\/li>\n<li>13.<\/li>\n<li>14.<\/li>\n<li>15.<\/li>\n<li>16.<\/li>\n<li>17.<\/li>\n<li>18.<\/li>\n<li>19.<\/li>\n<li>20.<\/li>\n<li>21.<\/li>\n<li>22.<\/li>\n<li>23.<\/li>\n<li>24.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>\u5bfc\u5165\u6211\u4eec\u4e4b\u524d\u914d\u7f6e\u7684\u6a21\u578b\u3002\u521b\u5efa\u4e00\u4e2a\u540d\u4e3agetContent\u7684\u51fd\u6570\u3002 getContent\u51fd\u6570\u83b7\u53d6\u6587\u4ef6\u5bf9\u8c61\uff08\u4ecefileUpload\u51fd\u6570\u8fd4\u56de\uff09\u3002<\/p>\n<p>\u5c06\u6587\u4ef6 URI \u548cmimi\u4f20\u9012\u7ed9\u6a21\u578b\u3002\u7136\u540e\uff0c\u6211\u4eec\u5c06\u63d0\u4f9b\u63d0\u793a\uff0c\u6307\u793a\u6a21\u578b\u4e3a\u6574\u4e2a\u89c6\u9891\u751f\u6210 SRT \u683c\u5f0f\u7684\u5b57\u5e55\u3002\u5982\u679c\u9700\u8981\uff0c\u4f60\u8fd8\u53ef\u4ee5\u6dfb\u52a0\u63d0\u793a\u3002\u7136\u540e\u8fd4\u56de\u54cd\u5e94\u3002<\/p>\n<h4>\u66f4\u65b0subs.controller.js\u6587\u4ef6<\/h4>\n<p>\u6700\u540e\uff0c\u6211\u4eec\u9700\u8981\u66f4\u65b0\u63a7\u5236\u5668\u6587\u4ef6\u3002\u6211\u4eec\u5df2\u7ecf\u521b\u5efa\u4e86fileUpload\u548cgetContent\u51fd\u6570\uff0c\u73b0\u5728\u6211\u4eec\u5c06\u5728\u63a7\u5236\u5668\u4e2d\u4f7f\u7528\u5b83\u4eec\u5e76\u63d0\u4f9b\u6240\u9700\u7684\u53c2\u6570\u3002<\/p>\n<p>\u5728 server\/controller\/subs.controller.js :<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_16\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-plain\" tabindex=\"0\"><code class=\"language-plain\">\/\/  server\/controller\/subs.controller.js\r\n\r\nimport { fileURLToPath } from \"url\";\r\nimport path from \"path\";\r\nimport fs from \"fs\";\r\nimport { fileUpload } from \"..\/utils\/fileUpload.js\";\r\nimport { getContent } from \"..\/utils\/genContent.js\";\r\n\r\nconst __filename = fileURLToPath(import.meta.url);\r\nconst __dirname = path.dirname(__filename);\r\n\r\nexport const uploadFile = async (req, res) =&gt; {\r\n  try {\r\n    if (!req.files || !req.files.video) {\r\n      return res.status(400).json({ error: \"No video uploaded\" });\r\n    }\r\n\r\n    const videoFile = req.files.video;\r\n    const uploadDir = path.join(__dirname, \"..\", \"uploads\");\r\n\r\n    if (!fs.existsSync(uploadDir)) {\r\n      fs.mkdirSync(uploadDir);\r\n    }\r\n\r\n    const uploadPath = path.join(uploadDir, videoFile.name);\r\n\r\n    await videoFile.mv(uploadPath);\r\n\r\n    const response = await fileUpload(uploadPath, req.files.video);  \/\/we pass 'uploadPath' and the video file data to 'fileUpload'\r\n    const genContent = await getContent(response);   \/\/the 'response' (containing the file URI) is passed to 'getContent'\r\n\r\n    return res.status(200).json({ subs: genContent });   \/\/\/\/ return the generated subtitles to the client\r\n  } catch (error) {\r\n    console.error(\"Error uploading video:\", error);\r\n    return res\r\n      .status(500)\r\n      .json({ error: \"Internal server error: \" + error.message });\r\n  }\r\n};<\/code><\/pre>\n<ul id=\"code_id_16\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<li>9.<\/li>\n<li>10.<\/li>\n<li>11.<\/li>\n<li>12.<\/li>\n<li>13.<\/li>\n<li>14.<\/li>\n<li>15.<\/li>\n<li>16.<\/li>\n<li>17.<\/li>\n<li>18.<\/li>\n<li>19.<\/li>\n<li>20.<\/li>\n<li>21.<\/li>\n<li>22.<\/li>\n<li>23.<\/li>\n<li>24.<\/li>\n<li>25.<\/li>\n<li>26.<\/li>\n<li>27.<\/li>\n<li>28.<\/li>\n<li>29.<\/li>\n<li>30.<\/li>\n<li>31.<\/li>\n<li>32.<\/li>\n<li>33.<\/li>\n<li>34.<\/li>\n<li>35.<\/li>\n<li>36.<\/li>\n<li>37.<\/li>\n<li>38.<\/li>\n<li>39.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>\u81f3\u6b64\uff0c\u540e\u53f0API\u5c31\u5b8c\u6210\u4e86\u3002\u73b0\u5728\uff0c\u6211\u4eec\u5c06\u7ee7\u7eed\u66f4\u65b0\u524d\u7aef\u3002<\/p>\n<h3>\u66f4\u65b0\u524d\u7aef<\/h3>\n<p>\u6211\u4eec\u7684\u524d\u7aef\u76ee\u524d\u53ea\u5141\u8bb8\u7528\u6237\u9009\u62e9\u89c6\u9891\u3002\u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u66f4\u65b0\u5b83\u4ee5\u5c06\u89c6\u9891\u6570\u636e\u53d1\u9001\u5230\u540e\u7aef\u8fdb\u884c\u5904\u7406\u3002\u7136\u540e\uff0c\u524d\u7aef\u5c06\u4ece\u540e\u7aef\u63a5\u6536\u751f\u6210\u7684\u5b57\u5e55\u5e76\u542f\u52a8.srt\u6587\u4ef6\u7684\u4e0b\u8f7d\u3002<\/p>\n<p>\u5bfc\u822a\u5230client\u6587\u4ef6\u5939\uff1a<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_17\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-plain\" tabindex=\"0\"><code class=\"language-plain\">cd client<\/code><\/pre>\n<ul id=\"code_id_17\" class=\"pre-numbering\">\n<li>1.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>\u5b89\u88c5axios \u3002\u6211\u4eec\u5c06\u4f7f\u7528\u5b83\u6765\u5904\u7406 HTTP \u8bf7\u6c42\u3002<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_18\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-plain\" tabindex=\"0\"><code class=\"language-plain\">npm install axios<\/code><\/pre>\n<ul id=\"code_id_18\" class=\"pre-numbering\">\n<li>1.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>\u5728client\/src\/App.tsx\u4e2d\uff1a<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_19\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-plain\" tabindex=\"0\"><code class=\"language-plain\">\/\/   client\/src\/App.tsx\r\n\r\nimport axios from \"axios\";\r\n\r\nconst App = () =&gt; {\r\n  const handleSubmit = async (e: React.FormEvent&lt;HTMLFormElement&gt;): Promise&lt;void&gt; =&gt; {\r\n    e.preventDefault();\r\n    try {\r\n      const formData = new FormData(e.currentTarget);\r\n      \/\/ sending a POST request with form data\r\n      const response = await axios.post(\r\n        \"http:\/\/localhost:3000\/api\/subs\/\",   \r\n        formData\r\n      );\r\n\/\/ creating a Blob from the server response and triggering the file download\r\n      const blob = new Blob([response.data.subs], { type: \"text\/plain\" }); \r\n      const link = document.createElement(\"a\");\r\n      link.href = URL.createObjectURL(blob);\r\n      link.download = \"subtitle.srt\";\r\n      link.click();\r\n      link.remove();\r\n    } catch (error) {\r\n      console.log(error);\r\n    }\r\n  };\r\n\r\n  return (\r\n    &lt;div&gt;\r\n      &lt;form onSubmit={handleSubmit}&gt;\r\n        &lt;input type=\"file\" accept=\"video\/*,.mkv\" name=\"video\" \/&gt;\r\n        &lt;input type=\"submit\" \/&gt;\r\n      &lt;\/form&gt;\r\n    &lt;\/div&gt;\r\n  );\r\n};\r\nexport default App;<\/code><\/pre>\n<ul id=\"code_id_19\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<li>9.<\/li>\n<li>10.<\/li>\n<li>11.<\/li>\n<li>12.<\/li>\n<li>13.<\/li>\n<li>14.<\/li>\n<li>15.<\/li>\n<li>16.<\/li>\n<li>17.<\/li>\n<li>18.<\/li>\n<li>19.<\/li>\n<li>20.<\/li>\n<li>21.<\/li>\n<li>22.<\/li>\n<li>23.<\/li>\n<li>24.<\/li>\n<li>25.<\/li>\n<li>26.<\/li>\n<li>27.<\/li>\n<li>28.<\/li>\n<li>29.<\/li>\n<li>30.<\/li>\n<li>31.<\/li>\n<li>32.<\/li>\n<li>33.<\/li>\n<li>34.<\/li>\n<li>35.<\/li>\n<li>36.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>axios\u5411\u540e\u7aef API \u7aef\u70b9(\/api\/subs)\u53d1\u51fa POST \u8bf7\u6c42\u3002\u670d\u52a1\u5668\u5c06\u5904\u7406\u89c6\u9891\uff0c\u8fd9\u53ef\u80fd\u9700\u8981\u4e00\u4e9b\u65f6\u95f4\u3002<\/p>\n<p>\u670d\u52a1\u5668\u53d1\u9001\u751f\u6210\u7684\u5b57\u5e55\u540e\uff0c\u524d\u7aef\u63a5\u6536\u5b83\u4eec\u4f5c\u4e3a\u54cd\u5e94\u3002\u4e3a\u4e86\u5904\u7406\u6b64\u54cd\u5e94\u5e76\u5141\u8bb8\u7528\u6237\u4e0b\u8f7d\u5b57\u5e55\uff0c\u6211\u4eec\u5c06\u4f7f\u7528 Blob\u3002 Blob\uff08\u4e8c\u8fdb\u5236\u5927\u5bf9\u8c61\uff09\u662f\u4e00\u79cd Web API \u5bf9\u8c61\uff0c\u8868\u793a\u539f\u59cb\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u672c\u8d28\u4e0a\u5c31\u50cf\u6587\u4ef6\u4e00\u6837\u3002\u5728\u6211\u4eec\u7684\u4f8b\u5b50\u4e2d\uff0c\u4ece\u670d\u52a1\u5668\u8fd4\u56de\u7684\u5b57\u5e55\u5c06\u88ab\u8f6c\u6362\u4e3a Blob\uff0c\u7136\u540e\u53ef\u4ee5\u5728\u7528\u6237\u7684\u6d4f\u89c8\u5668\u4e2d\u89e6\u53d1\u4e0b\u8f7d\u3002<\/p>\n<h3>\u6982\u62ec<\/h3>\n<p>\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u4f60\u5b66\u4e60\u4e86\u5982\u4f55\u4f7f\u7528 Google \u7684 Gemini API\u3001React \u548c Express \u6784\u5efa\u4eba\u5de5\u667a\u80fd\u9a71\u52a8\u7684\u5b57\u5e55\u751f\u6210\u5668\u3002\u4f60\u53ef\u4ee5\u4e0a\u4f20\u89c6\u9891\uff0c\u53d1\u9001\u5230Gemini API\u8fdb\u884c\u5b57\u5e55\u751f\u6210\uff0c\u5e76\u63d0\u4f9b\u751f\u6210\u7684\u5b57\u5e55\u4f9b\u4e0b\u8f7d\u3002<\/p>\n<h3>\u7ed3\u8bba<\/h3>\n<p>\u5c31\u662f\u8fd9\u6837\uff01\u4f60\u5df2\u4f7f\u7528 Gemini API \u6210\u529f\u6784\u5efa\u4e86\u4eba\u5de5\u667a\u80fd\u9a71\u52a8\u7684\u5b57\u5e55\u751f\u6210\u5668\u3002\u4e3a\u4e86\u66f4\u5feb\u5730\u8fdb\u884c\u6d4b\u8bd5\uff0c\u8bf7\u4ece\u8f83\u77ed\u7684\u89c6\u9891\u526a\u8f91\uff083-5 \u5206\u949f\uff09\u5f00\u59cb\u3002\u8f83\u957f\u7684\u89c6\u9891\u53ef\u80fd\u9700\u8981\u66f4\u591a\u65f6\u95f4\u6765\u5904\u7406\u3002<\/p>\n<p>\u60f3\u8981\u521b\u5efa\u53ef\u5b9a\u5236\u7684\u89c6\u9891\u63d0\u793a\u5e94\u7528\u7a0b\u5e8f\u5417\uff1f\u53ea\u9700\u6dfb\u52a0\u4e00\u4e2a\u8f93\u5165\u5b57\u6bb5\uff0c\u8ba9\u7528\u6237\u8f93\u5165\u63d0\u793a\uff0c\u5c06\u8be5\u63d0\u793a\u53d1\u9001\u5230\u670d\u52a1\u5668\uff0c\u5e76\u4f7f\u7528\u5b83\u4ee3\u66ff\u786c\u7f16\u7801\u7684\u63d0\u793a\u3002\u4ec5\u6b64\u800c\u5df2\u3002<\/p>\n<h3>\u8bd1\u8005\u4ecb\u7ecd<\/h3>\n<p>\u5d14\u7693\uff0c51CTO\u793e\u533a\u7f16\u8f91\uff0c\u8d44\u6df1\u67b6\u6784\u5e08\uff0c\u62e5\u670918\u5e74\u7684\u8f6f\u4ef6\u5f00\u53d1\u548c\u67b6\u6784\u7ecf\u9a8c\uff0c10\u5e74\u5206\u5e03\u5f0f\u67b6\u6784\u7ecf\u9a8c\u3002<\/p>\n<p>\u6587\u7ae0\u6765\u81ea\uff1a51CTO<\/p>\n<\/div>\n<\/div>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_23648\" class=\"pvc_stats total_only  \" data-element-id=\"23648\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" version=\"1.0\" viewBox=\"0 0 502 315\" preserveAspectRatio=\"xMidYMid meet\"><g transform=\"translate(0,332) scale(0.1,-0.1)\" fill=\"\" stroke=\"none\"><path d=\"M2394 3279 l-29 -30 -3 -207 c-2 -182 0 -211 15 -242 39 -76 157 -76 196 0 15 31 17 60 15 243 l-3 209 -33 29 c-26 23 -41 29 -80 29 -41 0 -53 -5 -78 -31z\"\/><path d=\"M3085 3251 c-45 -19 -58 -50 -96 -229 -47 -217 -49 -260 -13 -295 52 -53 146 -42 177 20 16 31 87 366 87 410 0 70 -86 122 -155 94z\"\/><path d=\"M1751 3234 c-13 -9 -29 -31 -37 -50 -12 -29 -10 -49 21 -204 19 -94 39 -189 45 -210 14 -50 54 -80 110 -80 34 0 48 6 76 34 21 21 34 44 34 59 0 14 -18 113 -40 219 -37 178 -43 195 -70 221 -36 32 -101 37 -139 11z\"\/><path d=\"M1163 3073 c-36 -7 -73 -59 -73 -102 0 -56 133 -378 171 -413 34 -32 83 -37 129 -13 70 36 67 87 -16 290 -86 209 -89 214 -129 231 -35 14 -42 15 -82 7z\"\/><path d=\"M3689 3066 c-15 -9 -33 -30 -42 -48 -48 -103 -147 -355 -147 -375 0 -98 131 -148 192 -74 13 15 57 108 97 206 80 196 84 226 37 273 -30 30 -99 39 -137 18z\"\/><path d=\"M583 2784 c-38 -19 -67 -74 -58 -113 9 -42 211 -354 242 -373 16 -10 45 -18 66 -18 51 0 107 52 107 100 0 39 -1 41 -124 234 -80 126 -108 162 -133 173 -41 17 -61 16 -100 -3z\"\/><path d=\"M4250 2784 c-14 -9 -74 -91 -133 -183 -95 -150 -107 -173 -107 -213 0 -55 33 -94 87 -104 67 -13 90 8 211 198 130 202 137 225 78 284 -27 27 -42 34 -72 34 -22 0 -50 -8 -64 -16z\"\/><path d=\"M2275 2693 c-553 -48 -1095 -270 -1585 -649 -135 -104 -459 -423 -483 -476 -23 -49 -22 -139 2 -186 73 -142 361 -457 571 -626 285 -228 642 -407 990 -497 242 -63 336 -73 660 -74 310 0 370 5 595 52 535 111 1045 392 1455 803 122 121 250 273 275 326 19 41 19 137 0 174 -41 79 -309 363 -465 492 -447 370 -946 591 -1479 653 -113 14 -422 18 -536 8z m395 -428 c171 -34 330 -124 456 -258 112 -119 167 -219 211 -378 27 -96 24 -300 -5 -401 -72 -255 -236 -447 -474 -557 -132 -62 -201 -76 -368 -76 -167 0 -236 14 -368 76 -213 98 -373 271 -451 485 -162 444 86 934 547 1084 153 49 292 57 452 25z m909 -232 c222 -123 408 -262 593 -441 76 -74 138 -139 138 -144 0 -16 -233 -242 -330 -319 -155 -123 -309 -223 -461 -299 l-81 -41 32 46 c18 26 49 83 70 128 143 306 141 649 -6 957 -25 52 -61 116 -79 142 l-34 47 45 -20 c26 -10 76 -36 113 -56z m-2057 25 c-40 -58 -105 -190 -130 -263 -110 -324 -59 -707 132 -981 25 -35 42 -64 37 -64 -19 0 -241 119 -326 174 -188 122 -406 314 -532 468 l-58 71 108 103 c185 178 428 349 672 473 66 33 121 60 123 61 2 0 -10 -19 -26 -42z\"\/><path d=\"M2375 1950 c-198 -44 -350 -190 -395 -379 -18 -76 -8 -221 19 -290 114 -284 457 -406 731 -260 98 52 188 154 231 260 27 69 37 214 19 290 -38 163 -166 304 -326 360 -67 23 -215 33 -279 19z\"\/><\/g><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/aif.amtbbs.org\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u4f60\u5c06\u4f7f\u7528 Google \u7684 Gemini API \u6784\u5efa\u4eba\u5de5\u667a\u80fd\u9a71\u52a8\u7684\u5b57\u5e55\u751f\u6210\u5668\u3002\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a [&hellip;]<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_23648\" class=\"pvc_stats total_only  \" data-element-id=\"23648\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" version=\"1.0\" viewBox=\"0 0 502 315\" preserveAspectRatio=\"xMidYMid meet\"><g transform=\"translate(0,332) scale(0.1,-0.1)\" fill=\"\" stroke=\"none\"><path d=\"M2394 3279 l-29 -30 -3 -207 c-2 -182 0 -211 15 -242 39 -76 157 -76 196 0 15 31 17 60 15 243 l-3 209 -33 29 c-26 23 -41 29 -80 29 -41 0 -53 -5 -78 -31z\"\/><path d=\"M3085 3251 c-45 -19 -58 -50 -96 -229 -47 -217 -49 -260 -13 -295 52 -53 146 -42 177 20 16 31 87 366 87 410 0 70 -86 122 -155 94z\"\/><path d=\"M1751 3234 c-13 -9 -29 -31 -37 -50 -12 -29 -10 -49 21 -204 19 -94 39 -189 45 -210 14 -50 54 -80 110 -80 34 0 48 6 76 34 21 21 34 44 34 59 0 14 -18 113 -40 219 -37 178 -43 195 -70 221 -36 32 -101 37 -139 11z\"\/><path d=\"M1163 3073 c-36 -7 -73 -59 -73 -102 0 -56 133 -378 171 -413 34 -32 83 -37 129 -13 70 36 67 87 -16 290 -86 209 -89 214 -129 231 -35 14 -42 15 -82 7z\"\/><path d=\"M3689 3066 c-15 -9 -33 -30 -42 -48 -48 -103 -147 -355 -147 -375 0 -98 131 -148 192 -74 13 15 57 108 97 206 80 196 84 226 37 273 -30 30 -99 39 -137 18z\"\/><path d=\"M583 2784 c-38 -19 -67 -74 -58 -113 9 -42 211 -354 242 -373 16 -10 45 -18 66 -18 51 0 107 52 107 100 0 39 -1 41 -124 234 -80 126 -108 162 -133 173 -41 17 -61 16 -100 -3z\"\/><path d=\"M4250 2784 c-14 -9 -74 -91 -133 -183 -95 -150 -107 -173 -107 -213 0 -55 33 -94 87 -104 67 -13 90 8 211 198 130 202 137 225 78 284 -27 27 -42 34 -72 34 -22 0 -50 -8 -64 -16z\"\/><path d=\"M2275 2693 c-553 -48 -1095 -270 -1585 -649 -135 -104 -459 -423 -483 -476 -23 -49 -22 -139 2 -186 73 -142 361 -457 571 -626 285 -228 642 -407 990 -497 242 -63 336 -73 660 -74 310 0 370 5 595 52 535 111 1045 392 1455 803 122 121 250 273 275 326 19 41 19 137 0 174 -41 79 -309 363 -465 492 -447 370 -946 591 -1479 653 -113 14 -422 18 -536 8z m395 -428 c171 -34 330 -124 456 -258 112 -119 167 -219 211 -378 27 -96 24 -300 -5 -401 -72 -255 -236 -447 -474 -557 -132 -62 -201 -76 -368 -76 -167 0 -236 14 -368 76 -213 98 -373 271 -451 485 -162 444 86 934 547 1084 153 49 292 57 452 25z m909 -232 c222 -123 408 -262 593 -441 76 -74 138 -139 138 -144 0 -16 -233 -242 -330 -319 -155 -123 -309 -223 -461 -299 l-81 -41 32 46 c18 26 49 83 70 128 143 306 141 649 -6 957 -25 52 -61 116 -79 142 l-34 47 45 -20 c26 -10 76 -36 113 -56z m-2057 25 c-40 -58 -105 -190 -130 -263 -110 -324 -59 -707 132 -981 25 -35 42 -64 37 -64 -19 0 -241 119 -326 174 -188 122 -406 314 -532 468 l-58 71 108 103 c185 178 428 349 672 473 66 33 121 60 123 61 2 0 -10 -19 -26 -42z\"\/><path d=\"M2375 1950 c-198 -44 -350 -190 -395 -379 -18 -76 -8 -221 19 -290 114 -284 457 -406 731 -260 98 52 188 154 231 260 27 69 37 214 19 290 -38 163 -166 304 -326 360 -67 23 -215 33 -279 19z\"\/><\/g><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/aif.amtbbs.org\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":56,"featured_media":23650,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,20,80],"tags":[1496,1495,113],"class_list":["post-23648","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-23","category-20","category-80","tag-api-","tag-gemini-api","tag-113"],"_links":{"self":[{"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/posts\/23648","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/users\/56"}],"replies":[{"embeddable":true,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/comments?post=23648"}],"version-history":[{"count":1,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/posts\/23648\/revisions"}],"predecessor-version":[{"id":23654,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/posts\/23648\/revisions\/23654"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/media\/23650"}],"wp:attachment":[{"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/media?parent=23648"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/categories?post=23648"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/tags?post=23648"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}