嗨,我正在尝试使用 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)
运行代码后出现以下错误:
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)
我该如何解决?
运行以下命令查看您的 LangChain 包是否已过时:
pip list --outdated
如果是,运行以下命令更新它:
pip install --upgrade langchain