使用Python3、Openai的自定义语言模型

问题描述 投票:0回答:1

https://medium.com/@sohaibshaheen/train-chatgpt-with-custom-data-and-create-your-own-chat-bot-using-macos-fb78c2f9646d

我正在使用本教程来创建我的语言模型。我在第一次运行时忽略了底部的 UPDATE 通知,因此遇到了无法找到 got.index 的错误。现在我更新了代码并使用 llama.index 代替我的密钥。现在我的问题是,当我尝试运行“Python3 app.py”命令时,我得到“无法打开文件'/Users/my-Name/app.py':[Errno 2]没有这样的文件或目录”错误。我尝试将 Docs 文件夹和 app.py(在 MacOS 中使用 textedit 创建)安装到 Users 目录中,但随后我得到“Traceback(最近一次调用最后): 文件“/Users/my.-Name/app.py”,第 1 行,位于 从 llama_index 导入 SimpleDirectoryReader、GPTSimpleVectorIndex、LLMPredictor、ServiceContext ImportError:无法从“llama_index”导入名称“GPTSimpleVectorIndex”(/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/llama_index/init.py)”错误。

我想知道我做错了什么或者为什么失败,即使我直接将其安装在用户目录中

model openai-api chatgpt-api
1个回答
0
投票

我也在关注这篇文章,发现大部分代码已经过时,并且功能已被弃用。 经过大量的尝试和错误并查看了文档,我现在有了一个可以工作的自定义LLM(在MacOS上 - 软件和软件包在其他操作系统上可能有所不同)。

需要以下软件和软件包:

  1. https://www.python.org/downloads/
  2. 安装 Python
  3. python3 -m pip install -U pip
  4. pip3安装openai
  5. pip3 安装 llama-index
  6. pip3 安装 PyPDF2
  7. pip3 安装渐变
  • 现在从 llama_index.core 导入
  • GPTSimpleVectorIndex 已弃用,取而代之的是 GPTVectorStoreIndex
  • LLMPredictor 和 ServiceContext 已弃用,取而代之的是 Settings、StorageContext 和 load_index_from_storage
  • index.save_to_disk 已弃用,取而代之的是
  • index.storage_context.persist()
  • index.query 已被弃用,取而代之的是 query_engine

我更新后的功能代码现在如下:

from llama_index.core import SimpleDirectoryReader, GPTVectorStoreIndex, Settings, StorageContext, load_index_from_storage
from llama_index.llms.openai import OpenAI
import gradio as gr
import os

os.environ["OPENAI_API_KEY"] = '--enter API Key---'

def construct_index(directory_path):
    num_outputs = 512

    Settings.llm = OpenAI(temperature=0.7, model_name="gpt-3.5-turbo", max_tokens=num_outputs)

    docs = SimpleDirectoryReader(directory_path).load_data()

    index = GPTVectorStoreIndex.from_documents(docs)

    index.storage_context.persist("storage")

    return index

def chatbot(input_text):
    storage_context = StorageContext.from_defaults(persist_dir="storage")
    index = load_index_from_storage(storage_context)
    query_engine = index.as_query_engine()
    response = query_engine.query(input_text)
    return response.response

iface = gr.Interface(fn=chatbot, inputs=gr.Textbox(lines=7, label="Enter your text"), outputs="text", title="Digital Identity SME Bot")

iface.launch(share=True)
© www.soinside.com 2019 - 2024. All rights reserved.