前端html部分: {%extends 'layout.html'%} {% block content %}{% endblock %}人工智能 AIChat
后端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')