共享基于python开发的chatGPT,调用4.0接口

前端html部分:
{%extends 'layout.html'%}
{% block content %}




    
    人工智能 AIChat


{% csrf_token %}
{% 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')

你可能感兴趣的:(python,开发语言)