如何使用自定义数据训练chatGPT,并创建专属与自己的chatGPT

随着chatGPT的更新,现在可以通过自定义数据来训练它,创建专属与自己的chatGPT应用了。 现在开始吧

准备工作

1.Python3安装/更新命令

python3 -m pip install --upgrade pip 

查看一下是否安装成功

python --version

2.依赖

pip3 install openai llama-index pypdf gradio
  1. openai — 这是安装OpenAI python 库
  2. llama-index — 这是为我们的 LLM 应用程序安装LlamaIndex数据框架
  3. pypdf — 这是开源 python 库,将用于读取我们训练的pdf文件
  4. gradio — Gradio.app是一种创建简单 Web UI

3.获取openAI 密钥(需要梯子)  

 

  1. 如果没注册:注册链接
  2. 如果你已经注册了创建一个新的API密钥(一定要保存到本地,因为只在生成的时候显示一次!!!):API 密钥

  3. 如果你想使用/体验gpt-4模型:教程

发车

1.创建一个文件夹myAIApp里面有trainingData(存放训练用的pdf文件)文件夹,在创建一个app.pypython脚本文件。如图:

 

 2.创建应用程序

app.py文件代码:

from llama_index import GPTVectorStoreIndex, SimpleDirectoryReader, LLMPredictor, ServiceContext, StorageContext, load_index_from_storage
from langchain import OpenAI
import gradio
import os

os.environ["OPENAI_API_KEY"] = '你的API key'

def construct_index(directory_path):
    # 设置数量输出标记
    num_outputs = 256

    _llm_predictor = LLMPredictor(llm=OpenAI(temperature=0.5, model_name="gpt-3.5-turbo", max_tokens=num_outputs))

    service_context = ServiceContext.from_defaults(llm_predictor=_llm_predictor)

    docs = SimpleDirectoryReader(directory_path).load_data()

    index = GPTVectorStoreIndex.from_documents(docs, service_context=service_context)
    
    #存储索引的目录
    index.storage_context.persist(persist_dir="indexes")

    return index

def chatbot(input_text):
    
    # 重建存储上下文 storage_context
    storage_context = StorageContext.from_defaults(persist_dir="indexes")
    
    #使用storage_context从目录加载索引
    query_engne = load_index_from_storage(storage_context).as_query_engine()
    
    response = query_engne.query(input_text)
    
    return response.response

#使用gradio创建Web UI
iface = gradio.Interface(fn=chatbot,
                     inputs=gradio.inputs.Textbox(lines=5, label="placeholder"),
                     outputs="text",
                     title="gpt-3自定义训练")

#基于构建索引在traininData文件夹中的文档上
#如果已经训练了应用程序并且需要重新运行它,则可以跳过该步骤
index = construct_index("trainingData")

#使用gradio启动Web UI
iface.launch(share=True)

运行应用程序:

python3 app.py

tip: trainingData文件夹中的数据越大,所需的时间就越长。

然后myAIApp目录中创建的两个新子文件夹。 indexes是包含根据trainingData文件夹中的数据创建的所有索引的文件夹。已标记的响应将转到flagged文件夹。如图:

至此大功告成:打开本地部署链接:http://127.0.0.1:7860 /

tip:当 AI 关联上下文时,它指的是你对其进行训练的数据集(trainingData 文件夹中的 pdf 文件)

如果你想使用/体验gpt-4模型:教程

  • 33
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值