forked from sudarshan-koirala/llamaparser-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser-openai.py
45 lines (32 loc) · 1.07 KB
/
parser-openai.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# ruff: noqa: E402
import os
import nest_asyncio
nest_asyncio.apply()
from IPython.display import Markdown, display
# bring in our LLAMA_CLOUD_API_KEY
from dotenv import load_dotenv
load_dotenv()
# bring in deps
from llama_parse import LlamaParse
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
llamaparse_api_key = os.getenv("LLAMA_CLOUD_API_KEY")
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
# set up parser
parser = LlamaParse(
api_key=llamaparse_api_key,
result_type="markdown", # "markdown" and "text" are available
)
# use SimpleDirectoryReader to parse our file
file_extractor = {".pdf": parser}
documents = SimpleDirectoryReader(
input_files=["data/gpt4all.pdf"], file_extractor=file_extractor
).load_data()
# print(documents)
# create an index from the parsed markdown
index = VectorStoreIndex.from_documents(documents)
# create a query engine for the index
query_engine = index.as_query_engine()
# query the engine
query = "Where was the collected loaded on?"
response = query_engine.query(query)
display(Markdown(f"<b>{response}</b>"))