-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
217 lines (187 loc) · 7.93 KB
/
Copy pathmain.py
File metadata and controls
217 lines (187 loc) · 7.93 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
try:
import pysqlite3
import sys
sys.modules["sqlite3"] = sys.modules.pop("pysqlite3") # handle incompatible OpenMP libs
except Exception as e:
print('module pysqlite3 not found (this is probably okay)')
from dotenv import load_dotenv
import os
import textwrap
from langchain.docstore.document import Document
from langchain_chroma import Chroma
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough, RunnableSequence
from langchain_openai import OpenAIEmbeddings
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_openai import ChatOpenAI
from langchain_core.prompts import PromptTemplate
import requests
from bs4 import BeautifulSoup
import tqdm
import config as conf
import logging
class ConversationManager:
"""
Purpose:
Maintain state of ongoing conversation with the LLM
"""
def __init__(self):
self.history = ""
def update(self, question: str, answer: str) -> None:
self.history += f'QUESTION: {question}\nANSWER: {answer}\n'
def get_history(self) -> str:
return self.history
def execute_rag_chain(rag_chain: RunnableSequence, question: str, conversation_history: str) -> str:
"""
Purpose:
Run the LLM using RAG and output to screen using streaming,
and limiting width of output to fixed width for added readability
in CLI
Outline:
1. log question and current conversation history state
2. MAIN LOOP: enumerate over chunked results of rag_chain, printing to stdout
3. log full response
4. return concatenated results
"""
logging.info(f"Processing question: {question}")
logging.info(f"Using conversation history: {conversation_history}")
context = f"Previous Conversation: {conversation_history}\nQuestion: {question}"
response = ""
line_buffer = "" # a line buffer to accumulate text for "smart" wrapping
try:
for chunk in rag_chain.stream(context):
line_buffer += chunk # append the newest chunk to the buffer
while True:
if '\n' in line_buffer:
pos_newline = line_buffer.find('\n')
# process text before the newline, if any
part_before_newline = line_buffer[:pos_newline]
if part_before_newline:
wrapped_text = textwrap.fill(part_before_newline, width=conf.max_width)
print(wrapped_text, end='')
response += wrapped_text
print()
response += '\n'
line_buffer = line_buffer[pos_newline + 1:]
else:
break
if len(line_buffer) > conf.max_width:
last_space = line_buffer.rfind(' ', 0, conf.max_width+1) # look for last space within limits
if last_space != -1:
to_print = textwrap.fill(line_buffer[:last_space], width=conf.max_width)
print(to_print)
response += to_print + '\n'
line_buffer = line_buffer[last_space+1:] # Continue with the rest in the buffer
except Exception as e:
logging.error(f"Error processing chunk: {e}")
if line_buffer: # handle any remaining text
wrapped_text = textwrap.fill(line_buffer, width=conf.max_width)
print(wrapped_text)
response += wrapped_text + '\n'
logging.info(f"Full response for the question:\n\n{response}")
return response
def scrape_and_parse_data(url: str = conf.source_data_url) -> str:
"""
Purpose:
Get the data to be used for retrieval augmented generation
Outline:
1. get page source from provided url
2. use BeautifulSoup library to parse out readable text
3. return the full concatenated text
"""
if not url.startswith('https'):
raise Exception('attempting to download from potentially insecure website')
response = requests.get(url)
if response.status_code != 200:
raise Exception(f'Failed to retrieve {url}. Status code: {response.status_code}')
print(f'Pulling data from {url} ...', flush=True)
soup = BeautifulSoup(response.content, 'html.parser')
paragraphs = soup.find_all('p')
chunks = []
progress = tqdm.tqdm(range(len(paragraphs)))
for i, p in enumerate(paragraphs, 1):
text = ' '.join(p.stripped_strings) # extract text
progress.update(1)
if conf.ignore_official_interpretation:
if text.startswith('Official interpretation') or text.startswith('See interpretation'):
continue
chunks.append(text)
return '\n'.join(chunks)
def ensure_api_key() -> None:
"""
Purpose:
Check for existence of OPENAI_API_KEY in env vars, and if not,
query for it and store in a local .env file
"""
load_dotenv() # load existing .env variables
if not os.getenv('OPENAI_API_KEY'):
api_key = input("Enter your OPENAI_API_KEY: ").strip()
print('storing api key in .env file at root of this project...')
with open('.env', 'a') as env_file:
env_file.write(f'OPENAI_API_KEY={api_key}\n')
load_dotenv()
def main():
"""
Purpose:
Core program logic
Outline:
1. setup logging
2. instantiate conversation manager
3. scrape and parse text from the source url
4. optionally add "sanity check" statements into data (for debugging)
5. split text into chunks using RecursiveCharacterTextSplitter, for RAG vectorization
6. load env vars, and possibly ask for OpenAI API key
7. instantiate llm, vectorstore, retriver, prompt, and rag_chain objects
8. MAIN LOOP: loop over Q/A conversation
9. garbage collection for Chroma vectorstore
"""
try:
logging.basicConfig(filename=conf.log_file, level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s')
conversation_manager = ConversationManager()
text = scrape_and_parse_data()
if conf.sanity_check: # optionally add special clauses for debugging
text += '\n' + conf.sanity_check_statement
docs = [Document(page_content=text)]
text_splitter = RecursiveCharacterTextSplitter(chunk_size=conf.chunk_size, chunk_overlap=conf.chunk_overlap)
splits = text_splitter.split_documents(docs)
ensure_api_key()
llm = ChatOpenAI(model=conf.openai_model)
vectorstore = Chroma.from_documents(documents=splits, embedding=OpenAIEmbeddings())
retriever = vectorstore.as_retriever()
with open(conf.prompt_path, 'r') as file:
prompt_template = file.read()
prompt = PromptTemplate(
input_variables=["context", "question", "conversation"],
template=prompt_template
)
def format_docs(docs):
return "\n\n".join(doc.page_content for doc in docs)
rag_chain = (
{"context": retriever | format_docs, "question": RunnablePassthrough(), "conversation": RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)
while True:
print('')
print('--------------------------------')
question = input('QUESTION: ')
if question.strip().lower() in conf.exit_commands:
print('Exiting program... goodbye.')
break
print()
print('ANSWER:')
conversation_history = conversation_manager.get_history()
response = execute_rag_chain(rag_chain, question, conversation_history)
conversation_manager.update(question, response)
except Exception as e:
print(e)
return
finally:
if vectorstore is not None:
vectorstore.delete_collection()
if __name__ == '__main__':
# display logo
print(conf.logo)
main()