lua对接GPT4实现对话

演示效果:

准备材料:

1、FastWeb网站开发服务: fwlua.com

2、一台服务器

该示例使用开源项目:fastweb 实现。

代码比较简单,主要是两部分,一个lua代码和一个html页面,用来用户发起请求和后台处理。

源码下载地址我放到文章尾部。

gpt.lua

利用httpclient发起一个POST请求,该请求携带了key、和对话内容,然后解析返回内容并发送给浏览器。

Lua 复制代码
local dkjson = require("dkjson")
-- 配置区
local API_KEY = "你的密钥"
local ENGINE = "gpt-4o"
-- 请求访问GPT
function gpt_request(content)
    -- 创建HTTP客户端实例
    local client = httpclient.new()
    -- 设置超时时间
    client:set_timeout(3000,1000*60)
    -- 设置请求头
    client:request_header("Content-Type", "application/json")
    client:request_header("Authorization", "Bearer " .. API_KEY)
    -- 发起POST请求
    local url = "https://api.openai.com/v1/chat/completions"
    local body = {
        model = ENGINE,
        messages = {
          {
            role =  "system",
            content =  "You are a helpful assistant."
          },
          {
            role =  "user",
            content = content
          }
        }
    }
    local success = client:post(url, dkjson.encode(body))
    
    -- 检查请求是否成功
    if success then
        local body = dkjson.decode(client:response())
        local status_code = client:status()
        -- 返回GPT的发送内容
        return body.choices[1].message.content
    else
        return "请求失败"
    end
end


-- 获取请求参数
local content = dkjson.decode(request:body()).content
-- 发送给浏览器
response:send(gpt_request(content))

index.html

html部分就更简单了,只是一个表单提交和markdown的格式化显示

html 复制代码
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Fast Web</title>
    <script src="js/jquery-3.4.1.min.js"></script>
    <style>
        .button-container {
            margin-bottom: 10px;
        }
        .button-container button {
            margin-right: 10px;
        }
    </style>
</head>
<body>
    <h1>Fast Web GPT</h1>
    <form id="interceptor-form">
        <label for="send">输入提问:</label>
        <input type="text" id="send" name="send" required>
        <button type="submit">提交</button>
    </form>
    <h3>回复内容:</h3>
    <div id="markdown">
        
    </div>
   <script src="/js/marked.min.js"></script>

    <script>
        $(document).ready(function(){
            $('#interceptor-form').on('submit', function(event){
                event.preventDefault();
                var send = $('#send').val();
                var data = JSON.stringify({ content: send });

                $.ajax({
                    url: "/api/gpt.lua",
                    method: 'POST',
                    contentType: 'application/json',
                    data: data,
                    success: function(response) {
                          // 使用marked.js将Markdown转换为HTML
                        var htmlContent = marked.parse(response);
                        
                          // 将生成的HTML设置到div中
                        document.getElementById('markdown').innerHTML = htmlContent;
                    },
                    error: function(jqXHR, textStatus, errorThrown) {
                        alert('请求失败: ' + textStatus + ' - ' + errorThrown);
                    }
                });
            });
        });
    </script>
    
</body>
</html>

下载地址:OpenAI-GPT - Fast Web

相关推荐
t***5444 小时前
如何配置Orwell Dev-C++使用Clang
开发语言·c++
CoderCodingNo4 小时前
【信奥业余科普】C++ 的奇妙之旅 | 13:为什么 0.1+0.2≠0.3?——解密“爆int”溢出与浮点数精度的底层原理
开发语言·c++
kongba0076 小时前
项目打包 Python Flask 项目发布与打包专家 提示词V1.0
开发语言·python·flask
froginwe116 小时前
C 语言测验
开发语言
今夕资源网6 小时前
powershell工具包 安装升级脚本并设置UTF-8 环境快捷方式创建 将powershell的编码默认改为UTF-8
开发语言·utf-8·powershell·utf-8编码·powershell7·powershell5·设置utf-8编码
机器视觉知识推荐、就业指导7 小时前
Qt:真正的门槛不是入门,而是维护
开发语言·qt
hhb_6187 小时前
Dylan 语言核心特性与工程实践深度解析
开发语言·c#
无巧不成书02188 小时前
零基础Java网络编程全解:从核心概念到Socket实战,一文打通Java网络通信
java·开发语言·网络
饭小猿人8 小时前
Flutter实现底部动画弹窗有两种方式
开发语言·前端·flutter
aq55356008 小时前
Workstation神技:一键克隆调试环境
java·开发语言