赞
踩
前端html部分: {%extends 'layout.html'%} {% block content %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>人工智能 AIChat</title> </head> <body> <h1> </h1> <img src="/static/img/6.png" style="width: 280px; height: 80px;margin-bottom:5px;margin-left: 10px" > <textarea class="op_translation_text op_translation_textbg" name="query" rows="6" style=" background-color: rgba(0,108,232,0.07);border-radius: 5px;border : none;margin-inline-end: 1000px; width: 440px; height:40px; margin-left: 5px;margin-top: 2px; text-align: left;">您:{{ question }}</textarea> <textarea class="op_translation_text op_translation_textbg" name="query" rows="6" style=" background-color: rgba(0,108,232,0.07);border-radius: 5px;border : none;margin-inline-end: 1000px; width: 440px; height:300px; margin-left: 5px;margin-top: 2px; text-align: left;">AI:{{ answer }}</textarea> <form method="post" action="/depart/add/"> {% csrf_token %} <input type="text" id="question" name="question" placeholder="请输入您的问题,按enter快捷发送" style="margin-top: 10px;margin-left: 10px;border-radius: 5px;width:400px;height:32px;font-size:13px;background-color:#FFF;line-height:25px;border:1px solid #ddd;position: absolute; bottom: 10px;" > <button type="submit" style="background-color: rgba(55,0,255,0.74); color: white; border-radius: 5px; padding: 3px 15px; text-align: center; display: inline-block; border: 8px; position: absolute; bottom: 13px; margin-left: 415px ">发 送</button> </form> </body> </html> {% endblock %}
后端python部分:
from django.shortcuts import render,HttpResponse,redirect from app01 import models import openai from app01.views.text import pri def depart_list(request): queryset=models.Department.objects.all() return render(request, "depart_list.html", {'queryset':queryset}) def depart_add(request): openai.api_key = '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' if request.method == 'POST': question = request.POST.get('question') response = openai.Completion.create( model="text-davinci-003", prompt=question, max_tokens=1024, ) answer = str(response.choices[0].text)[str(response.choices[0].text).find('\n')+1:] content='sdjlasjdladskljasadasdasdasd asdasdasdasdsadasdasdasdldkl' print(answer) # print(content) print(type(answer)) # print(type(content)) return render(request, 'depart_list.html', {'question': question, 'answer': answer, 'content':answer[1:]}) return render(request, 'depart_list.html')
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。