免费搭建ChatGPT接口服务器

注意:这是接口转发而已,最终还是调用OpenAI的服务。

图片[1]-免费搭建ChatGPT接口服务器-北忘山博客

一.解决网络问题

网络不通,一万个vvvvvip账号都没用

国外很多免费的云容器或服务器,这里推荐pythonanywhere,把网络和服务器都解决了。

注册网址:https://www.pythonanywhere.com/registration/register/beginner/

1.注册,只需要填用户名、邮箱和密码,其中邮箱随便写就可以了,它不用验证邮箱的,所以可以随便注册几个。

图片[2]-免费搭建ChatGPT接口服务器-北忘山博客

2.注册成功后,可以创建web服务,下面以Django为例

图片[3]-免费搭建ChatGPT接口服务器-北忘山博客

3.创建成功之后就可以写代码,Django的一些路由配置这里就不说了,说重点

需要安装模块,命令:pip install openai,pythonanywhere自带几个版本的python,安装模块时的版本要跟运行Django时一致

图片[4]-免费搭建ChatGPT接口服务器-北忘山博客

直接上核心代码

from django.shortcuts import render
from django.shortcuts import HttpResponse
import openai

#首页,打开index.html
def index(request):
    context          = {}
    return render(request, 'index.html', context)

#接收首页ajax post请求发来的信息
def chat(request):
    if 'msg' in request.POST and request.POST['msg']:
        msg= request.POST['msg']

    openai.api_key = "密钥"

    model_engine = "text-davinci-003"

    completions = openai.Completion.create(
        engine=model_engine,
        prompt=msg,
        max_tokens=1024,
        n=1,
        stop=None,
        temperature=0.5,
    )

    message = completions.choices[0].text
    return HttpResponse(message)

只需要把openai.api_key那里替换能用的密钥就可以了

model_engine那里是配置模型,text-davinci-003应该是目前最新的

二.API key

可以自行注册,b站也有博主分享出来,搜索关键字“chatgpt api”

成果截图

图片[5]-免费搭建ChatGPT接口服务器-北忘山博客

赶快试试吧

© 版权声明
THE END
点赞15 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容