gpt聊天机器人在使用open ai imports和langchain后无法工作

pgpifvop  于 2023-05-07  发布在  其他
关注(0)|答案(2)|浏览(531)

你好,我正试图建立一个聊天机器人使用openai的API。它的基本功能是读取PDF、TXT文件等。并据此做出回答。我正在学习https://beebom.com/how-train-ai-chatbot-custom-knowledge-base-chatgpt-api/的教程
我使用了以下代码并安装了必要的依赖项:

from gpt_index import SimpleDirectoryReader, GPTListIndex, GPTSimpleVectorIndex, LLMPredictor, PromptHelper, ServiceContext
from langchain.chat_models import ChatOpenAI
import gradio as gr
import sys
import os

os.environ["OPENAI_API_KEY"] = 'yourapikey'

def construct_index(directory_path):
    max_input_size = 4096
    num_outputs = 512
    max_chunk_overlap = 20
    chunk_size_limit = 600

    prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)

    llm_predictor = LLMPredictor(llm=ChatOpenAI(temperature=0.7, model_name="gpt-3.5-turbo", max_tokens=num_outputs))

    documents = SimpleDirectoryReader(directory_path).load_data()

    index = GPTSimpleVectorIndex(documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper)

    index.save_to_disk('index.json')

    return index

def chatbot(input_text):
    index = GPTSimpleVectorIndex.load_from_disk('index.json')
    response = index.query(input_text, response_mode="compact")
    return response.response

iface = gr.Interface(fn=chatbot,
                     inputs=gr.components.Textbox(lines=7, label="Enter your text"),
                     outputs="text",
                     title="Custom-trained AI Chatbot")

index = construct_index("docs")
iface.launch(share=True)

运行代码后,我得到以下错误:

File "C:\Users\USER\desktop\cht\app.py", line 1, in <module>
    from gpt_index import SimpleDirectoryReader, GPTListIndex, GPTSimpleVectorIndex, LLMPredictor, PromptHelper, ServiceContext
  File "C:\Users\USER\AppData\Local\Programs\Python\Python311\Lib\site-packages\gpt_index\__init__.py", line 18, in <module>
    from gpt_index.indices.common.struct_store.base import SQLDocumentContextBuilder
  File "C:\Users\USER\AppData\Local\Programs\Python\Python311\Lib\site-packages\gpt_index\indices\__init__.py", line 4, in <module>
    from gpt_index.indices.keyword_table.base import GPTKeywordTableIndex
  File "C:\Users\USER\AppData\Local\Programs\Python\Python311\Lib\site-packages\gpt_index\indices\keyword_table\__init__.py", line 4, in <module>
    from gpt_index.indices.keyword_table.base import GPTKeywordTableIndex
  File "C:\Users\USER\AppData\Local\Programs\Python\Python311\Lib\site-packages\gpt_index\indices\keyword_table\base.py", line 16, in <module>
    from gpt_index.indices.base import DOCUMENTS_INPUT, BaseGPTIndex
  File "C:\Users\USER\AppData\Local\Programs\Python\Python311\Lib\site-packages\gpt_index\indices\base.py", line 23, in <module>
    from gpt_index.indices.prompt_helper import PromptHelper
  File "C:\Users\USER\AppData\Local\Programs\Python\Python311\Lib\site-packages\gpt_index\indices\prompt_helper.py", line 12, in <module>
    from gpt_index.langchain_helpers.chain_wrapper import LLMPredictor
  File "C:\Users\USER\AppData\Local\Programs\Python\Python311\Lib\site-packages\gpt_index\langchain_helpers\chain_wrapper.py", line 13, in <module>
    from gpt_index.prompts.base import Prompt
  File "C:\Users\USER\AppData\Local\Programs\Python\Python311\Lib\site-packages\gpt_index\prompts\__init__.py", line 3, in <module>
    from gpt_index.prompts.base import Prompt
  File "C:\Users\USER\AppData\Local\Programs\Python\Python311\Lib\site-packages\gpt_index\prompts\base.py", line 9, in <module>
    from langchain.schema import BaseLanguageModel
ImportError: cannot import name 'BaseLanguageModel' from 'langchain.schema' (C:\Users\USER\AppData\Local\Programs\Python\Python311\Lib\site-packages\langchain\schema.py)

这个怎么办?

r1wp621o

r1wp621o1#

修复此与

pip install langchain==0.0.118

和/或

pip install gpt_index==0.4.24

不是很理想,但在这些更改之后,代码可以正常工作

hvvq6cgz

hvvq6cgz2#

我也有同样的问题。我也试着按照上面文章的说明进行应用,也遇到了同样的错误。
我使用所有库的最新版本,除了get_index,根据上面文章中的说明,我安装了版本0.4.24。(然而,我也尝试使用了最新版本的get_index,它没有帮助)。我用Python 3.10和Python 3.11做了实验。
我暂时设法解决了这个问题,方法是进入抛出错误的get_index源代码(C:\Users\USER\AppData\Local\Programs\Python\Python311\Lib\site-packages\gpt_index\prompts\base.py),并将BaseModel表达式替换为BaseLanguageModel表达式。
这当然是一个非常糟糕的解决方案,我正在寻找一个标准的解决方案。

相关问题