使用开放式人工智能导入和 langchain 后,gpt 聊天机器人无法工作

Moh*_*aan 1 python window openai-api gpt-3 langchain

你好,我正在尝试使用 openai 的 api 构建一个聊天机器人。它的基本功能是读取 pdf、txtfile 等并根据它进行回答。我正在关注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)

Run Code Online (Sandbox Code Playgroud)

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

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)
Run Code Online (Sandbox Code Playgroud)

我怎样才能解决这个问题?

小智 10

修复了这个问题

pip install langchain==0.0.118
Run Code Online (Sandbox Code Playgroud)

pip install gpt_index==0.4.24
Run Code Online (Sandbox Code Playgroud)

不理想,但在这些更改后让这段代码正常工作