Back to Blog

LlamaIndexの初体験

OpenAI

2024/03/16

用意
1.OPEN_API_KEYを作成しておく
2.dataフォルダにIndex作成用のテキストファイルを置いておく
依頼


pip install llama-index
pip install llama-index-readers-web

実行


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

from llama_index.core import (
    VectorStoreIndex,
    SimpleDirectoryReader,
    StorageContext,
    load_index_from_storage,
)
from llama_index.readers.web import SimpleWebPageReader

PERSIST_DIR = "./storage"
if not os.path.exists(PERSIST_DIR):
    documents = SimpleDirectoryReader("data").load_data()
    # documents = SimpleWebPageReader(html_to_text = True).load_data([
    #     "https://www.********.co.jp/products?q=43f93fb5-a4e9-425c-b7f2-0ca053f3b0cf"
    # ])
    index = VectorStoreIndex.from_documents(documents)
    index.storage_context.persist(persist_dir=PERSIST_DIR)
else:
    storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)
    index = load_index_from_storage(storage_context)

query_engine = index.as_query_engine()
response = query_engine.query("⚪︎でできることを教えて。")
print(response)

Related Posts