[toc]

✨你好啊,我是“ 罗师傅”,是一名程序猿哦。
🌍主页链接:楚门的世界 - 一个热爱学习和运动的程序猿
☀️博文主更方向为:fs实训二ing
❤️一个“不想让我曾没有做好的也成为你的遗憾”的博主。
💪很高兴与你相遇,一起加油!

前言

一直想要实现一个属于自己的ChatGpt,今天终于在舍友的帮助下实现了。

大家可以访问一下我的:ChatGPT Next Web

🌲🌲github

首先从github中找到这个优秀的开源项目:

🍀🍀vercel

vercel官网:Dashboard – Vercel

vercel注册:Twikoo Vercel 部署教程_哔哩哔哩_bilibili (后面部分是vercel的注册,记得一键三连)

可以学习这个博客,学完你就懂如何通过vercel将域名挂载到github对应仓库了Vercel+imsyy 部署个人博客 home 页面 | 小铭の博客 (xz133744.github.io)

进入到Vercel中(具体的注册流程我就省略了)

点击进行部署

出现下面这个页面表示将项目部署成功

🌍🌍硅云

要想实现上面一步,必须进行域名解析

进入硅云,进行域名解析

🌞🌞url访问

解析成功后就可以直接使用url进行访问了:

比如我的访问地址就是 https://ai.lzwgxust.top/

添加自己ChatGpt账号的密钥也就是key值(需要魔法才能进):Overview - OpenAI API

设置后刷新页面就可以进行愉快的交流啦~~

🍠🍠自定义chatgpt接口

使用F12打开调试工具–>选择网络(network)–>选择Fetch/XHR–>双击请求就可以难道属于自己的chatgpt接口了

🔥请求体参数

1
2
3
4
5
6
7
8
9
10
11
12
13
{
"messages": [
{
"role": "user",
"content": "你好"
}
],
"stream": true,
"model": "gpt-3.5-turbo",
"temperature": 1,
"presence_penalty": 0,
"frequency_penalty": 0
}

但是还需要一个请求头:

1
2
3
header: {
Authorization: "Bearer 自己chatgpt的key"
},

🍙返回体

返回数据类型如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
data: {"id":"chatcmpl-7VbOxTLneeUQoN9eJe71NeThFEdfp","object":"chat.completion.chunk","created":1687766295,"model":"gpt-3.5-turbo-0301","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null}]}

data: {"id":"chatcmpl-7VbOxTLneeUQoN9eJe71NeThFEdfp","object":"chat.completion.chunk","created":1687766295,"model":"gpt-3.5-turbo-0301","choices":[{"index":0,"delta":{"content":"你 "},"finish_reason":null}]}

data: {"id":"chatcmpl-7VbOxTLneeUQoN9eJe71NeThFEdfp","object":"chat.completion.chunk","created":1687766295,"model":"gpt-3.5-turbo-0301","choices":[{"index":0,"delta":{"content":"好"},"finish_reason":null}]}

data: {"id":"chatcmpl-7VbOxTLneeUQoN9eJe71NeThFEdfp","object":"chat.completion.chunk","created":1687766295,"model":"gpt-3.5-turbo-0301","choices":[{"index":0,"delta":{"content":","},"finish_reason":null}]}

data: {"id":"chatcmpl-7VbOxTLneeUQoN9eJe71NeThFEdfp","object":"chat.completion.chunk","created":1687766295,"model":"gpt-3.5-turbo-0301","choices":[{"index":0,"delta":{"content":"有"},"finish_reason":null}]}

data: {"id":"chatcmpl-7VbOxTLneeUQoN9eJe71NeThFEdfp","object":"chat.completion.chunk","created":1687766295,"model":"gpt-3.5-turbo-0301","choices":[{"index":0,"delta":{"content":"什"},"finish_reason":null}]}

data: {"id":"chatcmpl-7VbOxTLneeUQoN9eJe71NeThFEdfp","object":"chat.completion.chunk","created":1687766295,"model":"gpt-3.5-turbo-0301","choices":[{"index":0,"delta":{"content":"么"},"finish_reason":null}]}

data: {"id":"chatcmpl-7VbOxTLneeUQoN9eJe71NeThFEdfp","object":"chat.completion.chunk","created":1687766295,"model":"gpt-3.5-turbo-0301","choices":[{"index":0,"delta":{"content":"需要"},"finish_reason":null}]}

data: {"id":"chatcmpl-7VbOxTLneeUQoN9eJe71NeThFEdfp","object":"chat.completion.chunk","created":1687766295,"model":"gpt-3.5-turbo-0301","choices":[{"index":0,"delta":{"content":"帮"},"finish_reason":null}]}

data: {"id":"chatcmpl-7VbOxTLneeUQoN9eJe71NeThFEdfp","object":"chat.completion.chunk","created":1687766295,"model":"gpt-3.5-turbo-0301","choices":[{"index":0,"delta":{"content":"助"},"finish_reason":null}]}

data: {"id":"chatcmpl-7VbOxTLneeUQoN9eJe71NeThFEdfp","object":"chat.completion.chunk","created":1687766295,"model":"gpt-3.5-turbo-0301","choices":[{"index":0,"delta":{"content":"的"},"finish_reason":null}]}

data: {"id":"chatcmpl-7VbOxTLneeUQoN9eJe71NeThFEdfp","object":"chat.completion.chunk","created":1687766295,"model":"gpt-3.5-turbo-0301","choices":[{"index":0,"delta":{"content":"吗"},"finish_reason":null}]}

data: {"id":"chatcmpl-7VbOxTLneeUQoN9eJe71NeThFEdfp","object":"chat.completion.chunk","created":1687766295,"model":"gpt-3.5-turbo-0301","choices":[{"index":0,"delta":{"content":"?"},"finish_reason":null}]}

data: {"id":"chatcmpl-7VbOxTLneeUQoN9eJe71NeThFEdfp","object":"chat.completion.chunk","created":1687766295,"model":"gpt-3.5-turbo-0301","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}

data: [DONE]

我们主要是需要拿到 data.choices.delta.content的值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
sendAi(msg) {
wx.request({
url: '自己的chatgpt-api',
method: 'POST',
header: {
Authorization: "Bearer 自己的openai的密钥"
},
data: {
// role: "user",
// content: msg
"messages": [
{
"role": "user", // 用户输入
"content": msg
}
],
"stream": true,
"model": "gpt-3.5-turbo", // 使用的ChatGPT模型
"temperature": 1,
"presence_penalty": 0,
"frequency_penalty": 0
},
success: (({ data }) => {
// 对返回的数据进行处理
const newData = data.split('data:')
const data1 = newData.map(item => {
return item.split('"choices":')[1]
})
const data2 = data1.map(item => {
if (item != undefined) {
return item.slice(0, -3)
}
})
const content = []
data2.forEach(item => {
if (item != undefined) {
content.push(...JSON.parse(item))
}
})
const contents = content.map(item => {
if (item.delta && item.finish_reason === null) {
return item.delta
}
else {
return null
}
})
console.log(contents)
const a = []
contents.forEach(item => {
if (item != null) {
a.push(item.content)
}
})
console.log(a.join(''))
msgList.push({
id: msgList.length,
speaker: 'server', // chatGpt输出
content: a.join(''),
})
this.setData({ msgList })
})
})
},

代码解析后的样子:

❤️❤️❤️忙碌的敲代码也不要忘了浪漫鸭!

🍉🍉🍉此博文会持续更新,记得常回来看看哦,一起加油做出更好看更有用的博文呀💪。