Chapter 110
parse faq llm
NotebookPython 3 (ipykernel)5 cells
In [1]python · cell 1
python
import io
import requests
import docxIn [2]python · cell 2
python
def clean_line(line):
line = line.strip()
line = line.strip('\uFEFF')
return line
def read_faq(file_id):
url = f'https://docs.google.com/document/d/{file_id}/export?format=docx'
response = requests.get(url)
response.raise_for_status()
with io.BytesIO(response.content) as f_in:
doc = docx.Document(f_in)
questions = []
question_heading_style = 'heading 2'
section_heading_style = 'heading 1'
heading_id = ''
section_title = ''
question_title = ''
answer_text_so_far = ''
for p in doc.paragraphs:
style = p.style.name.lower()
p_text = clean_line(p.text)
if len(p_text) == 0:
continue
if style == section_heading_style:
section_title = p_text
continue
if style == question_heading_style:
answer_text_so_far = answer_text_so_far.strip()
if answer_text_so_far != '' and section_title != '' and question_title != '':
questions.append({
'text': answer_text_so_far,
'section': section_title,
'question': question_title,
})
answer_text_so_far = ''
question_title = p_text
continue
answer_text_so_far += '\n' + p_text
answer_text_so_far = answer_text_so_far.strip()
if answer_text_so_far != '' and section_title != '' and question_title != '':
questions.append({
'text': answer_text_so_far,
'section': section_title,
'question': question_title,
})
return questionsIn [3]python · cell 3
python
faq_documents = {
'llm-zoomcamp': '1m2KexowAXTmexfC5rVTCSnaShvdUQ8Ag2IEiwBDHxN0',
}In [4]python · cell 4
python
documents = []
for course, file_id in faq_documents.items():
print(course)
course_documents = read_faq(file_id)
documents.append({'course': course, 'documents': course_documents})Output
llm-zoomcamp
In [ ]python · cell 5
python
