ImportError: 无法从’llama_index’导入’load_index_from_storage’。

huangapple go评论67阅读模式
英文:

ImportError: cannot import name 'load_index_from_storage' from 'llama_index'

问题

遇到错误,尝试运行Llamaindex 0.6.5的示例代码时。如何正确导入'load_index_from_storage'?或者有其他选择?

语言: python。
IDE: VScode。

from llama_index import GPTVectorStoreIndex, SimpleDirectoryReader, load_index_from_storage, StorageContext
from IPython.display import Markdown, display
import openai
import os
import sys

os.environ['OPENAI_API_KEY']= "sk-***"

documents = SimpleDirectoryReader('data').load_data()
index = GPTVectorStoreIndex.from_documents(documents)

# save index to disk
index.set_index_id("vector_index")
index.storage_context.persist('storage')

# rebuild storage context
storage_context = StorageContext.from_defaults(persist_dir='storage')
# load index
index = load_index_from_storage(storage_context, index_id="vector_index")

# set Logging to DEBUG for more detailed outputs
query_engine = index.as_query_engine()
response = query_engine.query("What did the author do growing up?")
print(response)
英文:

I met the error when I tried to run the sample code in the git of Llamaindex 0.6.5.
How can I import 'load_index_from_storage' properly?
or is there any alternative?

Language: python.
IDE: VScode.

from llama_index import GPTVectorStoreIndex,          
SimpleDirectoryReader, load_index_from_storage, StorageContext
from IPython.display import Markdown, display
import openai
import os
import sys

os.environ['OPENAI_API_KEY']= "sk-***"


documents = SimpleDirectoryReader('data').load_data()
index = GPTVectorStoreIndex.from_documents(documents)

# save index to disk
index.set_index_id("vector_index")
index.storage_context.persist('storage')

# rebuild storage context
storage_context = 
StorageContext.from_defaults(persist_dir='storage')
# load index
index = load_index_from_storage(storage_context, 
index_id="vector_index")

# set Logging to DEBUG for more detailed outputs
query_engine = index.as_query_engine()
response = query_engine.query("What did the author do growing up?")
print(response)

答案1

得分: 1

以下是根据llama_index 0.6.1文档更新的代码:

# from gpt_index import SimpleDirectoryReader, GPTListIndex,readers, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
from langchain import OpenAI
from types import FunctionType
from llama_index import ServiceContext, GPTVectorStoreIndex, LLMPredictor, PromptHelper, SimpleDirectoryReader, load_index_from_storage
import sys
import os
import time 

os.environ["OPENAI_API_KEY"] = "你的API密钥" # gpt 3.5 turbo
from llama_index.node_parser import SimpleNodeParser

from llama_index import StorageContext, load_index_from_storage
from langchain.chat_models import ChatOpenAI
parser = SimpleNodeParser()

def construct_index(directory_path):
    max_input_size = 4096
    num_outputs = 500
    max_chunk_overlap = 256
    chunk_size_limit = 1024

    print("***** Documents parsing initiated *****")
    file_metadata = lambda x: {"filename": x}
    reader = SimpleDirectoryReader(directory_path, file_metadata=file_metadata)
    documents = reader.load_data()
    
    # nodes = parser.get_nodes_from_documents(documents)
    # index = GPTVectorStoreIndex(nodes)
    prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
    llm_predictor = LLMPredictor(llm=OpenAI(temperature=0, model_name="gpt-3.5-turbo", max_tokens=num_outputs))
    
    service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor, prompt_helper=prompt_helper)

    # print("***** Index creation initiated *****")
    index = GPTVectorStoreIndex.from_documents(
        documents=documents, service_context=service_context
    )
    # print("***** Index created *****")
    index.storage_context.persist("./entire_docs")
    return index

construct_index("./docs")
storage_context = StorageContext.from_defaults(persist_dir="./entire_docs")
index = load_index_from_storage(storage_context)
query_engine = index.as_query_engine()
while True:
    text_input = input("YOU: ")
    response = query_engine.query(text_input)
    print("Bot:", response)
    print('\n')

以上代码适用于llama_index==0.6.1

英文:

This is the updated code as per the documentation of llama_index for question answering.

# from gpt_index import SimpleDirectoryReader, GPTListIndex,readers, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
from langchain import OpenAI
from types import FunctionType
from llama_index import ServiceContext, GPTVectorStoreIndex, LLMPredictor, PromptHelper, SimpleDirectoryReader, load_index_from_storage
import sys
import os
import time 


os.environ["OPENAI_API_KEY"] = "your api key" # gpt 3.5 turbo
from llama_index.node_parser import SimpleNodeParser

from llama_index import StorageContext, load_index_from_storage
from langchain.chat_models import ChatOpenAI
parser = SimpleNodeParser()




def construct_index(directory_path):
    max_input_size = 4096
    num_outputs = 500
    max_chunk_overlap = 256
    chunk_size_limit = 1024

    print("*"*5, "Documents parsing initiated", "*"*5)
    file_metadata = lambda x : {"filename": x}
    reader = SimpleDirectoryReader(directory_path, file_metadata=file_metadata)
    documents = reader.load_data()
    
  
    # nodes = parser.get_nodes_from_documents(documents)
    # index = GPTVectorStoreIndex(nodes)
    prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
    llm_predictor = LLMPredictor(llm=OpenAI(temperature=0, model_name="gpt-3.5-turbo", max_tokens=num_outputs))
    
    service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor, prompt_helper=prompt_helper)

    # print("*"*5, "Index creation initiated", "*"*5)
    index = GPTVectorStoreIndex.from_documents(
        documents=documents, service_context = service_context
    )
    # print("*"*5, "Index created", "*"*5)
    index.storage_context.persist("./entire_docs")
    return index
    
construct_index("./docs")
storage_context = StorageContext.from_defaults(persist_dir="./entire_docs")
index = load_index_from_storage(storage_context)
query_engine = index.as_query_engine()
while True:
    text_input = input("YOU : ")
    response = query_engine.query(text_input)
    print("Bot : ", response)
    print('\n')


the above code will work for llama_index==0.6.1

huangapple
  • 本文由 发表于 2023年5月11日 17:31:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76226147.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定