-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_rag.py
More file actions
124 lines (99 loc) · 2.87 KB
/
Copy pathbasic_rag.py
File metadata and controls
124 lines (99 loc) · 2.87 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
# import os
# files = os.listdir("../Aihacks/txt_files")
# print(files)
# %%
"""
# Setup
"""
# %%
# OctoAI
# ! pip install langchain langchain-community faiss-cpu sentence-transformers octoai-sdk langchain-text-splitters lxml tiktoken python-dotenv 'arize-phoenix[evals]'
# %%
from dotenv import load_dotenv
import os
import PyPDF2
load_dotenv()
OCTOAI_API_TOKEN = os.environ["OCTOAI_API_TOKEN"]
# %%
"""
# change pdf to textfile
"""
def pdf_to_text(pdf_path, txt_path):
# Open the PDF file
with open(pdf_path, 'rb') as pdf_file:
# Create a PDF reader object
pdf_reader = PyPDF2.PdfFileReader(pdf_file)
# Open the text file in write mode
with open(txt_path, 'w') as text_file:
# Iterate through each page in the PDF
for page_num in range(pdf_reader.numPages):
# Get the page
page = pdf_reader.getPage(page_num)
# Extract the text from the page
text = page.extract_text()
# Write the text to the text file
text_file.write(text)
# %%
"""
# Ingest Data
"""
# %%
from langchain.text_splitter import CharacterTextSplitter
from langchain.schema import Document
# %%
files = os.listdir("../Aihacks/txt_files")
file_texts = []
for file in files:
with open(f"../Aihacks/txt_files/{file}") as f:
file_text = f.read()
text_splitter = CharacterTextSplitter.from_tiktoken_encoder(
chunk_size=512, chunk_overlap=64,
)
texts = text_splitter.split_text(file_text)
for i, chunked_text in enumerate(texts):
file_texts.append(Document(page_content=chunked_text,
metadata={"doc_title": file.split(".")[0], "chunk_num": i}))
# %%
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_community.vectorstores import FAISS
# %%
embeddings = HuggingFaceEmbeddings()
# %%
vector_store = FAISS.from_documents(
file_texts,
embedding=embeddings
)
# %%
"""
# Search the Data
"""
# %%
from langchain_community.llms.octoai_endpoint import OctoAIEndpoint
llm = OctoAIEndpoint(
model="meta-llama-3-8b-instruct",
max_tokens=1024,
presence_penalty=0,
temperature=0.1,
top_p=0.9,
)
# %%
retriever = vector_store.as_retriever()
# %%
from langchain.prompts import ChatPromptTemplate
template="""You are a investment banker. If you don't know the answer, just say that you don't know. Use three sentences maximum and keep the answer concise
Question: {question}
Context: {context}
Answer:"""
prompt = ChatPromptTemplate.from_template(template)
# %%
from langchain_core.runnables import RunnablePassthrough
from langchain_core.output_parsers import StrOutputParser
chain = (
{"context": retriever, "question": RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)
# %%
print(chain.invoke("What is microsofts outlook in 2025?"))
# %%