npx create-next-app@latest
cd xiaojin-react-chatgpt
npm i
npm run dev
npm install antd --save
"use client";
import { useState } from "react";
import { Input, Button } from "antd";
const { TextArea } = Input;
export default function Home() {
let [outputValue, setOutputValue] = useState("");
return (
Chat GPT 打字机效果
);
}
参数 | 类型 | 描述 |
---|---|---|
id | string | 聊天完成的唯一标识符 |
choices | array | 聊天完成选项列表。如果n大于1,可以有多个选项 |
created | integer | 创建聊天完成的Unix时间戳(秒) |
model | string | 用于聊天完成的模型 |
system_fingerprint | string | 该指纹表示模型运行的后端配置 |
object | string | 对象类型,总是 chat.completion |
usage | object | 完成请求的使用统计信息 |
completion_tokens | integer | 生成的完成中的标记数 |
prompt_tokens | integer | 提示中的标记数 |
total_tokens | integer | 请求中使用的标记总数(提示 + 完成) |
const data = {
model: "XXX",
messages: [
{
role: "user",
content: "写一篇1000字关于春天的作文",
},
],
prompt: "写一篇1000字关于春天的作文",
temperature: 0.75,
stream: true,
};
mdn文档
Fetch API 允许你跨网络获取资源,它提供了现代化的 API 去替代 XHR。它有一系列的优点,真正好的是,浏览器最近增加了将 fetch 响应作为可读流使用的能力。
Request.body 和 Response.body 属性也是这样,它们将主体内容暴露作为一个可读流的 getter。
const response = await fetch(url, {
method: "POST",
body: JSON.stringify(data),
headers: {
"Content-Type": "application/json",
},
});
const reader = response.body.getReader();
while (true) {
const { done, value } = await reader.read();
if (done) {
console.log("***********************done");
console.log(value);
break;
}
console.log("--------------------value");
console.log(value);
}
"use client";
import { useState } from "react";
import { Input, Button } from "antd";
const { TextArea } = Input;
export default function Home() {
let [outputValue, setOutputValue] = useState("");
const send = async () => {
const url = "http://10.169.112.194:7100/v1/chat/completions";
const data = {
model: "chatglm2-6b",
messages: [
{
role: "user",
content: "写一篇1000字关于春天的作文",
},
],
prompt: "写一篇1000字关于春天的作文",
temperature: 0.75,
stream: true,
};
const response = await fetch(url, {
method: "POST",
body: JSON.stringify(data),
headers: {
"Content-Type": "application/json",
},
});
const reader = response.body.getReader();
while (true) {
const { done, value } = await reader.read();
if (done) {
console.log("***********************done");
console.log(value);
break;
}
console.log("--------------------value");
console.log(value);
}
};
return (
Chat GPT 打字机效果
);
}
const encode = new TextDecoder("utf-8");
const reader = response.body.getReader();
while (true) {
const { done, value } = await reader.read();
const text = encode.decode(value);
if (done) {
console.log("***********************done");
console.log(text);
break;
}
console.log("--------------------value");
console.log(text);
}
data: {"id": "chatcmpl-3zmRJUd4TTpm9xP9NbQVHw", "model": "chatglm2-6b", "choices": [{"index": 0, "delta": {"content": "希望"}, "finish_reason": null}]}
我们编写一个函数~~然后打印数据
const getReaderText = (str) => {
// 定义正则表达式匹配模式
let result = str.match(/{"content": "(\S*)"}, "finish_reason":/);
return result && result[1] ? result[1] : "";
};
解决textarea显示\n但是并没有换行的问题,经过排查发现,是接口返回的数据是\n,所以渲染有异常,因此我们可以用下面的方式去解决.
replaceAll(/\\n/g,'\r\n')
欢迎大家指出文章需要改正之处~
学无止境,合作共赢