lua对接GPT4实现对话

演示效果:

 准备材料:

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

2、一台服务器

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

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

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

gpt.lua

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

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的格式化显示

<!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

  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_小生有礼了_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值