-
Notifications
You must be signed in to change notification settings - Fork 34
/
main.py
83 lines (66 loc) · 2.34 KB
/
main.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
71
72
73
74
75
76
77
78
79
80
81
82
83
import logging
logging.basicConfig(level=logging.CRITICAL)
import os
from pathlib import Path
import openai
from dotenv import load_dotenv
from langchain.chat_models import ChatOpenAI
from llama_index import (
GPTVectorStoreIndex,
LLMPredictor,
ServiceContext,
StorageContext,
download_loader,
load_index_from_storage,
)
from utils import CACHE, FILES, models, cls, handle_save, handle_exit, initialize, select_file
load_dotenv()
openai.api_key = os.environ["OPENAI_API_KEY"]
history = []
llm_predictor = LLMPredictor(llm=ChatOpenAI(temperature=0.618, model_name=models["gpt-3"], max_tokens=256))
service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor, chunk_size_limit=1024)
def make_index(file):
cls()
print("👀 Loading...")
PDFReader = download_loader("PDFReader")
loader = PDFReader()
documents = loader.load_data(file=Path(FILES) / file)
if os.path.exists(Path(CACHE) / file):
print("📚 Index found in cache")
return
else:
print("📚 Index not found in cache, creating it...")
index = GPTVectorStoreIndex.from_documents(documents, service_context=service_context)
index.storage_context.persist(persist_dir=Path(CACHE) / file)
def chat(file_name, index):
while True:
prompt = input("\n😎 Prompt: ")
if prompt == "exit":
handle_exit()
elif prompt == "save":
handle_save(str(file_name), history)
query_engine = index.as_query_engine(response_mode="compact")
response = query_engine.query(prompt)
print("\n👻 Response: " + str(response))
history.append({"user": prompt, "response": str(response)})
def ask(file_name):
try:
print("👀 Loading...")
storage_context = StorageContext.from_defaults(persist_dir=Path(CACHE) / file_name)
index = load_index_from_storage(storage_context, service_context=service_context)
cls()
print("✅ Ready! Let's start the conversation")
print("ℹ️ Press Ctrl+C to exit")
chat(file_name, index)
except KeyboardInterrupt:
handle_exit()
if __name__ == "__main__":
initialize()
file = select_file()
if file:
file_name = Path(file).name
make_index(file_name)
ask(file_name)
else:
print("No files found")
handle_exit()