123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658 |
- <!doctype html>
- <html class="no-js" lang="">
- <head>
- <meta charset="utf-8">
- <meta http-equiv="x-ua-compatible" content="ie=edge">
- <title>app-angular documentation</title>
- <meta name="description" content="">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <link rel="icon" type="image/x-icon" href="../images/favicon.ico">
- <link rel="stylesheet" href="../styles/style.css">
- </head>
- <body>
- <div class="navbar navbar-default navbar-fixed-top visible-xs">
- <a href="../" class="navbar-brand">app-angular documentation</a>
- <button type="button" class="btn btn-default btn-menu fa fa-bars" id="btn-menu"></button>
- </div>
- <div class="xs-menu menu" id="mobile-menu">
- <div id="book-search-input" role="search">
- <input type="text" placeholder="Type to search">
- </div>
- <nav>
-
- <ul class="list">
-
- <li class="title">
- <a href="../">app-angular documentation</a>
- </li>
-
- <li class="divider"></li>
-
-
- <li class="chapter">
- <a data-type="chapter-link" href="../"><span class="fa fa-home"></span>Getting started</a>
- <ul class="links">
- <li class="link">
- <a href="../" ><span class="fa fa-file-text-o"></span>README</a>
- </li>
- <li class="link">
- <a
-
- href="../overview.html"
- href="../overview.html"
- >
- <span class="fa fa-th"></span>Overview
- </a>
- </li>
- </ul>
- </li>
-
- <li class="chapter">
- <a data-type="chapter-link" href="./modules.html" >
- <span class="fa fa-archive"></span>
- <span>Modules</span>
- <span class="menu-toggler fa fa-angle-up" data-toggle="collapse"
- data-target="#xs-modules-links"
- ></span>
- </a>
- <ul class="links collapse in"
- id="xs-modules-links"
- >
- <li class="link">
- <a href="../modules/AppModule.html" >AppModule</a>
- </li>
- <li class="link">
- <a href="../modules/LessonModule.html" >LessonModule</a>
- </li>
- </ul>
- </li>
-
- <li class="chapter">
- <div class="simple">
- <span class="fa fa-cogs"></span>
- <span>Components</span>
- <span class="menu-toggler fa fa-angle-up" data-toggle="collapse"
- data-target="#xs-components-links"
- ></span>
- </div>
- <ul class="links collapse in"
- id="xs-components-links"
- >
- <li class="link">
- <a href="../components/AppComponent.html" >AppComponent</a>
- </li>
- <li class="link">
- <a href="../components/BblComponent.html" >BblComponent</a>
- </li>
- <li class="link">
- <a href="../components/DetailComponent.html" >DetailComponent</a>
- </li>
- <li class="link">
- <a href="../components/EcharsComponent.html" >EcharsComponent</a>
- </li>
- <li class="link">
- <a href="../components/EchartsExempleComponent.html" >EchartsExempleComponent</a>
- </li>
- <li class="link">
- <a href="../components/EditComponent.html" >EditComponent</a>
- </li>
- <li class="link">
- <a href="../components/FlowfatherComponent.html" >FlowfatherComponent</a>
- </li>
- <li class="link">
- <a href="../components/FlowtestComponent.html" >FlowtestComponent</a>
- </li>
- <li class="link">
- <a href="../components/HomeComponent.html" >HomeComponent</a>
- </li>
- <li class="link">
- <a href="../components/NavTabsComponent.html" >NavTabsComponent</a>
- </li>
- <li class="link">
- <a href="../components/PageMineComponent.html" >PageMineComponent</a>
- </li>
- <li class="link">
- <a href="../components/PageSpotDetailComponent.html" >PageSpotDetailComponent</a>
- </li>
- </ul>
- </li>
-
-
- <li class="chapter">
- <div class="simple">
- <span class="fa fa-file-code-o"></span>
- <span>Classes</span>
- <span class="menu-toggler fa fa-angle-up" data-toggle="collapse"
- data-target="#xs-classes-links"
- ></span>
- </div>
- <ul class="links collapse in"
- id="xs-classes-links"
- >
- <li class="link">
- <a href="../classes/TestChatCompletion.html" >TestChatCompletion</a>
- </li>
- </ul>
- </li>
-
-
- <li class="chapter">
- <div class="simple">
- <span class="fa fa-long-arrow-down"></span>
- <span>Injectables</span>
- <span class="menu-toggler fa fa-angle-up" data-toggle="collapse"
- data-target="#xs-injectables-links"
- ></span>
- </div>
- <ul class="links collapse in"
- id="xs-injectables-links"
- >
- <li class="link">
- <a href="../injectables/DataServiceService.html" >DataServiceService</a>
- </li>
- </ul>
- </li>
-
- <li class="chapter">
- <div class="simple">
- <span class="fa fa-info"></span>
- <span>Interfaces</span>
- <span class="menu-toggler fa fa-angle-up" data-toggle="collapse"
- data-target="#xs-interfaces-links"
- ></span>
- </div>
- <ul class="links collapse in"
- id="xs-interfaces-links"
- >
- <li class="link">
- <a href="../interfaces/TestChatMessage.html" class="active" >TestChatMessage</a>
- </li>
- </ul>
- </li>
-
- <li class="chapter">
- <div class="simple">
- <span class="fa fa-plus"></span>
- <span>Pipes</span>
- <span class="menu-toggler fa fa-angle-up" data-toggle="collapse"
- data-target="#xs-pipes-links"
- ></span>
- </div>
- <ul class="links collapse in"
- id="xs-pipes-links"
- >
- <li class="link">
- <a href="../pipes/TokPipe.html" >TokPipe</a>
- </li>
- </ul>
- </li>
-
-
- <li class="chapter">
- <a data-type="chapter-link" href="../coverage.html" ><span class="fa fa-tasks"></span>Documentation coverage</a>
- </li>
-
- <li class="divider"></li>
- <li class="copyright">
- Documentation generated using <a href="https://compodoc.github.io/compodoc/" target="_blank">
- <img src="..//images/compodoc-vectorise.svg" class="img-responsive">
- </a>
- </li>
-
- </ul>
-
- </nav>
- </div>
- <div class="container-fluid main">
- <div class="row main">
- <div class="hidden-xs menu">
- <nav>
-
- <ul class="list">
-
- <li class="title">
- <a href="../">app-angular documentation</a>
- </li>
-
- <li class="divider"></li>
-
- <div id="book-search-input" role="search">
- <input type="text" placeholder="Type to search">
- </div>
-
- <li class="chapter">
- <a data-type="chapter-link" href="../"><span class="fa fa-home"></span>Getting started</a>
- <ul class="links">
- <li class="link">
- <a href="../" ><span class="fa fa-file-text-o"></span>README</a>
- </li>
- <li class="link">
- <a
-
- href="../overview.html"
- href="../overview.html"
- >
- <span class="fa fa-th"></span>Overview
- </a>
- </li>
- </ul>
- </li>
-
- <li class="chapter">
- <a data-type="chapter-link" href="./modules.html" >
- <span class="fa fa-archive"></span>
- <span>Modules</span>
- <span class="menu-toggler fa fa-angle-up" data-toggle="collapse"
- data-target="#modules-links"
- ></span>
- </a>
- <ul class="links collapse in"
- id="modules-links"
- >
- <li class="link">
- <a href="../modules/AppModule.html" >AppModule</a>
- </li>
- <li class="link">
- <a href="../modules/LessonModule.html" >LessonModule</a>
- </li>
- </ul>
- </li>
-
- <li class="chapter">
- <div class="simple">
- <span class="fa fa-cogs"></span>
- <span>Components</span>
- <span class="menu-toggler fa fa-angle-up" data-toggle="collapse"
- data-target="#components-links"
- ></span>
- </div>
- <ul class="links collapse in"
- id="components-links"
- >
- <li class="link">
- <a href="../components/AppComponent.html" >AppComponent</a>
- </li>
- <li class="link">
- <a href="../components/BblComponent.html" >BblComponent</a>
- </li>
- <li class="link">
- <a href="../components/DetailComponent.html" >DetailComponent</a>
- </li>
- <li class="link">
- <a href="../components/EcharsComponent.html" >EcharsComponent</a>
- </li>
- <li class="link">
- <a href="../components/EchartsExempleComponent.html" >EchartsExempleComponent</a>
- </li>
- <li class="link">
- <a href="../components/EditComponent.html" >EditComponent</a>
- </li>
- <li class="link">
- <a href="../components/FlowfatherComponent.html" >FlowfatherComponent</a>
- </li>
- <li class="link">
- <a href="../components/FlowtestComponent.html" >FlowtestComponent</a>
- </li>
- <li class="link">
- <a href="../components/HomeComponent.html" >HomeComponent</a>
- </li>
- <li class="link">
- <a href="../components/NavTabsComponent.html" >NavTabsComponent</a>
- </li>
- <li class="link">
- <a href="../components/PageMineComponent.html" >PageMineComponent</a>
- </li>
- <li class="link">
- <a href="../components/PageSpotDetailComponent.html" >PageSpotDetailComponent</a>
- </li>
- </ul>
- </li>
-
-
- <li class="chapter">
- <div class="simple">
- <span class="fa fa-file-code-o"></span>
- <span>Classes</span>
- <span class="menu-toggler fa fa-angle-up" data-toggle="collapse"
- data-target="#classes-links"
- ></span>
- </div>
- <ul class="links collapse in"
- id="classes-links"
- >
- <li class="link">
- <a href="../classes/TestChatCompletion.html" >TestChatCompletion</a>
- </li>
- </ul>
- </li>
-
-
- <li class="chapter">
- <div class="simple">
- <span class="fa fa-long-arrow-down"></span>
- <span>Injectables</span>
- <span class="menu-toggler fa fa-angle-up" data-toggle="collapse"
- data-target="#injectables-links"
- ></span>
- </div>
- <ul class="links collapse in"
- id="injectables-links"
- >
- <li class="link">
- <a href="../injectables/DataServiceService.html" >DataServiceService</a>
- </li>
- </ul>
- </li>
-
- <li class="chapter">
- <div class="simple">
- <span class="fa fa-info"></span>
- <span>Interfaces</span>
- <span class="menu-toggler fa fa-angle-up" data-toggle="collapse"
- data-target="#interfaces-links"
- ></span>
- </div>
- <ul class="links collapse in"
- id="interfaces-links"
- >
- <li class="link">
- <a href="../interfaces/TestChatMessage.html" class="active" >TestChatMessage</a>
- </li>
- </ul>
- </li>
-
- <li class="chapter">
- <div class="simple">
- <span class="fa fa-plus"></span>
- <span>Pipes</span>
- <span class="menu-toggler fa fa-angle-up" data-toggle="collapse"
- data-target="#pipes-links"
- ></span>
- </div>
- <ul class="links collapse in"
- id="pipes-links"
- >
- <li class="link">
- <a href="../pipes/TokPipe.html" >TokPipe</a>
- </li>
- </ul>
- </li>
-
-
- <li class="chapter">
- <a data-type="chapter-link" href="../coverage.html" ><span class="fa fa-tasks"></span>Documentation coverage</a>
- </li>
-
- <li class="divider"></li>
- <li class="copyright">
- Documentation generated using <a href="https://compodoc.github.io/compodoc/" target="_blank">
- <img src="..//images/compodoc-vectorise.svg" class="img-responsive">
- </a>
- </li>
-
- </ul>
-
- </nav>
- </div>
- <div class="content interface">
- <div class="content-data">
- <ol class="breadcrumb">
- <li>Interfaces</li>
- <li>TestChatMessage</li>
- </ol>
- <ul class="nav nav-tabs" role="tablist">
- <li class="active">
- <a href="#infos" id="infos-tab" role="tab" data-toggle="tab">Infos</a>
- </li>
- <li>
- <a href="#source" role="tab" id="source-tab" data-toggle="tab">Source</a>
- </li>
- </ul>
- <div class="tab-content">
- <div class="tab-pane fade active in" id="infos">
- <p class="comment">
- <h3>File</h3>
- </p>
- <p class="comment">
- <code>src/modules/lesson/page-mine/class-chat-completion.ts</code>
- </p>
- <section>
- <h3 id="inputs">Properties</h3>
- <table class="table table-sm table-bordered">
- <tbody>
- <tr>
- <td class="col-md-4">
- <b>content</b>
- </td>
- </tr>
- <tr>
- <td class="col-md-4">
- <code>content: <code>string</code>
- </bcode>
- </td>
- </tr>
- <tr>
- <td class="col-md-4">
- <i>Type : </i> <code>string</code>
- </td>
- </tr>
- </tbody>
- </table>
- <table class="table table-sm table-bordered">
- <tbody>
- <tr>
- <td class="col-md-4">
- <b>role</b>
- </td>
- </tr>
- <tr>
- <td class="col-md-4">
- <code>role: <code>string</code>
- </bcode>
- </td>
- </tr>
- <tr>
- <td class="col-md-4">
- <i>Type : </i> <code>string</code>
- </td>
- </tr>
- </tbody>
- </table>
- </section>
- </div>
- <div class="tab-pane fade tab-source-code" id="source">
- <pre><code class="hljs javascript">export interface TestChatMessage {
- role: string
- content: string
- }
- // 导出类TestChatCompletion,用于创建聊天完成
- export class TestChatCompletion {
- // 定义一个消息列表
- messageList: Array<TestChatMessage>
- // 构造函数,传入消息列表
- constructor(messageList: Array<TestChatMessage>) {
- this.messageList = messageList
- }
- // 异步创建完成,使用流模式
- async createCompletionByStream() {
- // 从本地存储中获取token
- let token = localStorage.getItem("token");
- // 定义请求体
- let bodyJson = {
- "token": `Bearer ${token}`,
- "messages": this.messageList,
- "model": "gpt-3.5-turbo",
- "temperature": 0.5,
- "presence_penalty": 0,
- "frequency_penalty": 0,
- "top_p": 1,
- "stream": true
- };
- // 发起请求
- let response = await fetch("https://test.fmode.cn/api/apig/aigc/gpt/v1/chat/completions", {
- "headers": {
- "accept": "text/event-stream",
- "sec-fetch-dest": "empty",
- "sec-fetch-mode": "cors",
- "sec-fetch-site": "same-site"
- },
- "referrer": "https://ai.fmode.cn/",
- "referrerPolicy": "strict-origin-when-cross-origin",
- "body": JSON.stringify(bodyJson),
- "method": "POST",
- "mode": "cors",
- "credentials": "omit"
- });
- // 定义消息回复
- let messageAiReply = ""
- // 定义消息索引
- let messageIndex = this.messageList.length
- // 获取响应体读取器
- let reader = response.body?.getReader();
- if (!reader) {
- throw new Error("Failed to get the response reader.");
- }
- // 定义文本解码器
- let decoder = new TextDecoder();
- let buffer = "";
- // 循环读取响应体
- while (true) {
- let { done, value } = await reader.read();
- if (done) {
- break;
- }
- // 将响应体添加到缓冲区
- buffer += decoder.decode(value);
- // Split the buffer by newlines to get individual messages
- // 将缓冲区按换行符分割,获取每条消息
- let messages = buffer.split("\n");
- // Process each message
- // 循环处理每条消息
- for (let i = 0; i < messages.length - 1; i++) {
- let message = messages[i];
- // Process the message as needed
- // 处理消息,需要的操作
- /**
- * data: {"id":"chatcmpl-y2PLKqPDnwAFJIj2L5aqdH5TWK9Yv","object":"chat.completion.chunk","created":1696770162,"model":"gpt-3.5-turbo-0613","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null}]}
- * data: {"id":"chatcmpl-y2PLKqPDnwAFJIj2L5aqdH5TWK9Yv","object":"chat.completion.chunk","created":1696770162,"model":"gpt-3.5-turbo-0613","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
- * data: [DONE]
- */
- let dataText = message.replace("data:\ ", "")
- if (dataText.startsWith("{")) {
- try {
- let dataJson = JSON.parse(dataText)
- console.log(dataJson)
- messageAiReply += dataJson?.choices?.[0]?.delta?.content || ""
- this.messageList[messageIndex] = {
- role: "assistant",
- content: messageAiReply
- }
- } catch (err) { }
- }
- if (dataText.startsWith("[")) {
- console.log(message)
- console.log("完成")
- this.messageList[messageIndex] = {
- role: "assistant",
- content: messageAiReply
- }
- messageAiReply = ""
- }
- // Parse the message as JSON
- // 解析消息,获取每条消息的JSON
- // let data = JSON.parse(message);
- // Clear the processed message from the buffer
- // 清理已处理的响应消息
- buffer = buffer.slice(message.length + 1);
- }
- }
- }
- }</code></pre>
- </div>
- </div>
- </div><div class="search-results">
- <div class="has-results">
- <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
- <ul class="search-results-list"></ul>
- </div>
- <div class="no-results">
- <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
- </div>
- </div>
- </div>
- </div>
- </div>
- <script src="../js/libs/bootstrap-native.js"></script>
- <script src="../js/libs/es6-shim.min.js"></script>
- <script src="../js/libs/EventDispatcher.js"></script>
- <script src="../js/libs/promise.min.js"></script>
- <script src="../js/libs/zepto.min.js"></script>
- <script src="../js/compodoc.js"></script>
- <script src="../js/search/search.js"></script>
- <script src="../js/search/lunr.min.js"></script>
- <script src="../js/search/search-lunr.js"></script>
- <script src="../js/menu.js"></script>
- <script src="../js/libs/highlight.pack.js"></script>
- <script src="../js/libs/highlightjs-line-numbers.min.js"></script>
- <script src="../js/search/search_index.js"></script>
- <script>
- document.addEventListener('DOMContentLoaded', function() {
- hljs.initHighlightingOnLoad();
- hljs.initLineNumbersOnLoad();
- });
- </script>
- </body>
- </html>
|