AI 日报

GPT-4 教程:如何将 GPT-4 集成到您的网站中




GPT-4 教程:如何将 GPT-4 集成到您的网站中

GPT-4 来了!

GPT-4 是 OpenAI 的一种新的大型多模态模型。它是第四代 GPT 语言模型。GPT-4 在很多任务上都优于 GPT-3。由于其广泛的常识,最好使用它而不是以前的模型。在本教程中,我们将向您展示如何将 GPT-4 集成到 streamlit 项目中。

如何访问 GPT-4?

GPT-4 API 并非对所有人可用,它仍处于limited beta. 您可以在 OpenAI 的网站上申请访问。为此,您需要用您的数据填写表格并回答一些问题。如果您被接受,您将收到一封电子邮件,其中包含 GPT-4 测试版的邀请。

如何将 GPT-3 转换为 GPT-4?

将 GPT-3 更改为 GPT-4 有点棘手。类似 GPT-4 的gpt-3.5-turbo模型,针对聊天完成而非文本完成进行了优化。由于其广泛的一般知识,它也适用于传统的文本补全。因此,我们应该将completion函数更改为chat completion。在本教程中,我们将使用 Python SDK 和 lablab.ai 的GPT-3 Streamlit Boilerplate

GPT-4 教程:如何将 GPT-4 集成到您的网站中

克隆我们的存储库

首先,我们需要克隆我们的存储库。您可以通过运行以下命令来完成:

git clone https://github.com/nextgrid/streamlit_gpt3_boilerplate

让我们进入项目目录:

cd streamlit_gpt3_boilerplate

安装依赖

现在我们需要安装所有依赖项。你可以通过运行来做到这一点:

pip install -r requirements.txt

GPT-3 -> GPT-4

为此,我们需要打开model.py. 我们应该照顾GeneralModel班级。为了能够使用 GPT-4,我们需要更改openai.Completion.createopenai.ChatCompletion.create. 我们还需要调整kwargs参数以满足 GPT-4 的需求。同样在create函数本身中,我们必须添加model参数,将prompt关键字更改为messages,并以不同的方式从最终响应中提取内容。

随意更改传递给模型的参数(例如温度)并查看它如何影响输出。

class GeneralModel:    def __init__(self):        print("Model Initialization--->")    def query(self, prompt, myKwargs={}):        """        wrapper for the API to save the prompt and the result        """        # arguments to send the API        kwargs = {            "engine": "text-davinci-002",            "temperature": 0.85,            "max_tokens": 600,            "best_of": 1,            "top_p": 1,            "frequency_penalty": 0,            "presence_penalty": 0,            "stop": ["###"],        }        for kwarg in myKwargs:            kwargs[kwarg] = myKwargs[kwarg]        r = openai.Completion.create(prompt=prompt, **kwargs)["choices"][0][            "text"        ].strip()        return r    def model_prediction(self, inp, api_key):        """        wrapper for the API to save the prompt and the result        """        # Setting the OpenAI API key got from the OpenAI dashboard        set_openai_key(api_key)        output = self.query(poem.format(input=inp))        return output

class GeneralModel:    def __init__(self):        print("Model Initialization--->")    def query(self, prompt, myKwargs={}):        """        wrapper for the API to save the prompt and the result        """        # arguments to send the API        kwargs = {            "temperature": 0.9,            "max_tokens": 600,        }        for kwarg in myKwargs:            kwargs[kwarg] = myKwargs[kwarg]        r = openai.ChatCompletion.create(            model="gpt-4", messages=[{"role": "system", "content": prompt}], **kwargs        )        return r["choices"][0]["message"]["content"].strip()    def model_prediction(self, inp, api_key):        """        wrapper for the API to save the prompt and the result        """        # Setting the OpenAI API key got from the OpenAI dashboard        set_openai_key(api_key)        output = self.query(poem.format(inp=inp))        return output

运行项目

现在我们可以运行这个项目了。你可以通过运行来做到这一点:

streamlit run gpt_app.py

让我们尝试生成一首诗!

GPT-4 教程:如何将 GPT-4 集成到您的网站中
结果!

那么 GPT-4 API 值得实施吗?

GPT-4 教程:如何将 GPT-4 集成到您的网站中

好,当然!将 GPT-3 更改为 GPT-4 并不难,只需要进行一些更改,您可以在本教程或 OpenAI 的教程和文档中找到它们。值得一试,因为 GPT-4 是一个很棒的模型,至少值得一试!正如您可能已经读到的那样,它具有更强大的功能、更少的幻觉、新的惊人功能(如图像输入)等等!

这取决于你,你将在它之上构建什么,但你只是得到了一个新的强大工具可以使用。由于您目前正在使用 Whisper 和 ChatGPT API 进行构建,您可能会考虑使用 GPT-4 API 升级您的项目。

如果您想使用 AI 进行构建,我们鼓励您加入 aihubpro.cn 社区

或者

加入我们即将举行的 Whisper 和 ChatGPT AI 黑客松,或任何其他使用尖端 AI 技术的活动。

构建工作原型并使用我们的弹弓程序加速它。

用 AI 塑造世界用 AIHubPro.cn改变你的生活

感谢您的时间!– AI未来百科 ; 探索AI的边界与未来! 懂您的AI未来站