『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
查看: 3489|回复: 12

[进阶教程] 免翻墙用nodered连接vercel部署ChatGPT-Next-Web实现chatgpt接入HA

[复制链接]

2

主题

68

帖子

661

积分

高级会员

Rank: 4

积分
661
金钱
593
HASS币
0
发表于 2023-4-26 21:49:02 | 显示全部楼层 |阅读模式
本帖最后由 llgxdnn 于 2023-5-20 22:05 编辑

ChatGPT-Next-Web 的部署 参照GitHub - Yidadaa/ChatGPT-Next-Web: One-Click to deploy well-designed ChatGPT web UI on Vercel. 一键拥有你自己的 ChatGPT 网页服务。

要求:
1、有自己的域名,免费自购均可,否侧vercel提供的域名在国内有DNS污染。
2、有OPENAI 的账号和API

部署完成后可以实现免翻墙,访问自己的域名能进入ChatGPT-Next-Web页面并正常使用

参照 silas3082 的帖子

https://bbs.hassbian.com/thread-19751-1-1.html

1.在ha配置-辅助元素里边创建一个文本,取名:提问。
2.在ha里边把编辑仪表盘-添加卡片-增加一个提问的实体卡片用于输入和一个markdown卡片用于输出。

nodered 导入流

[
    {
        "id": "eeef036821e91e9e",
        "type": "tab",
        "label": "流程 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "769ecd327aa8d24e",
        "type": "http request",
        "z": "eeef036821e91e9e",
        "name": "",
        "method": "POST",
        "ret": "txt",
        "paytoqs": "ignore",
        "url": "https://xxx.xxx.cn/api/chat-stream",
        "tls": "25bd61bd66dac23a",
        "persist": false,
        "proxy": "",
        "insecureHTTPParser": false,
        "authType": "",
        "senderr": false,
        "headers": [
            {
                "keyType": "other",
                "keyValue": "access-code",
                "valueType": "other",
                "valueValue": "你部署的时候设置的密码"
            },
            {
                "keyType": "other",
                "keyValue": "content-type",
                "valueType": "other",
                "valueValue": "application/json"
            },
            {
                "keyType": "other",
                "keyValue": "path",
                "valueType": "other",
                "valueValue": "v1/chat/completions"
            },
            {
                "keyType": "other",
                "keyValue": "sec-fetch-mode",
                "valueType": "other",
                "valueValue": "cors"
            },
            {
                "keyType": "other",
                "keyValue": "sec-fetch-site",
                "valueType": "other",
                "valueValue": "same-origin"
            },
            {
                "keyType": "other",
                "keyValue": "sec-fetch-dest",
                "valueType": "other",
                "valueValue": "empty"
            }
        ],
        "x": 520,
        "y": 160,
        "wires": [
            [
                "6023e116067f3463",
                "10a4043aa456e772"
            ]
        ]
    },
    {
        "id": "d014fb254f9848e1",
        "type": "inject",
        "z": "eeef036821e91e9e",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "你是谁?",
        "payloadType": "str",
        "x": 100,
        "y": 40,
        "wires": [
            [
                "010ea751ee42127d"
            ]
        ]
    },
    {
        "id": "6023e116067f3463",
        "type": "debug",
        "z": "eeef036821e91e9e",
        "name": "debug 30",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 700,
        "y": 160,
        "wires": []
    },
    {
        "id": "010ea751ee42127d",
        "type": "function",
        "z": "eeef036821e91e9e",
        "name": "初始化模板,接入用户输入",
        "func": "var muban = {\n    messages: [\n        {\n            role: "system",\n            content: ""\n        },\n        {\n            role: "user",\n            content: ""\n        },\n        {\n            role: "assistant",\n            content: ""\n        },\n        {\n            role: "user",\n            content: ""\n        },\n        {\n            role: "assistant",\n            content: ""\n        },\n        {\n            role: "system",\n            content: ""\n        }\n    ],\n    stream: true,\n    model: "gpt-3.5-turbo-0301",\n    temperature: 1,\n    presence_penalty: 0\n}\nvar tiyao = {\n    role: "system",\n    content: "简要总结一下你和用户的对话,用作后续的上下文提示 prompt,控制在 200 字以内"\n}\n\nif (flow.get('muban') == undefined) {\n    flow.set('muban', muban);\n}\nif (flow.get('tiyao') == undefined) {\n    flow.set('tiyao', tiyao);\n}\n//jsonObj.messages[1].content = msg.payload;\nvar jsmsg = flow.get('muban');\nfor (let i = 1; i < 5; i++) {\n    jsmsg.messages[i].role = jsmsg.messages[i + 1].role;\n    jsmsg.messages[i].content = jsmsg.messages[i + 1].content;\n}\njsmsg.messages[5].role = "user";\njsmsg.messages[5].content = msg.payload;\nflow.set('muban', jsmsg);\nvar count = flow.get('count') || 0;\nif (count < 3) {\n    count += 1;\n    flow.set('count', count);\n\n} else {\n    count = 0;\n    flow.set('count', count);\n}\n\n\nmsg.payload = flow.get('muban');\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 300,
        "y": 80,
        "wires": [
            [
                "769ecd327aa8d24e",
                "718a3ea9e11ceca2",
                "6adbca0443856801"
            ]
        ]
    },
    {
        "id": "718a3ea9e11ceca2",
        "type": "debug",
        "z": "eeef036821e91e9e",
        "name": "debug 33",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 620,
        "y": 80,
        "wires": []
    },
    {
        "id": "10a4043aa456e772",
        "type": "function",
        "z": "eeef036821e91e9e",
        "name": "function 7",
        "func": "\nvar rep = {\n    role: "assistant",\n    content: msg.payload\n}\n\nvar jsmsg = flow.get('muban');\nfor (let i = 1; i < 5; i++) {\n    jsmsg.messages[i].role = jsmsg.messages[i + 1].role;\n    jsmsg.messages[i].content = jsmsg.messages[i + 1].content;\n}\njsmsg.messages[5]=rep;\nflow.set('muban', jsmsg);\nvar count = flow.get('count') || 0;\nif (count < 3) {\n    count += 1;\n    flow.set('count', count);\n\n} else {\n    count = 0;\n    flow.set('count', count);\n}\nmsg.payload = msg.payload;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 640,
        "y": 300,
        "wires": [
            [
                "b49250ab3f3db33d"
            ]
        ]
    },
    {
        "id": "3cd835bf150c81aa",
        "type": "http request",
        "z": "eeef036821e91e9e",
        "name": "",
        "method": "POST",
        "ret": "txt",
        "paytoqs": "ignore",
        "url": "https://xxx.xxx.cn/api/chat-stream",
        "tls": "25bd61bd66dac23a",
        "persist": false,
        "proxy": "",
        "insecureHTTPParser": false,
        "authType": "",
        "senderr": false,
        "headers": [
            {
                "keyType": "other",
                "keyValue": "access-code",
                "valueType": "other",
                "valueValue": "你部署的时候设置的密码"
            },
            {
                "keyType": "other",
                "keyValue": "message",
                "valueType": "other",
                "valueValue": "hello!"
            },
            {
                "keyType": "other",
                "keyValue": "content-type",
                "valueType": "other",
                "valueValue": "application/json"
            },
            {
                "keyType": "other",
                "keyValue": "path",
                "valueType": "other",
                "valueValue": "v1/chat/completions"
            },
            {
                "keyType": "other",
                "keyValue": "sec-fetch-mode",
                "valueType": "other",
                "valueValue": "cors"
            },
            {
                "keyType": "other",
                "keyValue": "sec-fetch-site",
                "valueType": "other",
                "valueValue": "same-origin"
            },
            {
                "keyType": "other",
                "keyValue": "sec-fetch-dest",
                "valueType": "other",
                "valueValue": "empty"
            }
        ],
        "x": 420,
        "y": 380,
        "wires": [
            [
                "a9e94c4e3c308c33"
            ]
        ]
    },
    {
        "id": "f5aa01a9dbcaaffd",
        "type": "function",
        "z": "eeef036821e91e9e",
        "name": "function 9",
        "func": "var jsmsg = flow.get('muban');\nfor (let i = 1; i < 5; i++) {\n    jsmsg.messages[i].role = jsmsg.messages[i + 1].role;\n    jsmsg.messages[i].content = jsmsg.messages[i + 1].content;\n}\njsmsg.messages[5] = flow.get('tiyao');\n\nflow.set('muban', jsmsg);\nmsg.payload = flow.get('muban');\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 180,
        "y": 380,
        "wires": [
            [
                "3cd835bf150c81aa",
                "9fe9edbc8c12642a"
            ]
        ]
    },
    {
        "id": "a9e94c4e3c308c33",
        "type": "function",
        "z": "eeef036821e91e9e",
        "name": "function 10",
        "func": "let rep=msg.payload;\nvar jsmsg = flow.get('muban');\njsmsg.messages[0].role = "system";\njsmsg.messages[0].content = "这是 ai 和用户的历史聊天总结作为前情提要:"+rep;\nflow.set('muban', jsmsg);\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 630,
        "y": 380,
        "wires": [
            [
                "c48d331236f99024"
            ]
        ]
    },
    {
        "id": "6adbca0443856801",
        "type": "switch",
        "z": "eeef036821e91e9e",
        "name": "用户提问3次,进行一次提要总结",
        "property": "count",
        "propertyType": "flow",
        "rules": [
            {
                "t": "eq",
                "v": "3",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 200,
        "y": 220,
        "wires": [
            [
                "f5aa01a9dbcaaffd",
                "ed3af89ca0b4bcd6"
            ]
        ]
    },
    {
        "id": "2df3a11f9589fac4",
        "type": "server-state-changed",
        "z": "eeef036821e91e9e",
        "name": "HA提问文本框",
        "server": "8987cb3f01f27fd1",
        "version": 4,
        "exposeToHomeAssistant": false,
        "haConfig": [
            {
                "property": "name",
                "value": ""
            },
            {
                "property": "icon",
                "value": ""
            }
        ],
        "entityidfilter": "input_text.ti_wen",
        "entityidfiltertype": "exact",
        "outputinitially": false,
        "state_type": "str",
        "haltifstate": "",
        "halt_if_type": "str",
        "halt_if_compare": "is",
        "outputs": 1,
        "output_only_on_state_change": true,
        "for": "0",
        "forType": "num",
        "forUnits": "minutes",
        "ignorePrevStateNull": false,
        "ignorePrevStateUnknown": false,
        "ignorePrevStateUnavailable": false,
        "ignoreCurrentStateUnknown": false,
        "ignoreCurrentStateUnavailable": false,
        "outputProperties": [
            {
                "property": "payload",
                "propertyType": "msg",
                "value": "",
                "valueType": "entityState"
            },
            {
                "property": "data",
                "propertyType": "msg",
                "value": "",
                "valueType": "eventData"
            },
            {
                "property": "topic",
                "propertyType": "msg",
                "value": "",
                "valueType": "triggerId"
            }
        ],
        "x": 90,
        "y": 120,
        "wires": [
            [
                "010ea751ee42127d"
            ]
        ]
    },
    {
        "id": "9fe9edbc8c12642a",
        "type": "debug",
        "z": "eeef036821e91e9e",
        "name": "debug 36",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 420,
        "y": 320,
        "wires": []
    },
    {
        "id": "c48d331236f99024",
        "type": "debug",
        "z": "eeef036821e91e9e",
        "name": "debug 37",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 860,
        "y": 380,
        "wires": []
    },
    {
        "id": "0b1e8c9372f47edf",
        "type": "debug",
        "z": "eeef036821e91e9e",
        "name": "debug 38",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 840,
        "y": 240,
        "wires": []
    },
    {
        "id": "ed3af89ca0b4bcd6",
        "type": "debug",
        "z": "eeef036821e91e9e",
        "name": "debug 39",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 460,
        "y": 220,
        "wires": []
    },
    {
        "id": "b49250ab3f3db33d",
        "type": "ha-sensor",
        "z": "eeef036821e91e9e",
        "name": "传入HA的Markdown卡片",
        "entityConfig": "412b2e862f7cea99",
        "version": 0,
        "state": "topic",
        "stateType": "msg",
        "attributes": [
            {
                "property": "content",
                "value": "payload",
                "valueType": "msg"
            },
            {
                "property": "topic",
                "value": "topic",
                "valueType": "str"
            }
        ],
        "inputOverride": "allow",
        "outputProperties": [],
        "x": 870,
        "y": 300,
        "wires": [
            [
                "0b1e8c9372f47edf"
            ]
        ]
    },
    {
        "id": "25bd61bd66dac23a",
        "type": "tls-config",
        "name": "test",
        "cert": "",
        "key": "",
        "ca": "",
        "certname": "",
        "keyname": "",
        "caname": "",
        "servername": "",
        "verifyservercert": true,
        "alpnprotocol": ""
    },
    {
        "id": "8987cb3f01f27fd1",
        "type": "server",
        "name": "Home Assistant",
        "version": 5,
        "addon": false,
        "rejectUnauthorizedCerts": true,
        "ha_boolean": "y|yes|true|on|home|open",
        "connectionDelay": true,
        "cacheJson": true,
        "heartbeat": false,
        "heartbeatInterval": "30",
        "areaSelector": "friendlyName",
        "deviceSelector": "friendlyName",
        "entitySelector": "friendlyName",
        "statusSeparator": ": ",
        "statusYear": "hidden",
        "statusMonth": "short",
        "statusDay": "numeric",
        "statusHourCycle": "default",
        "statusTimeFormat": "h:m",
        "enableGlobalContextStore": false
    },
    {
        "id": "412b2e862f7cea99",
        "type": "ha-entity-config",
        "server": "8987cb3f01f27fd1",
        "deviceConfig": "5be4437a345a0a57",
        "name": "chatgtphuifu",
        "version": "6",
        "entityType": "sensor",
        "haConfig": [
            {
                "property": "name",
                "value": "ChatGPT回复"
            },
            {
                "property": "icon",
                "value": ""
            },
            {
                "property": "entity_category",
                "value": ""
            },
            {
                "property": "entity_picture",
                "value": ""
            },
            {
                "property": "device_class",
                "value": ""
            },
            {
                "property": "unit_of_measurement",
                "value": ""
            },
            {
                "property": "state_class",
                "value": ""
            }
        ],
        "resend": false,
        "debugEnabled": false
    },
    {
        "id": "5be4437a345a0a57",
        "type": "ha-device-config",
        "name": "chatgpthuifu",
        "hwVersion": "",
        "manufacturer": "Node-RED",
        "model": "",
        "swVersion": ""
    }
]

导入后 修改一个HTTPS节点的   连接 地址,就是你自己部署时 添加的域名,例如   https://xxx.xxx.cn/api/chat-stream ,
然后修改下,下面 头里面的 access-code  改为你部署的时候 设置的 密码。

最后到 HA里面,添加 卡片,按实体名称 搜索 input  找到开始建立的 虚拟实体 “提问”,点添加。
再添加一个 markdown卡片 ,代码修改为  
{{ state_attr('sensor.chatgpthui_fu','content')}}

其中 sensor.chatgpthui_fu  传感器 是在 nodered 里面添加的。
游客,如果您要查看本帖隐藏内容请回复

然后,就可以测试了。

我也是新手,自己研究出来的,nodered里面的代码是边baidu,边编写的,可能不严谨,或有些细节参数需要微调。
抛砖引玉,希望大神们继续完善。
有这个例子之后,再接入TTS,智能语音对话
或是

https://bbs.hassbian.com/thread-19961-1-1.html

训练chatgpt,把训练代码写在总结里,删除,或者调整下面的总结流程,能完美训练chatgpt。


感谢大家仔细阅读,不懂的地方大家可以共同探讨。







chatgpt.zip

3.07 KB, 下载次数: 38

评分

参与人数 4金钱 +48 收起 理由
Alkali + 8 高手,这是高手!
binghun9806 + 10 高手,这是高手!
隔壁的王叔叔 + 10 感谢楼主分享!
dscao + 20 感谢楼主分享!

查看全部评分

回复

使用道具 举报

0

主题

10

帖子

96

积分

注册会员

Rank: 2

积分
96
金钱
86
HASS币
0
发表于 2023-6-1 19:55:36 | 显示全部楼层
感谢分享
回复

使用道具 举报

22

主题

612

帖子

2755

积分

金牌会员

Rank: 6Rank: 6

积分
2755
金钱
2143
HASS币
0
发表于 2023-6-1 22:18:37 | 显示全部楼层
免费的API过期了·································
回复

使用道具 举报

6

主题

114

帖子

970

积分

高级会员

Rank: 4

积分
970
金钱
856
HASS币
0
发表于 2023-7-12 17:13:39 | 显示全部楼层
ered连接vercel部署ChatG
回复

使用道具 举报

0

主题

41

帖子

288

积分

中级会员

Rank: 3Rank: 3

积分
288
金钱
247
HASS币
0
发表于 2023-7-28 16:30:55 | 显示全部楼层
感谢分享
回复

使用道具 举报

4

主题

55

帖子

1179

积分

金牌会员

Rank: 6Rank: 6

积分
1179
金钱
1124
HASS币
20
发表于 2023-7-29 00:02:41 | 显示全部楼层
在这个vercel上面部署了ChatGPT-Next-Web,自己的域名也加上了
但是这两个网址我都要翻墙才能连接到呢
Screenshot_20230729_000049.jpg
回复

使用道具 举报

0

主题

10

帖子

177

积分

注册会员

Rank: 2

积分
177
金钱
167
HASS币
0
发表于 2023-8-5 18:32:16 | 显示全部楼层
感谢分享
回复

使用道具 举报

0

主题

3

帖子

29

积分

新手上路

Rank: 1

积分
29
金钱
26
HASS币
0
发表于 2023-8-31 18:10:37 | 显示全部楼层
谢谢谢谢谢谢谢谢谢谢
回复

使用道具 举报

0

主题

11

帖子

114

积分

注册会员

Rank: 2

积分
114
金钱
103
HASS币
0
发表于 2023-11-4 10:37:16 | 显示全部楼层
回复一个学习一下
回复

使用道具 举报

0

主题

16

帖子

162

积分

注册会员

Rank: 2

积分
162
金钱
146
HASS币
0
发表于 2023-11-19 20:51:53 | 显示全部楼层
正好在找这个相关的接入ha的办法 希望可以成功
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-11-24 07:01 , Processed in 0.227604 second(s), 35 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表