TestChatMessage.html 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. <!doctype html>
  2. <html class="no-js" lang="">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="x-ua-compatible" content="ie=edge">
  6. <title>app-angular documentation</title>
  7. <meta name="description" content="">
  8. <meta name="viewport" content="width=device-width, initial-scale=1">
  9. <link rel="icon" type="image/x-icon" href="../images/favicon.ico">
  10. <link rel="stylesheet" href="../styles/style.css">
  11. </head>
  12. <body>
  13. <div class="navbar navbar-default navbar-fixed-top visible-xs">
  14. <a href="../" class="navbar-brand">app-angular documentation</a>
  15. <button type="button" class="btn btn-default btn-menu fa fa-bars" id="btn-menu"></button>
  16. </div>
  17. <div class="xs-menu menu" id="mobile-menu">
  18. <div id="book-search-input" role="search">
  19. <input type="text" placeholder="Type to search">
  20. </div>
  21. <nav>
  22. <ul class="list">
  23. <li class="title">
  24. <a href="../">app-angular documentation</a>
  25. </li>
  26. <li class="divider"></li>
  27. <li class="chapter">
  28. <a data-type="chapter-link" href="../"><span class="fa fa-home"></span>Getting started</a>
  29. <ul class="links">
  30. <li class="link">
  31. <a href="../" ><span class="fa fa-file-text-o"></span>README</a>
  32. </li>
  33. <li class="link">
  34. <a
  35. href="../overview.html"
  36. href="../overview.html"
  37. >
  38. <span class="fa fa-th"></span>Overview
  39. </a>
  40. </li>
  41. </ul>
  42. </li>
  43. <li class="chapter">
  44. <a data-type="chapter-link" href="./modules.html" >
  45. <span class="fa fa-archive"></span>
  46. <span>Modules</span>
  47. <span class="menu-toggler fa fa-angle-up" data-toggle="collapse"
  48. data-target="#xs-modules-links"
  49. ></span>
  50. </a>
  51. <ul class="links collapse in"
  52. id="xs-modules-links"
  53. >
  54. <li class="link">
  55. <a href="../modules/AppModule.html" >AppModule</a>
  56. </li>
  57. <li class="link">
  58. <a href="../modules/LessonModule.html" >LessonModule</a>
  59. </li>
  60. </ul>
  61. </li>
  62. <li class="chapter">
  63. <div class="simple">
  64. <span class="fa fa-cogs"></span>
  65. <span>Components</span>
  66. <span class="menu-toggler fa fa-angle-up" data-toggle="collapse"
  67. data-target="#xs-components-links"
  68. ></span>
  69. </div>
  70. <ul class="links collapse in"
  71. id="xs-components-links"
  72. >
  73. <li class="link">
  74. <a href="../components/AppComponent.html" >AppComponent</a>
  75. </li>
  76. <li class="link">
  77. <a href="../components/BblComponent.html" >BblComponent</a>
  78. </li>
  79. <li class="link">
  80. <a href="../components/DetailComponent.html" >DetailComponent</a>
  81. </li>
  82. <li class="link">
  83. <a href="../components/EcharsComponent.html" >EcharsComponent</a>
  84. </li>
  85. <li class="link">
  86. <a href="../components/EchartsExempleComponent.html" >EchartsExempleComponent</a>
  87. </li>
  88. <li class="link">
  89. <a href="../components/EditComponent.html" >EditComponent</a>
  90. </li>
  91. <li class="link">
  92. <a href="../components/FlowfatherComponent.html" >FlowfatherComponent</a>
  93. </li>
  94. <li class="link">
  95. <a href="../components/FlowtestComponent.html" >FlowtestComponent</a>
  96. </li>
  97. <li class="link">
  98. <a href="../components/HomeComponent.html" >HomeComponent</a>
  99. </li>
  100. <li class="link">
  101. <a href="../components/NavTabsComponent.html" >NavTabsComponent</a>
  102. </li>
  103. <li class="link">
  104. <a href="../components/PageMineComponent.html" >PageMineComponent</a>
  105. </li>
  106. <li class="link">
  107. <a href="../components/PageSpotDetailComponent.html" >PageSpotDetailComponent</a>
  108. </li>
  109. </ul>
  110. </li>
  111. <li class="chapter">
  112. <div class="simple">
  113. <span class="fa fa-file-code-o"></span>
  114. <span>Classes</span>
  115. <span class="menu-toggler fa fa-angle-up" data-toggle="collapse"
  116. data-target="#xs-classes-links"
  117. ></span>
  118. </div>
  119. <ul class="links collapse in"
  120. id="xs-classes-links"
  121. >
  122. <li class="link">
  123. <a href="../classes/TestChatCompletion.html" >TestChatCompletion</a>
  124. </li>
  125. </ul>
  126. </li>
  127. <li class="chapter">
  128. <div class="simple">
  129. <span class="fa fa-long-arrow-down"></span>
  130. <span>Injectables</span>
  131. <span class="menu-toggler fa fa-angle-up" data-toggle="collapse"
  132. data-target="#xs-injectables-links"
  133. ></span>
  134. </div>
  135. <ul class="links collapse in"
  136. id="xs-injectables-links"
  137. >
  138. <li class="link">
  139. <a href="../injectables/DataServiceService.html" >DataServiceService</a>
  140. </li>
  141. </ul>
  142. </li>
  143. <li class="chapter">
  144. <div class="simple">
  145. <span class="fa fa-info"></span>
  146. <span>Interfaces</span>
  147. <span class="menu-toggler fa fa-angle-up" data-toggle="collapse"
  148. data-target="#xs-interfaces-links"
  149. ></span>
  150. </div>
  151. <ul class="links collapse in"
  152. id="xs-interfaces-links"
  153. >
  154. <li class="link">
  155. <a href="../interfaces/TestChatMessage.html" class="active" >TestChatMessage</a>
  156. </li>
  157. </ul>
  158. </li>
  159. <li class="chapter">
  160. <div class="simple">
  161. <span class="fa fa-plus"></span>
  162. <span>Pipes</span>
  163. <span class="menu-toggler fa fa-angle-up" data-toggle="collapse"
  164. data-target="#xs-pipes-links"
  165. ></span>
  166. </div>
  167. <ul class="links collapse in"
  168. id="xs-pipes-links"
  169. >
  170. <li class="link">
  171. <a href="../pipes/TokPipe.html" >TokPipe</a>
  172. </li>
  173. </ul>
  174. </li>
  175. <li class="chapter">
  176. <a data-type="chapter-link" href="../coverage.html" ><span class="fa fa-tasks"></span>Documentation coverage</a>
  177. </li>
  178. <li class="divider"></li>
  179. <li class="copyright">
  180. Documentation generated using <a href="https://compodoc.github.io/compodoc/" target="_blank">
  181. <img src="..//images/compodoc-vectorise.svg" class="img-responsive">
  182. </a>
  183. </li>
  184. </ul>
  185. </nav>
  186. </div>
  187. <div class="container-fluid main">
  188. <div class="row main">
  189. <div class="hidden-xs menu">
  190. <nav>
  191. <ul class="list">
  192. <li class="title">
  193. <a href="../">app-angular documentation</a>
  194. </li>
  195. <li class="divider"></li>
  196. <div id="book-search-input" role="search">
  197. <input type="text" placeholder="Type to search">
  198. </div>
  199. <li class="chapter">
  200. <a data-type="chapter-link" href="../"><span class="fa fa-home"></span>Getting started</a>
  201. <ul class="links">
  202. <li class="link">
  203. <a href="../" ><span class="fa fa-file-text-o"></span>README</a>
  204. </li>
  205. <li class="link">
  206. <a
  207. href="../overview.html"
  208. href="../overview.html"
  209. >
  210. <span class="fa fa-th"></span>Overview
  211. </a>
  212. </li>
  213. </ul>
  214. </li>
  215. <li class="chapter">
  216. <a data-type="chapter-link" href="./modules.html" >
  217. <span class="fa fa-archive"></span>
  218. <span>Modules</span>
  219. <span class="menu-toggler fa fa-angle-up" data-toggle="collapse"
  220. data-target="#modules-links"
  221. ></span>
  222. </a>
  223. <ul class="links collapse in"
  224. id="modules-links"
  225. >
  226. <li class="link">
  227. <a href="../modules/AppModule.html" >AppModule</a>
  228. </li>
  229. <li class="link">
  230. <a href="../modules/LessonModule.html" >LessonModule</a>
  231. </li>
  232. </ul>
  233. </li>
  234. <li class="chapter">
  235. <div class="simple">
  236. <span class="fa fa-cogs"></span>
  237. <span>Components</span>
  238. <span class="menu-toggler fa fa-angle-up" data-toggle="collapse"
  239. data-target="#components-links"
  240. ></span>
  241. </div>
  242. <ul class="links collapse in"
  243. id="components-links"
  244. >
  245. <li class="link">
  246. <a href="../components/AppComponent.html" >AppComponent</a>
  247. </li>
  248. <li class="link">
  249. <a href="../components/BblComponent.html" >BblComponent</a>
  250. </li>
  251. <li class="link">
  252. <a href="../components/DetailComponent.html" >DetailComponent</a>
  253. </li>
  254. <li class="link">
  255. <a href="../components/EcharsComponent.html" >EcharsComponent</a>
  256. </li>
  257. <li class="link">
  258. <a href="../components/EchartsExempleComponent.html" >EchartsExempleComponent</a>
  259. </li>
  260. <li class="link">
  261. <a href="../components/EditComponent.html" >EditComponent</a>
  262. </li>
  263. <li class="link">
  264. <a href="../components/FlowfatherComponent.html" >FlowfatherComponent</a>
  265. </li>
  266. <li class="link">
  267. <a href="../components/FlowtestComponent.html" >FlowtestComponent</a>
  268. </li>
  269. <li class="link">
  270. <a href="../components/HomeComponent.html" >HomeComponent</a>
  271. </li>
  272. <li class="link">
  273. <a href="../components/NavTabsComponent.html" >NavTabsComponent</a>
  274. </li>
  275. <li class="link">
  276. <a href="../components/PageMineComponent.html" >PageMineComponent</a>
  277. </li>
  278. <li class="link">
  279. <a href="../components/PageSpotDetailComponent.html" >PageSpotDetailComponent</a>
  280. </li>
  281. </ul>
  282. </li>
  283. <li class="chapter">
  284. <div class="simple">
  285. <span class="fa fa-file-code-o"></span>
  286. <span>Classes</span>
  287. <span class="menu-toggler fa fa-angle-up" data-toggle="collapse"
  288. data-target="#classes-links"
  289. ></span>
  290. </div>
  291. <ul class="links collapse in"
  292. id="classes-links"
  293. >
  294. <li class="link">
  295. <a href="../classes/TestChatCompletion.html" >TestChatCompletion</a>
  296. </li>
  297. </ul>
  298. </li>
  299. <li class="chapter">
  300. <div class="simple">
  301. <span class="fa fa-long-arrow-down"></span>
  302. <span>Injectables</span>
  303. <span class="menu-toggler fa fa-angle-up" data-toggle="collapse"
  304. data-target="#injectables-links"
  305. ></span>
  306. </div>
  307. <ul class="links collapse in"
  308. id="injectables-links"
  309. >
  310. <li class="link">
  311. <a href="../injectables/DataServiceService.html" >DataServiceService</a>
  312. </li>
  313. </ul>
  314. </li>
  315. <li class="chapter">
  316. <div class="simple">
  317. <span class="fa fa-info"></span>
  318. <span>Interfaces</span>
  319. <span class="menu-toggler fa fa-angle-up" data-toggle="collapse"
  320. data-target="#interfaces-links"
  321. ></span>
  322. </div>
  323. <ul class="links collapse in"
  324. id="interfaces-links"
  325. >
  326. <li class="link">
  327. <a href="../interfaces/TestChatMessage.html" class="active" >TestChatMessage</a>
  328. </li>
  329. </ul>
  330. </li>
  331. <li class="chapter">
  332. <div class="simple">
  333. <span class="fa fa-plus"></span>
  334. <span>Pipes</span>
  335. <span class="menu-toggler fa fa-angle-up" data-toggle="collapse"
  336. data-target="#pipes-links"
  337. ></span>
  338. </div>
  339. <ul class="links collapse in"
  340. id="pipes-links"
  341. >
  342. <li class="link">
  343. <a href="../pipes/TokPipe.html" >TokPipe</a>
  344. </li>
  345. </ul>
  346. </li>
  347. <li class="chapter">
  348. <a data-type="chapter-link" href="../coverage.html" ><span class="fa fa-tasks"></span>Documentation coverage</a>
  349. </li>
  350. <li class="divider"></li>
  351. <li class="copyright">
  352. Documentation generated using <a href="https://compodoc.github.io/compodoc/" target="_blank">
  353. <img src="..//images/compodoc-vectorise.svg" class="img-responsive">
  354. </a>
  355. </li>
  356. </ul>
  357. </nav>
  358. </div>
  359. <div class="content interface">
  360. <div class="content-data">
  361. <ol class="breadcrumb">
  362. <li>Interfaces</li>
  363. <li>TestChatMessage</li>
  364. </ol>
  365. <ul class="nav nav-tabs" role="tablist">
  366. <li class="active">
  367. <a href="#infos" id="infos-tab" role="tab" data-toggle="tab">Infos</a>
  368. </li>
  369. <li>
  370. <a href="#source" role="tab" id="source-tab" data-toggle="tab">Source</a>
  371. </li>
  372. </ul>
  373. <div class="tab-content">
  374. <div class="tab-pane fade active in" id="infos">
  375. <p class="comment">
  376. <h3>File</h3>
  377. </p>
  378. <p class="comment">
  379. <code>src/modules/lesson/page-mine/class-chat-completion.ts</code>
  380. </p>
  381. <section>
  382. <h3 id="inputs">Properties</h3>
  383. <table class="table table-sm table-bordered">
  384. <tbody>
  385. <tr>
  386. <td class="col-md-4">
  387. <b>content</b>
  388. </td>
  389. </tr>
  390. <tr>
  391. <td class="col-md-4">
  392. <code>content: <code>string</code>
  393. </bcode>
  394. </td>
  395. </tr>
  396. <tr>
  397. <td class="col-md-4">
  398. <i>Type : </i> <code>string</code>
  399. </td>
  400. </tr>
  401. </tbody>
  402. </table>
  403. <table class="table table-sm table-bordered">
  404. <tbody>
  405. <tr>
  406. <td class="col-md-4">
  407. <b>role</b>
  408. </td>
  409. </tr>
  410. <tr>
  411. <td class="col-md-4">
  412. <code>role: <code>string</code>
  413. </bcode>
  414. </td>
  415. </tr>
  416. <tr>
  417. <td class="col-md-4">
  418. <i>Type : </i> <code>string</code>
  419. </td>
  420. </tr>
  421. </tbody>
  422. </table>
  423. </section>
  424. </div>
  425. <div class="tab-pane fade tab-source-code" id="source">
  426. <pre><code class="hljs javascript">export interface TestChatMessage {
  427. role: string
  428. content: string
  429. }
  430. // 导出类TestChatCompletion,用于创建聊天完成
  431. export class TestChatCompletion {
  432. // 定义一个消息列表
  433. messageList: Array&lt;TestChatMessage&gt;
  434. // 构造函数,传入消息列表
  435. constructor(messageList: Array&lt;TestChatMessage&gt;) {
  436. this.messageList &#x3D; messageList
  437. }
  438. // 异步创建完成,使用流模式
  439. async createCompletionByStream() {
  440. // 从本地存储中获取token
  441. let token &#x3D; localStorage.getItem(&quot;token&quot;);
  442. // 定义请求体
  443. let bodyJson &#x3D; {
  444. &quot;token&quot;: &#x60;Bearer ${token}&#x60;,
  445. &quot;messages&quot;: this.messageList,
  446. &quot;model&quot;: &quot;gpt-3.5-turbo&quot;,
  447. &quot;temperature&quot;: 0.5,
  448. &quot;presence_penalty&quot;: 0,
  449. &quot;frequency_penalty&quot;: 0,
  450. &quot;top_p&quot;: 1,
  451. &quot;stream&quot;: true
  452. };
  453. // 发起请求
  454. let response &#x3D; await fetch(&quot;https://test.fmode.cn/api/apig/aigc/gpt/v1/chat/completions&quot;, {
  455. &quot;headers&quot;: {
  456. &quot;accept&quot;: &quot;text/event-stream&quot;,
  457. &quot;sec-fetch-dest&quot;: &quot;empty&quot;,
  458. &quot;sec-fetch-mode&quot;: &quot;cors&quot;,
  459. &quot;sec-fetch-site&quot;: &quot;same-site&quot;
  460. },
  461. &quot;referrer&quot;: &quot;https://ai.fmode.cn/&quot;,
  462. &quot;referrerPolicy&quot;: &quot;strict-origin-when-cross-origin&quot;,
  463. &quot;body&quot;: JSON.stringify(bodyJson),
  464. &quot;method&quot;: &quot;POST&quot;,
  465. &quot;mode&quot;: &quot;cors&quot;,
  466. &quot;credentials&quot;: &quot;omit&quot;
  467. });
  468. // 定义消息回复
  469. let messageAiReply &#x3D; &quot;&quot;
  470. // 定义消息索引
  471. let messageIndex &#x3D; this.messageList.length
  472. // 获取响应体读取器
  473. let reader &#x3D; response.body?.getReader();
  474. if (!reader) {
  475. throw new Error(&quot;Failed to get the response reader.&quot;);
  476. }
  477. // 定义文本解码器
  478. let decoder &#x3D; new TextDecoder();
  479. let buffer &#x3D; &quot;&quot;;
  480. // 循环读取响应体
  481. while (true) {
  482. let { done, value } &#x3D; await reader.read();
  483. if (done) {
  484. break;
  485. }
  486. // 将响应体添加到缓冲区
  487. buffer +&#x3D; decoder.decode(value);
  488. // Split the buffer by newlines to get individual messages
  489. // 将缓冲区按换行符分割,获取每条消息
  490. let messages &#x3D; buffer.split(&quot;\n&quot;);
  491. // Process each message
  492. // 循环处理每条消息
  493. for (let i &#x3D; 0; i &lt; messages.length - 1; i++) {
  494. let message &#x3D; messages[i];
  495. // Process the message as needed
  496. // 处理消息,需要的操作
  497. /**
  498. * data: {&quot;id&quot;:&quot;chatcmpl-y2PLKqPDnwAFJIj2L5aqdH5TWK9Yv&quot;,&quot;object&quot;:&quot;chat.completion.chunk&quot;,&quot;created&quot;:1696770162,&quot;model&quot;:&quot;gpt-3.5-turbo-0613&quot;,&quot;choices&quot;:[{&quot;index&quot;:0,&quot;delta&quot;:{&quot;role&quot;:&quot;assistant&quot;,&quot;content&quot;:&quot;&quot;},&quot;finish_reason&quot;:null}]}
  499. * data: {&quot;id&quot;:&quot;chatcmpl-y2PLKqPDnwAFJIj2L5aqdH5TWK9Yv&quot;,&quot;object&quot;:&quot;chat.completion.chunk&quot;,&quot;created&quot;:1696770162,&quot;model&quot;:&quot;gpt-3.5-turbo-0613&quot;,&quot;choices&quot;:[{&quot;index&quot;:0,&quot;delta&quot;:{},&quot;finish_reason&quot;:&quot;stop&quot;}]}
  500. * data: [DONE]
  501. */
  502. let dataText &#x3D; message.replace(&quot;data:\ &quot;, &quot;&quot;)
  503. if (dataText.startsWith(&quot;{&quot;)) {
  504. try {
  505. let dataJson &#x3D; JSON.parse(dataText)
  506. console.log(dataJson)
  507. messageAiReply +&#x3D; dataJson?.choices?.[0]?.delta?.content || &quot;&quot;
  508. this.messageList[messageIndex] &#x3D; {
  509. role: &quot;assistant&quot;,
  510. content: messageAiReply
  511. }
  512. } catch (err) { }
  513. }
  514. if (dataText.startsWith(&quot;[&quot;)) {
  515. console.log(message)
  516. console.log(&quot;完成&quot;)
  517. this.messageList[messageIndex] &#x3D; {
  518. role: &quot;assistant&quot;,
  519. content: messageAiReply
  520. }
  521. messageAiReply &#x3D; &quot;&quot;
  522. }
  523. // Parse the message as JSON
  524. // 解析消息,获取每条消息的JSON
  525. // let data &#x3D; JSON.parse(message);
  526. // Clear the processed message from the buffer
  527. // 清理已处理的响应消息
  528. buffer &#x3D; buffer.slice(message.length + 1);
  529. }
  530. }
  531. }
  532. }</code></pre>
  533. </div>
  534. </div>
  535. </div><div class="search-results">
  536. <div class="has-results">
  537. <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
  538. <ul class="search-results-list"></ul>
  539. </div>
  540. <div class="no-results">
  541. <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
  542. </div>
  543. </div>
  544. </div>
  545. </div>
  546. </div>
  547. <script src="../js/libs/bootstrap-native.js"></script>
  548. <script src="../js/libs/es6-shim.min.js"></script>
  549. <script src="../js/libs/EventDispatcher.js"></script>
  550. <script src="../js/libs/promise.min.js"></script>
  551. <script src="../js/libs/zepto.min.js"></script>
  552. <script src="../js/compodoc.js"></script>
  553. <script src="../js/search/search.js"></script>
  554. <script src="../js/search/lunr.min.js"></script>
  555. <script src="../js/search/search-lunr.js"></script>
  556. <script src="../js/menu.js"></script>
  557. <script src="../js/libs/highlight.pack.js"></script>
  558. <script src="../js/libs/highlightjs-line-numbers.min.js"></script>
  559. <script src="../js/search/search_index.js"></script>
  560. <script>
  561. document.addEventListener('DOMContentLoaded', function() {
  562. hljs.initHighlightingOnLoad();
  563. hljs.initLineNumbersOnLoad();
  564. });
  565. </script>
  566. </body>
  567. </html>