forked from sudarshan-koirala/llamaparser-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser-ollama.py
70 lines (50 loc) · 1.64 KB
/
parser-ollama.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# ruff: noqa: E402
# bring in our LLAMA_CLOUD_API_KEY
import os
from dotenv import load_dotenv
load_dotenv()
import nest_asyncio
nest_asyncio.apply()
# bring in deps
from llama_parse import LlamaParse
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
# set up parser
llamaparse_api_key = os.getenv("LLAMA_CLOUD_API_KEY")
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()
documents
# len(documents)
# documents[0].text
documents[0].text[:200]
########### Ollama Models ###############
# by default llamaindex uses OpenAI models
from llama_index.embeddings.ollama import OllamaEmbedding
embed_model = OllamaEmbedding(
# model_name="nomic-embed-text",
model_name="llama2",
base_url="http://localhost:11434",
ollama_additional_kwargs={"mirostat": 0},
)
from llama_index.llms.ollama import Ollama
llm = Ollama(model="llama2", request_timeout=30.0)
from llama_index.core import Settings
Settings.llm = llm
Settings.embed_model = embed_model
# get the answer out of it
# 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
from IPython.display import Markdown, display
# query the engine
query = "what is the BoolQ value of GPT4All-J 6B v1.0* model ?"
response = query_engine.query(query)
display(Markdown(f"<b>{response}</b>"))