### File

`src/modules/lesson/page-mine/class-chat-completion.ts`

### Constructor

`constructor(messageList: TestChatMessage[])`

### Methods

createCompletionByStream
`createCompletionByStream()`
Returns: `void`

### Properties

messageList
`messageList: `TestChatMessage[]` </bcode>`

    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);
                }
            }
        }
    }

# results matching ""

# No results matching ""