Django中使用流式响应,自己也能实现ChatGPT的效果

最近在研究ChatGPT的时候,想通过openai提供的接口使国内用户也可以无限制访问,于是打算基于django开发一款应用。页面的渲染也得想ChatGPT一样采用流式响应,django中StreamingHttpResponse是支持流式响应的一种方式。

django 代码

python 复制代码
class ChatView(APIView):

    def get(self, request, *args, **kwargs):
        prompt= request.GET.get('prompt', '')
        chat_response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo",
            stream=True,
            messages=[
                {
                    'role': 'user',
                    'content': prompt,
                }
            ]
        )
        
        def get_streaming_content(chat_response):
            for chunk in chat_response:
                yield chunk
                
        response = StreamingHttpResponse(get_streaming_content(chat_response), content_type='application/octet-stream')
        return response

settings.py

python 复制代码
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR,'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

遇到的问题

在本地通过python manage runserver启动项目后,通过浏览器访问(一开始是GET请求)是能明显看到效果的,但是当使用Nginx+uwsgi部署之后,发现流式响应失效了,于是研究了一下写下这篇文章备忘。

解决方案

首先Nginx和uwsgi的通信方式不能使用socket方式,必须使用http方式,还得修改部分Nginx配置

nginx部分配置

复制代码
location / {
    proxy_pass http://127.0.0.1:8080;
    proxy_buffering off;  # 重点是这个配置
}

uwsgi部分配置

复制代码
[uwsgi]
; 不能使用socket方式通信
;socket = /tmp/uwsgi_%(site_name).sock
; 使用http方式通信
http = 0.0.0.0:8080

GET请求直接在浏览器打开才有此问题,POST请求前端处理流没有哦

附上前端处理响应并实时渲染的代码

html 复制代码
<!DOCTYPE html>
<html>
<head>
    <title>Demo</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>

<div class="container mt-5">
    <div class="row">
        <div class="col-md-6 offset-md-3">
            <div class="form-group">
                <label for="inputText">输入内容:</label>
                <textarea class="form-control" id="inputText" placeholder="在这里输入内容"></textarea>
            </div>
            <button type="button" id="submitBtn" class="btn btn-primary">提交</button>
        </div>
    </div>

    <div class="row mt-3">
        <div class="col-md-6 offset-md-3">
            <div id="responseContainer" class="border p-3"></div>
        </div>
    </div>
</div>


<script>
    $(document).ready(function () {

        $('#submitBtn').click(function () {
            var inputText = $('#inputText').val();

            function displayData(data) {
                var outputDiv = document.getElementById('responseContainer');
                outputDiv.innerText += data;
            }

            fetch('/api/chatgpt/chat/', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({'input': inputText}),
            }).then(function (response) {
                // 确保响应状态码正常
                if (!response.ok) {
                    throw new Error('Response failed');
                }

                // 使用响应的ReadableStream来处理数据
                var reader = response.body.getReader();

                function read() {
                    return reader.read().then(function (result) {
                        // 处理读取到的数据
                        if (!result.done) {
                            var value = result.value;
                            // 在这里处理接收到的数据,例如更新页面上的内容
                            var decoder = new TextDecoder('utf-8')
                            var chunk = decoder.decode(value)
                            displayData(chunk)

                            // 继续读取下一块数据
                            return read();
                        }
                    });
                }

                return read();
            }).catch(function (error) {
                // 处理错误
            });
        });
    });
</script>
</body>
</html>
相关推荐
AI视觉网奇43 分钟前
音频获取长度
java·前端·python
寄思~1 小时前
学习笔记:封装和单继承
开发语言·笔记·python·学习
汤姆yu1 小时前
基于springboot的快递分拣管理系统
java·spring boot·后端
NAGNIP1 小时前
GPT1:通用语言理解模型的开端
后端·算法
E_ICEBLUE1 小时前
Python 操作 Word 文档:主流库对比与选择指南
开发语言·经验分享·python·word·办公自动化
倔强青铜三1 小时前
苦练Python第38天:input() 高级处理,安全与异常管理
人工智能·python·面试
CF14年老兵2 小时前
SQL 是什么?初学者完全指南
前端·后端·sql
大模型真好玩2 小时前
深入浅出LangChain AI Agent智能体开发教程(六)—两行代码LangChain Agent API快速搭建智能体
人工智能·python·agent
用户4099322502122 小时前
FastAPI后台任务:是时候让你的代码飞起来了吗?
后端·github·trae